5c2a527b11de47b6b20ce7bc2cfc378f04035a98
[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   layout->addWidget(name_label);
37   layout->addWidget(position_label);
38   layout->addWidget(length_label);
39   name_label->setMaximumWidth(length_label->fontMetrics().width("01234567"));
40   setLayout(layout);
41   connect(name_label, SIGNAL(textChanged(const QString& )),
42           this, SLOT(setName(const QString& )));
43 }
44
45 void SequenceDescription::setGlSequence(
46   boost::shared_ptr<GlSequence> glseq  )
47 {
48   if (glseq != glsequence_) {
49     glsequence_ = glseq;
50     setName(glsequence_->sequence()->get_species());
51     setLength(glsequence_->sequence()->length());
52     emit glsequenceChanged(glsequence_);
53   }
54 }
55
56 boost::shared_ptr<GlSequence> SequenceDescription::glsequence() 
57 {
58   return glsequence_;
59 }
60
61 void SequenceDescription::setName(std::string name)
62 {
63   setName(QString(name.c_str()));
64 }
65
66 void SequenceDescription::setName(const QString& name_)
67 {
68   std::string std_name_ = name_.toStdString();
69
70   if (std_name_ != glsequence_->sequence()->get_species()) {
71     glsequence_->sequence()->set_species(std_name_);
72     emit nameChanged(name_);
73   }
74
75     // no need to setText again if its because of user editing
76   if (name_ != name_label->text()) {
77     name_label->setText(name_);
78     name_label->setCursorPosition(0);
79   }
80 }
81
82 std::string SequenceDescription::name() const 
83
84   if (glsequence_) 
85     return glsequence_->sequence()->get_species();
86   else
87     return string("");
88 }
89
90 void SequenceDescription::setLength(int length)
91 {
92   QString s;
93   float short_length = length;
94   if (short_length > 1000000) {
95     short_length /= 1000000;
96     s.setNum(short_length, 'f', 2);
97     s += "mb";
98   } if (short_length > 1000) {
99     short_length /= 1000;
100     s.setNum(short_length, 'f', 2);
101     s += "kb";
102   } else {
103     s.setNum(short_length);
104     s += "b";
105   }
106   length_label->setText(s);
107 }
108
109 int SequenceDescription::length() const 
110 {
111   return 0;
112
113   if (glsequence_)
114     return glsequence_->sequence()->size();
115   else
116     return 0;
117 }
118
119 void SequenceDescription::setPosition(int pos_) 
120
121   QString s; 
122   if (pos != pos_) {
123     pos = pos_;
124     position_label->setText(s.setNum(pos_));
125     emit positionChanged(pos);
126   }
127 }
128
129 int SequenceDescription::position() const
130 {
131   return pos;
132 }