Skip to content

armgit / krita

Public Imported from GitHub
0

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 basic application of text properties inside the text tool, via an interface.

Wolthera van Hövell tot Westerflierda73a83b0925
diff --git a/libs/flake/CMakeLists.txt b/libs/flake/CMakeLists.txt
index 6f1024fdff..a41900be7c 100644
--- a/libs/flake/CMakeLists.txt
+++ b/libs/flake/CMakeLists.txt
@@ -225,6 +225,7 @@ set(kritaflake_SRCS
text/KoCssTextUtils.cpp
text/KoPolygonUtils.cpp
text/KoSvgTextPropertyData.cpp
+ text/KoSvgTextPropertiesInterface.h
text/lager/KoSvgTextPropertiesModel.cpp
text/lager/CssLengthPercentageModel.cpp
text/lager/LineHeightModel.cpp
diff --git a/libs/flake/text/KoSvgTextPropertiesInterface.h b/libs/flake/text/KoSvgTextPropertiesInterface.h
new file mode 100644
index 0000000000..e2b8f7589f
--- /dev/null
+++ b/libs/flake/text/KoSvgTextPropertiesInterface.h
@@ -0,0 +1,42 @@
+/*
+ * SPDX-FileCopyrightText: 2024 Wolthera van Hövell tot Westerflier <[email protected]>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+#ifndef KOSVGTEXTPROPERTIESINTERFACE_H
+#define KOSVGTEXTPROPERTIESINTERFACE_H
+
+#include <QObject>
+#include <KoSvgTextProperties.h>
+#include <kritaflake_export.h>
+
+/**
+ * @brief The KoSvgTextPropertiesInterface class
+ *
+ * This is an interface that can be used by tools to communicate
+ * with the KisTextPropertiesManager.
+ */
+class KRITAFLAKE_EXPORT KoSvgTextPropertiesInterface : public QObject
+{
+ Q_OBJECT
+public:
+ explicit KoSvgTextPropertiesInterface(QObject *parent = nullptr): QObject(parent){}
+
+ /**
+ * @brief getSelectedProperties
+ * @return all KoSvgTextProperties for the given selection.
+ */
+ virtual QList<KoSvgTextProperties> getSelectedProperties() = 0;
+ /**
+ * @brief setPropertiesOnSelected
+ * This sets the properties on the selection. The implementation is responsible
+ * for handling the undo states.
+ * @param properties -- the properties to set.
+ */
+ virtual void setPropertiesOnSelected(KoSvgTextProperties properties) = 0;
+Q_SIGNALS:
+ void textSelectionChanged();
+};
+
+
+#endif // KOSVGTEXTPROPERTIESINTERFACE_H
diff --git a/libs/ui/KisTextPropertiesManager.cpp b/libs/ui/KisTextPropertiesManager.cpp
index 07524398e2..fe433dae55 100644
--- a/libs/ui/KisTextPropertiesManager.cpp
+++ b/libs/ui/KisTextPropertiesManager.cpp
@@ -9,6 +9,7 @@
#include <KoSvgTextPropertyData.h>
#include <KoSelection.h>
#include <KoSvgTextShape.h>
+#include <KoSvgTextPropertiesInterface.h>
#include <KisView.h>
#include <kis_canvas2.h>
@@ -16,8 +17,9 @@
#include <QPointer>
struct KisTextPropertiesManager::Private {
- KisCanvasResourceProvider *provider;
- QPointer<KisView> view {0};
+ KisCanvasResourceProvider *provider {nullptr};
+ QPointer<KisView> view {nullptr};
+ KoSvgTextPropertiesInterface *interface {nullptr};
};
KisTextPropertiesManager::KisTextPropertiesManager(QObject *parent)
@@ -32,19 +34,57 @@ KisTextPropertiesManager::~KisTextPropertiesManager()
void KisTextPropertiesManager::setView(QPointer<KisView> imageView)
{
- if (d->view) {
- disconnect(d->view->canvasBase()->selectedShapesProxy(), SIGNAL(selectionChanged()), this, SLOT(slotShapeSelectionChanged()));
- }
+ //if (d->view) {
+ // disconnect(d->view->canvasBase()->selectedShapesProxy(), SIGNAL(selectionChanged()), this, SLOT(slotShapeSelectionChanged()));
+ //}
d->view = imageView;
- if (d->view) {
- connect(d->view->canvasBase()->selectedShapesProxy(), SIGNAL(selectionChanged()), this, SLOT(slotShapeSelectionChanged()));
- }
+ //if (d->view) {
+ // connect(d->view->canvasBase()->selectedShapesProxy(), SIGNAL(selectionChanged()), this, SLOT(slotShapeSelectionChanged()));
+ //}
}
void KisTextPropertiesManager::setCanvasResourceProvider(KisCanvasResourceProvider *provider)
{
+ if (d->provider) {
+ disconnect(d->provider, SIGNAL(sigTextPropertiesChanged()), this, SLOT(slotTextPropertiesChanged()));
+ }
d->provider = provider;
+ if (d->provider) {
+ connect(d->provider, SIGNAL(sigTextPropertiesChanged()), this, SLOT(slotTextPropertiesChanged()));
+ }
+}
+
+void KisTextPropertiesManager::setTextPropertiesInterface(KoSvgTextPropertiesInterface *interface)
+{
+ if (d->interface) {
+ disconnect(d->interface, SIGNAL(textSelectionChanged()), this, SLOT(slotInterfaceSelectionChanged()));
+ }
+ d->interface = interface;
+ if (d->interface) {
+ connect(d->interface, SIGNAL(textSelectionChanged()), this, SLOT(slotInterfaceSelectionChanged()));
+ }
+}
+
+KoSvgTextPropertyData textDataProperties(QList<KoSvgTextProperties> props, QSet<KoSvgTextProperties::PropertyId> propIds) {
+ KoSvgTextPropertyData textData;
+ textData.commonProperties = props.first();
+
+ for (auto it = props.begin(); it != props.end(); it++) {
+ for (int i = 0; i < propIds.size(); i++) {
+ KoSvgTextProperties::PropertyId p = propIds.values().at(i);
+ if (it->hasProperty(p)) {
+ if (textData.commonProperties.property(p) != it->property(p)) {
+ textData.commonProperties.removeProperty(p);
+ textData.tristate.insert(p);
+ }
+ } else {
+ textData.commonProperties.removeProperty(p);
+ textData.tristate.insert(p);
+ }
+ }
+ }
+ return textData;
}
void KisTextPropertiesManager::slotShapeSelectionChanged()
@@ -55,7 +95,7 @@ void KisTextPropertiesManager::slotShapeSelectionChanged()
|| !d->view->canvasBase()->selectedShapesProxy()->selection()
|| !d->provider) return;
- KoSvgTextPropertyData textData;
+
const QList<KoShape*> shapes = d->view->canvasBase()->selectedShapesProxy()->selection()->selectedEditableShapes();
QList<KoSvgTextProperties> props;
@@ -75,25 +115,35 @@ void KisTextPropertiesManager::slotShapeSelectionChanged()
// Find common properties, and mark all non-common properties as tristate.
- textData.commonProperties = props.first();
+ KoSvgTextPropertyData textData = textDataProperties(props, propIds);
+
+ d->provider->setTextPropertyData(textData);
+}
+
+void KisTextPropertiesManager::slotInterfaceSelectionChanged() {
+ if (!d->interface || !d->provider) return;
+
+ QList<KoSvgTextProperties> props = d->interface->getSelectedProperties();
+ if (props.isEmpty()) return;
+
+ QSet<KoSvgTextProperties::PropertyId> propIds;
for (auto it = props.begin(); it != props.end(); it++) {
- for (int i = 0; i < propIds.size(); i++) {
- KoSvgTextProperties::PropertyId p = propIds.values().at(i);
- if (it->hasProperty(p)) {
- if (textData.commonProperties.property(p) != it->property(p)) {
- textData.commonProperties.removeProperty(p);
- textData.tristate.insert(p);
- }
- } else {
- textData.commonProperties.removeProperty(p);
- textData.tristate.insert(p);
- }
+ KoSvgTextProperties p = *it;
+ for (int i = 0; i < p.properties().size(); i++) {
+ propIds.insert(p.properties().at(i));
}
}
- qDebug() << "Setting text data on canvas resources";
- qDebug() << textData;
+ KoSvgTextPropertyData textData = textDataProperties(props, propIds);
d->provider->setTextPropertyData(textData);
+
+}
+
+void KisTextPropertiesManager::slotTextPropertiesChanged()
+{
+ if (!d->interface || !d->provider) return;
+
+ d->interface->setPropertiesOnSelected(d->provider->textPropertyData().commonProperties);
}
diff --git a/libs/ui/KisTextPropertiesManager.h b/libs/ui/KisTextPropertiesManager.h
index 3b645737e2..17e83b1d44 100644
--- a/libs/ui/KisTextPropertiesManager.h
+++ b/libs/ui/KisTextPropertiesManager.h
@@ -7,9 +7,11 @@
#define KISTEXTPROPERTYMANAGER_H
#include <QObject>
+#include "kritaui_export.h"
class KisView;
class KisCanvasResourceProvider;
+class KoSvgTextPropertiesInterface;
/**
* @brief The KisTextPropertyManager class
@@ -17,7 +19,7 @@ class KisCanvasResourceProvider;
* This class handles taking text property data from the currently selected
* text objects and then setting it on the resource provider.
*/
-class KisTextPropertiesManager : public QObject
+class KRITAUI_EXPORT KisTextPropertiesManager : public QObject
{
Q_OBJECT
public:
@@ -26,9 +28,12 @@ public:
void setView(QPointer<KisView> imageView);
void setCanvasResourceProvider(KisCanvasResourceProvider *provider);
+ void setTextPropertiesInterface(KoSvgTextPropertiesInterface *interface);
private Q_SLOTS:
void slotShapeSelectionChanged();
+ void slotInterfaceSelectionChanged();
+ void slotTextPropertiesChanged();
private:
struct Private;
diff --git a/libs/ui/KisViewManager.cpp b/libs/ui/KisViewManager.cpp
index 509f978902..8e4b2c72d5 100644
--- a/libs/ui/KisViewManager.cpp
+++ b/libs/ui/KisViewManager.cpp
@@ -675,6 +675,11 @@ KisIdleTasksManager *KisViewManager::idleTasksManager()
return &d->idleTasksManager;
}
+KisTextPropertiesManager *KisViewManager::textPropertyManager() const
+{
+ return &d->textPropertyManager;
+}
+
KisSelectionSP KisViewManager::selection()
{
if (d->currentImageView) {
diff --git a/libs/ui/kis_canvas_resource_provider.cpp b/libs/ui/kis_canvas_resource_provider.cpp
index 18d5cdefb6..f19e433dea 100644
--- a/libs/ui/kis_canvas_resource_provider.cpp
+++ b/libs/ui/kis_canvas_resource_provider.cpp
@@ -549,6 +549,7 @@ bool KisCanvasResourceProvider::disablePressure() const
void KisCanvasResourceProvider::setTextPropertyData(KoSvgTextPropertyData data)
{
m_resourceManager->setResource(KoCanvasResource::SvgTextPropertyData, QVariant::fromValue(data));
+ emit sigTextPropertiesChanged();
}
KoSvgTextPropertyData KisCanvasResourceProvider::textPropertyData() const
diff --git a/libs/ui/kis_canvas_resource_provider.h b/libs/ui/kis_canvas_resource_provider.h
index d311810072..e4318c760a 100644
--- a/libs/ui/kis_canvas_resource_provider.h
+++ b/libs/ui/kis_canvas_resource_provider.h
@@ -191,6 +191,8 @@ Q_SIGNALS:
void sigGamutMaskPreviewUpdate();
void sigGamutMaskDeactivated();
+ void sigTextPropertiesChanged();
+
private:
KisViewManager * m_view {nullptr};
diff --git a/plugins/dockers/textproperties/TextPropertiesDock.cpp b/plugins/dockers/textproperties/TextPropertiesDock.cpp
index 37f0909671..d70048410e 100644
--- a/plugins/dockers/textproperties/TextPropertiesDock.cpp
+++ b/plugins/dockers/textproperties/TextPropertiesDock.cpp
@@ -45,6 +45,7 @@ KIS_DECLARE_STATIC_INITIALIZER {
struct TextPropertiesDock::Private
{
KoSvgTextPropertiesModel *textData {new KoSvgTextPropertiesModel()};
+ KisCanvasResourceProvider *provider{nullptr};
};
TextPropertiesDock::TextPropertiesDock()
@@ -87,9 +88,11 @@ TextPropertiesDock::~TextPropertiesDock()
void TextPropertiesDock::setViewManager(KisViewManager *kisview)
{
- KisCanvasResourceProvider* resourceProvider = kisview->canvasResourceProvider();
- connect(resourceProvider->resourceManager(), SIGNAL(canvasResourceChanged(int, QVariant)),
- this, SLOT(slotCanvasResourcesChanged(int, QVariant)));
+ d->provider = kisview->canvasResourceProvider();
+ if (d->provider) {
+ connect(d->provider->resourceManager(), SIGNAL(canvasResourceChanged(int, QVariant)),
+ this, SLOT(slotCanvasResourcesChanged(int, QVariant)));
+ }
}
void TextPropertiesDock::setCanvas(KoCanvasBase *canvas)
@@ -129,4 +132,7 @@ void TextPropertiesDock::slotCanvasResourcesChanged(int key, const QVariant &val
void TextPropertiesDock::slotTextPropertiesChanged()
{
qDebug() << Q_FUNC_INFO << d->textData->textData.get();
+ if (d->provider) {
+ d->provider->setTextPropertyData(d->textData->textData.get());
+ }
}
diff --git a/plugins/tools/svgtexttool/SvgTextCursor.cpp b/plugins/tools/svgtexttool/SvgTextCursor.cpp
index 9e497fe916..a673ddae66 100644
--- a/plugins/tools/svgtexttool/SvgTextCursor.cpp
+++ b/plugins/tools/svgtexttool/SvgTextCursor.cpp
@@ -134,6 +134,8 @@ struct Q_DECL_HIDDEN SvgTextCursor::Private {
QPainterPath IMEDecoration; ///< The decorations for the current preedit string.
QRectF oldIMEDecorationRect; ///< Update Rectangle of previous decoration.
bool blockQueryUpdates = false; ///< Block qApp->inputMethod->update(), enabled during the inputmethod event flow.
+
+ SvgTextPropertyCursor *interface{nullptr};
};
SvgTextCursor::SvgTextCursor(KoCanvasBase *canvas) :
@@ -146,6 +148,7 @@ SvgTextCursor::SvgTextCursor(KoCanvasBase *canvas) :
connect(d->canvas->resourceManager(), SIGNAL(canvasResourceChanged(int,QVariant)),
this, SLOT(canvasResourceChanged(int,QVariant)));
}
+ d->interface = new SvgTextPropertyCursor(this);
}
SvgTextCursor::~SvgTextCursor()
@@ -327,6 +330,12 @@ KoSvgTextProperties SvgTextCursor::currentTextProperties() const
return KoSvgTextProperties();
}
+QList<KoSvgTextProperties> SvgTextCursor::propertiesForRange() const
+{
+ if (!d->shape) return QList<KoSvgTextProperties>();
+ return d->shape->propertiesForRange(d->pos, d->anchor);
+}
+
void SvgTextCursor::mergePropertiesIntoSelection(const KoSvgTextProperties props)
{
if (d->shape && hasSelection()) {
@@ -1172,6 +1181,11 @@ void SvgTextCursor::focusOut()
stopBlinkCursor();
}
+KoSvgTextPropertiesInterface *SvgTextCursor::textPropertyInterface()
+{
+ return d->interface;
+}
+
void SvgTextCursor::updateCursor(bool firstUpdate)
{
if (d->shape) {
@@ -1186,6 +1200,7 @@ void SvgTextCursor::updateCursor(bool firstUpdate)
qApp->inputMethod()->update(Qt::ImQueryInput);
}
updateCanvasResources();
+ d->interface->emitSelectionChange();
if (!(d->canvas->canvasWidget() && d->canvas->canvasController())) {
// Mockcanvas in the tests has neither.
return;
@@ -1393,3 +1408,23 @@ void SvgTextCursor::updateCanvasResources()
}
}
}
+
+SvgTextPropertyCursor::SvgTextPropertyCursor(SvgTextCursor *parent)
+ : KoSvgTextPropertiesInterface(parent), m_parent(parent)
+{
+
+}
+QList<KoSvgTextProperties> SvgTextPropertyCursor::getSelectedProperties()
+{
+ return m_parent->propertiesForRange();
+}
+
+void SvgTextPropertyCursor::setPropertiesOnSelected(KoSvgTextProperties properties)
+{
+ m_parent->mergePropertiesIntoSelection(properties);
+}
+
+void SvgTextPropertyCursor::emitSelectionChange()
+{
+ emit textSelectionChanged();
+}
diff --git a/plugins/tools/svgtexttool/SvgTextCursor.h b/plugins/tools/svgtexttool/SvgTextCursor.h
index dc43643a13..351855d436 100644
--- a/plugins/tools/svgtexttool/SvgTextCursor.h
+++ b/plugins/tools/svgtexttool/SvgTextCursor.h
@@ -9,6 +9,7 @@
#include <KoSvgTextShape.h>
#include <KoSvgTextProperties.h>
+#include <KoSvgTextPropertiesInterface.h>
#include <KoToolSelection.h>
#include <QPainter>
#include <KoShape.h>
@@ -130,6 +131,8 @@ public:
KoSvgTextProperties currentTextProperties() const;
+ QList<KoSvgTextProperties> propertiesForRange() const;
+
void mergePropertiesIntoSelection(const KoSvgTextProperties props);
/**
@@ -181,6 +184,8 @@ public:
/// Stops blinking cursor.
void focusOut();
+ KoSvgTextPropertiesInterface *textPropertyInterface();
+
Q_SIGNALS:
void updateCursorDecoration(QRectF updateRect);
@@ -220,6 +225,18 @@ private:
const QScopedPointer<Private> d;
};
+class KRITATOOLSVGTEXT_EXPORT SvgTextPropertyCursor : public KoSvgTextPropertiesInterface
+{
+public:
+ SvgTextPropertyCursor(SvgTextCursor *parent);
+ virtual QList<KoSvgTextProperties> getSelectedProperties() override;
+ virtual void setPropertiesOnSelected(KoSvgTextProperties properties) override;
+
+ void emitSelectionChange();
+private:
+ SvgTextCursor *m_parent{nullptr};
+};
+
Q_DECLARE_METATYPE(SvgTextCursor::MoveMode)
diff --git a/plugins/tools/svgtexttool/SvgTextTool.cpp b/plugins/tools/svgtexttool/SvgTextTool.cpp
index 15d5ff79c5..899f1f6764 100644
--- a/plugins/tools/svgtexttool/SvgTextTool.cpp
+++ b/plugins/tools/svgtexttool/SvgTextTool.cpp
@@ -57,6 +57,9 @@
#include <KoPathShape.h>
#include <KoPathSegment.h>
+#include <KisViewManager.h>
+#include <KisTextPropertiesManager.h>
+
#include "KisHandlePainterHelper.h"
#include "kis_tool_utils.h"
#include <commands/KoKeepShapesSelectedCommand.h>
@@ -110,6 +113,11 @@ void SvgTextTool::activate(const QSet<KoShape *> &shapes)
KoToolBase::activate(shapes);
m_canvasConnections.addConnection(canvas()->selectedShapesProxy(), SIGNAL(selectionChanged()), this, SLOT(slotShapeSelectionChanged()));
+ const KisCanvas2 *canvas2 = qobject_cast<const KisCanvas2 *>(this->canvas());
+ if (canvas2) {
+ canvas2->viewManager()->textPropertyManager()->setTextPropertiesInterface(m_textCursor.textPropertyInterface());
+ }
+
useCursor(m_base_cursor);
slotShapeSelectionChanged();
@@ -120,6 +128,10 @@ void SvgTextTool::deactivate()
{
KoToolBase::deactivate();
m_canvasConnections.clear();
+ const KisCanvas2 *canvas2 = qobject_cast<const KisCanvas2 *>(this->canvas());
+ if (canvas2) {
+ canvas2->viewManager()->textPropertyManager()->setTextPropertiesInterface(nullptr);
+ }
m_hoveredShapeHighlightRect = QPainterPath();