[krita/krita-grids-kazakov] /: Fix rulers to show mouse position

Dmitry Kazakov dimula73 at gmail.com
Wed Mar 2 11:38:07 UTC 2016


Git commit 70b30474d3e371a7354d648f55bf15dd27cbc4d4 by Dmitry Kazakov.
Committed on 02/03/2016 at 11:35.
Pushed by dkazakov into branch 'krita-grids-kazakov'.

Fix rulers to show mouse position

Now the user has two check boxes:
   * [ ] Show Rulers
   * [ ] Rulers Track Pointer

The first just shows the rulers and the second one activates
mouse tracking on them. That is done, because the tracking may
1) be distracting, 2) affect performance.

CC:kimageshop at kde.org

M  +2    -1    krita/krita.rc
M  +12   -0    krita/kritamenu.action
M  +0    -13   libs/flake/KoCanvasControllerWidget.cpp
M  +0    -3    libs/flake/KoCanvasControllerWidget.h
M  +8    -0    libs/ui/KisView.cpp
M  +24   -4    libs/ui/KisViewManager.cpp
M  +2    -0    libs/ui/KisViewManager.h
M  +25   -18   libs/ui/canvas/kis_canvas_controller.cpp
M  +9    -0    libs/ui/kis_config.cc
M  +3    -0    libs/ui/kis_config.h
M  +23   -19   libs/ui/kis_zoom_manager.cc
M  +7    -4    libs/ui/kis_zoom_manager.h
M  +5    -0    libs/widgets/KoRuler.cpp
M  +5    -0    libs/widgets/KoRuler.h

http://commits.kde.org/krita/70b30474d3e371a7354d648f55bf15dd27cbc4d4

diff --git a/krita/krita.rc b/krita/krita.rc
index 1b377ea..2812bc1 100644
--- a/krita/krita.rc
+++ b/krita/krita.rc
@@ -2,7 +2,7 @@
 <kpartgui xmlns="http://www.kde.org/standards/kxmlgui/1.0"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 name="Krita"
-version="91"
+version="92"
 xsi:schemaLocation="http://www.kde.org/standards/kxmlgui/1.0  http://www.kde.org/standards/kxmlgui/1.0/kxmlgui.xsd">
   <MenuBar>
     <Menu name="file" noMerge="1">
@@ -85,6 +85,7 @@ xsi:schemaLocation="http://www.kde.org/standards/kxmlgui/1.0  http://www.kde.org
 
       <Separator/>
       <Action name="view_ruler"/>
+      <Action name="rulers_track_mouse"/>
       <Action name="new_show_guides"/>
       <Action name="new_lock_guides"/>
       <Action name="new_snap_to_guides"/>
diff --git a/krita/kritamenu.action b/krita/kritamenu.action
index 30dc670..3af08ba 100644
--- a/krita/kritamenu.action
+++ b/krita/kritamenu.action
@@ -583,6 +583,18 @@
       <isCheckable>true</isCheckable>
       <statusTip></statusTip>
     </Action>
+    <Action name="rulers_track_mouse">
+      <icon></icon>
+      <text>Rulers Track Pointer</text>
+      <whatsThis>The rulers will track current mouse position and show it on screen. It can cause suptle performance slowdown</whatsThis>
+      <toolTip>Rulers Track Pointer</toolTip>
+      <iconText>Rulers Track Pointer</iconText>
+      <activationFlags>1</activationFlags>
+      <activationConditions>0</activationConditions>
+      <shortcut></shortcut>
+      <isCheckable>true</isCheckable>
+      <statusTip></statusTip>
+    </Action>
     <Action name="new_show_guides">
       <icon></icon>
       <text>Show Guides</text>
diff --git a/libs/flake/KoCanvasControllerWidget.cpp b/libs/flake/KoCanvasControllerWidget.cpp
index 64a82bd..caaba45 100644
--- a/libs/flake/KoCanvasControllerWidget.cpp
+++ b/libs/flake/KoCanvasControllerWidget.cpp
@@ -291,9 +291,6 @@ void KoCanvasControllerWidget::changeCanvasWidget(QWidget *widget)
 
     d->viewportWidget->setCanvas(widget);
     setFocusProxy(d->canvas->canvasWidget());
-
-    widget->installEventFilter(this);
-    widget->setMouseTracking(true);
 }
 
 int KoCanvasControllerWidget::visibleHeight() const
@@ -368,16 +365,6 @@ void KoCanvasControllerWidget::updateCanvasOffsetY()
                                  + verticalScrollBar()->pageStep() / 2.0) / documentSize().height());
 }
 
-bool KoCanvasControllerWidget::eventFilter(QObject *watched, QEvent *event)
-{
-    if (d->canvas && d->canvas->canvasWidget() && (watched == d->canvas->canvasWidget())) {
-         if (event->type() == QEvent::MouseMove || event->type() == QEvent::TabletMove) {
-            d->emitPointerPositionChangedSignals(event);
-        }
-    }
-    return false;
-}
-
 void KoCanvasControllerWidget::ensureVisible(KoShape *shape)
 {
     Q_ASSERT(shape);
diff --git a/libs/flake/KoCanvasControllerWidget.h b/libs/flake/KoCanvasControllerWidget.h
index 5e4855d..b503421 100644
--- a/libs/flake/KoCanvasControllerWidget.h
+++ b/libs/flake/KoCanvasControllerWidget.h
@@ -48,9 +48,6 @@ public:
     explicit KoCanvasControllerWidget(KActionCollection * actionCollection, QWidget *parent = 0);
     virtual ~KoCanvasControllerWidget();
 
-    /// Reimplemented from QObject
-    virtual bool eventFilter(QObject *watched, QEvent *event);
-
     /**
      * Reimplemented from QAbstractScrollArea.
      */
diff --git a/libs/ui/KisView.cpp b/libs/ui/KisView.cpp
index 9554e06..31bb21e 100644
--- a/libs/ui/KisView.cpp
+++ b/libs/ui/KisView.cpp
@@ -87,6 +87,7 @@
 #include "kis_filter_manager.h"
 #include "krita/gemini/ViewModeSwitchEvent.h"
 #include "krita_utils.h"
+#include "kis_input_manager.h"
 
 
 //static
@@ -274,6 +275,13 @@ void KisView::notifyCurrentStateChanged(bool isCurrent)
     if (!d->isCurrent && d->savedFloatingMessage) {
         d->savedFloatingMessage->removeMessage();
     }
+
+    KisInputManager *inputManager = globalInputManager();
+    if (d->isCurrent) {
+        inputManager->attachPriorityEventFilter(&d->canvasController);
+    } else {
+        inputManager->detachPriorityEventFilter(&d->canvasController);
+    }
 }
 
 void KisView::setShowFloatingMessage(bool show)
diff --git a/libs/ui/KisViewManager.cpp b/libs/ui/KisViewManager.cpp
index ad42daf..5e5efae 100644
--- a/libs/ui/KisViewManager.cpp
+++ b/libs/ui/KisViewManager.cpp
@@ -161,6 +161,7 @@ public:
         , wrapAroundAction(0)
         , levelOfDetailAction(0)
         , showRulersAction(0)
+        , rulersTrackMouseAction(0)
         , zoomTo100pct(0)
         , zoomIn(0)
         , zoomOut(0)
@@ -198,6 +199,7 @@ public:
     KisAction *wrapAroundAction;
     KisAction *levelOfDetailAction;
     KisAction *showRulersAction;
+    KisAction *rulersTrackMouseAction;
     KisAction *zoomTo100pct;
     KisAction *zoomIn;
     KisAction *zoomOut;
@@ -353,12 +355,15 @@ void KisViewManager::setCurrentView(KisView *view)
 
         d->viewConnections.addUniqueConnection(d->currentImageView->canvasController(), SIGNAL(toolOptionWidgetsChanged(QList<QPointer<QWidget> >)), mainWindow(), SLOT(newOptionWidgets(QList<QPointer<QWidget> >)));
         d->viewConnections.addUniqueConnection(d->currentImageView->image(), SIGNAL(sigColorSpaceChanged(const KoColorSpace*)), d->controlFrame.paintopBox(), SLOT(slotColorSpaceChanged(const KoColorSpace*)));
-        d->viewConnections.addUniqueConnection(d->showRulersAction, SIGNAL(toggled(bool)), imageView->zoomManager(), SLOT(toggleShowRulers(bool)));
-        d->showRulersAction->setChecked(imageView->zoomManager()->horizontalRulerVisible() && imageView->zoomManager()->verticalRulerVisible());
+        d->viewConnections.addUniqueConnection(d->showRulersAction, SIGNAL(toggled(bool)), imageView->zoomManager(), SLOT(setShowRulers(bool)));
+        d->viewConnections.addUniqueConnection(d->rulersTrackMouseAction, SIGNAL(toggled(bool)), imageView->zoomManager(), SLOT(setRulersTrackMouse(bool)));
         d->viewConnections.addUniqueConnection(d->zoomTo100pct, SIGNAL(triggered()), imageView->zoomManager(), SLOT(zoomTo100()));
         d->viewConnections.addUniqueConnection(d->zoomIn, SIGNAL(triggered()), imageView->zoomController()->zoomAction(), SLOT(zoomIn()));
         d->viewConnections.addUniqueConnection(d->zoomOut, SIGNAL(triggered()), imageView->zoomController()->zoomAction(), SLOT(zoomOut()));
 
+        imageView->zoomManager()->setShowRulers(d->showRulersAction->isChecked());
+        imageView->zoomManager()->setRulersTrackMouse(d->rulersTrackMouseAction->isChecked());
+
         showHideScrollbars();
     }
 
@@ -612,6 +617,11 @@ void KisViewManager::createActions()
     KisConfig cfg;
     d->showRulersAction = actionManager()->createAction("view_ruler");
     d->showRulersAction->setChecked(cfg.showRulers());
+    connect(d->showRulersAction, SIGNAL(toggled(bool)), SLOT(slotSaveShowRulersState(bool)));
+
+    d->rulersTrackMouseAction = actionManager()->createAction("rulers_track_mouse");
+    d->rulersTrackMouseAction->setChecked(cfg.rulersTrackMouse());
+    connect(d->rulersTrackMouseAction, SIGNAL(toggled(bool)), SLOT(slotSaveRulersTrackMouseState(bool)));
 
     d->zoomTo100pct = actionManager()->createAction("zoom_to_100pct");
 
@@ -625,8 +635,6 @@ void KisViewManager::createActions()
 
 }
 
-
-
 void KisViewManager::setupManagers()
 {
     // Create the managers for filters, selections, layers etc.
@@ -1154,6 +1162,18 @@ void KisViewManager::showHideScrollbars()
     }
 }
 
+void KisViewManager::slotSaveShowRulersState(bool value)
+{
+    KisConfig cfg;
+    cfg.setShowRulers(value);
+}
+
+void KisViewManager::slotSaveRulersTrackMouseState(bool value)
+{
+    KisConfig cfg;
+    cfg.setRulersTrackMouse(value);
+}
+
 void KisViewManager::setShowFloatingMessage(bool show)
 {
     d->showFloatingMessage = show;
diff --git a/libs/ui/KisViewManager.h b/libs/ui/KisViewManager.h
index 0b3a431..50d63aa 100644
--- a/libs/ui/KisViewManager.h
+++ b/libs/ui/KisViewManager.h
@@ -226,6 +226,8 @@ private Q_SLOTS:
     void guiUpdateTimeout();
     void changeAuthorProfile(const QString &profileName);
     void slotUpdateAuthorProfileActions();
+    void slotSaveShowRulersState(bool value);
+    void slotSaveRulersTrackMouseState(bool value);
 
 private:
     void createActions();
diff --git a/libs/ui/canvas/kis_canvas_controller.cpp b/libs/ui/canvas/kis_canvas_controller.cpp
index 0a9ffe2..0cd016a 100644
--- a/libs/ui/canvas/kis_canvas_controller.cpp
+++ b/libs/ui/canvas/kis_canvas_controller.cpp
@@ -30,39 +30,44 @@
 #include "kis_image.h"
 #include "KisViewManager.h"
 #include "KisView.h"
-#include "input/kis_tablet_event.h"
 #include "krita_utils.h"
+#include "kis_signal_compressor_with_param.h"
 
+static const int gRulersUpdateDelay = 80 /* ms */;
 
 struct KisCanvasController::Private {
     Private(KisCanvasController *qq)
         : q(qq),
           paintOpTransformationConnector(0)
     {
+        using namespace std::placeholders;
+
+        std::function<void (QPoint)> callback(
+            std::bind(&KisCanvasController::Private::emitPointerPositionChangedSignals, this, _1));
+
+        mousePositionCompressor.reset(
+            new KisSignalCompressorWithParam<QPoint>(
+                gRulersUpdateDelay,
+                callback,
+                KisSignalCompressor::FIRST_ACTIVE));
     }
 
-    QPointer<KisView>view;
+    QPointer<KisView> view;
     KisCoordinatesConverter *coordinatesConverter;
     KisCanvasController *q;
     KisPaintopTransformationConnector *paintOpTransformationConnector;
+    QScopedPointer<KisSignalCompressorWithParam<QPoint> > mousePositionCompressor;
 
-    void emitPointerPositionChangedSignals(QEvent *event);
+    void emitPointerPositionChangedSignals(QPoint pointerPos);
     void updateDocumentSizeAfterTransform();
     void showRotationValueOnCanvas();
     void showMirrorStateOnCanvas();
 };
 
-void KisCanvasController::Private::emitPointerPositionChangedSignals(QEvent *event)
+void KisCanvasController::Private::emitPointerPositionChangedSignals(QPoint pointerPos)
 {
     if (!coordinatesConverter) return;
 
-    QPoint pointerPos;
-    if (QMouseEvent *mouseEvent = dynamic_cast<QMouseEvent*>(event)) {
-        pointerPos = mouseEvent->pos();
-    } else if (QTabletEvent *tabletEvent = dynamic_cast<QTabletEvent*>(event)) {
-        pointerPos = tabletEvent->pos();
-    }
-
     QPointF documentPos = coordinatesConverter->widgetToDocument(pointerPos);
 
     q->proxyObject->emitDocumentMousePositionChanged(documentPos);
@@ -105,7 +110,6 @@ void KisCanvasController::setCanvas(KoCanvasBase *canvas)
 
     m_d->paintOpTransformationConnector =
         new KisPaintopTransformationConnector(kritaCanvas, this);
-
 }
 
 void KisCanvasController::changeCanvasWidget(QWidget *widget)
@@ -141,14 +145,17 @@ void KisCanvasController::wheelEvent(QWheelEvent *event)
 bool KisCanvasController::eventFilter(QObject *watched, QEvent *event)
 {
     KoCanvasBase *canvas = this->canvas();
-    if (canvas && canvas->canvasWidget() && (watched == canvas->canvasWidget())) {
-        if (event->type() == QEvent::MouseMove || event->type() == QEvent::TabletMove) {
-            m_d->emitPointerPositionChangedSignals(event);
-            return false;
-        }
+    if (!canvas || !canvas->canvasWidget() || canvas->canvasWidget() != watched) return false;
+
+    if (event->type() == QEvent::MouseMove) {
+        QMouseEvent *mevent = static_cast<QMouseEvent*>(event);
+        m_d->mousePositionCompressor->start(mevent->pos());
+    } else if (event->type() == QEvent::TabletMove) {
+        QTabletEvent *tevent = static_cast<QTabletEvent*>(event);
+        m_d->mousePositionCompressor->start(tevent->pos());
     }
 
-    return KoCanvasControllerWidget::eventFilter(watched, event);
+    return false;
 }
 
 void KisCanvasController::updateDocumentSize(const QSize &sz, bool recalculateCenter)
diff --git a/libs/ui/kis_config.cc b/libs/ui/kis_config.cc
index 273619f..64da11a 100644
--- a/libs/ui/kis_config.cc
+++ b/libs/ui/kis_config.cc
@@ -542,6 +542,15 @@ void KisConfig::setShowRulers(bool rulers) const
     m_cfg.writeEntry("showrulers", rulers);
 }
 
+bool KisConfig::rulersTrackMouse(bool defaultValue) const
+{
+    return (defaultValue ? true : m_cfg.readEntry("rulersTrackMouse", true));
+}
+
+void KisConfig::setRulersTrackMouse(bool value) const
+{
+    m_cfg.writeEntry("rulersTrackMouse", value);
+}
 
 qint32 KisConfig::pasteBehaviour(bool defaultValue) const
 {
diff --git a/libs/ui/kis_config.h b/libs/ui/kis_config.h
index ad9fe26..3e5bc63 100644
--- a/libs/ui/kis_config.h
+++ b/libs/ui/kis_config.h
@@ -141,6 +141,9 @@ public:
     bool showRulers(bool defaultValue = false) const;
     void setShowRulers(bool rulers) const;
 
+    bool rulersTrackMouse(bool defaultValue = false) const;
+    void setRulersTrackMouse(bool value) const;
+
     qint32 pasteBehaviour(bool defaultValue = false) const;
     void setPasteBehaviour(qint32 behaviour) const;
 
diff --git a/libs/ui/kis_zoom_manager.cc b/libs/ui/kis_zoom_manager.cc
index 8257e08..87a14e0 100644
--- a/libs/ui/kis_zoom_manager.cc
+++ b/libs/ui/kis_zoom_manager.cc
@@ -83,9 +83,6 @@ KisZoomManager::KisZoomManager(QPointer<KisView> view, KoZoomHandler * zoomHandl
 
 KisZoomManager::~KisZoomManager()
 {
-    KisConfig cfg;
-    cfg.setShowRulers(m_horizontalRuler->isVisible());
-
     if (m_zoomActionWidget && !m_zoomActionWidget->parent()) {
         delete m_zoomActionWidget;
     }
@@ -149,10 +146,6 @@ void KisZoomManager::setup(KActionCollection * actionCollection)
     connect(m_canvasController->proxyObject, SIGNAL(canvasOffsetYChanged(int)),
             this, SLOT(pageOffsetChanged()));
 
-    connect(m_canvasController->proxyObject,
-            SIGNAL(canvasMousePositionChanged(const QPoint &)),
-            SLOT(mousePositionChanged(const QPoint &)));
-
     connect(m_zoomController, SIGNAL(zoomChanged(KoZoomMode::Mode, qreal)),
             this, SLOT(slotZoomChanged(KoZoomMode::Mode, qreal)));
 
@@ -160,10 +153,23 @@ void KisZoomManager::setup(KActionCollection * actionCollection)
             this, SLOT(changeAspectMode(bool)));
 
     applyRulersUnit(m_view->document()->unit());
+}
 
-    KisConfig cfg;
-    toggleShowRulers(cfg.showRulers());
+void KisZoomManager::updateMouseTrackingConnections()
+{
+    bool value = m_horizontalRuler->isVisible() &&
+        m_verticalRuler->isVisible() &&
+        m_horizontalRuler->showMousePosition() &&
+        m_verticalRuler->showMousePosition();
 
+    m_mouseTrackingConnections.clear();
+
+    if (value) {
+        connect(m_canvasController->proxyObject,
+                SIGNAL(canvasMousePositionChanged(const QPoint &)),
+                SLOT(mousePositionChanged(const QPoint &)));
+
+    }
 }
 
 KoRuler* KisZoomManager::horizontalRuler() const
@@ -184,20 +190,18 @@ void KisZoomManager::mousePositionChanged(const QPoint &viewPos)
     m_verticalRuler->updateMouseCoordinate(pt.y());
 }
 
-bool KisZoomManager::horizontalRulerVisible() const
+void KisZoomManager::setShowRulers(bool show)
 {
-    return m_horizontalRuler->isVisible();
-}
-
-bool KisZoomManager::verticalRulerVisible() const
-{
-    return m_verticalRuler->isVisible();
+    m_horizontalRuler->setVisible(show);
+    m_verticalRuler->setVisible(show);
+    updateMouseTrackingConnections();
 }
 
-void KisZoomManager::toggleShowRulers(bool show)
+void KisZoomManager::setRulersTrackMouse(bool value)
 {
-    m_horizontalRuler->setVisible(show);
-    m_verticalRuler->setVisible(show);
+    m_horizontalRuler->setShowMousePosition(value);
+    m_verticalRuler->setShowMousePosition(value);
+    updateMouseTrackingConnections();
 }
 
 void KisZoomManager::applyRulersUnit(const KoUnit &baseUnit)
diff --git a/libs/ui/kis_zoom_manager.h b/libs/ui/kis_zoom_manager.h
index 2945df0..2591fc0 100644
--- a/libs/ui/kis_zoom_manager.h
+++ b/libs/ui/kis_zoom_manager.h
@@ -27,6 +27,7 @@
 #include <KoZoomAction.h>
 #include <KoZoomHandler.h>
 #include <KoZoomController.h>
+#include "kis_signal_auto_connection.h"
 
 #include "KisView.h"
 
@@ -67,7 +68,8 @@ public Q_SLOTS:
 
     void slotZoomChanged(KoZoomMode::Mode mode, qreal zoom);
     void slotScrollAreaSizeChanged();
-    void toggleShowRulers(bool show);
+    void setShowRulers(bool show);
+    void setRulersTrackMouse(bool value);
     void mousePositionChanged(const QPoint &viewPos);
     void changeAspectMode(bool aspectMode);
     void pageOffsetChanged();
@@ -75,9 +77,8 @@ public Q_SLOTS:
     void applyRulersUnit(const KoUnit &baseUnit);
     void setMinMaxZoom();
 
-public:
-    bool horizontalRulerVisible() const;
-    bool verticalRulerVisible() const;
+private:
+    void updateMouseTrackingConnections();
 
 private:
 
@@ -90,6 +91,8 @@ private:
     KoZoomAction * m_zoomAction;
     QPointer<QWidget> m_zoomActionWidget;
     QPoint m_rulersOffset;
+    KisSignalAutoConnectionsStore m_mouseTrackingConnections;
 };
 
+
 #endif
diff --git a/libs/widgets/KoRuler.cpp b/libs/widgets/KoRuler.cpp
index 64faa90..dcef848 100644
--- a/libs/widgets/KoRuler.cpp
+++ b/libs/widgets/KoRuler.cpp
@@ -928,6 +928,11 @@ void KoRuler::setShowMousePosition(bool show)
     update();
 }
 
+bool KoRuler::showMousePosition() const
+{
+    return d->showMousePosition;
+}
+
 void KoRuler::setRightToLeft(bool isRightToLeft)
 {
     d->rightToLeft = isRightToLeft;
diff --git a/libs/widgets/KoRuler.h b/libs/widgets/KoRuler.h
index 7ce02f1..35e311e 100644
--- a/libs/widgets/KoRuler.h
+++ b/libs/widgets/KoRuler.h
@@ -161,6 +161,11 @@ public Q_SLOTS:
       */
     void setShowMousePosition(bool show);
 
+    /**
+      * \see setShowMousePosition
+      */
+    bool showMousePosition() const;
+
     /** Update the current position of the mouse pointer, repainting if changed.
       * The ruler offset will be applied before painting.
       * @param coordinate Either the x or y coordinate of the mouse depending



More information about the kimageshop mailing list