5c856968139fca01ded6f1d6dbf2985c3bc03c1e
[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     basepair_spinner(0),
14     window_size_label(0),
15     percent_label(0)
16 {
17   setupWidgets();
18   basepair_spinner->setWhatsThis("How many base pairs need to be conserved in the window size");
19
20   setRange(min, max);
21
22   connect(basepair_spinner, SIGNAL(valueChanged(int)), 
23           this, SLOT(setBasepairThreshold(int)));
24
25   basepair_spinner->setValue(min);
26   updatePercentThreshold();
27
28   layout->addWidget(new QLabel("Threshold"));
29   layout->addWidget(basepair_spinner);
30   layout->addWidget(window_size_label);
31   layout->addWidget(percent_label);
32   setLayout(layout);
33 }
34
35 void ThresholdWidget::setupWidgets()
36 {
37   layout = new QHBoxLayout(this);
38   basepair_spinner = new QSpinBox(this);
39   window_size_label = new QLabel(this);
40   percent_label = new QLabel(this);
41 }
42   
43 void ThresholdWidget::setRange(int min, int max)
44 {
45   basepair_spinner->setRange(min, max);
46   cur_bp_threshold = min;
47   basepair_spinner->setValue(min);
48
49   QString num;
50   num.setNum(max);
51   window_size_label->setText("/"+num);
52   updatePercentThreshold();
53 }
54
55 bool ThresholdWidget::isReadOnly() const
56 {
57   return basepair_spinner->isReadOnly();
58 }
59
60 void ThresholdWidget::setReadOnly(bool read_only)
61 {
62   basepair_spinner->setReadOnly(read_only);
63 }
64
65 double ThresholdWidget::min_ratio() const
66 {
67   double ratio=((double)basepair_spinner->minimum()/
68                 (double)basepair_spinner->maximum());
69
70   return ratio;
71 }
72
73 double ThresholdWidget::ratio() const
74 {
75   return ((float)threshold())/((float)basepair_spinner->maximum()); 
76 }
77
78 void ThresholdWidget::setBasepairThreshold(int threshold)
79 {
80  if (cur_bp_threshold!= threshold)
81   {
82     cur_bp_threshold = threshold;
83     cur_percent_threshold = (int)(roundl(ratio() * 100));
84     basepair_spinner->setValue(threshold);
85     updatePercentThreshold();
86     emit thresholdChanged(threshold);
87   }
88 }
89
90 void ThresholdWidget::updatePercentThreshold()
91 {
92   int min_percent = (int)fmaxl(0.0, roundl(ratio()*100));
93   QString label;
94   label.setNum(min_percent);
95   label += "%";
96   percent_label->setText(label);
97 }
98
99 int ThresholdWidget::threshold() const
100 {
101   return cur_bp_threshold;
102 }