Make MussaWindow UI objects pointers
[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
21   basepair_spinner.setValue(min);
22   updatePercentThreshold();
23
24   QHBoxLayout *layout = new QHBoxLayout;
25   layout->addWidget(new QLabel("Threshold"));
26   layout->addWidget(&basepair_spinner);
27   layout->addWidget(&window_size_label);
28   layout->addWidget(&percent_label);
29   setLayout(layout);
30 }
31
32 void ThresholdWidget::setRange(int min, int max)
33 {
34   basepair_spinner.setRange(min, max);
35   cur_bp_threshold = min;
36   basepair_spinner.setValue(min);
37
38   QString num;
39   num.setNum(max);
40   window_size_label.setText("/"+num);
41   updatePercentThreshold();
42 }
43
44 double ThresholdWidget::min_ratio() const
45 {
46   double ratio=((double)basepair_spinner.minimum()/
47                 (double)basepair_spinner.maximum());
48
49   return ratio;
50 }
51
52 double ThresholdWidget::ratio() const
53 {
54   return ((float)threshold())/((float)basepair_spinner.maximum()); 
55 }
56
57 void ThresholdWidget::setBasepairThreshold(int threshold)
58 {
59  if (cur_bp_threshold!= threshold)
60   {
61     cur_bp_threshold = threshold;
62     cur_percent_threshold = (int)(roundl(ratio() * 100));
63     basepair_spinner.setValue(threshold);
64     updatePercentThreshold();
65     emit thresholdChanged(threshold);
66   }
67 }
68
69 void ThresholdWidget::updatePercentThreshold()
70 {
71   int min_percent = (int)fmaxl(0.0, roundl(ratio()*100));
72   QString label;
73   label.setNum(min_percent);
74   label += "%";
75   percent_label.setText(label);
76 }
77
78 int ThresholdWidget::threshold() const
79 {
80   return cur_bp_threshold;
81 }