refactored PathScene into SequenceBrowserWidget
[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 }
100
101 void SequenceBrowser::paintGL()
102 {
103   GlSeqBrowser::paintGL();
104 }
105
106 void SequenceBrowser::mousePressEvent( QMouseEvent *e)
107 {
108   drawingBand = true;
109
110   selectedMode = false;
111   bandOrigin = e->pos();
112   if (!rubberBand)
113     rubberBand = new QRubberBand(QRubberBand::Rectangle, this);
114   
115   rubberBand->setGeometry(QRect(bandOrigin, QSize()));
116   rubberBand->show();
117 }
118
119 void SequenceBrowser::mouseMoveEvent( QMouseEvent *e)
120 {
121   if (drawingBand)
122     rubberBand->setGeometry(QRect(bandOrigin, e->pos()).normalized());
123 }
124
125 void SequenceBrowser::mouseReleaseEvent( QMouseEvent *e)
126 {
127   drawingBand = false;
128   rubberBand->hide();
129   QRect r = QRect(bandOrigin, e->pos()).normalized();
130   bandOrigin = r.topLeft();
131
132   selectRegion(r.top(), r.left(), r.bottom(), r.right());
133 }