Allow changing the name for a sequence in the sequence browser.
[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   }
71 }
72
73 std::string SequenceDescription::name() const 
74
75   if (glsequence_) 
76     return glsequence_->sequence()->get_species();
77   else
78     return string("");
79 }
80
81 void SequenceDescription::setLength(int length)
82 {
83   QString s;
84   float short_length = length;
85   if (short_length > 1000000) {
86     short_length /= 1000000;
87     s.setNum(short_length, 'f', 2);
88     s += "mb";
89   } if (short_length > 1000) {
90     short_length /= 1000;
91     s.setNum(short_length, 'f', 2);
92     s += "kb";
93   } else {
94     s.setNum(short_length);
95     s += "b";
96   }
97   length_label.setText(s);
98 }
99
100 int SequenceDescription::length() const 
101 {
102   return 0;
103
104   if (glsequence_)
105     return glsequence_->sequence()->size();
106   else
107     return 0;
108 }
109
110 void SequenceDescription::setPosition(int pos_) 
111
112   QString s; 
113   if (pos != pos_) {
114     pos = pos_;
115     position_label.setText(s.setNum(pos_));
116     emit positionChanged(pos);
117   }
118 }
119
120 int SequenceDescription::position() const
121 {
122   return pos;
123 }