Skip to content

Commit 16b61e3

Browse files
committed
Work on DD mode
1 parent e85e6d2 commit 16b61e3

File tree

6 files changed

+365
-4
lines changed

6 files changed

+365
-4
lines changed

SmartTimer/SmartTimer.pro

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ SOURCES += \
3737
alertwidget.cpp \
3838
addalarmdialog.cpp \
3939
changealarmdialog.cpp \
40-
globalsettingsdialog.cpp
40+
globalsettingsdialog.cpp \
41+
rangewidget.cpp
4142

4243
HEADERS += \
4344
mainwindow.h \
@@ -50,7 +51,8 @@ HEADERS += \
5051
addalarmdialog.h \
5152
changealarmdialog.h \
5253
widgetsettings.h \
53-
globalsettingsdialog.h
54+
globalsettingsdialog.h \
55+
rangewidget.h
5456

5557
FORMS += \
5658
mainwindow.ui \

SmartTimer/globalsettingsdialog.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
#include "globalsettingsdialog.h"
22
#include "ui_globalsettingsdialog.h"
3+
#include "rangewidget.h"
34

45
#include <QFile>
56
#include <QSlider>
7+
#include <QHBoxLayout>
68

79
GlobalSettingsDialog::GlobalSettingsDialog(GlobalSettings old, QWidget *parent) :
810
QDialog(parent),
@@ -24,6 +26,12 @@ GlobalSettingsDialog::GlobalSettingsDialog(GlobalSettings old, QWidget *parent)
2426
ui->alarmFormat->setText(old.alarmTimeFormat);
2527
ui->TimerFormat->setText(old.timerTimeFormat);
2628

29+
RangeWidget *DDrange = new RangeWidget(Qt::Horizontal);
30+
ui->RangeLayout->addWidget(DDrange);
31+
32+
connect(DDrange,SIGNAL(rangeChanged(int , int )),this,SLOT(rangeChanged(int,int)));
33+
34+
2735
connect(ui->cancelButton,SIGNAL(clicked()),this,SLOT(canceled()));
2836
connect(ui->confirmButton,SIGNAL(clicked()),this, SLOT(confirmed()));
2937
connect( ui->opacitySlider,SIGNAL(valueChanged(int)),this,SLOT(opacityChanged()));
@@ -56,3 +64,8 @@ void GlobalSettingsDialog::opacityChanged()
5664

5765
emit changeSettings(newSettings);
5866
}
67+
68+
void GlobalSettingsDialog::rangeChanged(int min, int max)
69+
{
70+
ui->RangeValue->setText(QString::number(min)+" : "+QString::number(max));
71+
}

SmartTimer/globalsettingsdialog.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public slots:
2020
void confirmed();
2121
void canceled();
2222
void opacityChanged();
23+
void rangeChanged(int min,int max);
2324
signals:
2425
void changeSettings(GlobalSettings settings);
2526

SmartTimer/globalsettingsdialog.ui

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<x>0</x>
88
<y>0</y>
99
<width>600</width>
10-
<height>400</height>
10+
<height>600</height>
1111
</rect>
1212
</property>
1313
<property name="sizePolicy">
@@ -19,7 +19,7 @@
1919
<property name="minimumSize">
2020
<size>
2121
<width>600</width>
22-
<height>400</height>
22+
<height>600</height>
2323
</size>
2424
</property>
2525
<property name="maximumSize">
@@ -97,6 +97,29 @@
9797
</item>
9898
</layout>
9999
</item>
100+
<item>
101+
<layout class="QHBoxLayout" name="RangeLayout">
102+
<item>
103+
<widget class="QLabel" name="RangeValue">
104+
<property name="minimumSize">
105+
<size>
106+
<width>100</width>
107+
<height>0</height>
108+
</size>
109+
</property>
110+
<property name="maximumSize">
111+
<size>
112+
<width>100</width>
113+
<height>16777215</height>
114+
</size>
115+
</property>
116+
<property name="text">
117+
<string/>
118+
</property>
119+
</widget>
120+
</item>
121+
</layout>
122+
</item>
100123
<item>
101124
<layout class="QHBoxLayout" name="horizontalLayout_3">
102125
<item>

SmartTimer/rangewidget.cpp

Lines changed: 242 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,242 @@
1+
#include "RangeWidget.h"
2+
3+
#include <QtDebug>
4+
5+
RangeWidget::RangeWidget(Qt::Orientation orientation, QWidget *parent)
6+
: QWidget(parent),
7+
_orientation(orientation),
8+
_handleWidth(8),
9+
_handleHeight(20),
10+
_minimum(0),
11+
_maximum(100),
12+
_firstValue(10),
13+
_secondValue(90),
14+
_firstHandlePressed(false),
15+
_secondHandlePressed(false),
16+
_firstHandleColor(style()->standardPalette().highlight().color()),
17+
_secondHandleColor(style()->standardPalette().highlight().color())
18+
{
19+
setMouseTracking(true);
20+
}
21+
22+
void RangeWidget::paintEvent(QPaintEvent *event)
23+
{
24+
QPainter p(this);
25+
26+
// First value handle rect
27+
QRectF rv1 = firstHandleRect();
28+
QColor c1(_firstHandleColor);
29+
if(_firstHandleHovered)
30+
c1 = c1.darker();
31+
32+
// Second value handle rect
33+
QRectF rv2 = secondHandleRect();
34+
QColor c2(_secondHandleColor);
35+
if(_secondHandleHovered)
36+
c2 = c2.darker();
37+
38+
// Background
39+
QRect r;
40+
if(_orientation == Qt::Horizontal)
41+
r = QRect(0, (height()-_handleWidth)/2, width()-1, _handleWidth);
42+
else
43+
r = QRect((width()-_handleWidth)/2, 0, _handleWidth, height()-1);
44+
p.drawRect(r);
45+
46+
// Handles
47+
QRectF rf(r);
48+
if(_orientation == Qt::Horizontal)
49+
{
50+
rf.setLeft(rv1.right());
51+
rf.setRight(rv2.left());
52+
rf.setBottom(rf.bottom()+1);
53+
}
54+
else
55+
{
56+
rf.setTop(rv1.bottom());
57+
rf.setBottom(rv2.top());
58+
rf.setRight(rf.right()+1);
59+
}
60+
p.fillRect(rf, QColor(Qt::green).darker(150));
61+
p.fillRect(rv1, c1);
62+
p.fillRect(rv2, c2);
63+
}
64+
65+
qreal RangeWidget::span() const
66+
{
67+
int interval = qAbs(_maximum-_minimum);
68+
69+
if(_orientation == Qt::Horizontal)
70+
return qreal(width()-_handleWidth)/qreal(interval);
71+
else
72+
return qreal(height()-_handleWidth)/qreal(interval);
73+
}
74+
75+
QRectF RangeWidget::firstHandleRect() const
76+
{
77+
return handleRect(_firstValue);
78+
}
79+
80+
QRectF RangeWidget::secondHandleRect() const
81+
{
82+
return handleRect(_secondValue);
83+
}
84+
85+
QRectF RangeWidget::handleRect(int value) const
86+
{
87+
qreal s = span();
88+
89+
QRectF r;
90+
if(_orientation == Qt::Horizontal)
91+
{
92+
r = QRectF(0, (height()-_handleHeight)/2, _handleWidth, _handleHeight);
93+
r.moveLeft(s*(value-_minimum));
94+
}
95+
else
96+
{
97+
r = QRectF((width()-_handleHeight)/2, 0, _handleHeight, _handleWidth);
98+
r.moveTop(s*(value-_minimum));
99+
}
100+
return r;
101+
}
102+
103+
void RangeWidget::mousePressEvent(QMouseEvent* event)
104+
{
105+
if(event->buttons() & Qt::LeftButton)
106+
{
107+
_secondHandlePressed = secondHandleRect().contains(event->pos());
108+
_firstHandlePressed = !_secondHandlePressed && firstHandleRect().contains(event->pos());
109+
emit sliderPressed();
110+
}
111+
}
112+
113+
void RangeWidget::mouseMoveEvent(QMouseEvent* event)
114+
{
115+
if(event->buttons() & Qt::LeftButton)
116+
{
117+
int interval = qAbs(_maximum-_minimum);
118+
119+
if(_secondHandlePressed)
120+
{
121+
if(_orientation == Qt::Horizontal)
122+
setSecondValue(event->pos().x()*interval/(width()-_handleWidth));
123+
else
124+
setSecondValue(event->pos().y()*interval/(height()-_handleWidth));
125+
}
126+
else if(_firstHandlePressed)
127+
{
128+
if(_orientation == Qt::Horizontal)
129+
setFirstValue(event->pos().x()*interval/(width()-_handleWidth));
130+
else
131+
setFirstValue(event->pos().y()*interval/(height()-_handleWidth));
132+
}
133+
}
134+
135+
QRectF rv2 = secondHandleRect();
136+
QRectF rv1 = firstHandleRect();
137+
_secondHandleHovered = _secondHandlePressed || (!_firstHandlePressed && rv2.contains(event->pos()));
138+
_firstHandleHovered = _firstHandlePressed || (!_secondHandleHovered && rv1.contains(event->pos()));
139+
update(rv2.toRect());
140+
update(rv1.toRect());
141+
}
142+
143+
void RangeWidget::mouseReleaseEvent(QMouseEvent* event)
144+
{
145+
if(_firstHandlePressed || _secondHandlePressed)
146+
emit sliderReleased();
147+
148+
_firstHandlePressed = false;
149+
_secondHandlePressed = false;
150+
}
151+
152+
QSize RangeWidget::minimumSizeHint() const
153+
{
154+
return QSize(_handleHeight, _handleHeight);
155+
}
156+
157+
void RangeWidget::setSecondValue(int secondValue)
158+
{
159+
if(secondValue > _maximum)
160+
secondValue = _maximum;
161+
162+
if(secondValue < _minimum)
163+
secondValue = _minimum;
164+
165+
_secondValue = secondValue;
166+
emit secondValueChanged(_secondValue);
167+
168+
update();
169+
}
170+
171+
void RangeWidget::setFirstValue(int firstValue)
172+
{
173+
if(firstValue > _maximum)
174+
firstValue = _maximum;
175+
176+
if(firstValue < _minimum)
177+
firstValue = _minimum;
178+
179+
_firstValue = firstValue;
180+
emit firstValueChanged(_firstValue);
181+
182+
update();
183+
}
184+
185+
void RangeWidget::setMaximum(int max)
186+
{
187+
if(max >= minimum())
188+
_maximum = max;
189+
else
190+
{
191+
int oldMin = minimum();
192+
_maximum = oldMin;
193+
_minimum = max;
194+
}
195+
196+
update();
197+
198+
if(firstValue() > maximum())
199+
setFirstValue(maximum());
200+
201+
if(secondValue() > maximum())
202+
setSecondValue(maximum());
203+
204+
emit rangeChanged(minimum(), maximum());
205+
}
206+
207+
void RangeWidget::setRange(int min, int max)
208+
{
209+
setMinimum(min);
210+
setMaximum(max);
211+
}
212+
213+
void RangeWidget::setMinimum(int min)
214+
{
215+
if(min <= maximum())
216+
_minimum = min;
217+
else
218+
{
219+
int oldMax = maximum();
220+
_minimum = oldMax;
221+
_maximum = min;
222+
}
223+
224+
update();
225+
226+
if(firstValue() < minimum())
227+
setFirstValue(minimum());
228+
229+
if(secondValue() < minimum())
230+
setSecondValue(minimum());
231+
232+
emit rangeChanged(minimum(), maximum());
233+
}
234+
235+
void RangeWidget::setOrientation(Qt::Orientation orientation)
236+
{
237+
if(_orientation == orientation)
238+
return;
239+
240+
_orientation = orientation;
241+
update();
242+
}

0 commit comments

Comments
 (0)