@@ -49,17 +49,22 @@ class ctkCheckablePushButtonPrivate: public ctkPushButtonPrivate
49
49
virtual QStyleOptionButton drawIcon (QPainter* p);
50
50
51
51
// Tuning of the button look&feel
52
- Qt::ItemFlags CheckBoxFlags;
53
52
Qt::CheckState CheckState;
53
+
54
+ bool CheckBoxControlsButton;
55
+ bool CheckBoxUserCheckable;
56
+ bool CheckBoxControlsButtonToggleState;
54
57
};
55
58
56
59
// -----------------------------------------------------------------------------
57
60
ctkCheckablePushButtonPrivate::ctkCheckablePushButtonPrivate (ctkCheckablePushButton& object)
58
61
: ctkPushButtonPrivate(object)
59
62
, q_ptr(&object)
60
63
{
61
- this ->CheckBoxFlags = Qt::ItemIsEnabled | Qt::ItemIsUserCheckable;
64
+ this ->CheckBoxControlsButton = true ;
65
+ this ->CheckBoxUserCheckable = true ;
62
66
this ->CheckState = Qt::Unchecked;
67
+ this ->CheckBoxControlsButtonToggleState = false ;
63
68
}
64
69
65
70
// -----------------------------------------------------------------------------
@@ -84,7 +89,7 @@ QStyleOptionButton ctkCheckablePushButtonPrivate::drawIcon(QPainter* p)
84
89
QStyleOptionButton indicatorOpt;
85
90
86
91
indicatorOpt.init (q);
87
- if (!( this ->CheckBoxFlags & Qt::ItemIsUserCheckable) )
92
+ if (!this ->CheckBoxUserCheckable )
88
93
{
89
94
indicatorOpt.state &= ~QStyle::State_Enabled;
90
95
}
@@ -158,17 +163,22 @@ void ctkCheckablePushButton::setCheckState(Qt::CheckState checkState)
158
163
return ;
159
164
}
160
165
d->CheckState = checkState;
161
- bool emitToggled = false ;
162
- if (d->CheckBoxFlags & Qt::ItemIsEnabled)
166
+ if (d->CheckBoxControlsButton )
163
167
{
164
168
bool wasChecked = this ->isChecked ();
165
- // QCheckBox::setCheckable() doesn't emit toggled signal
166
169
this ->setCheckable (checkState == Qt::Checked);
167
- emitToggled = (wasChecked != this ->isChecked ());
168
- }
169
- if (emitToggled)
170
- {
171
- emit toggled (this ->isChecked ());
170
+ // QCheckBox::setCheckable() doesn't emit toggled signal
171
+ if (wasChecked != this ->isChecked ())
172
+ {
173
+ emit toggled (this ->isChecked ());
174
+ }
175
+ if (d->CheckBoxControlsButtonToggleState )
176
+ {
177
+ if (this ->isChecked () != (checkState == Qt::Checked))
178
+ {
179
+ this ->setChecked (checkState == Qt::Checked);
180
+ }
181
+ }
172
182
}
173
183
emit checkStateChanged (d->CheckState );
174
184
emit checkBoxToggled (d->CheckState == Qt::Checked);
@@ -186,47 +196,64 @@ Qt::CheckState ctkCheckablePushButton::checkState()const
186
196
void ctkCheckablePushButton::setCheckBoxControlsButton (bool b)
187
197
{
188
198
Q_D (ctkCheckablePushButton);
199
+ d->CheckBoxControlsButton = b;
189
200
if (b)
190
201
{
191
- d->CheckBoxFlags |= Qt::ItemIsEnabled;
192
202
// synchronize checkstate with the checkable property.
193
203
this ->setCheckState (
194
204
this ->isCheckable () ? Qt::Checked : Qt::Unchecked);
195
205
}
196
- else
197
- {
198
- d->CheckBoxFlags &= ~Qt::ItemIsEnabled;
199
- }
200
206
this ->update ();
201
207
}
202
208
203
209
// -----------------------------------------------------------------------------
204
210
bool ctkCheckablePushButton::checkBoxControlsButton ()const
205
211
{
206
212
Q_D (const ctkCheckablePushButton);
207
- return d->CheckBoxFlags & Qt::ItemIsEnabled ;
213
+ return d->CheckBoxControlsButton ;
208
214
}
209
215
210
216
// -----------------------------------------------------------------------------
211
- void ctkCheckablePushButton::setCheckBoxUserCheckable (bool b)
217
+ void ctkCheckablePushButton::setCheckBoxControlsButtonToggleState (bool b)
212
218
{
213
219
Q_D (ctkCheckablePushButton);
214
- if (b)
220
+ if (d-> CheckBoxControlsButtonToggleState == b)
215
221
{
216
- d-> CheckBoxFlags |= Qt::ItemIsUserCheckable ;
222
+ return ;
217
223
}
218
- else
224
+ d->CheckBoxControlsButtonToggleState = b;
225
+ if (d->CheckBoxControlsButtonToggleState )
219
226
{
220
- d->CheckBoxFlags &= ~Qt::ItemIsUserCheckable;
227
+ // We have just enabled sync between toggle state and checkbox.
228
+ // If checkbox is enabled then make the button toggled.
229
+ if (this ->checkState () && !this ->isChecked ())
230
+ {
231
+ this ->setChecked (true );
232
+ }
221
233
}
222
234
this ->update ();
223
235
}
224
236
237
+ // -----------------------------------------------------------------------------
238
+ bool ctkCheckablePushButton::checkBoxControlsButtonToggleState ()const
239
+ {
240
+ Q_D (const ctkCheckablePushButton);
241
+ return d->CheckBoxControlsButtonToggleState ;
242
+ }
243
+
244
+ // -----------------------------------------------------------------------------
245
+ void ctkCheckablePushButton::setCheckBoxUserCheckable (bool b)
246
+ {
247
+ Q_D (ctkCheckablePushButton);
248
+ d->CheckBoxUserCheckable = b;
249
+ this ->update ();
250
+ }
251
+
225
252
// -----------------------------------------------------------------------------
226
253
bool ctkCheckablePushButton::isCheckBoxUserCheckable ()const
227
254
{
228
255
Q_D (const ctkCheckablePushButton);
229
- return d->CheckBoxFlags & Qt::ItemIsUserCheckable ;
256
+ return d->CheckBoxUserCheckable ;
230
257
}
231
258
232
259
// -----------------------------------------------------------------------------
@@ -247,7 +274,7 @@ void ctkCheckablePushButton::mousePressEvent(QMouseEvent *e)
247
274
return ;
248
275
}
249
276
if (d->iconRect ().contains (e->pos ()) &&
250
- (d->CheckBoxFlags & Qt::ItemIsUserCheckable ))
277
+ (d->CheckBoxUserCheckable ))
251
278
{
252
279
Qt::CheckState newCheckState;
253
280
switch (d->CheckState )
@@ -265,3 +292,33 @@ void ctkCheckablePushButton::mousePressEvent(QMouseEvent *e)
265
292
e->accept ();
266
293
}
267
294
}
295
+
296
+ // -----------------------------------------------------------------------------
297
+ void ctkCheckablePushButton::checkStateSet ()
298
+ {
299
+ Q_D (ctkCheckablePushButton);
300
+ this ->ctkPushButton ::checkStateSet ();
301
+ if (d->CheckBoxControlsButtonToggleState )
302
+ {
303
+ // Uncheck the checkbox if button is untoggled
304
+ if (!this ->isChecked () && this ->checkState ())
305
+ {
306
+ this ->setCheckState (Qt::Unchecked);
307
+ }
308
+ }
309
+ }
310
+
311
+ // -----------------------------------------------------------------------------
312
+ void ctkCheckablePushButton::nextCheckState ()
313
+ {
314
+ Q_D (ctkCheckablePushButton);
315
+ this ->ctkPushButton ::nextCheckState ();
316
+ if (d->CheckBoxControlsButtonToggleState )
317
+ {
318
+ // Uncheck the checkbox if button is untoggled
319
+ if (!this ->isChecked () && this ->checkState () == Qt::Checked)
320
+ {
321
+ this ->setCheckState (Qt::Unchecked);
322
+ }
323
+ }
324
+ }
0 commit comments