qvolray

view src/ui.cc @ 26:f4cc61b5a3eb

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