Krita Source Code Documentation
Loading...
Searching...
No Matches
KDcrawIface::RAbstractSliderSpinBox Class Referenceabstract

#include <rsliderspinbox.h>

+ Inheritance diagram for KDcrawIface::RAbstractSliderSpinBox:

Public Member Functions

void hideEdit ()
 
void setExponentRatio (double dbl)
 
void setSuffix (const QString &suffix)
 
void showEdit ()
 
 ~RAbstractSliderSpinBox () override
 

Protected Slots

void contextMenuEvent (QContextMenuEvent *event) override
 
void editLostFocus ()
 

Protected Member Functions

QRect downButtonRect (const QStyleOptionSpinBox &spinBoxOptions) const
 
QRect editRect (const QStyleOptionSpinBox &spinBoxOptions) const
 
bool eventFilter (QObject *recv, QEvent *e) override
 
void keyPressEvent (QKeyEvent *e) override
 
QSize minimumSizeHint () const override
 
void mouseMoveEvent (QMouseEvent *e) override
 
void mousePressEvent (QMouseEvent *e) override
 
void mouseReleaseEvent (QMouseEvent *e) override
 
void paintEvent (QPaintEvent *e) override
 
QStyleOptionProgressBar progressBarOptions () const
 
QRect progressRect (const QStyleOptionProgressBar &progressBarOptions) const
 
 RAbstractSliderSpinBox (QWidget *const parent, RAbstractSliderSpinBoxPrivate *const q)
 
virtual void setInternalValue (int value)=0
 
QSize sizeHint () const override
 
QStyleOptionSpinBox spinBoxOptions () const
 
QRect upButtonRect (const QStyleOptionSpinBox &spinBoxOptions) const
 
int valueForX (int x, Qt::KeyboardModifiers modifiers=Qt::NoModifier) const
 
virtual QString valueString () const =0
 
void wheelEvent (QWheelEvent *) override
 

Protected Attributes

RAbstractSliderSpinBoxPrivate *const d_ptr
 

Detailed Description

TODO: when inactive, also show the progress bar part as inactive!

Definition at line 49 of file rsliderspinbox.h.

Constructor & Destructor Documentation

◆ RAbstractSliderSpinBox()

KDcrawIface::RAbstractSliderSpinBox::RAbstractSliderSpinBox ( QWidget *const parent,
RAbstractSliderSpinBoxPrivate *const q )
explicitprotected

Definition at line 92 of file rsliderspinbox.cpp.

93 : QWidget(parent),
94 d_ptr(q)
95{
97
98 d->edit = new QLineEdit(this);
99 d->edit->setFrame(false);
100 d->edit->setAlignment(Qt::AlignCenter);
101 d->edit->hide();
102 d->edit->installEventFilter(this);
103
104 // Make edit transparent
105 d->edit->setAutoFillBackground(false);
106 QPalette pal = d->edit->palette();
107 pal.setColor(QPalette::Base, Qt::transparent);
108 d->edit->setPalette(pal);
109
110 connect(d->edit, SIGNAL(editingFinished()),
111 this, SLOT(editLostFocus()));
112
113 d->validator = new QDoubleValidator(d->edit);
114 d->edit->setValidator(d->validator);
115
116 setExponentRatio(1.0);
117
118 // Set sane defaults
119 setFocusPolicy(Qt::StrongFocus);
120 setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
121
122 // dummy needed to fix a bug in the polyester theme
123 d->dummySpinBox = new QSpinBox(this);
124 d->dummySpinBox->hide();
125}
connect(this, SIGNAL(optionsChanged()), this, SLOT(saveOptions()))
RAbstractSliderSpinBoxPrivate *const d_ptr
RAbstractSliderSpinBox(QWidget *const parent, RAbstractSliderSpinBoxPrivate *const q)

References connect(), editLostFocus(), and setExponentRatio().

◆ ~RAbstractSliderSpinBox()

KDcrawIface::RAbstractSliderSpinBox::~RAbstractSliderSpinBox ( )
override

Definition at line 127 of file rsliderspinbox.cpp.

128{
130 delete d;
131}

Member Function Documentation

◆ contextMenuEvent

void KDcrawIface::RAbstractSliderSpinBox::contextMenuEvent ( QContextMenuEvent * event)
overrideprotectedslot

Definition at line 544 of file rsliderspinbox.cpp.

545{
546 event->accept();
547}

◆ downButtonRect()

QRect KDcrawIface::RAbstractSliderSpinBox::downButtonRect ( const QStyleOptionSpinBox & spinBoxOptions) const
protected

Definition at line 477 of file rsliderspinbox.cpp.

478{
479 return style()->subControlRect(QStyle::CC_SpinBox, &spinBoxOptions,
480 QStyle::SC_SpinBoxDown);
481}
QStyleOptionSpinBox spinBoxOptions() const

References spinBoxOptions().

◆ editLostFocus

void KDcrawIface::RAbstractSliderSpinBox::editLostFocus ( )
protectedslot

Definition at line 549 of file rsliderspinbox.cpp.

550{
551 // only hide on focus lost, if editing is finished that will be handled in eventFilter
553
554 if (!d->edit->hasFocus())
555 {
556 hideEdit();
557 }
558}

References hideEdit().

◆ editRect()

QRect KDcrawIface::RAbstractSliderSpinBox::editRect ( const QStyleOptionSpinBox & spinBoxOptions) const
protected

Definition at line 460 of file rsliderspinbox.cpp.

461{
462 return style()->subControlRect(QStyle::CC_SpinBox, &spinBoxOptions,
463 QStyle::SC_SpinBoxEditField);
464}

References spinBoxOptions().

◆ eventFilter()

bool KDcrawIface::RAbstractSliderSpinBox::eventFilter ( QObject * recv,
QEvent * e )
overrideprotected

Definition at line 331 of file rsliderspinbox.cpp.

332{
334
335 if (recv == static_cast<QObject*>(d->edit) && e->type() == QEvent::KeyRelease)
336 {
337 QKeyEvent* const keyEvent = static_cast<QKeyEvent*>(e);
338
339 switch (keyEvent->key())
340 {
341 case Qt::Key_Enter:
342 case Qt::Key_Return:
343 setInternalValue(QLocale::system().toDouble(d->edit->text()) * d->factor);
344 hideEdit();
345 return true;
346 case Qt::Key_Escape:
347 hideEdit();
348 return true;
349 default:
350 break;
351 }
352 }
353
354 return false;
355}
virtual void setInternalValue(int value)=0
double toDouble(const quint8 *data, int channelpos)

References hideEdit(), setInternalValue(), and toDouble().

◆ hideEdit()

void KDcrawIface::RAbstractSliderSpinBox::hideEdit ( )

Definition at line 147 of file rsliderspinbox.cpp.

148{
150 d->edit->hide();
151 update();
152}
bool update(QSpinBox *spinBox)

◆ keyPressEvent()

void KDcrawIface::RAbstractSliderSpinBox::keyPressEvent ( QKeyEvent * e)
overrideprotected

Definition at line 273 of file rsliderspinbox.cpp.

274{
276
277 switch (e->key())
278 {
279 case Qt::Key_Up:
280 case Qt::Key_Right:
281 setInternalValue(d->value + d->singleStep);
282 break;
283 case Qt::Key_Down:
284 case Qt::Key_Left:
285 setInternalValue(d->value - d->singleStep);
286 break;
287 case Qt::Key_Shift:
288 d->shiftPercent = pow( double(d->value - d->minimum)/double(d->maximum - d->minimum), 1/double(d->exponentRatio) );
289 d->shiftMode = true;
290 break;
291 case Qt::Key_Enter: // Line edit isn't "accepting" key strokes...
292 case Qt::Key_Return:
293 case Qt::Key_Escape:
294 case Qt::Key_Control:
295 case Qt::Key_Alt:
296 case Qt::Key_AltGr:
297 case Qt::Key_Super_L:
298 case Qt::Key_Super_R:
299 break;
300 default:
301 showEdit();
302 d->edit->event(e);
303 break;
304 }
305}

References setInternalValue(), and showEdit().

◆ minimumSizeHint()

QSize KDcrawIface::RAbstractSliderSpinBox::minimumSizeHint ( ) const
overrideprotected

Definition at line 387 of file rsliderspinbox.cpp.

388{
389 return sizeHint();
390}

References sizeHint().

◆ mouseMoveEvent()

void KDcrawIface::RAbstractSliderSpinBox::mouseMoveEvent ( QMouseEvent * e)
overrideprotected

Definition at line 247 of file rsliderspinbox.cpp.

248{
250
251 if( e->modifiers() & Qt::ShiftModifier )
252 {
253 if( !d->shiftMode )
254 {
255 d->shiftPercent = pow(double(d->value - d->minimum)/double(d->maximum - d->minimum),
256 1/double(d->exponentRatio));
257 d->shiftMode = true;
258 }
259 }
260 else
261 {
262 d->shiftMode = false;
263 }
264
265 // Respect emulated mouse grab.
266 if (e->buttons() & Qt::LeftButton && !(d->downButtonDown || d->upButtonDown))
267 {
268 setInternalValue(valueForX(e->pos().x(),e->modifiers()));
269 update();
270 }
271}
int valueForX(int x, Qt::KeyboardModifiers modifiers=Qt::NoModifier) const

References setInternalValue(), and valueForX().

◆ mousePressEvent()

void KDcrawIface::RAbstractSliderSpinBox::mousePressEvent ( QMouseEvent * e)
overrideprotected

Definition at line 193 of file rsliderspinbox.cpp.

194{
196 QStyleOptionSpinBox spinOpts = spinBoxOptions();
197
198 // Depress buttons or highlight slider. Also used to emulate mouse grab.
199
200 if (e->buttons() & Qt::LeftButton)
201 {
202 if (upButtonRect(spinOpts).contains(e->pos()))
203 {
204 d->upButtonDown = true;
205 }
206 else if (downButtonRect(spinOpts).contains(e->pos()))
207 {
208 d->downButtonDown = true;
209 }
210 }
211 else if (e->buttons() & Qt::RightButton)
212 {
213 showEdit();
214 }
215
216 update();
217}
QRect upButtonRect(const QStyleOptionSpinBox &spinBoxOptions) const
QRect downButtonRect(const QStyleOptionSpinBox &spinBoxOptions) const

References downButtonRect(), showEdit(), spinBoxOptions(), and upButtonRect().

◆ mouseReleaseEvent()

void KDcrawIface::RAbstractSliderSpinBox::mouseReleaseEvent ( QMouseEvent * e)
overrideprotected

Definition at line 219 of file rsliderspinbox.cpp.

220{
222 QStyleOptionSpinBox spinOpts = spinBoxOptions();
223
224 // Step up/down for buttons. Emulating mouse grab too.
225
226 if (upButtonRect(spinOpts).contains(e->pos()) && d->upButtonDown)
227 {
228 setInternalValue(d->value + d->singleStep);
229 }
230 else if (downButtonRect(spinOpts).contains(e->pos()) && d->downButtonDown)
231 {
232 setInternalValue(d->value - d->singleStep);
233 }
234 else if (editRect(spinOpts).contains(e->pos()) &&
235 !(d->edit->isVisible()) &&
236 !(d->upButtonDown || d->downButtonDown))
237 {
238 // Snap to percentage for progress area
239 setInternalValue(valueForX(e->pos().x(),e->modifiers()));
240 }
241
242 d->upButtonDown = false;
243 d->downButtonDown = false;
244 update();
245}
QRect editRect(const QStyleOptionSpinBox &spinBoxOptions) const

References downButtonRect(), editRect(), setInternalValue(), spinBoxOptions(), upButtonRect(), and valueForX().

◆ paintEvent()

void KDcrawIface::RAbstractSliderSpinBox::paintEvent ( QPaintEvent * e)
overrideprotected

Definition at line 154 of file rsliderspinbox.cpp.

155{
157 Q_UNUSED(e);
158
159 QPainter painter(this);
160
161 // Create options to draw spin box parts
162 QStyleOptionSpinBox spinOpts = spinBoxOptions();
163
164 // Draw "SpinBox".Clip off the area of the lineEdit to avoid double borders being drawn
165
166 painter.save();
167 painter.setClipping(true);
168 QRect eraseRect(QPoint(rect().x(), rect().y()),
169 QPoint(editRect(spinOpts).right(), rect().bottom()));
170 painter.setClipRegion(QRegion(rect()).subtracted(eraseRect));
171 style()->drawComplexControl(QStyle::CC_SpinBox, &spinOpts, &painter, d->dummySpinBox);
172 painter.setClipping(false);
173 painter.restore();
174
175
176 // Create options to draw progress bar parts
177 QStyleOptionProgressBar progressOpts = progressBarOptions();
178
179 // Draw "ProgressBar" in SpinBox
180 style()->drawControl(QStyle::CE_ProgressBar, &progressOpts, &painter, 0);
181
182 // Draw focus if necessary
183 if (hasFocus() && d->edit->hasFocus())
184 {
185 QStyleOptionFocusRect focusOpts;
186 focusOpts.initFrom(this);
187 focusOpts.rect = progressOpts.rect;
188 focusOpts.backgroundColor = palette().color(QPalette::Window);
189 style()->drawPrimitive(QStyle::PE_FrameFocusRect, &focusOpts, &painter, this);
190 }
191}
QStyleOptionProgressBar progressBarOptions() const
rgba palette[MAX_PALETTE]
Definition palette.c:35

References editRect(), palette, progressBarOptions(), and spinBoxOptions().

◆ progressBarOptions()

QStyleOptionProgressBar KDcrawIface::RAbstractSliderSpinBox::progressBarOptions ( ) const
protected

Definition at line 433 of file rsliderspinbox.cpp.

434{
435 const Q_D(RAbstractSliderSpinBox);
436
437 QStyleOptionSpinBox spinOpts = spinBoxOptions();
438
439 // Create opts for drawing the progress portion
440
441 QStyleOptionProgressBar progressOpts;
442 progressOpts.initFrom(this);
443 progressOpts.maximum = d->maximum;
444 progressOpts.minimum = d->minimum;
445
446 double minDbl = d->minimum;
447 double dValues = (d->maximum - minDbl);
448
449 progressOpts.progress = dValues * pow((d->value - minDbl) / dValues, 1.0 / d->exponentRatio) + minDbl;
450 progressOpts.text = valueString() + d->suffix;
451 progressOpts.textAlignment = Qt::AlignCenter;
452 progressOpts.textVisible = !(d->edit->isVisible());
453
454 // Change opts rect to be only the ComboBox's text area
455 progressOpts.rect = editRect(spinOpts);
456
457 return progressOpts;
458}
virtual QString valueString() const =0

References editRect(), spinBoxOptions(), and valueString().

◆ progressRect()

QRect KDcrawIface::RAbstractSliderSpinBox::progressRect ( const QStyleOptionProgressBar & progressBarOptions) const
protected

Definition at line 466 of file rsliderspinbox.cpp.

467{
468 return style()->subElementRect(QStyle::SE_ProgressBarGroove, &progressBarOptions);
469}

References progressBarOptions().

◆ setExponentRatio()

void KDcrawIface::RAbstractSliderSpinBox::setExponentRatio ( double dbl)

Definition at line 537 of file rsliderspinbox.cpp.

538{
540 Q_ASSERT(dbl > 0);
541 d->exponentRatio = dbl;
542}

◆ setInternalValue()

virtual void KDcrawIface::RAbstractSliderSpinBox::setInternalValue ( int value)
protectedpure virtual

◆ setSuffix()

void KDcrawIface::RAbstractSliderSpinBox::setSuffix ( const QString & suffix)

Definition at line 531 of file rsliderspinbox.cpp.

532{
534 d->suffix = suffix;
535}

◆ showEdit()

void KDcrawIface::RAbstractSliderSpinBox::showEdit ( )

Definition at line 133 of file rsliderspinbox.cpp.

134{
136
137 if (d->edit->isVisible()) return;
138
139 d->edit->setGeometry(editRect(spinBoxOptions()));
140 d->edit->setText(valueString());
141 d->edit->selectAll();
142 d->edit->show();
143 d->edit->setFocus(Qt::OtherFocusReason);
144 update();
145}

References editRect(), spinBoxOptions(), and valueString().

◆ sizeHint()

QSize KDcrawIface::RAbstractSliderSpinBox::sizeHint ( ) const
overrideprotected

Definition at line 357 of file rsliderspinbox.cpp.

358{
359 const Q_D(RAbstractSliderSpinBox);
360
361 QStyleOptionSpinBox spinOpts = spinBoxOptions();
362 QFontMetrics fm(font());
363
364 // We need at least 50 pixels or things start to look bad
365 int w = qMax(fm.horizontalAdvance(QString::number(d->maximum)), 50);
366 QSize hint(w, d->edit->sizeHint().height() + 3);
367
368 // Getting the size of the buttons is a pain as the calcs require a rect
369 // that is "big enough". We run the calc twice to get the "smallest" buttons
370 // This code was inspired by QAbstractSpinBox.
371
372 QSize extra(35, 6);
373 spinOpts.rect.setSize(hint + extra);
374 extra += hint - style()->subControlRect(QStyle::CC_SpinBox, &spinOpts,
375 QStyle::SC_SpinBoxEditField, this).size();
376
377 spinOpts.rect.setSize(hint + extra);
378 extra += hint - style()->subControlRect(QStyle::CC_SpinBox, &spinOpts,
379 QStyle::SC_SpinBoxEditField, this).size();
380 hint += extra;
381
382 spinOpts.rect = rect();
383
384 return style()->sizeFromContents(QStyle::CT_SpinBox, &spinOpts, hint, 0);
385}

References spinBoxOptions().

◆ spinBoxOptions()

QStyleOptionSpinBox KDcrawIface::RAbstractSliderSpinBox::spinBoxOptions ( ) const
protected

Definition at line 392 of file rsliderspinbox.cpp.

393{
394 const Q_D(RAbstractSliderSpinBox);
395
396 QStyleOptionSpinBox opts;
397 opts.initFrom(this);
398 opts.frame = false;
399 opts.buttonSymbols = QAbstractSpinBox::UpDownArrows;
400 opts.subControls = QStyle::SC_SpinBoxUp | QStyle::SC_SpinBoxDown;
401
402 // Disable non-logical buttons
403 if (d->value == d->minimum)
404 {
405 opts.stepEnabled = QAbstractSpinBox::StepUpEnabled;
406 }
407 else if (d->value == d->maximum)
408 {
409 opts.stepEnabled = QAbstractSpinBox::StepDownEnabled;
410 }
411 else
412 {
413 opts.stepEnabled = QAbstractSpinBox::StepUpEnabled | QAbstractSpinBox::StepDownEnabled;
414 }
415
416 // Deal with depressed buttons
417 if (d->upButtonDown)
418 {
419 opts.activeSubControls = QStyle::SC_SpinBoxUp;
420 }
421 else if (d->downButtonDown)
422 {
423 opts.activeSubControls = QStyle::SC_SpinBoxDown;
424 }
425 else
426 {
427 opts.activeSubControls = QFlags<QStyle::SubControl>();
428 }
429
430 return opts;
431}

◆ upButtonRect()

QRect KDcrawIface::RAbstractSliderSpinBox::upButtonRect ( const QStyleOptionSpinBox & spinBoxOptions) const
protected

Definition at line 471 of file rsliderspinbox.cpp.

472{
473 return style()->subControlRect(QStyle::CC_SpinBox, &spinBoxOptions,
474 QStyle::SC_SpinBoxUp);
475}

References spinBoxOptions().

◆ valueForX()

int KDcrawIface::RAbstractSliderSpinBox::valueForX ( int x,
Qt::KeyboardModifiers modifiers = Qt::NoModifier ) const
protected

Definition at line 483 of file rsliderspinbox.cpp.

484{
485 const Q_D(RAbstractSliderSpinBox);
486
487 QStyleOptionSpinBox spinOpts = spinBoxOptions();
488 QStyleOptionProgressBar progressOpts = progressBarOptions();
489
490 // Adjust for magic number in style code (margins)
491 QRect correctedProgRect = progressRect(progressOpts).adjusted(2, 2, -2, -2);
492
493 // Compute the distance of the progress bar, in pixel
494 double leftDbl = correctedProgRect.left();
495 double xDbl = x - leftDbl;
496
497 // Compute the ration of the progress bar used, linearly (ignoring the exponent)
498 double rightDbl = correctedProgRect.right();
499 double minDbl = d->minimum;
500 double maxDbl = d->maximum;
501 double dValues = (maxDbl - minDbl);
502 double percent = (xDbl / (rightDbl - leftDbl));
503
504 // If SHIFT is pressed, movement should be slowed.
505 if ( modifiers & Qt::ShiftModifier )
506 {
507 percent = d->shiftPercent + (percent - d->shiftPercent) * d->slowFactor;
508 }
509
510 // Final value
511 double realvalue = ((dValues * pow(percent, d->exponentRatio)) + minDbl);
512
513 // If key CTRL is pressed, round to the closest step.
514
515 if ( modifiers & Qt::ControlModifier )
516 {
517 double fstep = d->fastSliderStep;
518
519 if( modifiers & Qt::ShiftModifier )
520 {
521 fstep *= d->slowFactor;
522 }
523
524 realvalue = floor((realvalue + fstep / 2) / fstep) * fstep;
525 }
526
527 // Return the value
528 return int(realvalue);
529}
QRect progressRect(const QStyleOptionProgressBar &progressBarOptions) const

References progressBarOptions(), progressRect(), and spinBoxOptions().

◆ valueString()

virtual QString KDcrawIface::RAbstractSliderSpinBox::valueString ( ) const
protectedpure virtual

◆ wheelEvent()

void KDcrawIface::RAbstractSliderSpinBox::wheelEvent ( QWheelEvent * e)
overrideprotected

Definition at line 307 of file rsliderspinbox.cpp.

308{
309
311
312 int step = d->fastSliderStep;
313 if( e->modifiers() & Qt::ShiftModifier )
314 {
315 step = d->singleStep;
316 }
317
318 if ( e->angleDelta().y() > 0)
319 {
320 setInternalValue(d->value + step);
321 }
322 else
323 {
324 setInternalValue(d->value - step);
325 }
326
327 update();
328 e->accept();
329}

References setInternalValue().

Member Data Documentation

◆ d_ptr

RAbstractSliderSpinBoxPrivate* const KDcrawIface::RAbstractSliderSpinBox::d_ptr
protected

Definition at line 103 of file rsliderspinbox.h.


The documentation for this class was generated from the following files: