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