ccbd7e0f0e758ea6686f908aa9fef01f0324d651
[mussa.git] / qui / PathScene.cpp
1 #include <QDir>
2 #include <QFileDialog>
3 #include <QMessageBox>
4 #include <QMouseEvent>
5 #include <QRubberBand>
6 #include <QRect>
7 #include <QString>
8 #include <iostream>
9 #include <set>
10
11 #include <GL/gl.h>
12 #include <math.h>
13
14 #include "qui/PathScene.hpp"
15 #include "alg/glsequence.hpp"
16 #include "mussa_exceptions.hpp"
17
18 using namespace std;
19
20 PathScene::PathScene(QWidget *parent)
21   : QGLWidget(parent),
22     rubberBand(0),
23     drawingBand(false)
24
25 }
26
27 QSize PathScene::sizeHint() const
28 {
29   return QSize((int)GlTracks::viewportHeight(), (int)GlTracks::viewportWidth());
30 }
31
32 void PathScene::setViewportCenter(float x)
33 {
34   const float epsilon = 1e-10;
35   float center = GlTracks::viewportCenter();
36   float difference = fabsf(x - center);
37   float abs_x = fabsf(x);
38   center = fabsf(center);
39
40   // the difference < epsilon * val is one of the recommended tests
41   // for float equality.
42   // of course since we're looking for not equals, we need to toss a
43   // not at the beginning
44   if (not (difference < epsilon * abs_x or difference < epsilon * center))
45   {
46     GlTracks::setViewportCenter(x);
47     emit viewportChanged();
48     update();
49   }
50 }
51
52 void PathScene::setZoom(int new_zoom)
53 {
54   if (new_zoom != GlTracks::zoom()) {
55     GlTracks::setZoom(new_zoom);
56     emit viewportChanged();
57     update();
58   }
59 }
60
61 void PathScene::setClipPlane(int )
62 {
63 /*
64   if (clipZ != (double) newZ){
65     clipZ = (double) newZ;
66     update();
67   }
68 */
69 }
70
71 ////////////////////
72 // Rendering code
73 void PathScene::initializeGL()
74 {
75   GlTracks::initializeGL();
76 }
77
78 void PathScene::resizeGL(int width, int height)
79 {
80   GlTracks::resizeGL(width, height);
81 }
82
83 void PathScene::paintGL()
84 {
85   GlTracks::paintGL();
86 }
87
88 void PathScene::mousePressEvent( QMouseEvent *e)
89 {
90   drawingBand = true;
91
92   selectedMode = false;
93   bandOrigin = e->pos();
94   if (!rubberBand)
95     rubberBand = new QRubberBand(QRubberBand::Rectangle, this);
96   
97   rubberBand->setGeometry(QRect(bandOrigin, QSize()));
98   rubberBand->show();
99 }
100
101 void PathScene::mouseMoveEvent( QMouseEvent *e)
102 {
103   if (drawingBand)
104     rubberBand->setGeometry(QRect(bandOrigin, e->pos()).normalized());
105 }
106
107 void PathScene::mouseReleaseEvent( QMouseEvent *e)
108 {
109   drawingBand = false;
110   rubberBand->hide();
111   QRect r = QRect(bandOrigin, e->pos()).normalized();
112   bandOrigin = r.topLeft();
113
114   selectRegion(r.top(), r.left(), r.bottom(), r.right());
115 }