Krita is a free and open source cross-platform application that offers an end-to-end solution for creating digital art files from scratch built on the KDE and Qt frameworks.
C++
Implement automatic gutter width for Knife Tool
Agata Cackoebb893ce0d87
diff --git a/plugins/tools/tool_knife/CMakeLists.txt b/plugins/tools/tool_knife/CMakeLists.txtindex d59e72b94b..0ba936fa3f 100644--- a/plugins/tools/tool_knife/CMakeLists.txt+++ b/plugins/tools/tool_knife/CMakeLists.txt@@ -4,6 +4,7 @@ set(kritatoolKnife_SOURCESKisToolKnifeOptionsWidget.cppCutThroughShapeStrategy.cppRemoveGutterStrategy.cpp+ GutterWidthsConfig.cpp)ki18n_wrap_ui(kritatoolKnife_SOURCES KisToolKnifeOptionsWidget.ui)diff --git a/plugins/tools/tool_knife/CutThroughShapeStrategy.cpp b/plugins/tools/tool_knife/CutThroughShapeStrategy.cppindex 89c17521ae..ac41a12d4d 100644--- a/plugins/tools/tool_knife/CutThroughShapeStrategy.cpp+++ b/plugins/tools/tool_knife/CutThroughShapeStrategy.cpp@@ -23,9 +23,10 @@#include <KoShapeController.h>#include <kundo2command.h>#include <KoKeepShapesSelectedCommand.h>+#include <QtMath>-CutThroughShapeStrategy::CutThroughShapeStrategy(KoToolBase *tool, KoSelection *selection, QPointF startPoint, qreal width)+CutThroughShapeStrategy::CutThroughShapeStrategy(KoToolBase *tool, KoSelection *selection, QPointF startPoint, const GutterWidthsConfig &width): KoInteractionStrategy(tool), m_startPoint(startPoint), m_endPoint(startPoint)@@ -77,7 +78,7 @@ void CutThroughShapeStrategy::handleMouseMove(const QPointF &mouseLocation, Qt::QRectF dirtyRect;KisAlgebra2D::accumulateBounds(m_startPoint, &dirtyRect);KisAlgebra2D::accumulateBounds(m_endPoint, &dirtyRect);- dirtyRect = kisGrowRect(dirtyRect, gutterWidthInDocumentCoordinates()); // twice as much as it should need to account for lines showing the effect+ dirtyRect = kisGrowRect(dirtyRect, gutterWidthInDocumentCoordinates(calculateLineAngle(m_startPoint, m_endPoint))); // twice as much as it should need to account for lines showing the effect//dirtyRect = canvas()->viewConverter()->viewToDocument(dirtyRect);//qCritical() << "And the dirty rect is: " << dirtyRect;@@ -138,7 +139,8 @@ void CutThroughShapeStrategy::finishInteraction(Qt::KeyboardModifiers modifiers)return;}- qreal gutterWidth = gutterWidthInDocumentCoordinates();+ qCritical() << "line angle = " << calculateLineAngle(m_startPoint, m_endPoint);+ qreal gutterWidth = gutterWidthInDocumentCoordinates(calculateLineAngle(m_startPoint, m_endPoint));QList<QLineF> gapLines = KisAlgebra2D::getParallelLines(gapLine, gutterWidth/2);@@ -276,7 +278,7 @@ void CutThroughShapeStrategy::paint(QPainter &painter, const KoViewConverter &copainter.setRenderHint(QPainter::RenderHint::Antialiasing, true);- qreal gutterWidth = gutterWidthInDocumentCoordinates();+ qreal gutterWidth = gutterWidthInDocumentCoordinates(calculateLineAngle(m_startPoint, m_endPoint));QLineF gutterCenterLine = QLineF(m_startPoint, m_endPoint);gutterCenterLine = converter.documentToView().map(gutterCenterLine);@@ -309,11 +311,18 @@ void CutThroughShapeStrategy::paint(QPainter &painter, const KoViewConverter &copainter.restore();}-qreal CutThroughShapeStrategy::gutterWidthInDocumentCoordinates()+qreal CutThroughShapeStrategy::gutterWidthInDocumentCoordinates(qreal lineAngle){KisCanvas2 *kisCanvas = static_cast<KisCanvas2 *>(tool()->canvas());- KIS_SAFE_ASSERT_RECOVER_RETURN_VALUE(kisCanvas, m_width);- QLineF helperGapWidthLine = QLineF(QPointF(0, 0), QPointF(0, m_width));+ KIS_SAFE_ASSERT_RECOVER_RETURN_VALUE(kisCanvas, m_width.widthForAngleInPixels(lineAngle));+ QLineF helperGapWidthLine = QLineF(QPointF(0, 0), QPointF(0, m_width.widthForAngleInPixels(lineAngle)));QLineF helperGapWidthLineTransformed = kisCanvas->coordinatesConverter()->imageToDocument(helperGapWidthLine);return helperGapWidthLineTransformed.length();}++qreal CutThroughShapeStrategy::calculateLineAngle(QPointF start, QPointF end)+{+ QPointF vec = end - start;+ qreal angleDegrees = KisAlgebra2D::wrapValue(kisRadiansToDegrees(std::atan2(vec.y(), vec.x())), 0.0, 360.0);+ return angleDegrees;+}diff --git a/plugins/tools/tool_knife/CutThroughShapeStrategy.h b/plugins/tools/tool_knife/CutThroughShapeStrategy.hindex e336c728ca..6849219a33 100644--- a/plugins/tools/tool_knife/CutThroughShapeStrategy.h+++ b/plugins/tools/tool_knife/CutThroughShapeStrategy.h@@ -13,6 +13,8 @@#include <KoInteractionStrategy.h>#include <KoShape.h>+#include "GutterWidthsConfig.h"+class KoSelection;@@ -20,7 +22,7 @@ class KoSelection;class CutThroughShapeStrategy : public KoInteractionStrategy{public:- CutThroughShapeStrategy(KoToolBase *tool, KoSelection *selection, QPointF startPoint, qreal width);+ CutThroughShapeStrategy(KoToolBase *tool, KoSelection *selection, QPointF startPoint, const GutterWidthsConfig &width);~CutThroughShapeStrategy() override;@@ -32,7 +34,8 @@ public:private:- qreal gutterWidthInDocumentCoordinates();+ qreal gutterWidthInDocumentCoordinates(qreal lineAngle);+ qreal calculateLineAngle(QPointF start, QPointF end);private:@@ -41,7 +44,7 @@ private:QPointF m_endPoint = QPointF();QRectF m_previousLineDirtyRect = QRectF();QList<KoShape *> m_selectedShapes;- qreal m_width {10};+ GutterWidthsConfig m_width;};diff --git a/plugins/tools/tool_knife/GutterWidthsConfig.cpp b/plugins/tools/tool_knife/GutterWidthsConfig.cppnew file mode 100644index 0000000000..f5ca677386--- /dev/null+++ b/plugins/tools/tool_knife/GutterWidthsConfig.cpp@@ -0,0 +1,77 @@+/*+ * SPDX-FileCopyrightText: 2025 Agata Cacko+ *+ * SPDX-License-Identifier: GPL-2.0-or-later+ */++#include "GutterWidthsConfig.h"+#include <kis_algebra_2d.h>+#include <kis_debug.h>+++GutterWidthsConfig::GutterWidthsConfig(KoUnit _baseUnit, qreal _resolution, qreal _horizontal, qreal _vertical, qreal _diagonal, qreal _angleDegrees)+ : baseUnit(_baseUnit)+ , resolution(_resolution)+ , horizontal(_horizontal)+ , vertical(_vertical)+ , diagonal(_diagonal)+ , angleDegrees(_angleDegrees)+{+}+++GutterWidthsConfig::GutterWidthsConfig(KoUnit _baseUnit, qreal _resolution, qreal _all, qreal _angleDegrees)+ : baseUnit(_baseUnit)+ , resolution(_resolution)+ , horizontal(_all)+ , vertical(_all)+ , diagonal(_all)+ , angleDegrees(_angleDegrees)+{+}++bool inRangeWrapped(qreal value, qreal min, qreal max, qreal rangeMax) {+ min = KisAlgebra2D::wrapValue(min, 0.0, rangeMax);+ max = KisAlgebra2D::wrapValue(max, 0.0, rangeMax);++ qCritical() << "in range wrapped: " << ppVar(value) << ppVar(min) << ppVar(max) << ppVar(rangeMax);++ if (min > max) {+ return value > min || value < max; // range is on the edges instead of in the middle+ } else {+ return value > min && value < max;+ }++++}++qreal convertToPixels(KoUnit baseUnit, qreal resolution, qreal length)+{+ KoUnit toUnit = KoUnit::fromSymbol("px");+ return KoUnit::convertFromUnitToUnit(length, baseUnit, toUnit, resolution);+}++qreal GutterWidthsConfig::widthForAngleInPixels(qreal lineAngleDegrees)+{+ qreal angleMax = 360;+ qCritical() << "~~~ Searching for line angle: " << lineAngleDegrees;+ qCritical() << "~~~ Values for gutters are: " << horizontal << vertical << diagonal << "with angle diff = " << angleDegrees;++ if (inRangeWrapped(lineAngleDegrees, -angleDegrees, angleDegrees, angleMax)) {+ qCritical() << "horizontal 1";+ return convertToPixels(baseUnit, resolution, horizontal);+ } else if (inRangeWrapped(lineAngleDegrees, 180-angleDegrees, 180+angleDegrees, angleMax)) {+ qCritical() << "horizontal 2";+ return convertToPixels(baseUnit, resolution, horizontal);+ } else if (inRangeWrapped(lineAngleDegrees, 90-angleDegrees, 90+angleDegrees, angleMax)) {+ qCritical() << "vertical 1";+ return convertToPixels(baseUnit, resolution, vertical);+ } else if (inRangeWrapped(lineAngleDegrees, 270-angleDegrees, 270+angleDegrees, angleMax)) {+ qCritical() << "vertical 2";+ return convertToPixels(baseUnit, resolution, vertical);+ } else {+ qCritical() << "diagonal";+ return convertToPixels(baseUnit, resolution, diagonal);+ }+}diff --git a/plugins/tools/tool_knife/GutterWidthsConfig.h b/plugins/tools/tool_knife/GutterWidthsConfig.hnew file mode 100644index 0000000000..8d09ace084--- /dev/null+++ b/plugins/tools/tool_knife/GutterWidthsConfig.h@@ -0,0 +1,43 @@+/*+ * SPDX-FileCopyrightText: 2025 Agata Cacko+ *+ * SPDX-License-Identifier: GPL-2.0-or-later+ */++#ifndef GUTTER_WIDTHS_CONFIG_H_+#define GUTTER_WIDTHS_CONFIG_H_++#include <QScopedPointer>+#include <QPoint>+#include <QRectF>+#include <KoUnit.h>+++class GutterWidthsConfig+{+public:+ GutterWidthsConfig(KoUnit _baseUnit, qreal resolution, qreal _horizontal, qreal _vertical, qreal _diagonal, qreal _angleDegrees);+ GutterWidthsConfig(KoUnit _baseUnit, qreal resolution, qreal _all, qreal _angleDegrees);++public:++ qreal widthForAngleInPixels(qreal lineAngleDegrees);++public:+++ const KoUnit baseUnit;++ const qreal horizontal;+ const qreal vertical;+ const qreal diagonal;+ const qreal angleDegrees;+ const qreal resolution;+++};+++++#endif // GUTTER_WIDTHS_CONFIG_H_diff --git a/plugins/tools/tool_knife/KisToolKnife.cpp b/plugins/tools/tool_knife/KisToolKnife.cppindex da8df778c4..f46d262d6a 100644--- a/plugins/tools/tool_knife/KisToolKnife.cpp+++ b/plugins/tools/tool_knife/KisToolKnife.cpp@@ -315,7 +315,7 @@ KoInteractionStrategy *KisToolKnife::createStrategy(KoPointerEvent *event)KoSelection *selection = canvas()->selectedShapesProxy()->selection();if (m_d->optionsWidget->getToolMode() == KisToolKnifeOptionsWidget::ToolMode::AddGutter) {- return new CutThroughShapeStrategy(this, selection, event->point, m_d->optionsWidget->getCurrentWidth());+ return new CutThroughShapeStrategy(this, canvas()->selectedShapesProxy()->selection(), event->point, m_d->optionsWidget->getCurrentWidthsConfig());} else {return new RemoveGutterStrategy(this, selection, event->point);}diff --git a/plugins/tools/tool_knife/KisToolKnifeOptionsWidget.cpp b/plugins/tools/tool_knife/KisToolKnifeOptionsWidget.cppindex 354d0d287a..739e2e3ab4 100644--- a/plugins/tools/tool_knife/KisToolKnifeOptionsWidget.cpp+++ b/plugins/tools/tool_knife/KisToolKnifeOptionsWidget.cpp@@ -17,6 +17,10 @@#include <KisOptionButtonStrip.h>#include <kis_icon_utils.h>#include <KoGroupButton.h>+#include <KoUnit.h>+++Q_DECLARE_METATYPE(KisToolKnifeOptionsWidget::GapWidthType)#include <KoUnit.h>@@ -36,17 +40,17 @@ struct KisToolKnifeOptionsWidget::Private {- int getThickGapWidth(void)+ qreal getThickGapWidth(void){return ui->thickGapWidth->value();}- int getThinGapWidth(void)+ qreal getThinGapWidth(void){return ui->thinGapWidth->value();}- int getSpecialGapWidth(void)+ qreal getSpecialGapWidth(void){return ui->customGapWidth->value();}@@ -57,12 +61,14 @@ struct KisToolKnifeOptionsWidget::Private {return KisToolKnifeOptionsWidget::Thick;} else if (ui->thinGapWidthRadioButton->isChecked()) {return KisToolKnifeOptionsWidget::Thin;- } else { // ui->specialGapWidthRadioButton->isChecked()+ } else if (ui->customWidthRadioButton->isChecked()) {return KisToolKnifeOptionsWidget::Special;+ } else { // if (ui->automaticGapWidthRadioButton->isChecked())+ return KisToolKnifeOptionsWidget::Automatic;}}- int getWidthForType(KisToolKnifeOptionsWidget::GapWidthType type) {+ qreal getWidthForType(KisToolKnifeOptionsWidget::GapWidthType type) {switch(type) {case KisToolKnifeOptionsWidget::Thick:return getThickGapWidth();@@ -70,11 +76,42 @@ struct KisToolKnifeOptionsWidget::Private {return getThinGapWidth();case KisToolKnifeOptionsWidget::Special:return getSpecialGapWidth();- default:+ default: // this handles automatic, too...return getSpecialGapWidth();}}+ qreal getWidthHorizontal() {+ return getWidthForType(ui->automaticHorizontalCombobox->currentData().value<KisToolKnifeOptionsWidget::GapWidthType>());+ }++ qreal getWidthVertical() {+ return getWidthForType(ui->automaticVerticalCombobox->currentData().value<KisToolKnifeOptionsWidget::GapWidthType>());+ }++ qreal getWidthDiagonal() {+ return getWidthForType(ui->automaticDiagonalCombobox->currentData().value<KisToolKnifeOptionsWidget::GapWidthType>());+ }++++ GutterWidthsConfig getCurrentWidthsConfig(KisToolKnifeOptionsWidget::GapWidthType type) {+ switch(type) {+ case KisToolKnifeOptionsWidget::Thick:+ return GutterWidthsConfig(currentUnit(), resolution, getThickGapWidth(), ui->gutterWidthAngleSpinBox->value());+ case KisToolKnifeOptionsWidget::Thin:+ return GutterWidthsConfig(currentUnit(), resolution, getThinGapWidth(), ui->gutterWidthAngleSpinBox->value());+ case KisToolKnifeOptionsWidget::Special:+ return GutterWidthsConfig(currentUnit(), resolution, getSpecialGapWidth(), ui->gutterWidthAngleSpinBox->value());+ case KisToolKnifeOptionsWidget::Automatic:+ return GutterWidthsConfig(currentUnit(), resolution, getWidthHorizontal(), getWidthVertical(), getWidthDiagonal(), ui->gutterWidthAngleSpinBox->value());+ default:+ return GutterWidthsConfig(currentUnit(), resolution, getSpecialGapWidth(), ui->gutterWidthAngleSpinBox->value());+ }+ }+++KisToolKnifeOptionsWidget::ToolMode getToolMode() {if (buttonModeAddGutter && buttonModeAddGutter->isChecked()) {return KisToolKnifeOptionsWidget::AddGutter;@@ -110,6 +147,10 @@ struct KisToolKnifeOptionsWidget::Private {ui->customGapWidth->setValue(KoUnit::convertFromUnitToUnit(ui->customGapWidth->value(), unitBefore, unitNow, resolution));}++ void readFromConfig(QString toolId) {+ //+ }};KisToolKnifeOptionsWidget::KisToolKnifeOptionsWidget(KisCanvasResourceProvider */*provider*/, QWidget *parent, QString toolId, qreal resolution)@@ -143,48 +184,37 @@ KisToolKnifeOptionsWidget::KisToolKnifeOptionsWidget(KisCanvasResourceProvider *m_d->ui->unitsCombobox->addItem(i18n("Pixels"), QVariant(" px"));m_d->ui->unitsCombobox->addItem(i18n("Millimeters"), QVariant(" mm"));- connect(m_d->ui->unitsCombobox, SIGNAL(currentIndexChanged(int)), this, SLOT(unitForWidthChanged(int)));+ m_d->ui->automaticHorizontalCombobox->addItem(i18nc("Thick type of gutter (in comics); keep consistent with the label in the GUI", "Thick"), QVariant(KisToolKnifeOptionsWidget::Thick));+ m_d->ui->automaticHorizontalCombobox->addItem(i18nc("Thin type of gutter (in comics); keep consistent with the label in the GUI", "Thin"), QVariant(KisToolKnifeOptionsWidget::Thin));+ m_d->ui->automaticHorizontalCombobox->addItem(i18nc("Special type of gutter (in comics); keep consistent with the label in the GUI", "Special"), QVariant(KisToolKnifeOptionsWidget::Special));+ m_d->ui->automaticVerticalCombobox->addItem(i18nc("Thick type of gutter (in comics); keep consistent with the label in the GUI", "Thick"), QVariant(KisToolKnifeOptionsWidget::Thick));+ m_d->ui->automaticVerticalCombobox->addItem(i18nc("Thin type of gutter (in comics); keep consistent with the label in the GUI", "Thin"), QVariant(KisToolKnifeOptionsWidget::Thin));+ m_d->ui->automaticVerticalCombobox->addItem(i18nc("Special type of gutter (in comics); keep consistent with the label in the GUI", "Special"), QVariant(KisToolKnifeOptionsWidget::Special));- //m_d->ui->mainLayout->addWidget(optionButtonStripToolMode);-}-KisToolKnifeOptionsWidget::~KisToolKnifeOptionsWidget()-{- delete m_d->ui;- m_d->ui = nullptr;-}+ m_d->ui->automaticDiagonalCombobox->addItem(i18nc("Thick type of gutter (in comics); keep consistent with the label in the GUI", "Thick"), QVariant(KisToolKnifeOptionsWidget::Thick));+ m_d->ui->automaticDiagonalCombobox->addItem(i18nc("Thin type of gutter (in comics); keep consistent with the label in the GUI", "Thin"), QVariant(KisToolKnifeOptionsWidget::Thin));+ m_d->ui->automaticDiagonalCombobox->addItem(i18nc("Special type of gutter (in comics); keep consistent with the label in the GUI", "Special"), QVariant(KisToolKnifeOptionsWidget::Special));-int KisToolKnifeOptionsWidget::getThickGapWidth()-{- return m_d->getThickGapWidth();-}-int KisToolKnifeOptionsWidget::getThinGapWidth()-{- return m_d->getThinGapWidth();-}+ m_d->readFromConfig(toolId);+ connect(m_d->ui->unitsCombobox, SIGNAL(currentIndexChanged(int)), this, SLOT(unitForWidthChanged(int)));-int KisToolKnifeOptionsWidget::getSpecialGapWidth()-{- return m_d->getSpecialGapWidth();}-KisToolKnifeOptionsWidget::GapWidthType KisToolKnifeOptionsWidget::getWidthType()-{- return m_d->getWidthType();-}-int KisToolKnifeOptionsWidget::getCurrentWidth()++KisToolKnifeOptionsWidget::~KisToolKnifeOptionsWidget(){- return getWidthForType(getWidthType());+ delete m_d->ui;+ m_d->ui = nullptr;}-int KisToolKnifeOptionsWidget::getWidthForType(GapWidthType type)+GutterWidthsConfig KisToolKnifeOptionsWidget::getCurrentWidthsConfig(){- return m_d->getWidthForType(type);-+ return m_d->getCurrentWidthsConfig(m_d->getWidthType());}KisToolKnifeOptionsWidget::ToolMode KisToolKnifeOptionsWidget::getToolMode()diff --git a/plugins/tools/tool_knife/KisToolKnifeOptionsWidget.h b/plugins/tools/tool_knife/KisToolKnifeOptionsWidget.hindex cb161e8c6d..ebc3f33fe7 100644--- a/plugins/tools/tool_knife/KisToolKnifeOptionsWidget.h+++ b/plugins/tools/tool_knife/KisToolKnifeOptionsWidget.h@@ -12,6 +12,7 @@#include <QModelIndex>#include "kis_types.h"+#include "GutterWidthsConfig.h"class KisCanvasResourceProvider;class KoColor;@@ -28,7 +29,8 @@ public:enum GapWidthType {Thick,Thin,- Special+ Special,+ Automatic,};enum ToolMode {@@ -37,12 +39,7 @@ public:MoveGutterEndPoint,};- int getThickGapWidth(void);- int getThinGapWidth(void);- int getSpecialGapWidth(void);- GapWidthType getWidthType();- int getCurrentWidth();- int getWidthForType(GapWidthType type);+ GutterWidthsConfig getCurrentWidthsConfig();ToolMode getToolMode();