Fraxinus  16.5.0-fx-rc9
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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) 2008-2014, SINTEF Department of Medical Technology
5 All rights reserved.
6 
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9 
10 1. Redistributions of source code must retain the above copyright notice,
11  this list of conditions and the following disclaimer.
12 
13 2. Redistributions in binary form must reproduce the above copyright notice,
14  this list of conditions and the following disclaimer in the documentation
15  and/or other materials provided with the distribution.
16 
17 3. Neither the name of the copyright holder nor the names of its contributors
18  may be used to endorse or promote products derived from this software
19  without specific prior written permission.
20 
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 =========================================================================*/
32 #include "cxDoubleSpanSlider.h"
33 #include <QDoubleSpinBox>
34 #include "cxHelperWidgets.h"
35 
36 namespace cx
37 {
38 
39 void SliderRangeGroupWidget::addToGridLayout(QGridLayout* gridLayout, int row)
40 {
41  gridLayout->addWidget(mLabel, row, 0);
42  gridLayout->addWidget(mLowerEdit, row, 1);
43  gridLayout->addWidget(mUpperEdit, row, 1);
44  gridLayout->addWidget(mSpanSlider, row, 2);
45 }
46 
47 SliderRangeGroupWidget::SliderRangeGroupWidget(QWidget* parent, DoublePairPropertyBasePtr dataInterface, QGridLayout* gridLayout, int row) : OptimizedUpdateWidget(parent)
48 {
49  mData = dataInterface;
50  this->init(gridLayout, row);
51 }
52 
53 void SliderRangeGroupWidget::init(QGridLayout *gridLayout, int row)
54 {
55  mLabel = new QLabel(this);
56  mLabel->setText(mData->getDisplayName());
57  mLowerEdit = new QDoubleSpinBox(this);
58  mSpanSlider = new DoubleSpanSlider(this);
59  mSpanSlider->setOrientation(Qt::Horizontal);
60  mSpanSlider->setHandleMovementMode(QxtSpanSlider::NoOverlapping);
61  mUpperEdit = new QDoubleSpinBox(this);
62 
63  this->setDecimals(mData->getValueDecimals());
64 
65  if (gridLayout) // add to input gridlayout
66  {
67  gridLayout->addLayout(mergeWidgetsIntoHBoxLayout(mLabel, addDummyMargin(this)), row, 0);
68 
69  QHBoxLayout* controlsLayout = new QHBoxLayout;
70  controlsLayout->setSpacing(0);
71  controlsLayout->setMargin(0);
72  gridLayout->addLayout(controlsLayout, row, 1);
73 
74  controlsLayout->addWidget(mLowerEdit);
75  controlsLayout->addWidget(mSpanSlider, 1);
76  controlsLayout->addWidget(mUpperEdit);
77  }
78  else // add directly to this
79  {
80  QHBoxLayout* topLayout = new QHBoxLayout;
81  topLayout->setMargin(0);
82  this->setLayout(topLayout);
83 
84  topLayout->addWidget(mLabel);
85  topLayout->addWidget(mLowerEdit);
86  topLayout->addWidget(mSpanSlider, 1);
87  topLayout->addWidget(mUpperEdit);
88  }
89 
90  connect(mLowerEdit, SIGNAL(valueChanged(double)), this, SLOT(textEditedSlot()));
91  // connect to slider
92  connect(mSpanSlider, SIGNAL(doubleSpanChanged(double, double)), this, SLOT(doubleSpanChangedSlot(double, double)));
93  connect(mUpperEdit, SIGNAL(valueChanged(double)), this, SLOT(textEditedSlot()));
94 
95  // connect to backend
96  connect(mData.get(), SIGNAL(changed()), this, SLOT(dataChanged()));
97  this->dataChanged();
98 }
99 
101 {
102  mData->setValueRange(range);
103  this->updateGuiRange();
104 }
105 
107 {
108  mLowerEdit->setDecimals(decimals);
109  mUpperEdit->setDecimals(decimals);
110 }
111 
112 std::pair<double,double> SliderRangeGroupWidget::getValue() const
113 {
114  return std::make_pair(mData->getValue()[0], mData->getValue()[1]);
115 }
116 
117 void SliderRangeGroupWidget::doubleSpanChangedSlot(double lower, double upper)
118 {
119  this->setValue(lower, upper);
120 }
121 
122 bool SliderRangeGroupWidget::setValue(double lower, double upper)
123 {
124  Eigen::Vector2d val = Eigen::Vector2d(lower, upper);
125  if (val == mData->getValue())
126  return false;
127 
128  mData->setValue(val);
129  return true;
130 }
131 
132 void SliderRangeGroupWidget::textEditedSlot()
133 {
134  this->setValue(mLowerEdit->value(), mUpperEdit->value());
135 }
136 
137 void SliderRangeGroupWidget::dataChanged()
138 {
139  this->updateGuiRange();
140 
141  mSpanSlider->blockSignals(true);
142  mLowerEdit->blockSignals(true);
143  mUpperEdit->blockSignals(true);
144 
145  mSpanSlider->setDoubleSpan(mData->getValue()[0], mData->getValue()[1]);
146  mLowerEdit->setValue(mData->getValue()[0]);
147  mUpperEdit->setValue(mData->getValue()[1]);
148 
149  mSpanSlider->blockSignals(false);
150  mLowerEdit->blockSignals(false);
151  mUpperEdit->blockSignals(false);
152 
153  emit valueChanged(mData->getValue()[0], mData->getValue()[1]);
154  this->setModified();
155 }
156 
157 void SliderRangeGroupWidget::updateGuiRange()
158 {
159  DoubleRange range = mData->getValueRange();
160 
161  mSpanSlider->setDoubleRange(range);
162 
163  mLowerEdit->setRange(range.min(), range.max());
164  mLowerEdit->setSingleStep(range.step());
165 
166  mUpperEdit->setRange(range.min(), range.max());
167  mUpperEdit->setSingleStep(range.step());
168 }
169 
170 
171 } // namespace cx
QWidget * addDummyMargin(QWidget *widget)
void setDoubleSpan(double lower, double upper)
bool setValue(double lower, double upper)
Utility class for describing a bounded numeric range.
Definition: cxDoubleRange.h:53
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's int...
std::pair< double, double > getValue() const
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)