Move zoom spinbox into its own widget
[mussa.git] / alg / glseqbrowser.hpp
1 #ifndef _GLTRACKS_H_
2 #define _GLTRACKS_H_
3
4 #include <map>
5 #include <set>
6 #include <vector>
7
8 #include "alg/annotation_colors.hpp"
9 #include "alg/sequence.hpp"
10 #include "alg/glsequence.hpp"
11
12 //! Manage rendering a collection of glSequences
13 class GlSeqBrowser
14 {
15 public:
16   GlSeqBrowser();
17   GlSeqBrowser(const GlSeqBrowser&);
18
19   //! setup the opengl canvas
20   void initializeGL();
21   //! called when our screen canvas is resized
22   void resizeGL(int width, int height);
23   //! render our scene
24   void paintGL() const;
25
26   //! select a region (using canvas coordinates)
27   void GlSeqBrowser::selectRegion(int top, int left, int bottom, int right);
28
29   //! border size
30   float border() const;
31
32   //! max world left coordinate
33   float left() const;
34   //! max world right coordinate
35   float right() const;
36
37   void setViewportCenter(float x);
38   float viewportLeft() const;
39   float viewportCenter() const;
40   float viewportRight() const;
41   float viewportHeight() const;
42   float viewportWidth() const;
43
44   //! zoom out far enough to show the full size of the sequence
45   double zoomOut();
46   //! zoom in to a reasonable sequence view
47   double zoomToSequence();
48   //! set the current zoom level in (base pairs / pix )
49   /*! its a double as zoom levels of [0.01, 1.0] make nice sequence views
50    */
51   void setZoom(double zoom_level);
52   //! returns the current zoom level
53   double zoom() const;
54
55   void setColorMapper(AnnotationColors& cm);
56   AnnotationColors& colorMapper();
57
58   //! clear our tracks and connections
59   void clear();
60
61   //! add a sequence to the back of our track container
62   void push_sequence(const Sequence &s);
63   //! add a glsequence to the back of our track container
64   void push_sequence(GlSequence &s);
65   //! return our track container
66   const std::vector<GlSequence>& sequences() const;
67
68   //! clear all the line segments between all the sequences
69   void clear_links();
70   //! define a path
71   void link(const std::vector<int>& path, const std::vector<bool>& isRC, int length);
72   //! returns the index of pathids based on order added by link
73   const std::set<int>& selectedPaths() const;
74   //! center the provided path in the current viewport
75   void centerOnPath(const std::vector<int>&);
76
77   //! Provide a logical name for a type discriminator for our glName stack
78   enum FeatureType { MussaTrack, MussaSegment };
79
80   //! a useful point class
81   template<class T> struct point {
82     T x;
83     T y;
84
85     point(T x_, T y_):x(x_), y(y_) {}
86   };
87
88   //! a useful rectangle, where 0,0 is in the lower left
89   template<class T> struct rect {
90     T top;
91     T left;
92     T bottom;
93     T right;
94
95     rect() {}
96     rect(T t, T l, T b, T r) : top(t), left(l), bottom(b), right(r) {}
97     T width() { return right - left; }
98     T height() { return top - bottom; }
99   };
100
101   struct Segment
102   {
103     point<float> start;
104     point<float> end;
105     bool reversed;
106     std::set<int> path_ids;
107
108     Segment() : start(0.0, 0.0), end(0.0, 0.0) {}
109     Segment(float x1, float y1, float x2, float y2, bool isRC) 
110       : start(x1, y1), end(x2, y2), reversed(isRC) {}
111   };
112
113   //! data structure holding our line segments
114   /*! the vector is of size track_container.size()-1
115    *  it's indexed by the pair x1, x2 (the two x coordinates between
116    *  the two tracks
117    */
118   typedef std::pair<int, int> segment_key;
119   typedef std::map<segment_key, Segment> pair_segment_map;
120   typedef std::vector<pair_segment_map> path_segment_map_vector;
121   path_segment_map_vector path_segments;
122 private:
123   //! recalculate the viewable world
124   /*! depending on the size of our canvas, our zoom level and
125    *  how far we've been offset
126    */
127   void update_viewport(float center, double new_zoom);
128
129   //! determine where all the tracks should be placed
130   void update_layout();
131
132   //! convert opengl selections into the list of paths we should highlight
133   void processSelection(GLuint hits, GLuint buffer[], GLuint bufsize);
134
135   //! master scene drawing function
136   /*! draw is broken out for the opengl selection code
137    */
138   void draw() const;
139   //! draw glsequence tracks
140   void draw_tracks() const;
141   //! draw line segments between tracks
142   void draw_segments() const;
143   //! draw selection box
144   void draw_selection() const;
145
146   //! number of pixels to reserve around the edges of our canvas
147   const int border_width;
148   //! the current viewable region (world coord)
149   rect<float> cur_ortho;
150   //! how many pixels our viewport is (screen coord)
151   point<int> viewport_size;
152   //! the center of our current viewport (world coord) (used for scrollbar)
153   float viewport_center;
154   double zoom_level;
155   AnnotationColors color_mapper;
156   //! container of all the GlSequences loaded into our scene
157   std::vector<GlSequence> track_container;
158   //! counter for each path added to us via connect
159   int pathid;
160
161 protected:
162   //! where to draw our box 
163   rect<float> selectedRegion;
164   //! true if we have a selection
165   bool selectedMode;
166   //! indicate which paths are selected
167   std::set<int> selected_paths;
168   //! which track is selected (it only makes sense to have one track selected).
169   std::set<int> selected_tracks;
170
171 };
172 #endif