make sure we show the start of our sequence name
[mussa.git] / qui / seqbrowser / SequenceDescription.cpp
1 #include <QVBoxLayout>
2 #include "qui/seqbrowser/SequenceDescription.hpp"
3
4 using namespace std;
5
6 SequenceDescription::SequenceDescription(QWidget *parent)
7   : QFrame(parent), 
8     pos(-1) // set pos to an invalid value so the setPos function will call
9             // setText
10 {
11   createWidget();
12 }
13
14 SequenceDescription::SequenceDescription(
15     boost::shared_ptr<GlSequence> glseq,
16     QWidget *parent
17 ) : QFrame(parent) 
18 {
19   setGlSequence(glseq);  
20 }
21
22 void SequenceDescription::createWidget()
23 {
24   setFrameStyle(QFrame::Panel | QFrame::Sunken);
25   setLineWidth(1);
26   QLayout *layout = new QVBoxLayout;
27   layout->addWidget(&name_label);
28   layout->addWidget(&length_label);
29   layout->addWidget(&position_label);
30   name_label.setMaximumWidth(length_label.fontMetrics().width("01234567"));
31   setLayout(layout);
32
33   connect(&name_label, SIGNAL(textChanged(const QString& )),
34           this, SLOT(setName(const QString& )));
35 }
36
37 void SequenceDescription::setGlSequence(
38   boost::shared_ptr<GlSequence> glseq  )
39 {
40   if (glseq != glsequence_) {
41     glsequence_ = glseq;
42     setName(glsequence_->sequence()->get_species());
43     setLength(glsequence_->sequence()->length());
44     emit glsequenceChanged(glsequence_);
45   }
46 }
47
48 boost::shared_ptr<GlSequence> SequenceDescription::glsequence() 
49 {
50   return glsequence_;
51 }
52
53 void SequenceDescription::setName(std::string name)
54 {
55   setName(QString(name.c_str()));
56 }
57
58 void SequenceDescription::setName(const QString& name_)
59 {
60   std::string std_name_ = name_.toStdString();
61
62   if (std_name_ != glsequence_->sequence()->get_species()) {
63     glsequence_->sequence()->set_species(std_name_);
64     emit nameChanged(name_);
65   }
66
67     // no need to setText again if its because of user editing
68   if (name_ != name_label.text()) {
69     name_label.setText(name_);
70     name_label.setCursorPosition(0);
71   }
72 }
73
74 std::string SequenceDescription::name() const 
75
76   if (glsequence_) 
77     return glsequence_->sequence()->get_species();
78   else
79     return string("");
80 }
81
82 void SequenceDescription::setLength(int length)
83 {
84   QString s;
85   float short_length = length;
86   if (short_length > 1000000) {
87     short_length /= 1000000;
88     s.setNum(short_length, 'f', 2);
89     s += "mb";
90   } if (short_length > 1000) {
91     short_length /= 1000;
92     s.setNum(short_length, 'f', 2);
93     s += "kb";
94   } else {
95     s.setNum(short_length);
96     s += "b";
97   }
98   length_label.setText(s);
99 }
100
101 int SequenceDescription::length() const 
102 {
103   return 0;
104
105   if (glsequence_)
106     return glsequence_->sequence()->size();
107   else
108     return 0;
109 }
110
111 void SequenceDescription::setPosition(int pos_) 
112
113   QString s; 
114   if (pos != pos_) {
115     pos = pos_;
116     position_label.setText(s.setNum(pos_));
117     emit positionChanged(pos);
118   }
119 }
120
121 int SequenceDescription::position() const
122 {
123   return pos;
124 }