[calligra/calligra/2.9] krita/ui: Added an optional optimization to slider spin box

Stefano Bonicatti smjert at gmail.com
Fri Jul 3 18:11:52 UTC 2015


Git commit 1815fdb849db97f839a8aa7f2e760c025b55e56b by Stefano Bonicatti.
Committed on 03/07/2015 at 18:11.
Pushed by stefanobonicatti into branch 'calligra/2.9'.

Added an optional optimization to slider spin box

We are not always interested in constant updates through signal
when dragging the slider with the mouse or a tablet pen, so calling
setBlockUpdateSignalOnDrag(true) will inform the widget that
it has to internally update the slider value and ask inheriting classes
(in this case KisDoubleSliderSpinBox and KisSliderSpinBox)
to not send any valueChanged signal.

Since an update is sent anyway when releasing the left mouse button,
everything still works fine and for instance  now there's no slowdown
when dragging the brush size slider.

Currently that option is set to true to the Opacity, Flow and Size slider of KisPaintOpBox.

CCMAIL: kimageshop at kde.org

M  +3    -0    krita/ui/kis_paintop_box.cc
M  +25   -7    krita/ui/widgets/kis_slider_spin_box.cpp
M  +17   -3    krita/ui/widgets/kis_slider_spin_box.h

http://commits.kde.org/calligra/1815fdb849db97f839a8aa7f2e760c025b55e56b

diff --git a/krita/ui/kis_paintop_box.cc b/krita/ui/kis_paintop_box.cc
index 52054e4..7de6aca 100644
--- a/krita/ui/kis_paintop_box.cc
+++ b/krita/ui/kis_paintop_box.cc
@@ -221,12 +221,14 @@ KisPaintopBox::KisPaintopBox(KisViewManager *view, QWidget *parent, const char *
         slOpacity->setSingleStep(0.05);
         slOpacity->setMinimumWidth(qMax(sliderWidth, slOpacity->sizeHint().width()));
         slOpacity->setFixedHeight(iconsize);
+        slOpacity->setBlockUpdateSignalOnDrag(true);
 
         slFlow->setRange(0.0, 1.0, 2);
         slFlow->setValue(1.0);
         slFlow->setSingleStep(0.05);
         slFlow->setMinimumWidth(qMax(sliderWidth, slFlow->sizeHint().width()));
         slFlow->setFixedHeight(iconsize);
+        slFlow->setBlockUpdateSignalOnDrag(true);
 
         slSize->setRange(0, 1000, 2);
         slSize->setValue(100);
@@ -236,6 +238,7 @@ KisPaintopBox::KisPaintopBox(KisViewManager *view, QWidget *parent, const char *
         slSize->setSuffix(" px");
         slSize->setMinimumWidth(qMax(sliderWidth, slSize->sizeHint().width()));
         slSize->setFixedHeight(iconsize);
+        slSize->setBlockUpdateSignalOnDrag(true);
 
         m_sliderChooser[i]->chooseWidget(cfg.toolbarSlider(i + 1));
     }
diff --git a/krita/ui/widgets/kis_slider_spin_box.cpp b/krita/ui/widgets/kis_slider_spin_box.cpp
index c23f244..b14f2f4 100644
--- a/krita/ui/widgets/kis_slider_spin_box.cpp
+++ b/krita/ui/widgets/kis_slider_spin_box.cpp
@@ -62,6 +62,7 @@ public:
     int singleStep;
     QSpinBox* dummySpinBox;
     Style style;
+    bool blockUpdateSignalOnDrag;
 };
 
 KisAbstractSliderSpinBox::KisAbstractSliderSpinBox(QWidget* parent, KisAbstractSliderSpinBoxPrivate* _d)
@@ -369,7 +370,7 @@ void KisAbstractSliderSpinBox::mouseMoveEvent(QMouseEvent* e)
     //Respect emulated mouse grab.
     if (e->buttons() & Qt::LeftButton &&
             !(d->downButtonDown || d->upButtonDown)) {
-        setInternalValue(valueForX(e->pos().x(),e->modifiers()));
+        setInternalValue(valueForX(e->pos().x(),e->modifiers()), d->blockUpdateSignalOnDrag);
         update();
     }
 }
@@ -655,6 +656,12 @@ void KisAbstractSliderSpinBox::setExponentRatio(qreal dbl)
     d->exponentRatio = dbl;
 }
 
+void KisAbstractSliderSpinBox::setBlockUpdateSignalOnDrag(bool blockUpdateSignal)
+{
+    Q_D(KisAbstractSliderSpinBox);
+    d->blockUpdateSignalOnDrag = blockUpdateSignal;
+}
+
 void KisAbstractSliderSpinBox::contextMenuEvent(QContextMenuEvent* event)
 {
     event->accept();
@@ -669,6 +676,11 @@ void KisAbstractSliderSpinBox::editLostFocus()
     }
 }
 
+void KisAbstractSliderSpinBox::setInternalValue(int value)
+{
+    setInternalValue(value, false);
+}
+
 class KisSliderSpinBoxPrivate : public KisAbstractSliderSpinBoxPrivate {
 };
 
@@ -735,7 +747,7 @@ int KisSliderSpinBox::value()
 
 void KisSliderSpinBox::setValue(int value)
 {
-    setInternalValue(value);
+    setInternalValue(value, false);
     update();
 }
 
@@ -756,11 +768,14 @@ void KisSliderSpinBox::setPageStep(int value)
     Q_UNUSED(value);
 }
 
-void KisSliderSpinBox::setInternalValue(int _value)
+void KisSliderSpinBox::setInternalValue(int _value, bool blockUpdateSignal)
 {
     Q_D(KisAbstractSliderSpinBox);
     d->value = qBound(d->minimum, _value, d->maximum);
-    emit(valueChanged(value()));
+
+    if(!blockUpdateSignal) {
+        emit(valueChanged(value()));
+    }
 }
 
 class KisDoubleSliderSpinBoxPrivate : public KisAbstractSliderSpinBoxPrivate {
@@ -839,7 +854,7 @@ qreal KisDoubleSliderSpinBox::value()
 void KisDoubleSliderSpinBox::setValue(qreal value)
 {
     Q_D(KisAbstractSliderSpinBox);
-    setInternalValue(d->value = qRound(value * d->factor));
+    setInternalValue(d->value = qRound(value * d->factor), false);
     update();
 }
 
@@ -855,11 +870,14 @@ QString KisDoubleSliderSpinBox::valueString() const
     return QString::number((qreal)d->value / d->factor, 'f', d->validator->decimals());
 }
 
-void KisDoubleSliderSpinBox::setInternalValue(int _value)
+void KisDoubleSliderSpinBox::setInternalValue(int _value, bool blockUpdateSignal)
 {
     Q_D(KisAbstractSliderSpinBox);
     d->value = qBound(d->minimum, _value, d->maximum);
-    emit(valueChanged(value()));
+
+    if(!blockUpdateSignal) {
+        emit(valueChanged(value()));
+    }
 }
 
 
diff --git a/krita/ui/widgets/kis_slider_spin_box.h b/krita/ui/widgets/kis_slider_spin_box.h
index 229e445..e55da86 100644
--- a/krita/ui/widgets/kis_slider_spin_box.h
+++ b/krita/ui/widgets/kis_slider_spin_box.h
@@ -50,6 +50,13 @@ public:
 
     void setExponentRatio(qreal dbl);
 
+    /**
+     * If set to block, it informs inheriting classes that they shouldn't emit signals
+     * if the update comes from a mouse dragging the slider.
+     * Set this to true when dragging the slider and updates during the drag are not needed.
+     */
+    void setBlockUpdateSignalOnDrag(bool block);
+
     virtual QSize sizeHint() const;
     virtual QSize minimumSizeHint() const;
     virtual QSize minimumSize() const;
@@ -74,7 +81,11 @@ protected:
     int valueForX(int x, Qt::KeyboardModifiers modifiers = Qt::NoModifier) const;
 
     virtual QString valueString() const = 0;
-    virtual void setInternalValue(int value) = 0;
+    /**
+     * Sets the slider internal value. Inheriting classes should respect blockUpdateSignal
+     * so that, in specific cases, we have a performance improvement. See setIgnoreMouseMoveEvents.
+     */
+    virtual void setInternalValue(int value, bool blockUpdateSignal) = 0;
 
 protected Q_SLOTS:
     void contextMenuEvent(QContextMenuEvent * event);
@@ -88,6 +99,9 @@ protected:
     void paint(QPainter& painter);
     void paintPlastique(QPainter& painter);
     void paintBreeze(QPainter& painter);
+
+private:
+    void setInternalValue(int value);
 };
 
 class KRITAUI_EXPORT KisSliderSpinBox : public KisAbstractSliderSpinBox
@@ -122,7 +136,7 @@ public Q_SLOTS:
 
 protected:
     virtual QString valueString() const;
-    virtual void setInternalValue(int value);
+    virtual void setInternalValue(int value, bool blockUpdateSignal);
 Q_SIGNALS:
     void valueChanged(int value);
 };
@@ -150,7 +164,7 @@ public:
     void setSingleStep(qreal value);
 protected:
     virtual QString valueString() const;
-    virtual void setInternalValue(int value);
+    virtual void setInternalValue(int value, bool blockUpdateSignal);
 Q_SIGNALS:
     void valueChanged(qreal value);
 };


More information about the kimageshop mailing list