threshold widget shows both base pair and percents
[mussa.git] / qui / ThresholdWidget.cpp
1 #include <QHBoxLayout>
2 #include <QSlider>
3 #include <QString>
4
5 #include "qui/ThresholdWidget.hpp"
6 #include <iostream>
7 #include <math.h>
8
9 // This is completely and totally derived from the Qt example
10 // LCDRange.cpp
11 ThresholdWidget::ThresholdWidget(QWidget *parent, int min, int max) 
12   : QWidget(parent)
13 {
14   basepair_spinner.setWhatsThis("How many base pairs need to be conserved in the window size");
15
16   setRange(min, max);
17
18   connect(&basepair_spinner, SIGNAL(valueChanged(int)), 
19           this, SLOT(setBasepairThreshold(int)));
20   connect(&percent_spinner, SIGNAL(valueChanged(int)),
21           this, SLOT(setPercentThreshold(int)));
22
23   basepair_spinner.setValue(min);
24
25   QHBoxLayout *layout = new QHBoxLayout;
26   layout->addWidget(&basepair_spinner);
27   layout->addWidget(&window_size_label);
28   layout->addWidget(&percent_spinner);
29   layout->addWidget(new QLabel("%"));
30   setLayout(layout);
31 }
32
33 void ThresholdWidget::setRange(int min, int max)
34 {
35   basepair_spinner.setRange(min, max);
36   cur_bp_threshold = min;
37   basepair_spinner.setValue(min);
38
39   QString num;
40   num.setNum(max);
41   window_size_label.setText("/"+num);
42   int min_percent =(int)fmaxl(0.0, roundl(min_ratio()*100));
43   cur_percent_threshold = min_percent;
44   percent_spinner.setRange( min_percent, 100);
45   if (min_percent != 0.0) {
46     percent_spinner.setSingleStep((int)(roundl(1.0/(double)(max)*100.0)));
47   }
48   percent_spinner.setValue(min_percent);
49 }
50
51 double ThresholdWidget::min_ratio() const
52 {
53   double ratio=((double)basepair_spinner.minimum()/
54                 (double)basepair_spinner.maximum());
55
56   return ratio;
57 }
58
59 double ThresholdWidget::ratio() const
60 {
61   return ((float)threshold())/((float)basepair_spinner.maximum()); 
62 }
63
64 void ThresholdWidget::setBasepairThreshold(int threshold)
65 {
66  if (cur_bp_threshold!= threshold)
67   {
68     cur_bp_threshold = threshold;
69     cur_percent_threshold = (int)(roundl(ratio() * 100));
70     basepair_spinner.setValue(threshold);
71     percent_spinner.setValue(cur_percent_threshold);
72     emit thresholdChanged(threshold);
73   }
74 }
75
76 void ThresholdWidget::setPercentThreshold(int percent_threshold)
77 {
78   if (cur_percent_threshold != percent_threshold)
79   {
80     cur_percent_threshold = percent_threshold;
81     long t=roundl(basepair_spinner.maximum()*((float)percent_threshold/100.0));
82     cur_bp_threshold = (int)t;
83     percent_spinner.setValue(percent_threshold);
84     basepair_spinner.setValue(cur_bp_threshold);
85     emit thresholdChanged(cur_bp_threshold);
86   }
87 }
88
89 int ThresholdWidget::threshold() const
90 {
91   return cur_bp_threshold;
92 }