WhatsThis update
[mussa.git] / qui / seqbrowser / SequenceDescription.cpp
1 #include "qui/seqbrowser/SequenceDescription.hpp"
2
3 using namespace std;
4
5 SequenceDescription::SequenceDescription(QWidget *parent)
6   : QFrame(parent),
7     layout(0),
8     name_label(0),
9     position_label(0),
10     length_label(0),
11     pos(-1) // set pos to an invalid value so the setPos function will call
12             // setText
13 {
14   setupWidgets();
15 }
16
17 SequenceDescription::SequenceDescription(
18     boost::shared_ptr<GlSequence> glseq,
19     QWidget *parent
20 ) : QFrame(parent) 
21 {
22   setupWidgets();
23   setGlSequence(glseq);  
24 }
25
26 void SequenceDescription::setupWidgets()
27 {
28   layout = new QVBoxLayout;
29   name_label = new QLineEdit;
30   position_label = new QLabel;
31   length_label = new QLabel;
32   
33   setFrameStyle(QFrame::Panel | QFrame::Sunken);
34   setLineWidth(1);
35
36   // What's this feature descriptions
37   // FIXME: The '*' is added because for some reason
38   // Qt's whatsthis feature is chopping off the last word
39   // If the '*' eventually shows up in a whatsthis pop-up
40   // then the '*' should be removed.
41   name_label->setWhatsThis("Name of sequence *");
42   position_label->setWhatsThis("Edge of viewport base pair position *");
43   length_label->setWhatsThis("Total length of sequence *");
44   
45   layout->addWidget(name_label);
46   layout->addWidget(position_label);
47   layout->addWidget(length_label);
48   name_label->setMaximumWidth(length_label->fontMetrics().width("01234567"));
49   setLayout(layout);
50   connect(name_label, SIGNAL(textChanged(const QString& )),
51           this, SLOT(setName(const QString& )));
52 }
53
54 void SequenceDescription::setGlSequence(
55   boost::shared_ptr<GlSequence> glseq  )
56 {
57   if (glseq != glsequence_) {
58     glsequence_ = glseq;
59     setName(glsequence_->sequence()->get_species());
60     setLength(glsequence_->sequence()->length());
61     emit glsequenceChanged(glsequence_);
62   }
63 }
64
65 boost::shared_ptr<GlSequence> SequenceDescription::glsequence() 
66 {
67   return glsequence_;
68 }
69
70 void SequenceDescription::setName(std::string name)
71 {
72   setName(QString(name.c_str()));
73 }
74
75 void SequenceDescription::setName(const QString& name_)
76 {
77   std::string std_name_ = name_.toStdString();
78
79   if (std_name_ != glsequence_->sequence()->get_species()) {
80     glsequence_->sequence()->set_species(std_name_);
81     emit nameChanged(name_);
82   }
83
84     // no need to setText again if its because of user editing
85   if (name_ != name_label->text()) {
86     name_label->setText(name_);
87     name_label->setCursorPosition(0);
88   }
89 }
90
91 std::string SequenceDescription::name() const 
92
93   if (glsequence_) 
94     return glsequence_->sequence()->get_species();
95   else
96     return string("");
97 }
98
99 void SequenceDescription::setLength(int length)
100 {
101   QString s;
102   float short_length = length;
103   if (short_length > 1000000) {
104     short_length /= 1000000;
105     s.setNum(short_length, 'f', 2);
106     s += "mb";
107   } if (short_length > 1000) {
108     short_length /= 1000;
109     s.setNum(short_length, 'f', 2);
110     s += "kb";
111   } else {
112     s.setNum(short_length);
113     s += "b";
114   }
115   length_label->setText(s);
116 }
117
118 int SequenceDescription::length() const 
119 {
120   return 0;
121
122   if (glsequence_)
123     return glsequence_->sequence()->size();
124   else
125     return 0;
126 }
127
128 void SequenceDescription::setPosition(int pos_) 
129
130   QString s; 
131   if (pos != pos_) {
132     pos = pos_;
133     position_label->setText(s.setNum(pos_));
134     emit positionChanged(pos);
135   }
136 }
137
138 int SequenceDescription::position() const
139 {
140   return pos;
141 }