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