-
Notifications
You must be signed in to change notification settings - Fork 2
/
NIButton.h
203 lines (176 loc) · 3.7 KB
/
NIButton.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#pragma once
#pragma region Header Files
// STL Header
#include <algorithm>
#include <array>
#include <functional>
// Boost Header
#include <boost/chrono.hpp>
// QT Header
#include <QtGui/QtGui>
#pragma endregion
/**
* A baisc circle button with progress arc
*/
class QBaseProgressButton : public QGraphicsItem
{
public:
std::function<void()> m_funcPress;
std::function<void()> m_funcRelease;
public:
QBaseProgressButton() : QGraphicsItem()
{
m_eStatus = BS_OUTSIDE;
m_fProgress = 0;
m_funcPress = [](){};
m_funcRelease = [](){};
SetSize( 70 );
m_aColor[0] = QBrush( qRgba( 0, 128, 128, 128 ) );
m_aColor[1] = QBrush( qRgba( 128, 128, 255, 128 ) );
m_aColor[2] = QBrush( qRgba( 255, 0, 0, 128 ) );
}
virtual QRectF boundingRect() const
{
return m_qRect;
}
virtual void paint( QPainter *pPainter, const QStyleOptionGraphicsItem *option, QWidget *widget )
{
pPainter->setPen( QPen( qRgba( 0, 0, 0, 0 ) ) );
pPainter->setBrush( m_aColor[m_eStatus] );
pPainter->drawEllipse( m_qRect );
if( m_eStatus != BS_OUTSIDE )
{
QPen mPen = QPen( qRgba( 255, 0, 0, 128 ) );
mPen.setWidth(10);
pPainter->setPen( mPen );
pPainter->drawArc( m_qRect, 90*16, 360 * 16 * m_fProgress );
}
}
virtual bool CheckInSide( const QPointF& rPos, const float& fDepth ) = 0;
virtual void SetSize( float fSize )
{
float fS = fSize / 2;
m_qRect = QRectF( -fS, -fS, fSize, fSize );
}
protected:
enum ESTATUS
{
BS_OUTSIDE,
BS_INSIDE,
BS_PRESSED
};
protected:
ESTATUS m_eStatus;
float m_fProgress;
QRectF m_qRect;
std::array<QBrush, 3> m_aColor;
};
/**
* A time-base button.
*/
class QTimerButton : public QBaseProgressButton
{
public:
typedef boost::chrono::system_clock TTimeColock;
typedef boost::chrono::milliseconds TDurationType;
TDurationType m_duTimeToPress;
public:
QTimerButton() : QBaseProgressButton()
{
m_duTimeToPress = boost::chrono::milliseconds( 500 );
}
virtual bool CheckInSide( const QPointF& rPt, const float& fDepth = 0 )
{
if( shape().contains( mapFromScene( rPt ) ) )
{
switch( m_eStatus )
{
case BS_OUTSIDE:
m_eStatus = BS_INSIDE;
m_tpFirstIn = TTimeColock::now();
break;
case BS_INSIDE:
m_fProgress = float( boost::chrono::duration_cast<TDurationType>( TTimeColock::now() - m_tpFirstIn ).count() ) / m_duTimeToPress.count();
if( m_fProgress > 1 )
{
m_fProgress = 1;
m_eStatus = BS_PRESSED;
m_funcPress();
}
break;
}
return true;
}
else
{
switch( m_eStatus )
{
case BS_PRESSED:
m_funcRelease();
break;
}
m_eStatus = BS_OUTSIDE;
m_fProgress = 0;
}
return false;
}
protected:
TTimeColock::time_point m_tpFirstIn;
};
/**
* A depth-base button
*/
class QDepthButton : public QBaseProgressButton
{
public:
float m_fPressDepth;
public:
QDepthButton() : QBaseProgressButton()
{
m_fPressDepth = 50;
}
bool CheckInSide( const QPointF& rPt, const float& fDepth )
{
if( shape().contains( mapFromScene( rPt ) ) )
{
switch( m_eStatus )
{
case BS_OUTSIDE:
m_eStatus = BS_INSIDE;
m_fFirstInDepth = fDepth;
break;
case BS_INSIDE:
m_fProgress = ComputeProgess( fDepth );
if( m_fProgress >= 1.0f )
{
m_fProgress = 1;
m_eStatus = BS_PRESSED;
m_funcPress();
}
break;
case BS_PRESSED:
m_fProgress = ComputeProgess( fDepth );
if( m_fProgress < 1 )
{
m_eStatus = BS_INSIDE;
m_funcRelease();
}
break;
}
return true;
}
else
{
m_eStatus = BS_OUTSIDE;
m_fProgress = 0;
}
return false;
}
protected:
float m_fFirstInDepth;
protected:
float ComputeProgess( float fDepth )
{
return std::min( std::max( ( m_fFirstInDepth - fDepth ) / m_fPressDepth, 0.0f ), 1.0f );
}
};