qvolray

view src/ui.cc @ 22:2d0dfb5751dc

foo
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 11 Apr 2012 18:35:12 +0300
parents 4c62be57fc1a
children f4cc61b5a3eb
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <QtGui>
4 #include "ui.h"
6 static GLView *glview;
7 static Volume *volume;
9 MainWindow::MainWindow()
10 {
11 setWindowTitle("Volume Renderer");
13 // OpenGL view
14 glview = new GLView;
15 setCentralWidget(glview);
17 // side-window
18 sidewin = new SideWindow;
19 sidewin->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
20 sidewin->setFeatures(QDockWidget::DockWidgetMovable | QDockWidget::DockWidgetFloatable);
21 addDockWidget(Qt::LeftDockWidgetArea, sidewin);
23 // actions
24 QAction *act_open = new QAction(qApp->style()->standardIcon(QStyle::SP_DialogOpenButton), "&Open...", this);
25 act_open->setStatusTip("Open a volume dataset");
26 QObject::connect(act_open, SIGNAL(triggered()), this, SLOT(open_volume()));
28 QAction *act_quit = new QAction("&Quit", this);
29 act_quit->setStatusTip("Quit");
30 QObject::connect(act_quit, SIGNAL(triggered()), this, SLOT(close()));
32 // menus
33 QMenu *mfile = menuBar()->addMenu("&File");
34 mfile->addAction(act_open);
35 mfile->addAction(act_quit);
37 // toolbars
38 QToolBar *tfile = addToolBar("&File");
39 tfile->addAction(act_open);
41 statusBar();
42 show();
43 }
45 void MainWindow::open_volume()
46 {
47 QString fname = QFileDialog::getOpenFileName(this, "Open volume dataset", QString(), "Volume descriptors (*.vol)");
48 if(!fname.isNull()) {
49 Volume *vol = new Volume;
51 if(vol->load(qPrintable(fname))) {
52 delete volume;
53 volume = vol;
54 volray_setvolume(vol);
55 post_redisplay();
56 } else {
57 delete vol;
58 }
59 }
60 }
63 SideWindow::SideWindow()
64 {
65 QGroupBox *groupbox = new QGroupBox("Volume slice");
66 {
67 QCheckBox *chk_clip = new QCheckBox("clip");
68 chk_clip->setChecked(false);
69 connect(chk_clip, SIGNAL(stateChanged(int)), this, SLOT(clip_change(int)));
71 QSlider *zslider = new QSlider(Qt::Horizontal);
72 zslider->setRange(0, 256);
73 zslider->setValue(volray_getvalue(VOLRAY_ZCURSOR) * 256.0);
74 connect(zslider, SIGNAL(valueChanged(int)), this, SLOT(zslider_change(int)));
76 QVBoxLayout *vbox = new QVBoxLayout;
77 vbox->addWidget(chk_clip);
78 vbox->addWidget(zslider);
80 groupbox->setLayout(vbox);
81 }
83 QWidget *win = new QWidget;
84 setWidget(win);
86 QVBoxLayout *vbox = new QVBoxLayout;
87 vbox->addWidget(groupbox);
88 vbox->addStretch();
90 win->setLayout(vbox);
91 }
93 void SideWindow::zslider_change(int val)
94 {
95 volray_setvalue(VOLRAY_ZCURSOR, (float)val / 256.0);
96 }
98 void SideWindow::clip_change(int checked)
99 {
100 volray_setvalue(VOLRAY_ZCLIP, checked);
101 }
103 void post_redisplay()
104 {
105 glview->updateGL();
106 }
109 GLView::GLView(QWidget *parent)
110 : QGLWidget(QGLFormat(QGL::DoubleBuffer), parent)
111 {
112 }
114 QSize GLView::minimumSizeHint() const
115 {
116 return QSize(320, 200);
117 }
119 QSize GLView::sizeHint() const
120 {
121 return QSize(1280, 800);
122 }
124 void GLView::initializeGL()
125 {
126 if(!volray_init()) {
127 exit(0);
128 }
129 }
131 void GLView::resizeGL(int xsz, int ysz)
132 {
133 volray_resize(xsz, ysz);
134 }
136 void GLView::paintGL()
137 {
138 volray_draw();
139 }
141 static int button_number(Qt::MouseButton bn)
142 {
143 switch(bn) {
144 case Qt::LeftButton:
145 return 0;
146 case Qt::MidButton:
147 return 1;
148 case Qt::RightButton:
149 return 2;
150 default:
151 break;
152 }
153 return -1;
154 }
156 void GLView::mousePressEvent(QMouseEvent *ev)
157 {
158 int bn = button_number(ev->button());
159 if(bn >= 0) {
160 volray_mouse(bn, 1, ev->x(), ev->y());
161 }
162 }
164 void GLView::mouseReleaseEvent(QMouseEvent *ev)
165 {
166 int bn = button_number(ev->button());
167 if(bn >= 0) {
168 volray_mouse(bn, 0, ev->x(), ev->y());
169 }
170 }
172 void GLView::mouseMoveEvent(QMouseEvent *ev)
173 {
174 volray_motion(ev->x(), ev->y());
175 }