qvolray

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