Update mussa to build on ubuntu 10.04 with qt 4.6.2 +boost 1.40.0.1
[mussa.git] / qui / ThresholdWidget.cpp
1 #include <QSlider>
2 #include <QString>
3
4 #include "qui/ThresholdWidget.hpp"
5 #include <iostream>
6 #include <math.h>
7
8 // This is completely and totally derived from the Qt example
9 // LCDRange.cpp
10 ThresholdWidget::ThresholdWidget(QWidget *parent, int min, int max) 
11   : QWidget(parent),
12     layout(0),
13     cur_bp_threshold(0),
14     basepair_spinner(0),
15     window_size_label(0),
16     percent_label(0)
17 {
18   setupWidgets();
19   basepair_spinner->setWhatsThis("How many base pairs need to be conserved in the window size");
20
21   setRange(min, max);
22
23   connect(basepair_spinner, SIGNAL(valueChanged(int)), 
24           this, SLOT(setBasepairThreshold(int)));
25
26   basepair_spinner->setValue(min);
27   updatePercentThreshold();
28
29   layout->addWidget(new QLabel("Threshold"));
30   layout->addWidget(basepair_spinner);
31   layout->addWidget(window_size_label);
32   layout->addWidget(percent_label);
33   setLayout(layout);
34 }
35
36 void ThresholdWidget::setupWidgets()
37 {
38   layout = new QHBoxLayout(this);
39   basepair_spinner = new QSpinBox(this);
40   window_size_label = new QLabel(this);
41   percent_label = new QLabel(this);
42 }
43   
44 void ThresholdWidget::setRange(int min, int max)
45 {
46   basepair_spinner->setRange(min, max);
47   if (cur_bp_threshold < min || cur_bp_threshold > max)
48   {
49     cur_bp_threshold = min;
50     basepair_spinner->setValue(min);
51   }
52
53   QString num;
54   num.setNum(max);
55   window_size_label->setText("/"+num);
56   updatePercentThreshold();
57 }
58
59 bool ThresholdWidget::isReadOnly() const
60 {
61   return basepair_spinner->isReadOnly();
62 }
63
64 void ThresholdWidget::setReadOnly(bool read_only)
65 {
66   basepair_spinner->setReadOnly(read_only);
67 }
68
69 double ThresholdWidget::min_ratio() const
70 {
71   double ratio=((double)basepair_spinner->minimum()/
72                 (double)basepair_spinner->maximum());
73
74   return ratio;
75 }
76
77 double ThresholdWidget::ratio() const
78 {
79   return ((float)threshold())/((float)basepair_spinner->maximum()); 
80 }
81
82 void ThresholdWidget::setBasepairThreshold(int threshold)
83 {
84  if (cur_bp_threshold!= threshold)
85   {
86     cur_bp_threshold = threshold;
87     cur_percent_threshold = (int)(roundl(ratio() * 100));
88     basepair_spinner->setValue(threshold);
89     updatePercentThreshold();
90     emit thresholdChanged(threshold);
91   }
92 }
93
94 void ThresholdWidget::updatePercentThreshold()
95 {
96   int min_percent = (int)fmaxl(0.0, roundl(ratio()*100));
97   QString label;
98   label.setNum(min_percent);
99   label += "%";
100   percent_label->setText(label);
101 }
102
103 int ThresholdWidget::threshold() const
104 {
105   return cur_bp_threshold;
106 }
107
108 void ThresholdWidget::reset(int min, int max, int threshold)
109 {
110   
111   cur_bp_threshold = threshold;
112   cur_percent_threshold = (int)(roundl(ratio() * 100));
113   setRange(min, max);
114   basepair_spinner->setValue(threshold);
115   updatePercentThreshold();
116   
117 }