Fraxinus  18.10
An IGT application
cxDoubleSpanSlider.cpp
Go to the documentation of this file.
1 /*=========================================================================
2 This file is part of CustusX, an Image Guided Therapy Application.
3 
4 Copyright (c) SINTEF Department of Medical Technology.
5 All rights reserved.
6 
7 CustusX is released under a BSD 3-Clause license.
8 
9 See Lisence.txt (https://github.com/SINTEFMedtek/CustusX/blob/master/License.txt) for details.
10 =========================================================================*/
11 #include "cxDoubleSpanSlider.h"
12 #include <QDoubleSpinBox>
13 #include "cxHelperWidgets.h"
14 
15 namespace cx
16 {
17 
18 void SliderRangeGroupWidget::addToGridLayout(QGridLayout* gridLayout, int row)
19 {
20  gridLayout->addWidget(mLabel, row, 0);
21  gridLayout->addWidget(mLowerEdit, row, 1);
22  gridLayout->addWidget(mUpperEdit, row, 1);
23  gridLayout->addWidget(mSpanSlider, row, 2);
24 }
25 
26 SliderRangeGroupWidget::SliderRangeGroupWidget(QWidget* parent, DoublePairPropertyBasePtr dataInterface, QGridLayout* gridLayout, int row) : OptimizedUpdateWidget(parent)
27 {
28  mData = dataInterface;
29  this->init(gridLayout, row);
30 }
31 
32 void SliderRangeGroupWidget::init(QGridLayout *gridLayout, int row)
33 {
34  mLabel = new QLabel(this);
35  mLabel->setText(mData->getDisplayName());
36  mLowerEdit = new QDoubleSpinBox(this);
37  mSpanSlider = new DoubleSpanSlider(this);
38  mSpanSlider->setOrientation(Qt::Horizontal);
39  mSpanSlider->setHandleMovementMode(QxtSpanSlider::NoOverlapping);
40  mUpperEdit = new QDoubleSpinBox(this);
41 
42  this->setDecimals(mData->getValueDecimals());
43 
44  if (gridLayout) // add to input gridlayout
45  {
46  gridLayout->addLayout(mergeWidgetsIntoHBoxLayout(mLabel, addDummyMargin(this)), row, 0);
47 
48  QHBoxLayout* controlsLayout = new QHBoxLayout;
49  controlsLayout->setSpacing(0);
50  controlsLayout->setMargin(0);
51  gridLayout->addLayout(controlsLayout, row, 1);
52 
53  controlsLayout->addWidget(mLowerEdit);
54  controlsLayout->addWidget(mSpanSlider, 1);
55  controlsLayout->addWidget(mUpperEdit);
56  }
57  else // add directly to this
58  {
59  QHBoxLayout* topLayout = new QHBoxLayout;
60  topLayout->setMargin(0);
61  this->setLayout(topLayout);
62 
63  topLayout->addWidget(mLabel);
64  topLayout->addWidget(mLowerEdit);
65  topLayout->addWidget(mSpanSlider, 1);
66  topLayout->addWidget(mUpperEdit);
67  }
68 
69  connect(mLowerEdit, SIGNAL(valueChanged(double)), this, SLOT(textEditedSlot()));
70  // connect to slider
71  connect(mSpanSlider, SIGNAL(doubleSpanChanged(double, double)), this, SLOT(doubleSpanChangedSlot(double, double)));
72  connect(mUpperEdit, SIGNAL(valueChanged(double)), this, SLOT(textEditedSlot()));
73 
74  // connect to backend
75  connect(mData.get(), SIGNAL(changed()), this, SLOT(dataChanged()));
76  this->dataChanged();
77 }
78 
80 {
81  mData->setValueRange(range);
82  this->updateGuiRange();
83 }
84 
86 {
87  mLowerEdit->setDecimals(decimals);
88  mUpperEdit->setDecimals(decimals);
89 }
90 
91 std::pair<double,double> SliderRangeGroupWidget::getValue() const
92 {
93  return std::make_pair(mData->getValue()[0], mData->getValue()[1]);
94 }
95 
96 void SliderRangeGroupWidget::doubleSpanChangedSlot(double lower, double upper)
97 {
98  this->setValue(lower, upper);
99 }
100 
101 bool SliderRangeGroupWidget::setValue(double lower, double upper)
102 {
103  Eigen::Vector2d val = Eigen::Vector2d(lower, upper);
104  if (val == mData->getValue())
105  return false;
106 
107  mData->setValue(val);
108  return true;
109 }
110 
111 void SliderRangeGroupWidget::textEditedSlot()
112 {
113  this->setValue(mLowerEdit->value(), mUpperEdit->value());
114 }
115 
116 void SliderRangeGroupWidget::dataChanged()
117 {
118  this->updateGuiRange();
119 
120  mSpanSlider->blockSignals(true);
121  mLowerEdit->blockSignals(true);
122  mUpperEdit->blockSignals(true);
123 
124  mSpanSlider->setDoubleSpan(mData->getValue()[0], mData->getValue()[1]);
125  mLowerEdit->setValue(mData->getValue()[0]);
126  mUpperEdit->setValue(mData->getValue()[1]);
127 
128  mSpanSlider->blockSignals(false);
129  mLowerEdit->blockSignals(false);
130  mUpperEdit->blockSignals(false);
131 
132  emit valueChanged(mData->getValue()[0], mData->getValue()[1]);
133  this->setModified();
134 }
135 
136 void SliderRangeGroupWidget::updateGuiRange()
137 {
138  DoubleRange range = mData->getValueRange();
139 
140  mSpanSlider->setDoubleRange(range);
141 
142  mLowerEdit->setRange(range.min(), range.max());
143  mLowerEdit->setSingleStep(range.step());
144 
145  mUpperEdit->setRange(range.min(), range.max());
146  mUpperEdit->setSingleStep(range.step());
147 }
148 
149 
150 } // namespace cx
QWidget * addDummyMargin(QWidget *widget)
void setDoubleSpan(double lower, double upper)
bool setValue(double lower, double upper)
double max() const
maximum value
Definition: cxDoubleRange.h:49
Utility class for describing a bounded numeric range.
Definition: cxDoubleRange.h:32
void setRange(const DoubleRange &range)
boost::shared_ptr< DoublePairPropertyBase > DoublePairPropertyBasePtr
QHBoxLayout * mergeWidgetsIntoHBoxLayout(QWidget *first, QWidget *second)
Custom widget for display of double-valued data.Use the double-named methods instead of qslider&#39;s int...
std::pair< double, double > getValue() const
double min() const
minimum value
Definition: cxDoubleRange.h:45
double step() const
smallest reasonable increment
Definition: cxDoubleRange.h:53
void addToGridLayout(QGridLayout *gridLayout, int row)
void valueChanged(double lower, double upper)
Interface for all classes following the modified/prepaint paradigm.
void setDoubleRange(const DoubleRange &range)
Namespace for all CustusX production code.