c1ca76c2ffae3474bbdf5666c8a7eccda372ea5f
[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 double ThresholdWidget::min_ratio() const
47 {
48   double ratio=((double)basepair_spinner->minimum()/
49                 (double)basepair_spinner->maximum());
50
51   return ratio;
52 }
53
54 double ThresholdWidget::ratio() const
55 {
56   return ((float)threshold())/((float)basepair_spinner->maximum()); 
57 }
58
59 void ThresholdWidget::setBasepairThreshold(int threshold)
60 {
61  if (cur_bp_threshold!= threshold)
62   {
63     cur_bp_threshold = threshold;
64     cur_percent_threshold = (int)(roundl(ratio() * 100));
65     basepair_spinner->setValue(threshold);
66     updatePercentThreshold();
67     emit thresholdChanged(threshold);
68   }
69 }
70
71 void ThresholdWidget::updatePercentThreshold()
72 {
73   int min_percent = (int)fmaxl(0.0, roundl(ratio()*100));
74   QString label;
75   label.setNum(min_percent);
76   label += "%";
77   percent_label->setText(label);
78 }
79
80 int ThresholdWidget::threshold() const
81 {
82   return cur_bp_threshold;
83 }