qvolray

view src/ui.cc @ 21:4c62be57fc1a

foo
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 11 Apr 2012 16:59:45 +0300
parents 88312413467c
children 2d0dfb5751dc
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 QSlider *zslider = new QSlider(Qt::Horizontal);
66 zslider->setRange(0, 256);
67 zslider->setValue(volray_getvalue(VOLRAY_ZCURSOR) * 256.0);
68 connect(zslider, SIGNAL(valueChanged(int)), this, SLOT(zslider_change(int)));
70 QVBoxLayout *vbox = new QVBoxLayout;
71 vbox->addWidget(zslider);
73 QWidget *win = new QWidget;
74 setWidget(win);
75 win->setLayout(vbox);
76 }
78 void SideWindow::zslider_change(int val)
79 {
80 volray_setvalue(VOLRAY_ZCURSOR, (float)val / 256.0);
81 }
84 void post_redisplay()
85 {
86 glview->updateGL();
87 }
90 GLView::GLView(QWidget *parent)
91 : QGLWidget(QGLFormat(QGL::DoubleBuffer), parent)
92 {
93 }
95 QSize GLView::minimumSizeHint() const
96 {
97 return QSize(320, 200);
98 }
100 QSize GLView::sizeHint() const
101 {
102 return QSize(1280, 800);
103 }
105 void GLView::initializeGL()
106 {
107 if(!volray_init()) {
108 exit(0);
109 }
110 }
112 void GLView::resizeGL(int xsz, int ysz)
113 {
114 volray_resize(xsz, ysz);
115 }
117 void GLView::paintGL()
118 {
119 volray_draw();
120 }
122 static int button_number(Qt::MouseButton bn)
123 {
124 switch(bn) {
125 case Qt::LeftButton:
126 return 0;
127 case Qt::MidButton:
128 return 1;
129 case Qt::RightButton:
130 return 2;
131 default:
132 break;
133 }
134 return -1;
135 }
137 void GLView::mousePressEvent(QMouseEvent *ev)
138 {
139 int bn = button_number(ev->button());
140 if(bn >= 0) {
141 volray_mouse(bn, 1, ev->x(), ev->y());
142 }
143 }
145 void GLView::mouseReleaseEvent(QMouseEvent *ev)
146 {
147 int bn = button_number(ev->button());
148 if(bn >= 0) {
149 volray_mouse(bn, 0, ev->x(), ev->y());
150 }
151 }
153 void GLView::mouseMoveEvent(QMouseEvent *ev)
154 {
155 volray_motion(ev->x(), ev->y());
156 }