qvolray

view src/ui.cc @ 14:88312413467c

adding a sidewidget
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 10 Apr 2012 06:35:34 +0300
parents 17d9dc2edc91
children 4c62be57fc1a
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 }
68 void post_redisplay()
69 {
70 glview->updateGL();
71 }
74 GLView::GLView(QWidget *parent)
75 : QGLWidget(QGLFormat(QGL::DoubleBuffer), parent)
76 {
77 }
79 QSize GLView::minimumSizeHint() const
80 {
81 return QSize(320, 200);
82 }
84 QSize GLView::sizeHint() const
85 {
86 return QSize(1280, 800);
87 }
89 void GLView::initializeGL()
90 {
91 if(!volray_init()) {
92 exit(0);
93 }
94 }
96 void GLView::resizeGL(int xsz, int ysz)
97 {
98 volray_resize(xsz, ysz);
99 }
101 void GLView::paintGL()
102 {
103 volray_draw();
104 }
106 static int button_number(Qt::MouseButton bn)
107 {
108 switch(bn) {
109 case Qt::LeftButton:
110 return 0;
111 case Qt::MidButton:
112 return 1;
113 case Qt::RightButton:
114 return 2;
115 default:
116 break;
117 }
118 return -1;
119 }
121 void GLView::mousePressEvent(QMouseEvent *ev)
122 {
123 int bn = button_number(ev->button());
124 if(bn >= 0) {
125 volray_mouse(bn, 1, ev->x(), ev->y());
126 }
127 }
129 void GLView::mouseReleaseEvent(QMouseEvent *ev)
130 {
131 int bn = button_number(ev->button());
132 if(bn >= 0) {
133 volray_mouse(bn, 0, ev->x(), ev->y());
134 }
135 }
137 void GLView::mouseMoveEvent(QMouseEvent *ev)
138 {
139 volray_motion(ev->x(), ev->y());
140 }