qvolray
diff 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 diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/ui.cc Tue Apr 10 06:11:16 2012 +0300 1.3 @@ -0,0 +1,129 @@ 1.4 +#include <stdio.h> 1.5 +#include <stdlib.h> 1.6 +#include <QtGui> 1.7 +#include "ui.h" 1.8 + 1.9 +static GLView *glview; 1.10 +static Volume *volume; 1.11 + 1.12 +MainWindow::MainWindow() 1.13 +{ 1.14 + setWindowTitle("Volume Renderer"); 1.15 + 1.16 + // OpenGL view 1.17 + glview = new GLView; 1.18 + setCentralWidget(glview); 1.19 + 1.20 + // actions 1.21 + QAction *act_open = new QAction(qApp->style()->standardIcon(QStyle::SP_DialogOpenButton), "&Open...", this); 1.22 + act_open->setStatusTip("Open a volume dataset"); 1.23 + QObject::connect(act_open, SIGNAL(triggered()), this, SLOT(open_volume())); 1.24 + 1.25 + QAction *act_quit = new QAction("&Quit", this); 1.26 + act_quit->setStatusTip("Quit"); 1.27 + QObject::connect(act_quit, SIGNAL(triggered()), this, SLOT(close())); 1.28 + 1.29 + // menus 1.30 + QMenu *mfile = menuBar()->addMenu("&File"); 1.31 + mfile->addAction(act_open); 1.32 + mfile->addAction(act_quit); 1.33 + 1.34 + // toolbars 1.35 + QToolBar *tfile = addToolBar("&File"); 1.36 + tfile->addAction(act_open); 1.37 + 1.38 + statusBar(); 1.39 + show(); 1.40 +} 1.41 + 1.42 +void MainWindow::open_volume() 1.43 +{ 1.44 + QString fname = QFileDialog::getOpenFileName(this, "Open volume dataset", QString(), "Volume descriptors (*.vol)"); 1.45 + if(!fname.isNull()) { 1.46 + Volume *vol = new Volume; 1.47 + 1.48 + if(vol->load(qPrintable(fname))) { 1.49 + delete volume; 1.50 + volume = vol; 1.51 + volray_setvolume(vol); 1.52 + post_redisplay(); 1.53 + } else { 1.54 + delete vol; 1.55 + } 1.56 + } 1.57 +} 1.58 + 1.59 +void post_redisplay() 1.60 +{ 1.61 + glview->updateGL(); 1.62 +} 1.63 + 1.64 + 1.65 +GLView::GLView(QWidget *parent) 1.66 + : QGLWidget(QGLFormat(QGL::DoubleBuffer), parent) 1.67 +{ 1.68 +} 1.69 + 1.70 +QSize GLView::minimumSizeHint() const 1.71 +{ 1.72 + return QSize(320, 200); 1.73 +} 1.74 + 1.75 +QSize GLView::sizeHint() const 1.76 +{ 1.77 + return QSize(1280, 800); 1.78 +} 1.79 + 1.80 +void GLView::initializeGL() 1.81 +{ 1.82 + if(!volray_init()) { 1.83 + exit(0); 1.84 + } 1.85 + glClearColor(1, 0, 0, 1); 1.86 +} 1.87 + 1.88 +void GLView::resizeGL(int xsz, int ysz) 1.89 +{ 1.90 + volray_resize(xsz, ysz); 1.91 +} 1.92 + 1.93 +void GLView::paintGL() 1.94 +{ 1.95 + volray_draw(); 1.96 +} 1.97 + 1.98 +static int button_number(Qt::MouseButton bn) 1.99 +{ 1.100 + switch(bn) { 1.101 + case Qt::LeftButton: 1.102 + return 0; 1.103 + case Qt::MidButton: 1.104 + return 1; 1.105 + case Qt::RightButton: 1.106 + return 2; 1.107 + default: 1.108 + break; 1.109 + } 1.110 + return -1; 1.111 +} 1.112 + 1.113 +void GLView::mousePressEvent(QMouseEvent *ev) 1.114 +{ 1.115 + int bn = button_number(ev->button()); 1.116 + if(bn >= 0) { 1.117 + volray_mouse(bn, 1, ev->x(), ev->y()); 1.118 + } 1.119 +} 1.120 + 1.121 +void GLView::mouseReleaseEvent(QMouseEvent *ev) 1.122 +{ 1.123 + int bn = button_number(ev->button()); 1.124 + if(bn >= 0) { 1.125 + volray_mouse(bn, 0, ev->x(), ev->y()); 1.126 + } 1.127 +} 1.128 + 1.129 +void GLView::mouseMoveEvent(QMouseEvent *ev) 1.130 +{ 1.131 + volray_motion(ev->x(), ev->y()); 1.132 +}