incorporate drawable and annotations
[mussa.git] / alg / glsequence.cpp
index 73fc4daaeeac20b95fe27769ca2c613748cd0534..17fb561c830c5cf3fc241a01e8de2353c4cc8691 100644 (file)
@@ -9,83 +9,96 @@ using namespace std;
 GlSequence::GlSequence(const Sequence &s, 
                        boost::shared_ptr<AnnotationColors> cm) 
   : Sequence(s),
-    seq_x(0.0), 
-    seq_y(0.0), 
-    seq_z(1.0), 
-    seq_height(12.0),
     color_mapper(cm),
-    drawColor(new Color(0.0, 0.0, 0.0)),
     char_pix_per_world_unit(2.5)
 {
+  seq->setDrawable(default_drawable());
 }
 
 GlSequence::GlSequence(const GlSequence &s)
   : Sequence(s),
-    seq_x(s.seq_x),
-    seq_y(s.seq_y),
-    seq_z(s.seq_z),
-    seq_height(s.seq_height),
     color_mapper(s.color_mapper),
-    drawColor(s.drawColor),
     char_pix_per_world_unit(s.char_pix_per_world_unit)
 {
+  seq->setDrawable(copy_drawable(s.seq->drawable()));
 }
 
 GlSequence::GlSequence(const GlSequence *s)
   : Sequence(s),
-    seq_x(s->seq_x),
-    seq_y(s->seq_y),
-    seq_z(s->seq_z),
-    seq_height(s->seq_height),
     color_mapper(s->color_mapper),
-    drawColor(s->drawColor),
     char_pix_per_world_unit(s->char_pix_per_world_unit)
 {
+  seq->setDrawable(copy_drawable(s->seq->drawable()));
+}
+
+GlSequence::GlSequence(const GlSequenceRef s)
+  : Sequence( (SequenceRef)s ),
+    color_mapper(s->color_mapper),
+    char_pix_per_world_unit(s->char_pix_per_world_unit)
+{
+  seq->setDrawable(copy_drawable(s->seq->drawable()));
 }
 
 GlSequence &GlSequence::operator=(const GlSequence & s)
 {
   if (this != &s) {
     Sequence::operator=(s);
-    seq_x = s.seq_x;
-    seq_y = s.seq_y;
-    seq_z = s.seq_z;
-    seq_height = s.seq_height;
+    seq->setDrawable(copy_drawable(s.seq->drawable()));
     color_mapper = s.color_mapper;
-    drawColor = s.drawColor;
     assert(char_pix_per_world_unit == s.char_pix_per_world_unit);
   }
   return *this;
 }
 
-void GlSequence::setX(GLfloat value)
+DrawableRef GlSequence::default_drawable()
 {
-  seq_x = value;
+  ColorRef c(new Color(0.0, 0.0, 0.0));
+  DrawableRef d(new Drawable(0.0, 0.0, 1.0, 12.0, c));
+  return d;
 }
 
-GLfloat GlSequence::x() const
+DrawableRef GlSequence::copy_drawable(DrawableRef old_d)
 {
-  return seq_x;
+  ColorRef c(old_d->color());
+  DrawableRef d(new Drawable(old_d));
+  // use the same color
+  d->setColor(c);
+  return d;
 }
 
-GLfloat GlSequence::right() const
+void GlSequence::setX(float value)
 {
-  return size()+seq_x;
+  seq->drawable()->setX(value);
+}
+
+float GlSequence::x() const
+{
+  return seq->drawable()->x();
 }
 
 void GlSequence::setY(GLfloat value)
 {
-  seq_y = value;
+  seq->drawable()->setY(value);
 }
 
-GLfloat GlSequence::y() const
+float GlSequence::y() const
 {
-  return seq_y;
+  return seq->drawable()->y();
 }
 
-GLfloat GlSequence::height() const
+float GlSequence::z() const
 {
-  return seq_height;
+  return seq->drawable()->z();
+}
+
+float GlSequence::height() const
+{
+  return seq->drawable()->height();
+}
+
+GLfloat GlSequence::right() const
+{
+  return size()+x();
 }
 
 GLfloat GlSequence::size() const
@@ -95,7 +108,7 @@ GLfloat GlSequence::size() const
 
 Sequence::size_type GlSequence::leftbase(GLfloat left) const
 {
-  left = ceil(left - seq_x);
+  left = ceil(left - x());
   if (left < 0)
     return 0;
   else if (left > Sequence::size() )
@@ -106,7 +119,7 @@ Sequence::size_type GlSequence::leftbase(GLfloat left) const
 
 Sequence::size_type GlSequence::rightbase(GLfloat right) const
 {
-  right = floor(right) - seq_x;
+  right = floor(right) - x();
   if (right > Sequence::size())
     return Sequence::size();
   else if ( right < 0) 
@@ -137,21 +150,31 @@ GlSequence GlSequence::subseq(size_type start, size_type count) const
 {
   GlSequence new_seq(*this);
   new_seq.seq = seq->subseq(start, count);
+  // make sure our subseq has a drawable attached to it
+  // perhaps we should figure out correct x,y,z,h coords
+  DrawableRef d(default_drawable());
+  // and default to our current color
+  ColorRef c(color());
+  d->setColor(c);
+  new_seq.seq->setDrawable(d);
   copy_children(new_seq, start, count);
   
   return new_seq;
 }
 
-//! set default track draw color 
-void GlSequence::setColor(boost::shared_ptr<Color> &c)
+void GlSequence::setColor(ColorRef &c)
+{
+  seq->drawable()->setColor(c);
+}
+
+ColorRef GlSequence::color()
 {
-  drawColor = c;
+  return seq->drawable()->color();
 }
 
-//! get default track draw color
-boost::shared_ptr<Color> GlSequence::color()
+const ColorRef GlSequence::color() const
 {
-  return drawColor;
+  return seq->drawable()->color();
 }
 
 int GlSequence::get_viewport_width_in_pixels()
@@ -207,8 +230,8 @@ void GlSequence::draw_box(GLfloat world_left, GLfloat world_right,
 {
   GLfloat pixel_width = pixelWidth(world_left, world_right);
   GLfloat offset = height/2.0;
-  GLfloat top = seq_y + offset;
-  GLfloat bottom = seq_y - offset;
+  GLfloat top = y() + offset;
+  GLfloat bottom = y() - offset;
   
   // make our box be at least 1 pixel
   if ((right-left) < pixel_width) {
@@ -224,15 +247,15 @@ void GlSequence::draw_box(GLfloat world_left, GLfloat world_right,
 
 void GlSequence::draw_track(GLfloat left, GLfloat right) const
 {
-  glColor3fv(drawColor->get());
+  glColor3fv(color()->get());
   // draw main sequence track
-  draw_box(left, right, seq_x, seq_x+Sequence::size(), seq_height, 0.0);
+  draw_box(left, right, x(), x()+Sequence::size(), height(), 0.0);
 }
 
 void GlSequence::draw_annotations(GLfloat left, GLfloat right) const
 {
   // draw annotations
-  GLfloat annotation_z = seq_z + 10.0;
+  GLfloat annotation_z = z() + 10.0;
   const std::list<annot>& annots = Sequence::annotations();
   const std::list<motif>& motifs = Sequence::motifs();
   for (std::list<annot>::const_iterator annot_itor = annots.begin();
@@ -240,8 +263,8 @@ void GlSequence::draw_annotations(GLfloat left, GLfloat right) const
        ++annot_itor)
   {
     glColor3f(0.0, 0.8, 0.0);
-    draw_box(left, right, seq_x+annot_itor->begin, seq_x+annot_itor->end, 
-             seq_height, annotation_z);
+    draw_box(left, right, x()+annot_itor->begin, x()+annot_itor->end, 
+             height(), annotation_z);
   }
   // if motifs?
   for (std::list<motif>::const_iterator motifs_itor = motifs.begin();
@@ -249,8 +272,8 @@ void GlSequence::draw_annotations(GLfloat left, GLfloat right) const
        ++motifs_itor)
   {
     glColor3fv(color_mapper->lookup("motif", motifs_itor->sequence).get());
-    draw_box(left, right, seq_x+motifs_itor->begin, seq_x+motifs_itor->end, 
-             seq_height, annotation_z+1.0);
+    draw_box(left, right, x()+motifs_itor->begin, x()+motifs_itor->end, 
+             height(), annotation_z+1.0);
   }
 
 }
@@ -321,7 +344,7 @@ void GlSequence::draw_sequence(GLfloat left, GLfloat right) const
   // FIXME: basically this needs to be greater than the number of annotations
   const GLfloat z = 30;
   glLineWidth(1);
-  glColor3fv(drawColor->get());
+  glColor3fv(color()->get());
 
   Sequence::const_iterator seq_itor = region_begin(left, right);
   Sequence::const_iterator seq_end = region_end(left, right);
@@ -337,7 +360,7 @@ void GlSequence::draw_sequence(GLfloat left, GLfloat right) const
   {
     assert ( basepair < Sequence::size() );
     glPushMatrix();
-    glTranslatef( seq_x+leftbase(left) + basepair + glyph_margin, seq_y, 1.0 );
+    glTranslatef( x()+leftbase(left) + basepair + glyph_margin, y(), 1.0 );
     glScalef(glyph_x_scale, 1.0, 1.0);
     switch (*seq_itor) {
       case 'A': case 'a':
@@ -367,10 +390,10 @@ void GlSequence::draw_sequence(GLfloat left, GLfloat right) const
 
 bool operator==(const GlSequence &left, const GlSequence &right)
 {
-  return ( (left.seq_x == right.seq_x) and
-           (left.seq_y == right.seq_y) and
-           (left.seq_z == right.seq_z) and
-           (left.seq_height == right.seq_height) and
-           (left.drawColor == right.drawColor));
+  return ( (left.x() == right.x()) and
+           (left.y() == right.y()) and
+           (left.z() == right.z()) and
+           (left.height() == right.height()) and
+           (left.color() == right.color()));
 }