spurious mouseReleaseEvent caused a crash
[mussa.git] / qui / seqbrowser / SequenceBrowser.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/seqbrowser/SequenceBrowser.hpp"
15 #include "mussa_exceptions.hpp"
16
17 using namespace std;
18
19 SequenceBrowser::SequenceBrowser(QWidget *parent)
20   : QGLWidget(parent),
21     rubberBand(0),
22     drawingBand(false)
23
24 }
25
26 QSize SequenceBrowser::sizeHint() const
27 {
28   //return QSize((int)GlSeqBrowser::viewportHeight(), (int)GlSeqBrowser::viewportWidth());
29   return QSize(600, 400);
30 }
31
32 void SequenceBrowser::setViewportCenter(float x)
33 {
34   const float epsilon = 1e-10;
35   float center = GlSeqBrowser::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     GlSeqBrowser::setViewportCenter(x);
47     emit viewportChanged();
48     update();
49   }
50 }
51
52 void SequenceBrowser::setZoom(int new_zoom)
53 {
54   if (new_zoom != GlSeqBrowser::zoom()) {
55     GlSeqBrowser::setZoom(new_zoom);
56     emit viewportChanged();
57     update();
58   }
59 }
60
61 void SequenceBrowser::setClipPlane(int )
62 {
63 /*
64   if (clipZ != (double) newZ){
65     clipZ = (double) newZ;
66     update();
67   }
68 */
69 }
70
71 void SequenceBrowser::clear()
72 {
73   GlSeqBrowser::clear();
74   emit tracksChanged();
75 }
76
77 void SequenceBrowser::push_sequence(const Sequence &s)
78 {
79   GlSeqBrowser::push_sequence(s);
80   emit tracksChanged();
81 }
82
83 void SequenceBrowser::push_sequence(GlSequence &gs)
84 {
85   GlSeqBrowser::push_sequence(gs);
86   emit tracksChanged();
87 }
88
89 ////////////////////
90 // Rendering code
91 void SequenceBrowser::initializeGL()
92 {
93   GlSeqBrowser::initializeGL();
94 }
95
96 void SequenceBrowser::resizeGL(int width, int height)
97 {
98   GlSeqBrowser::resizeGL(width, height);
99   emit viewportChanged();
100 }
101
102 void SequenceBrowser::paintGL()
103 {
104   GlSeqBrowser::paintGL();
105 }
106
107 void SequenceBrowser::mousePressEvent( QMouseEvent *e)
108 {
109   drawingBand = true;
110
111   selectedMode = false;
112   bandOrigin = e->pos();
113   if (!rubberBand)
114     rubberBand = new QRubberBand(QRubberBand::Rectangle, this);
115   
116   rubberBand->setGeometry(QRect(bandOrigin, QSize()));
117   rubberBand->show();
118 }
119
120 void SequenceBrowser::mouseMoveEvent( QMouseEvent *e)
121 {
122   if (drawingBand)
123     rubberBand->setGeometry(QRect(bandOrigin, e->pos()).normalized());
124 }
125
126 void SequenceBrowser::mouseReleaseEvent( QMouseEvent *e)
127 {
128   drawingBand = false;
129   if (rubberBand != 0) {
130     rubberBand->hide();
131     QRect r = QRect(bandOrigin, e->pos()).normalized();
132     bandOrigin = r.topLeft();
133   
134     selectRegion(r.top(), r.left(), r.bottom(), r.right());
135   }
136 }