qvolray

view src/ui.cc @ 13:17d9dc2edc91

first qt version
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 10 Apr 2012 06:11:16 +0300
parents
children 88312413467c
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 // actions
18 QAction *act_open = new QAction(qApp->style()->standardIcon(QStyle::SP_DialogOpenButton), "&Open...", this);
19 act_open->setStatusTip("Open a volume dataset");
20 QObject::connect(act_open, SIGNAL(triggered()), this, SLOT(open_volume()));
22 QAction *act_quit = new QAction("&Quit", this);
23 act_quit->setStatusTip("Quit");
24 QObject::connect(act_quit, SIGNAL(triggered()), this, SLOT(close()));
26 // menus
27 QMenu *mfile = menuBar()->addMenu("&File");
28 mfile->addAction(act_open);
29 mfile->addAction(act_quit);
31 // toolbars
32 QToolBar *tfile = addToolBar("&File");
33 tfile->addAction(act_open);
35 statusBar();
36 show();
37 }
39 void MainWindow::open_volume()
40 {
41 QString fname = QFileDialog::getOpenFileName(this, "Open volume dataset", QString(), "Volume descriptors (*.vol)");
42 if(!fname.isNull()) {
43 Volume *vol = new Volume;
45 if(vol->load(qPrintable(fname))) {
46 delete volume;
47 volume = vol;
48 volray_setvolume(vol);
49 post_redisplay();
50 } else {
51 delete vol;
52 }
53 }
54 }
56 void post_redisplay()
57 {
58 glview->updateGL();
59 }
62 GLView::GLView(QWidget *parent)
63 : QGLWidget(QGLFormat(QGL::DoubleBuffer), parent)
64 {
65 }
67 QSize GLView::minimumSizeHint() const
68 {
69 return QSize(320, 200);
70 }
72 QSize GLView::sizeHint() const
73 {
74 return QSize(1280, 800);
75 }
77 void GLView::initializeGL()
78 {
79 if(!volray_init()) {
80 exit(0);
81 }
82 glClearColor(1, 0, 0, 1);
83 }
85 void GLView::resizeGL(int xsz, int ysz)
86 {
87 volray_resize(xsz, ysz);
88 }
90 void GLView::paintGL()
91 {
92 volray_draw();
93 }
95 static int button_number(Qt::MouseButton bn)
96 {
97 switch(bn) {
98 case Qt::LeftButton:
99 return 0;
100 case Qt::MidButton:
101 return 1;
102 case Qt::RightButton:
103 return 2;
104 default:
105 break;
106 }
107 return -1;
108 }
110 void GLView::mousePressEvent(QMouseEvent *ev)
111 {
112 int bn = button_number(ev->button());
113 if(bn >= 0) {
114 volray_mouse(bn, 1, ev->x(), ev->y());
115 }
116 }
118 void GLView::mouseReleaseEvent(QMouseEvent *ev)
119 {
120 int bn = button_number(ev->button());
121 if(bn >= 0) {
122 volray_mouse(bn, 0, ev->x(), ev->y());
123 }
124 }
126 void GLView::mouseMoveEvent(QMouseEvent *ev)
127 {
128 volray_motion(ev->x(), ev->y());
129 }