[calligra] /: Fixed autoscrolling bug in Krita

Dmitry Kazakov dimula73 at gmail.com
Mon May 9 10:30:00 CEST 2011


Git commit 49f67ef803e98d259de7dbac01f91fc13746c57d by Dmitry Kazakov.
Committed on 13/03/2011 at 17:33.
Pushed by dkazakov into branch 'master'.

Fixed autoscrolling bug in Krita

I've extracted one method from KoToolProxy and overridden it in newly
created KisToolProxy. This method just converts widget coordinates
into document coordinates.

Fixed a bug reported by Thorsten.

WARNING: Be careful, the definition of 'document origin' differs in
Krita and other flake-based applications. In Krita 'origin' is used
only when the size of the image is less than the size of the image and
vast scrolling is disabled. It is used for centering and means "the
offset from the topLeft of the widget to the corner of the
document". In flake, 'origin' means "the offset of the document in a
big white rectangle that surrounds the document". Krita has no such
rectangle and the origin is used in a different way due to historical
reasons, i guess.

BUG:265528
CC:kimageshop at kde.org,t.zachmann at zagge.de

M  +1    -0    krita/ui/CMakeLists.txt     
M  +2    -2    krita/ui/canvas/kis_canvas2.cpp     
A  +34   -0    krita/ui/canvas/kis_tool_proxy.cpp         [License: GPL (v2+)]
A  +33   -0    krita/ui/canvas/kis_tool_proxy.h         [License: GPL (v2+)]
M  +32   -9    libs/flake/KoToolProxy.cpp     
M  +5    -1    libs/flake/KoToolProxy.h     
M  +1    -1    libs/flake/KoToolProxy_p.h     

http://commits.kde.org/calligra/49f67ef803e98d259de7dbac01f91fc13746c57d

diff --git a/krita/ui/CMakeLists.txt b/krita/ui/CMakeLists.txt
index 3bdcb53..517adb6 100644
--- a/krita/ui/CMakeLists.txt
+++ b/krita/ui/CMakeLists.txt
@@ -21,6 +21,7 @@ set(kritaui_LIB_SRCS
     canvas/kis_canvas_widget_base.cpp 
     canvas/kis_canvas2.cpp
     canvas/kis_canvas_controller.cpp
+    canvas/kis_tool_proxy.cpp
     canvas/kis_canvas_decoration.cc
     canvas/kis_coordinates_converter.cpp 
     canvas/kis_grid_manager.cpp 
diff --git a/krita/ui/canvas/kis_canvas2.cpp b/krita/ui/canvas/kis_canvas2.cpp
index 3e4dc6c..ce65d34 100644
--- a/krita/ui/canvas/kis_canvas2.cpp
+++ b/krita/ui/canvas/kis_canvas2.cpp
@@ -34,9 +34,9 @@
 #include <KoColorSpaceRegistry.h>
 #include <KoCanvasControllerWidget.h>
 #include <KoDocument.h>
-#include <KoToolProxy.h>
 #include <KoSelection.h>
 
+#include "kis_tool_proxy.h"
 #include "kis_coordinates_converter.h"
 #include "kis_prescaled_projection.h"
 #include "kis_image.h"
@@ -78,7 +78,7 @@ public:
         , monitorProfile(0)
         , currentCanvasIsOpenGL(false)
         , currentCanvasUsesOpenGLShaders(false)
-        , toolProxy(new KoToolProxy(parent))
+        , toolProxy(new KisToolProxy(parent))
         , favoriteResourceManager(0)
         , vastScrolling(true) {
     }
diff --git a/krita/ui/canvas/kis_tool_proxy.cpp b/krita/ui/canvas/kis_tool_proxy.cpp
new file mode 100644
index 0000000..17479dd
--- /dev/null
+++ b/krita/ui/canvas/kis_tool_proxy.cpp
@@ -0,0 +1,34 @@
+/*
+ *  Copyright (c) 2011 Dmitry Kazakov <dimula73 at gmail.com>
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+#include "kis_tool_proxy.h"
+#include "kis_canvas2.h"
+
+KisToolProxy::KisToolProxy(KoCanvasBase *canvas, QObject *parent)
+    : KoToolProxy(canvas, parent)
+{
+}
+
+QPointF KisToolProxy::widgetToDocument(const QPointF &widgetPoint) const
+{
+    KisCanvas2 *kritaCanvas = dynamic_cast<KisCanvas2*>(canvas());
+    Q_ASSERT(kritaCanvas);
+
+    return kritaCanvas->coordinatesConverter()->widgetToDocument(widgetPoint);
+}
+
diff --git a/krita/ui/canvas/kis_tool_proxy.h b/krita/ui/canvas/kis_tool_proxy.h
new file mode 100644
index 0000000..d30924b
--- /dev/null
+++ b/krita/ui/canvas/kis_tool_proxy.h
@@ -0,0 +1,33 @@
+/*
+ *  Copyright (c) 2011 Dmitry Kazakov <dimula73 at gmail.com>
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+#ifndef __KIS_TOOL_PROXY_H
+#define __KIS_TOOL_PROXY_H
+
+#include <KoToolProxy.h>
+
+class KisToolProxy : public KoToolProxy
+{
+public:
+    KisToolProxy(KoCanvasBase *canvas, QObject *parent = 0);
+
+protected:
+    QPointF widgetToDocument(const QPointF &widgetPoint) const;
+};
+
+#endif /* __KIS_TOOL_PROXY_H */
diff --git a/libs/flake/KoToolProxy.cpp b/libs/flake/KoToolProxy.cpp
index 4f1608e..1c68fbd 100644
--- a/libs/flake/KoToolProxy.cpp
+++ b/libs/flake/KoToolProxy.cpp
@@ -48,20 +48,27 @@ KoToolProxyPrivate::KoToolProxyPrivate(KoToolProxy *p)
 void KoToolProxyPrivate::timeout() // Auto scroll the canvas
 {
     Q_ASSERT(controller);
-    int offsetX = controller->canvasOffsetX();
-    int offsetY = controller->canvasOffsetY();
-    QRectF mouseArea(scrollEdgePoint, QSizeF(10, 10));
+
+    QPoint offset = QPoint(controller->canvasOffsetX(), controller->canvasOffsetY());
+    QPoint origin = controller->canvas()->documentOrigin();
+    QPoint viewPoint = widgetScrollPoint - origin - offset;
+
+    QRectF mouseArea(viewPoint, QSizeF(10, 10));
     mouseArea.setTopLeft(mouseArea.center());
 
     controller->ensureVisible(mouseArea, true);
 
-    QPoint moved(offsetX - controller->canvasOffsetX(), offsetY - controller->canvasOffsetY());
-    if (moved.x() == 0 && moved.y() == 0)
+    QPoint newOffset = QPoint(controller->canvasOffsetX(), controller->canvasOffsetY());
+
+    QPoint moved = offset - newOffset;
+    if (moved.isNull())
         return;
-    scrollEdgePoint += moved;
 
-    QMouseEvent event(QEvent::MouseMove, scrollEdgePoint, Qt::LeftButton, Qt::LeftButton, 0);
-    KoPointerEvent ev(&event, controller->canvas()->viewConverter()->viewToDocument(scrollEdgePoint));
+    widgetScrollPoint += moved;
+
+    QPointF documentPoint = parent->widgetToDocument(widgetScrollPoint);
+    QMouseEvent event(QEvent::MouseMove, widgetScrollPoint, Qt::LeftButton, Qt::LeftButton, 0);
+    KoPointerEvent ev(&event, documentPoint);
     activeTool->mouseMoveEvent(&ev);
 }
 
@@ -72,7 +79,9 @@ void KoToolProxyPrivate::checkAutoScroll(const KoPointerEvent &event)
     if (!activeTool->wantsAutoScroll()) return;
     if (!event.isAccepted()) return;
     if (event.buttons() != Qt::LeftButton) return;
-    scrollEdgePoint = controller->canvas()->viewConverter()->documentToView(event.point).toPoint();
+
+    widgetScrollPoint = event.pos();
+
     if (! scrollTimer.isActive())
         scrollTimer.start();
 }
@@ -121,6 +130,20 @@ void KoToolProxy::repaintDecorations()
     if (d->activeTool) d->activeTool->repaintDecorations();
 }
 
+QPointF KoToolProxy::widgetToDocument(const QPointF &widgetPoint) const
+{
+    QPoint offset = QPoint(d->controller->canvasOffsetX(), d->controller->canvasOffsetY());
+    QPoint origin = d->controller->canvas()->documentOrigin();
+    QPoint viewPoint = widgetPoint.toPoint() - origin - offset;
+
+    return d->controller->canvas()->viewConverter()->viewToDocument(viewPoint);
+}
+
+KoCanvasBase* KoToolProxy::canvas() const
+{
+    return d->controller->canvas();
+}
+
 #include <KDebug>
 void KoToolProxy::tabletEvent(QTabletEvent *event, const QPointF &point)
 {
diff --git a/libs/flake/KoToolProxy.h b/libs/flake/KoToolProxy.h
index c5393f2..c36fd2c 100644
--- a/libs/flake/KoToolProxy.h
+++ b/libs/flake/KoToolProxy.h
@@ -61,7 +61,7 @@ public:
      * @param parent a parent QObject for memory management purposes.
      */
     explicit KoToolProxy(KoCanvasBase *canvas, QObject *parent = 0);
-    ~KoToolProxy();
+    virtual ~KoToolProxy();
 
     /// Forwarded to the current KoToolBase
     void paint(QPainter &painter, const KoViewConverter &converter);
@@ -140,6 +140,10 @@ signals:
      */
     void toolChanged(const QString &toolId);
 
+protected:
+    virtual QPointF widgetToDocument(const QPointF &widgetPoint) const;
+    KoCanvasBase* canvas() const;
+
 private:
     Q_PRIVATE_SLOT(d, void timeout())
     Q_PRIVATE_SLOT(d, void selectionChanged(bool))
diff --git a/libs/flake/KoToolProxy_p.h b/libs/flake/KoToolProxy_p.h
index 8baf4ac..937ebd1 100644
--- a/libs/flake/KoToolProxy_p.h
+++ b/libs/flake/KoToolProxy_p.h
@@ -48,7 +48,7 @@ public:
     bool tabletPressed;
     bool hasSelection;
     QTimer scrollTimer;
-    QPoint scrollEdgePoint;
+    QPoint widgetScrollPoint;
     KoCanvasController *controller;
     KoToolProxy *parent;
 


More information about the kimageshop mailing list