[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    [calligra/krita-gpen-boud] krita/ui: reorganize smoothing options
From:       Boudewijn Rempt <boud () valdyas ! org>
Date:       2012-12-31 15:09:25
Message-ID: 20121231150925.B7CC2A6091 () git ! kde ! org
[Download RAW message or body]

Git commit ebf059a22d97dead95327fac191dea189cd28e5d by Boudewijn Rempt.
Committed on 31/12/2012 at 16:10.
Pushed by rempt into branch 'krita-gpen-boud'.

reorganize smoothing options

M  +1    -0    krita/ui/CMakeLists.txt
A  +25   -0    krita/ui/tool/kis_smoothing_options.cpp     [License: GPL (v2+)]
A  +40   -0    krita/ui/tool/kis_smoothing_options.h     [License: GPL (v2+)]
M  +1    -10   krita/ui/tool/kis_tool_freehand.cc
M  +2    -4    krita/ui/tool/kis_tool_freehand.h
M  +59   -55   krita/ui/tool/kis_tool_freehand_helper.cpp
M  +2    -2    krita/ui/tool/kis_tool_freehand_helper.h

http://commits.kde.org/calligra/ebf059a22d97dead95327fac191dea189cd28e5d

diff --git a/krita/ui/CMakeLists.txt b/krita/ui/CMakeLists.txt
index 2d8abc4..3eebb0a 100644
--- a/krita/ui/CMakeLists.txt
+++ b/krita/ui/CMakeLists.txt
@@ -147,6 +147,7 @@ set(kritaui_LIB_SRCS
     tool/kis_tool_polyline_base.cpp
     tool/kis_color_picker_utils.cpp
     tool/kis_resources_snapshot.cpp
+    tool/kis_smoothing_options.cpp
     tool/strokes/freehand_stroke.cpp
     tool/strokes/kis_painter_based_stroke_strategy.cpp
     widgets/kis_channelflags_widget.cpp
diff --git a/krita/ui/tool/kis_smoothing_options.cpp \
b/krita/ui/tool/kis_smoothing_options.cpp new file mode 100644
index 0000000..14326c4
--- /dev/null
+++ b/krita/ui/tool/kis_smoothing_options.cpp
@@ -0,0 +1,25 @@
+/*
+ *  Copyright (c) 2012 Boudewijn Rempt <boud@valdyas.org>
+ *
+ *  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_smoothing_options.h"
+
+KisSmoothingOptions::KisSmoothingOptions()
+    : smoothingType(WEIGHTED_SMOOTHING)
+    , smoothnessFactor(50.0)
+    , smoothnessQuality(20)
+{
+}
diff --git a/krita/ui/tool/kis_smoothing_options.h \
b/krita/ui/tool/kis_smoothing_options.h new file mode 100644
index 0000000..b28144d
--- /dev/null
+++ b/krita/ui/tool/kis_smoothing_options.h
@@ -0,0 +1,40 @@
+/*
+ *  Copyright (c) 2012 Boudewijn Rempt <boud@valdyas.org>
+ *
+ *  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_SMOOTHING_OPTIONS_H
+#define KIS_SMOOTHING_OPTIONS_H
+
+#include <qglobal.h>
+
+struct KisSmoothingOptions
+{
+
+    enum SmoothingType {
+        NO_SMOOTHING = 0,
+        SIMPLE_SMOOTHING,
+        WEIGHTED_SMOOTHING
+    };
+
+    KisSmoothingOptions();
+
+    SmoothingType smoothingType;
+    qreal smoothnessFactor;
+    int smoothnessQuality;
+
+};
+
+#endif // KIS_SMOOTHING_OPTIONS_H
diff --git a/krita/ui/tool/kis_tool_freehand.cc b/krita/ui/tool/kis_tool_freehand.cc
index b257c1a..87595aa 100644
--- a/krita/ui/tool/kis_tool_freehand.cc
+++ b/krita/ui/tool/kis_tool_freehand.cc
@@ -71,10 +71,6 @@ KisToolFreehand::KisToolFreehand(KoCanvasBase * canvas, const \
QCursor & cursor,  {
     m_explicitShowOutline = false;
 
-    m_smooth = true;
-    m_smoothnessFactor = 50;
-    m_smoothnessQuality = 20;
-
     m_assistant = false;
     m_magnetism = 1.0;
 
@@ -162,7 +158,7 @@ void KisToolFreehand::initStroke(KoPointerEvent *event)
 {
     setCurrentNodeLocked(true);
 
-    m_helper->setSmoothness(m_smooth, m_smoothnessFactor, m_smoothnessQuality);
+    m_helper->setSmoothness(m_smoothingOptions);
     m_helper->initPaint(event, canvas()->resourceManager(),
                         image(),
                         image().data(),
@@ -338,11 +334,6 @@ bool KisToolFreehand::wantsAutoScroll() const
     return false;
 }
 
-void KisToolFreehand::setSmooth(bool smooth)
-{
-    m_smooth = smooth;
-}
-
 void KisToolFreehand::setAssistant(bool assistant)
 {
     m_assistant = assistant;
diff --git a/krita/ui/tool/kis_tool_freehand.h b/krita/ui/tool/kis_tool_freehand.h
index 524cfea..adc8445 100644
--- a/krita/ui/tool/kis_tool_freehand.h
+++ b/krita/ui/tool/kis_tool_freehand.h
@@ -25,6 +25,7 @@
 #include "kis_resources_snapshot.h"
 #include "kis_paintop_settings.h"
 #include "kis_distance_information.h"
+#include "kis_smoothing_options.h"
 
 #include "krita_export.h"
 
@@ -83,7 +84,6 @@ protected:
 
 protected slots:
 
-    void setSmooth(bool smooth);
     void setAssistant(bool assistant);
 
 private:
@@ -114,10 +114,8 @@ private slots:
     void hideOutline();
 
 protected:
-    bool m_smooth;
-    qreal m_smoothnessFactor;
-    int m_smoothnessQuality;
 
+    KisSmoothingOptions m_smoothingOptions;
     bool m_assistant;
     double m_magnetism;
 
diff --git a/krita/ui/tool/kis_tool_freehand_helper.cpp \
b/krita/ui/tool/kis_tool_freehand_helper.cpp index 94e63c7..0356656 100644
--- a/krita/ui/tool/kis_tool_freehand_helper.cpp
+++ b/krita/ui/tool/kis_tool_freehand_helper.cpp
@@ -30,6 +30,7 @@
 #include "kis_recording_adapter.h"
 #include "kis_image.h"
 #include "kis_painter.h"
+#include "kis_smoothing_options.h"
 
 #include <math.h>
 #include <qnumeric.h> // for qIsNaN
@@ -55,9 +56,7 @@ struct KisToolFreehandHelper::Private
     KisPaintInformation previousPaintInformation;
     KisPaintInformation olderPaintInformation;
 
-    bool smooth;
-    qreal smoothnessFactor;
-    int smoothnessQuality;
+    KisSmoothingOptions smoothingOptions;
 
     QTimer airbrushingTimer;
 
@@ -73,10 +72,6 @@ KisToolFreehandHelper::KisToolFreehandHelper(KisPaintingInformationBuilder \
*info  m_d->infoBuilder = infoBuilder;
     m_d->recordingAdapter = recordingAdapter;
 
-    m_d->smooth = true;
-    m_d->smoothnessFactor = 50.0;
-    m_d->smoothnessQuality = 20;
-
     m_d->strokeTimeoutTimer.setSingleShot(true);
     connect(&m_d->strokeTimeoutTimer, SIGNAL(timeout()), SLOT(finishStroke()));
 
@@ -88,11 +83,9 @@ KisToolFreehandHelper::~KisToolFreehandHelper()
     delete m_d;
 }
 
-void KisToolFreehandHelper::setSmoothness(bool smooth, qreal smoothnessFactor, int \
smoothnessQuality) +void KisToolFreehandHelper::setSmoothness(const \
KisSmoothingOptions &smoothingOptions)  {
-    m_d->smooth = smooth;
-    m_d->smoothnessQuality = smoothnessQuality;
-    m_d->smoothnessFactor = smoothnessFactor;
+    m_d->smoothingOptions = smoothingOptions;
 }
 
 void KisToolFreehandHelper::initPaint(KoPointerEvent *event,
@@ -129,13 +122,13 @@ void KisToolFreehandHelper::initPaint(KoPointerEvent *event,
     }
 
     KisStrokeStrategy *stroke =
-        new FreehandStrokeStrategy(indirectPainting,
-                                   m_d->resources, m_d->painterInfos, i18n("Freehand \
Stroke")); +            new FreehandStrokeStrategy(indirectPainting,
+                                       m_d->resources, m_d->painterInfos, \
i18n("Freehand Stroke"));  
     m_d->strokeId = m_d->strokesFacade->startStroke(stroke);
 
     m_d->previousPaintInformation =
-        m_d->infoBuilder->startStroke(event, m_d->strokeTime.elapsed());
+            m_d->infoBuilder->startStroke(event, m_d->strokeTime.elapsed());
 
     m_d->history.clear();
     m_d->history.append(m_d->previousPaintInformation);
@@ -150,14 +143,18 @@ void KisToolFreehandHelper::initPaint(KoPointerEvent *event,
 void KisToolFreehandHelper::paint(KoPointerEvent *event)
 {
     KisPaintInformation info =
-        m_d->infoBuilder->continueStroke(event,
-                                         m_d->previousPaintInformation.pos(),
-                                         m_d->strokeTime.elapsed());
+            m_d->infoBuilder->continueStroke(event,
+                                             m_d->previousPaintInformation.pos(),
+                                             m_d->strokeTime.elapsed());
 
     // Smooth the coordinates out using the history and the velocity. See
     // https://bugs.kde.org/show_bug.cgi?id=281267 and \
                http://www24.atwiki.jp/sigetch_2007/pages/17.html.
     // This is also implemented in gimp, which is where I cribbed the code from.
-    if (m_d->smooth && m_d->smoothnessQuality > 1 && m_d->smoothnessFactor > 3.0) {
+    if (m_d->smoothingOptions.smoothingType == \
KisSmoothingOptions::WEIGHTED_SMOOTHING +            && \
m_d->smoothingOptions.smoothnessQuality > 1 +            && \
m_d->smoothingOptions.smoothnessFactor > 3.0) { +
+        qDebug() << "going to smooth";
 
         m_d->history.append(info);
         m_d->velocityHistory.append(std::numeric_limits<qreal>::signaling_NaN()); // \
Fake velocity! @@ -167,16 +164,16 @@ void KisToolFreehandHelper::paint(KoPointerEvent \
*event)  
         if (m_d->history.size() > 3) {
 
-            int length = qMin(m_d->smoothnessQuality, m_d->history.size());
+            int length = qMin(m_d->smoothingOptions.smoothnessQuality, \
m_d->history.size());  int minIndex = m_d->history.size() - length;
 
             qreal gaussianWeight = 0.0;
-            qreal gaussianWeight2 = m_d->smoothnessFactor * m_d->smoothnessFactor;
+            qreal gaussianWeight2 = m_d->smoothingOptions.smoothnessFactor * \
m_d->smoothingOptions.smoothnessFactor;  qreal velocitySum = 0.0;
             qreal scaleSum = 0.0;
 
             if (gaussianWeight2 != 0.0) {
-                gaussianWeight = 1 / (sqrt(2 * M_PI) * m_d->smoothnessFactor);
+                gaussianWeight = 1 / (sqrt(2 * M_PI) * \
m_d->smoothingOptions.smoothnessFactor);  }
 
             Q_ASSERT(m_d->history.size() == m_d->velocityHistory.size());
@@ -187,7 +184,7 @@ void KisToolFreehandHelper::paint(KoPointerEvent *event)
                 const KisPaintInformation nextInfo = m_d->history.at(i);
                 double velocity = m_d->velocityHistory.at(i);
 
-               if (qIsNaN(velocity)) {
+                if (qIsNaN(velocity)) {
 
                     int previousTime = nextInfo.currentTime();
                     if (i > 0) {
@@ -219,32 +216,39 @@ void KisToolFreehandHelper::paint(KoPointerEvent *event)
         }
     }
 
-    // Now paint between the coordinates, using the bezier curve interpolation
-    // The old, completely unsmoothed line-between-points option is gone.
-    if (!m_d->haveTangent) {
-        m_d->haveTangent = true;
-
-        // XXX: 3.0 is a magic number I don't know anything about
-        //      1.0 was the old default value for smoothness, and anything lower \
                than that
-        //      gave horrible results, so remove that setting.
-        m_d->previousTangent =
-                (info.pos() - m_d->previousPaintInformation.pos()) /
-                (3.0 * (info.currentTime() - \
                m_d->previousPaintInformation.currentTime()));
-    } else {
-        QPointF newTangent = (info.pos() - m_d->olderPaintInformation.pos()) /
-                (3.0 * (info.currentTime() - \
                m_d->olderPaintInformation.currentTime()));
-        qreal scaleFactor = (m_d->previousPaintInformation.currentTime() - \
                m_d->olderPaintInformation.currentTime());
-        QPointF control1 = m_d->olderPaintInformation.pos() + m_d->previousTangent * \
                scaleFactor;
-        QPointF control2 = m_d->previousPaintInformation.pos() - newTangent * \
                scaleFactor;
-        paintBezierCurve(m_d->painterInfos,
-                         m_d->olderPaintInformation,
-                         control1,
-                         control2,
-                         m_d->previousPaintInformation);
-        m_d->previousTangent = newTangent;
+    if (m_d->smoothingOptions.smoothingType == KisSmoothingOptions::SIMPLE_SMOOTHING
+            || m_d->smoothingOptions.smoothingType == \
KisSmoothingOptions::WEIGHTED_SMOOTHING) +    {
+        // Now paint between the coordinates, using the bezier curve interpolation
+        // The old, completely unsmoothed line-between-points option is gone.
+        if (!m_d->haveTangent) {
+            m_d->haveTangent = true;
+
+            // XXX: 3.0 is a magic number I don't know anything about
+            //      1.0 was the old default value for smoothness, and anything lower \
than that +            //      gave horrible results, so remove that setting.
+            m_d->previousTangent =
+                    (info.pos() - m_d->previousPaintInformation.pos()) /
+                    (3.0 * (info.currentTime() - \
m_d->previousPaintInformation.currentTime())); +        } else {
+            QPointF newTangent = (info.pos() - m_d->olderPaintInformation.pos()) /
+                    (3.0 * (info.currentTime() - \
m_d->olderPaintInformation.currentTime())); +            qreal scaleFactor = \
(m_d->previousPaintInformation.currentTime() - \
m_d->olderPaintInformation.currentTime()); +            QPointF control1 = \
m_d->olderPaintInformation.pos() + m_d->previousTangent * scaleFactor; +            \
QPointF control2 = m_d->previousPaintInformation.pos() - newTangent * scaleFactor; +  \
paintBezierCurve(m_d->painterInfos, +                             \
m_d->olderPaintInformation, +                             control1,
+                             control2,
+                             m_d->previousPaintInformation);
+            m_d->previousTangent = newTangent;
+        }
+        m_d->olderPaintInformation = m_d->previousPaintInformation;
+        m_d->strokeTimeoutTimer.start(100);
+    }
+    else {
+        paintLine(m_d->painterInfos, m_d->previousPaintInformation, info);
     }
-    m_d->olderPaintInformation = m_d->previousPaintInformation;
-    m_d->strokeTimeoutTimer.start(100);
 
 
 
@@ -259,7 +263,7 @@ void KisToolFreehandHelper::endPaint()
 {
     if (!m_d->hasPaintAtLeastOnce) {
         paintAt(m_d->painterInfos, m_d->previousPaintInformation);
-    } else if (m_d->smooth) {
+    } else if (m_d->smoothingOptions.smoothingType != \
KisSmoothingOptions::NO_SMOOTHING) {  finishStroke();
     }
     m_d->strokeTimeoutTimer.stop();
@@ -318,8 +322,8 @@ void KisToolFreehandHelper::paintAt(PainterInfo *painterInfo,
 {
     m_d->hasPaintAtLeastOnce = true;
     m_d->strokesFacade->addJob(m_d->strokeId,
-        new FreehandStrokeStrategy::Data(m_d->resources->currentNode(),
-                                         painterInfo, pi));
+                               new \
FreehandStrokeStrategy::Data(m_d->resources->currentNode(), +                         \
painterInfo, pi));  
     if(m_d->recordingAdapter) {
         m_d->recordingAdapter->addPoint(pi);
@@ -332,8 +336,8 @@ void KisToolFreehandHelper::paintLine(PainterInfo *painterInfo,
 {
     m_d->hasPaintAtLeastOnce = true;
     m_d->strokesFacade->addJob(m_d->strokeId,
-        new FreehandStrokeStrategy::Data(m_d->resources->currentNode(),
-                                         painterInfo, pi1, pi2));
+                               new \
FreehandStrokeStrategy::Data(m_d->resources->currentNode(), +                         \
painterInfo, pi1, pi2));  
     if(m_d->recordingAdapter) {
         m_d->recordingAdapter->addLine(pi1, pi2);
@@ -348,9 +352,9 @@ void KisToolFreehandHelper::paintBezierCurve(PainterInfo \
*painterInfo,  {
     m_d->hasPaintAtLeastOnce = true;
     m_d->strokesFacade->addJob(m_d->strokeId,
-        new FreehandStrokeStrategy::Data(m_d->resources->currentNode(),
-                                         painterInfo,
-                                         pi1, control1, control2, pi2));
+                               new \
FreehandStrokeStrategy::Data(m_d->resources->currentNode(), +                         \
painterInfo, +                                                                pi1, \
control1, control2, pi2));  
     if(m_d->recordingAdapter) {
         m_d->recordingAdapter->addCurve(pi1, control1, control2, pi2);
diff --git a/krita/ui/tool/kis_tool_freehand_helper.h \
b/krita/ui/tool/kis_tool_freehand_helper.h index b3ad1b0..ebfd526 100644
--- a/krita/ui/tool/kis_tool_freehand_helper.h
+++ b/krita/ui/tool/kis_tool_freehand_helper.h
@@ -35,7 +35,7 @@ class KisStrokesFacade;
 class KisPostExecutionUndoAdapter;
 class KisPaintOp;
 class KisPainter;
-
+class KisSmoothingOptions;
 
 class KRITAUI_EXPORT KisToolFreehandHelper : public QObject
 {
@@ -51,7 +51,7 @@ public:
                           KisRecordingAdapter *recordingAdapter = 0);
     ~KisToolFreehandHelper();
 
-    void setSmoothness(bool smooth, qreal smoothnessFactor, int smoothnessQuality);
+    void setSmoothness(const KisSmoothingOptions &smoothingOptions);
 
     void initPaint(KoPointerEvent *event,
                    KoCanvasResourceManager *resourceManager,


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic