-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPixmapSlider.cpp
More file actions
350 lines (280 loc) · 9.35 KB
/
PixmapSlider.cpp
File metadata and controls
350 lines (280 loc) · 9.35 KB
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
#include "PixmapSlider.h"
#include <QPixmapCache>
PixmapSlider::PixmapSlider(QWidget *parent)
: QWidget(parent)
{
m_bVertical = false;
m_flag_mousePress = false;
m_flag_ThumbPress = false;
m_BuddySpinFocus = false;
m_bEnable = false;
m_nFocus = false;
m_nPos = 0;
m_nStep = 1;
m_nRect = QRect(0,0,this->width(),this->height());
m_nMarginLeft = 0;
m_nMarginRight = 0;
m_nMarginTop = 0;
m_nMarginBottom = 0;
m_nThumbHeight = 0;
m_nThumbWidth = 0;
setCursor(Qt::PointingHandCursor);
setFocusPolicy(Qt::ClickFocus);
this->setMouseTracking(true);
QPixmapCache::setCacheLimit(1);
}
PixmapSlider::~PixmapSlider()
{
}
// 传入数值 等价于 setValue
void PixmapSlider::SetPos(int mPos,bool nFlag)
{
if(mPos >m_nMax)
mPos =m_nMax;
if(mPos <m_nMin)
mPos =m_nMin;
m_nPos = mPos;
if(nFlag)
emit valueChanged(m_nPos);
update();
}
// 设置Slider背景图:normalPic为正常底图,activePic为活动底图
void PixmapSlider::SetChannelImage(QString normalPic,QString activePic)
{
m_nNormalChannelPic = QPixmap(normalPic);
m_nActiveChannelPic = QPixmap(activePic);
m_nRect.setWidth(m_nNormalChannelPic.width());
m_nRect.setHeight(m_nNormalChannelPic.height());
this->setFixedSize(m_nRect.width(),m_nRect.height());
if(m_nNormalChannelPic.width() > m_nNormalChannelPic.height())
m_bVertical = false;
else
m_bVertical = true;
}
// 设置推子图
void PixmapSlider::SetThumbImage(QString normalPic,QString activePic)
{
m_nThumb_Normal = QPixmap(normalPic);
m_nThumb_Active = QPixmap(activePic);
m_nThumbHeight = m_nThumb_Normal.height();
m_nThumbWidth = m_nThumb_Normal.width();
}
// 设置推子上下左右的间距
void PixmapSlider::SetMargin( int nLeft, int nTop, int nRight, int nBottom )
{
m_nMarginLeft = nLeft;
m_nMarginRight = nRight;
m_nMarginTop = nTop;
m_nMarginBottom = nBottom;
}
// 自定义控件 有效值 范围
void PixmapSlider::SetRange(int m_MinPos, int m_MaxPos)
{
m_nMax = m_MaxPos;
m_nMin = m_MinPos;
}
// 外部滚轮或鼠标调用 自增
void PixmapSlider::UpdatePosAdd()
{
int mPos;
mPos = m_nPos+m_nStep;
if(mPos >m_nMax)
mPos =m_nMax;
SetPos(mPos,true);
}
// 外部滚轮或鼠标调用 自减
void PixmapSlider::UpdatePosRedu()
{
int mPos;
mPos = m_nPos-m_nStep;
if(mPos <m_nMin)
mPos =m_nMin;
SetPos(mPos,true);
}
// 绑定编辑框,用于点击控件 焦点转移
//void PixmapSlider::SetBuddy(NumSpin *m_nSpin,bool m_Flag)
//{
// m_nBuddySpin = m_nSpin;
// m_BuddySpinFocus = m_Flag;
//}
void PixmapSlider::SetDisableFlg(bool nFlag)
{
m_bEnable = nFlag;
if(m_bEnable)
setCursor(Qt::PointingHandCursor);
else
setCursor(Qt::ArrowCursor);
}
void PixmapSlider::paintEvent(QPaintEvent *e) //从QWidget子类化一个Widget,为了能够使用样式表则需要为自定义Widget提供paintEvent事件
{
Q_UNUSED(e);
QPainter p(this);
p.setRenderHint(QPainter::Antialiasing);
if( m_bVertical) {
fillRect = m_nRect.adjusted(0, 0, 0, Pos2Pixel(m_nMin + (m_nMax - m_nPos))-m_nRect.height());
m_nThumbRect = QRect(m_nMarginLeft,Pos2Pixel(m_nMin + (m_nMax - m_nPos)) - m_nThumbHeight/2,m_nThumbWidth,m_nThumbHeight);
}else{
fillRect = m_nRect.adjusted(0, 0, Pos2Pixel(m_nPos)-m_nRect.width(), 0);
m_nThumbRect = QRect(Pos2Pixel(m_nPos) - m_nThumbWidth/2,m_nMarginTop,m_nThumbWidth,m_nThumbHeight);
}
//画背景
p.drawPixmap(m_nRect, m_nNormalChannelPic);
p.drawPixmap(fillRect, m_nActiveChannelPic, fillRect);
// 画滑块
if(m_nFocus)
p.drawPixmap(m_nThumbRect,m_nThumb_Active);
else
p.drawPixmap(m_nThumbRect,m_nThumb_Normal);
}
void PixmapSlider::mousePressEvent(QMouseEvent *event) //单击实现
{
if (event->button() == Qt::LeftButton && !m_bEnable) //按下鼠标左键
{
m_flag_mousePress = true;
if(m_nThumbRect.contains(event->pos()))
m_flag_ThumbPress = true;
if( m_bVertical ) {
if( abs( event->pos().y() - Pos2Pixel( m_nMin + (m_nMax - m_nPos) ) ) <= m_nThumbHeight / 2 )
m_nMouseOffset = event->pos().y() - Pos2Pixel( m_nMin + (m_nMax - m_nPos) );
else
m_nMouseOffset = 0;
} else {
if( abs( event->pos().x() - Pos2Pixel( m_nPos ) ) <= m_nThumbWidth / 2 )
m_nMouseOffset = event->pos().x() - Pos2Pixel( m_nPos );
else
m_nMouseOffset = 0;
}
UpDataPressDown(event->pos());
// if(m_BuddySpinFocus)
// {
// m_nFocus = true;
// if(!m_nBuddySpin->hasFocus())
// m_nBuddySpin->setFocus();
// }else{
// m_nFocus = true;
// this->setFocus();
// }
emit sliderClicked();
}
QWidget::mousePressEvent(event);
}
void PixmapSlider::UpDataPressDown(QPoint point)
{
if( !m_flag_mousePress)
return;
int nPixel;
// Boundary check
if( m_bVertical ) {
nPixel = point.y() - m_nMouseOffset;
if( nPixel > m_nRect.height() - m_nMarginBottom - m_nThumbHeight/2 )
nPixel = m_nRect.height() - m_nMarginBottom - m_nThumbHeight/2;
if( nPixel < m_nMarginTop + m_nThumbHeight/2 )
nPixel = m_nMarginTop + m_nThumbHeight/2;
} else {
nPixel = point.x() - m_nMouseOffset;
if( nPixel < m_nMarginLeft + m_nThumbWidth/2 )
nPixel = m_nMarginLeft + m_nThumbWidth/2;
if( nPixel > m_nRect.width() - m_nMarginRight - m_nThumbWidth/2 )
nPixel = m_nRect.width() - m_nMarginRight - m_nThumbWidth/2;
}
// Apply change
if(m_bVertical)
{
if( Pos2Pixel(m_nPos) != nPixel )
{
if(((m_nMin + (m_nMax - m_nPos))-Pixel2Pos(nPixel)) >0)
SetPos(m_nPos+m_nStep,true);
else
SetPos(m_nPos-m_nStep,true);
}
}else{
if( Pos2Pixel(m_nPos) != nPixel )
{
if((m_nPos-Pixel2Pos(nPixel)) >0)
SetPos(m_nPos-m_nStep,true);
else
SetPos(m_nPos+m_nStep,true);
}
}
}
void PixmapSlider::mouseReleaseEvent(QMouseEvent *event) //释放实现
{
Q_UNUSED(event);
m_flag_mousePress = false;
m_flag_ThumbPress = false;
QWidget::mouseReleaseEvent(event);
}
void PixmapSlider::focusOutEvent(QFocusEvent *e)
{
Q_UNUSED(e);
m_nFocus = false;
update();
}
void PixmapSlider::mouseMoveEvent(QMouseEvent *event) //移动实现
{
if(m_flag_ThumbPress)
{
int nPixel;
if( m_bVertical ) {
nPixel = event->pos().y() - m_nMouseOffset;
if( nPixel > m_nRect.height() - m_nMarginBottom - m_nThumbHeight/2 )
nPixel = m_nRect.height() - m_nMarginBottom - m_nThumbHeight/2;
if( nPixel < m_nMarginTop + m_nThumbHeight/2 )
nPixel = m_nMarginTop + m_nThumbHeight/2;
} else {
nPixel = event->pos().x() - m_nMouseOffset;
if( nPixel < m_nMarginLeft + m_nThumbWidth/2 )
nPixel = m_nMarginLeft + m_nThumbWidth/2;
if( nPixel > m_nRect.width() - m_nMarginRight - m_nThumbWidth/2 )
nPixel = m_nRect.width() - m_nMarginRight - m_nThumbWidth/2;
}
// Apply change
if(m_bVertical)
{
if(Pixel2Pos(m_nMin + (m_nMax - m_nPos)) <= 0)
SetPos( m_nMin + (m_nMax - Pixel2Pos( nPixel )) ,true);
if( Pos2Pixel(m_nMax - m_nPos) != nPixel )
SetPos( m_nMin + (m_nMax - Pixel2Pos( nPixel )) ,true);
}else{
if(Pixel2Pos(m_nPos) <= 0)
SetPos( Pixel2Pos( nPixel ) ,true);
if( Pos2Pixel(m_nPos) != nPixel )
SetPos( Pixel2Pos( nPixel ) ,true);
}
}
QWidget::mouseMoveEvent(event);
}
int PixmapSlider::Pos2Pixel(int nPos)
{
if( m_bVertical) {
return
m_nMarginTop + m_nThumbHeight/2 +
(int)(
( m_nRect.height() - m_nMarginTop - m_nMarginBottom - m_nThumbHeight ) *
((double) ( nPos - m_nMin ) / ( m_nMax - m_nMin ) )
);
} else {
return (int)(
( m_nRect.width() - m_nMarginLeft - m_nMarginRight - m_nThumbWidth ) *
((double) ( nPos - m_nMin ) / ( m_nMax - m_nMin ) )
) + m_nMarginLeft + m_nThumbWidth/2;
}
}
int PixmapSlider::Pixel2Pos(int nPixel)
{
if( m_bVertical) {
return (int)(
m_nMin +
(double)( nPixel - m_nMarginTop - m_nThumbHeight/2) /
( m_nRect.height() - m_nMarginBottom - m_nMarginTop - m_nThumbHeight ) *
( m_nMax - m_nMin )
);
} else {
return (int)(
m_nMin +
(double)( nPixel - m_nMarginLeft - m_nThumbWidth/2 ) /
( m_nRect.width() - m_nMarginLeft - m_nMarginRight - m_nThumbWidth ) *
( m_nMax - m_nMin )
);
}
}