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 action based shotcuts inside the text tool cursor.

Wolthera van Hövell tot Westerflierea2fbafd5ac9
diff --git a/plugins/tools/svgtexttool/CMakeLists.txt b/plugins/tools/svgtexttool/CMakeLists.txt
index 3e7c539970..2fa738a69a 100644
--- a/plugins/tools/svgtexttool/CMakeLists.txt
+++ b/plugins/tools/svgtexttool/CMakeLists.txt
@@ -24,6 +24,7 @@ set (SvgTextTool_SRCS
FontSizeAction.cpp
SvgTextShapeManagerBlocker.cpp
SvgConvertTextTypeCommand.cpp
+ SvgTextShortCuts.cpp
glyphpalette/GlyphPaletteDialog.cpp
glyphpalette/GlyphPaletteProxyModel.cpp
diff --git a/plugins/tools/svgtexttool/SvgTextCursor.cpp b/plugins/tools/svgtexttool/SvgTextCursor.cpp
index 8f26dc3b9b..5399fa437a 100644
--- a/plugins/tools/svgtexttool/SvgTextCursor.cpp
+++ b/plugins/tools/svgtexttool/SvgTextCursor.cpp
@@ -11,6 +11,7 @@
#include "SvgTextMergePropertiesRangeCommand.h"
#include "SvgTextRemoveCommand.h"
#include "SvgTextShapeManagerBlocker.h"
+#include "SvgTextShortCuts.h"
#include "KoSvgTextShapeMarkupConverter.h"
#include "KoSvgPaste.h"
@@ -139,6 +140,8 @@ struct Q_DECL_HIDDEN SvgTextCursor::Private {
bool blockQueryUpdates = false; ///< Block qApp->inputMethod->update(), enabled during the inputmethod event flow.
SvgTextCursorPropertyInterface *interface{nullptr};
+
+ QList<QAction*> actions;
};
SvgTextCursor::SvgTextCursor(KoCanvasBase *canvas) :
@@ -928,6 +931,17 @@ void SvgTextCursor::toggleProperty(KoSvgTextProperties::PropertyId property)
}
}
+void SvgTextCursor::propertyAction()
+{
+ QAction *action = dynamic_cast<QAction*>(QObject::sender());
+ if (!action || !d->shape) return;
+
+ QList<KoSvgTextProperties> p = d->shape->propertiesForRange(qMin(d->pos, d->anchor), qMax(d->pos, d->anchor));
+ KoSvgTextProperties properties = SvgTextShortCuts::getModifiedProperties(action, p);
+ if (properties.isEmpty()) return;
+ mergePropertiesIntoSelection(properties);
+}
+
bool SvgTextCursor::hasSelection()
{
return d->pos != d->anchor;
@@ -1082,15 +1096,12 @@ void SvgTextCursor::keyPressEvent(QKeyEvent *event)
// keys, as one of the standard keys for deleting a line is ctrl+u
// which would probably be expected to do underline before deleting.
- if (testSequence == QKeySequence::Bold) {
- toggleProperty(KoSvgTextProperties::FontWeightId);
- event->accept();
- } else if (testSequence == QKeySequence::Italic) {
- toggleProperty(KoSvgTextProperties::FontStyleId);
- event->accept();
- } else if (testSequence == QKeySequence::Underline) {
- toggleProperty(KoSvgTextProperties::TextDecorationLineId);
- event->accept();
+ Q_FOREACH(QAction *action, d->actions) {
+ if (action->shortcut() == testSequence) {
+ event->accept();
+ action->trigger();
+ break;
+ }
}
if (event->isAccepted()) return;
@@ -1213,6 +1224,16 @@ void SvgTextCursor::focusOut()
stopBlinkCursor();
}
+bool SvgTextCursor::registerPropertyAction(QAction *action, const QString &name)
+{
+ if (SvgTextShortCuts::configureAction(action, name)) {
+ d->actions.append(action);
+ connect(action, SIGNAL(triggered(bool)), this, SLOT(propertyAction()));
+ return true;
+ }
+ return false;
+}
+
KoSvgTextPropertiesInterface *SvgTextCursor::textPropertyInterface()
{
return d->interface;
@@ -1441,6 +1462,11 @@ void SvgTextCursor::updateCanvasResources()
d->canvas->resourceManager()->setBackgroundColor(c);
}
}
+ Q_FOREACH (QAction *action, d->actions) {
+ if (action->isCheckable()) {
+ action->setChecked(SvgTextShortCuts::actionEnabled(action, {props}));
+ }
+ }
}
}
diff --git a/plugins/tools/svgtexttool/SvgTextCursor.h b/plugins/tools/svgtexttool/SvgTextCursor.h
index bf372db889..67261b8f01 100644
--- a/plugins/tools/svgtexttool/SvgTextCursor.h
+++ b/plugins/tools/svgtexttool/SvgTextCursor.h
@@ -21,6 +21,7 @@ class SvgTextRemoveCommand;
class KUndo2Command;
class QKeyEvent;
class QInputMethodEvent;
+class QAction;
/**
* @brief The SvgTextCursor class
@@ -190,6 +191,8 @@ public:
/// Stops blinking cursor.
void focusOut();
+ bool registerPropertyAction(QAction *action, const QString &name);
+
KoSvgTextPropertiesInterface *textPropertyInterface();
Q_SIGNALS:
@@ -203,6 +206,7 @@ private Q_SLOTS:
void updateInputMethodItemTransform();
void canvasResourceChanged(int key, const QVariant &value);
void toggleProperty(KoSvgTextProperties::PropertyId property);
+ void propertyAction();
private:
diff --git a/plugins/tools/svgtexttool/SvgTextShortCuts.cpp b/plugins/tools/svgtexttool/SvgTextShortCuts.cpp
new file mode 100644
index 0000000000..95e8181ffa
--- /dev/null
+++ b/plugins/tools/svgtexttool/SvgTextShortCuts.cpp
@@ -0,0 +1,315 @@
+/*
+ * SPDX-FileCopyrightText: 2025 Wolthera van Hövell tot Westerflier <[email protected]>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+#include "SvgTextShortCuts.h"
+#include <QAction>
+#include <KoSvgTextProperties.h>
+
+#include <KisStaticInitializer.h>
+
+class SvgTextShortcutInfo;
+Q_DECLARE_METATYPE(SvgTextShortcutInfo)
+KIS_DECLARE_STATIC_INITIALIZER {
+ qRegisterMetaType<SvgTextShortcutInfo>("SvgTextShortcutInfo");
+#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
+ QMetaType::registerEqualsComparator<SvgTextShortcutInfo>();
+#endif
+}
+
+/**
+ * @brief The SvgTextShortcutInfo class
+ * This
+ */
+struct SvgTextShortcutInfo : public boost::equality_comparable<SvgTextShortcutInfo> {
+
+ enum ActionType {
+ Set, ///< Will set value1, cannot be toggled.
+ Toggle, ///< Toggle will test "testValue", and toggle between value1 and value 2;
+ Increase, //< Will increase by value 1.
+ Decrease //< Will decrease by value 1.
+ };
+ SvgTextShortcutInfo() {}
+
+ static SvgTextShortcutInfo propertyToggle(KoSvgTextProperties::PropertyId _propertyId,
+ QVariant _value1, QVariant _value2, QVariant _testValue) {
+ SvgTextShortcutInfo info;
+ info.propertyId = _propertyId;
+ info.type = Toggle;
+ info.value1 = _value1;
+ info.value2 = _value2;
+ info.testValue = _testValue;
+ return info;
+ }
+
+ static SvgTextShortcutInfo propertyChange(KoSvgTextProperties::PropertyId _propertyId, QVariant _value1, bool _increase) {
+ SvgTextShortcutInfo info;
+ info.propertyId = _propertyId;
+ info.type = _increase? Increase: Decrease;
+ info.value1 = _value1;
+ return info;
+ }
+
+ static SvgTextShortcutInfo propertySet(KoSvgTextProperties::PropertyId _propertyId, QVariant _value1) {
+ SvgTextShortcutInfo info;
+ info.propertyId = _propertyId;
+ info.type = Set;
+ info.value1 = _value1;
+ return info;
+ }
+
+ KoSvgTextProperties::PropertyId propertyId;
+ ActionType type;
+ QVariant value1;
+ QVariant value2;
+ QVariant testValue;
+
+
+
+ bool operator==(const SvgTextShortcutInfo & other) const {
+ return (propertyId == other.propertyId
+ && type == other.type
+ && value1 == other.value1
+ && value2 == other.value2
+ && testValue == other.testValue);
+ }
+};
+
+
+
+const QMap<QString, SvgTextShortcutInfo> textShortCuts = {
+ {
+ "svg_weight_bold",
+ SvgTextShortcutInfo::propertyToggle(KoSvgTextProperties::FontWeightId,
+ QVariant(400), QVariant(700), QVariant(500))
+ },
+
+ {
+ "svg_format_italic",
+ SvgTextShortcutInfo::propertyToggle(KoSvgTextProperties::FontStyleId,
+ QVariant::fromValue(KoSvgText::CssFontStyleData(QFont::StyleNormal)),
+ QVariant::fromValue(KoSvgText::CssFontStyleData(QFont::StyleItalic)),
+ QVariant::fromValue(KoSvgText::CssFontStyleData(QFont::StyleNormal)))
+ },
+
+ {
+ "svg_increase_font_size",
+ SvgTextShortcutInfo::propertyChange(KoSvgTextProperties::FontSizeId,
+ QVariant::fromValue(1), true)
+ },
+
+ {
+ "svg_decrease_font_size",
+ SvgTextShortcutInfo::propertyChange(KoSvgTextProperties::FontSizeId,
+ QVariant::fromValue(1), false)
+ },
+
+ {
+ "svg_format_underline",
+ SvgTextShortcutInfo::propertyToggle(KoSvgTextProperties::TextDecorationLineId,
+ QVariant(true), QVariant(false), QVariant(KoSvgText::DecorationUnderline))
+ },
+ {
+ "svg_font_kerning",
+ SvgTextShortcutInfo::propertyToggle(KoSvgTextProperties::KerningId,
+ QVariant(true), QVariant(false), QVariant(true))
+ },
+ {
+ "svg_align_right",
+ SvgTextShortcutInfo::propertySet(KoSvgTextProperties::TextAlignAllId,
+ QVariant(KoSvgText::AlignStart))
+ },
+ {
+ "svg_align_left",
+ SvgTextShortcutInfo::propertySet(KoSvgTextProperties::TextAlignAllId,
+ QVariant(KoSvgText::AlignEnd))
+ },
+ {
+ "svg_align_center",
+ SvgTextShortcutInfo::propertySet(KoSvgTextProperties::TextAlignAllId,
+ QVariant(KoSvgText::AlignCenter))
+ },
+ {
+ "svg_align_justified",
+ SvgTextShortcutInfo::propertySet(KoSvgTextProperties::TextAlignAllId,
+ QVariant(KoSvgText::AlignJustify))
+ },
+ {
+ "svg_format_subscript",
+ SvgTextShortcutInfo::propertySet(KoSvgTextProperties::BaselineShiftModeId,
+ QVariant(KoSvgText::ShiftSub))
+ },
+ {
+ "svg_format_superscript",
+ SvgTextShortcutInfo::propertySet(KoSvgTextProperties::BaselineShiftModeId,
+ QVariant(KoSvgText::ShiftSuper))
+ }
+};
+
+QStringList SvgTextShortCuts::possibleActions()
+{
+ return textShortCuts.keys();
+}
+
+bool SvgTextShortCuts::configureAction(QAction *action, const QString &name)
+{
+ if (!textShortCuts.contains(name)) return false;
+ if (!action) return false;
+ SvgTextShortcutInfo info = textShortCuts.value(name);
+ action->setData(QVariant::fromValue(info));
+ return true;
+}
+
+bool SvgTextShortCuts::actionEnabled(QAction *action, const QList<KoSvgTextProperties> currentProperties) {
+ if (!action || !action->isCheckable() || !action->data().canConvert<SvgTextShortcutInfo>()) return false;
+ SvgTextShortcutInfo info = action->data().value<SvgTextShortcutInfo>();
+
+ if (info.type != SvgTextShortcutInfo::Toggle || info.type != SvgTextShortcutInfo::Set) {
+ return false;
+ }
+ const QVariant testValue = info.type == SvgTextShortcutInfo::Toggle? info.testValue: info.value1;
+
+ for (auto properties = currentProperties.begin(); properties != currentProperties.end(); properties++) {
+ const QVariant oldValue = properties->propertyOrDefault(info.propertyId);
+
+
+ if (oldValue.canConvert<KoSvgText::TextDecorations>() && info.propertyId == KoSvgTextProperties::TextDecorationLineId) {
+
+ const KoSvgText::TextDecorations oldDecor = oldValue.value<KoSvgText::TextDecorations>();
+ return oldDecor.testFlag(KoSvgText::TextDecoration(testValue.toInt()));
+
+ } else if (oldValue.canConvert<KoSvgText::CssFontStyleData>()) {
+ const KoSvgText::CssFontStyleData testVal = testValue.value<KoSvgText::CssFontStyleData>();
+ const KoSvgText::CssFontStyleData currentVal = oldValue.value<KoSvgText::CssFontStyleData>();
+ return (testVal.style == currentVal.style);
+ } else if (oldValue.canConvert<KoSvgText::AutoValue>()) {
+ const KoSvgText::AutoValue currentVal = oldValue.value<KoSvgText::AutoValue>();
+ if (testValue.canConvert<KoSvgText::AutoValue>()) {
+ return (testValue == oldValue);
+ } else if (testValue.canConvert<double>()) {
+ return currentVal.customValue == testValue.toDouble();
+ } else {
+ return currentVal.isAuto == testValue.toBool();
+ }
+ } else if (oldValue.canConvert<KoSvgText::CssLengthPercentage>()) {
+ const KoSvgText::CssLengthPercentage currentVal = oldValue.value<KoSvgText::CssLengthPercentage>();
+ if (testValue.canConvert<KoSvgText::CssLengthPercentage>()) {
+ return (testValue == oldValue);
+ } else {
+ return currentVal.value == testValue.toDouble();
+ }
+ } else if (oldValue.canConvert<KoSvgText::AutoLengthPercentage>()) {
+ const KoSvgText::AutoLengthPercentage currentVal = oldValue.value<KoSvgText::CssLengthPercentage>();
+ if (testValue.canConvert<KoSvgText::AutoLengthPercentage>()) {
+ return (testValue == oldValue);
+ } else if (testValue.canConvert<KoSvgText::CssLengthPercentage>()) {
+ return (testValue == QVariant::fromValue(currentVal.length));
+ } else {
+ return currentVal.length.value == testValue.toDouble();
+ }
+ } else if (oldValue.canConvert<KoSvgText::LineHeightInfo>()) {
+ const KoSvgText::LineHeightInfo currentVal = oldValue.value<KoSvgText::LineHeightInfo>();
+ if (testValue.canConvert<KoSvgText::LineHeightInfo>()) {
+ return (testValue == oldValue);
+ } else if (testValue.canConvert<KoSvgText::CssLengthPercentage>()) {
+ return (testValue == QVariant::fromValue(currentVal.length));
+ } else {
+ return currentVal.length.value == testValue.toDouble();
+ }
+ }
+ return (testValue == oldValue);
+ }
+ return false;
+}
+
+KoSvgTextProperties SvgTextShortCuts::getModifiedProperties(const QAction *action, QList<KoSvgTextProperties> currentProperties)
+{
+ if (!action || !action->data().canConvert<SvgTextShortcutInfo>() || currentProperties.isEmpty()) return KoSvgTextProperties();
+ SvgTextShortcutInfo info = action->data().value<SvgTextShortcutInfo>();
+
+ QVariant newVal;
+ if (info.type == SvgTextShortcutInfo::Toggle) {
+ for(auto properties = currentProperties.begin(); properties != currentProperties.end(); properties++) {
+ QVariant oldValue = properties->propertyOrDefault(info.propertyId);
+
+ if (oldValue.canConvert<KoSvgText::TextDecorations>() && info.propertyId == KoSvgTextProperties::TextDecorationLineId) {
+ KoSvgText::TextDecoration decor = KoSvgText::TextDecoration(info.testValue.toInt());
+ KoSvgText::TextDecorations oldDecor = oldValue.value<KoSvgText::TextDecorations>();
+ KoSvgText::TextDecorations newDecor;
+ newDecor.setFlag(decor, !oldDecor.testFlag(decor));
+ newVal = QVariant::fromValue(newDecor);
+ if (oldDecor.testFlag(decor)) {
+ break;
+ }
+ } else if (oldValue.canConvert<KoSvgText::CssFontStyleData>() && info.propertyId == KoSvgTextProperties::FontStyleId) {
+ KoSvgText::CssFontStyleData testVal = info.testValue.value<KoSvgText::CssFontStyleData>();
+ KoSvgText::CssFontStyleData currentVal = oldValue.value<KoSvgText::CssFontStyleData>();
+ if (currentVal.style == testVal.style) {
+ newVal = info.value2;
+ break;
+ } else {
+ newVal = info.value1;
+ }
+ } else if (oldValue.canConvert<KoSvgText::AutoValue>() && info.propertyId == KoSvgTextProperties::KerningId) {
+ bool testVal = info.testValue.toBool();
+ KoSvgText::AutoValue currentVal = oldValue.value<KoSvgText::AutoValue>();
+ if (currentVal.isAuto == testVal) {
+ currentVal.customValue = 0.0;
+ currentVal.isAuto = info.value2.toBool();
+ newVal = QVariant::fromValue(currentVal);
+ break;
+ } else {
+ currentVal.isAuto = info.value1.toBool();
+ newVal = QVariant::fromValue(currentVal);
+ }
+ } else if (oldValue.canConvert<double>()) {
+ if (oldValue.toDouble() > info.testValue.toDouble()) {
+ newVal = info.value1;
+ break;
+ } else {
+ newVal = info.value2;
+ }
+ } else if (oldValue.canConvert<int>()) {
+ if (oldValue.toInt() > info.testValue.toInt()) {
+ newVal = info.value1;
+ break;
+ } else {
+ newVal = info.value2;
+ }
+ }
+ }
+ } else if (info.type == SvgTextShortcutInfo::Set) {
+ KoSvgTextProperties properties = currentProperties.first();
+ QVariant oldValue = properties.propertyOrDefault(info.propertyId);
+
+ if (oldValue.canConvert<int>()) {
+ newVal = info.value1;
+ }
+ } else {
+ KoSvgTextProperties properties = currentProperties.first();
+ QVariant oldValue = properties.propertyOrDefault(info.propertyId);
+
+ if (oldValue.canConvert<KoSvgText::CssLengthPercentage>()) {
+ KoSvgText::CssLengthPercentage length = oldValue.value<KoSvgText::CssLengthPercentage>();
+ if (info.type == SvgTextShortcutInfo::Increase) {
+ length.value += info.value1.toDouble();
+ } else {
+ length.value -= info.value1.toDouble();
+ }
+ newVal = QVariant::fromValue(length);
+ } else if (oldValue.canConvert<KoSvgText::AutoLengthPercentage>()) {
+ KoSvgText::AutoLengthPercentage length = oldValue.value<KoSvgText::AutoLengthPercentage>();
+ length.isAuto = false;
+ if (info.type == SvgTextShortcutInfo::Increase) {
+ length.length.value += info.value1.toDouble();
+ } else {
+ length.length.value -= info.value1.toDouble();
+ }
+ newVal = QVariant::fromValue(length);
+ }
+ }
+ KoSvgTextProperties newProperties;
+ newProperties.setProperty(info.propertyId, newVal);
+ return newProperties;
+}
diff --git a/plugins/tools/svgtexttool/SvgTextShortCuts.h b/plugins/tools/svgtexttool/SvgTextShortCuts.h
new file mode 100644
index 0000000000..085f93b710
--- /dev/null
+++ b/plugins/tools/svgtexttool/SvgTextShortCuts.h
@@ -0,0 +1,26 @@
+/*
+ * SPDX-FileCopyrightText: 2025 Wolthera van Hövell tot Westerflier <[email protected]>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+#ifndef SVGTEXTSHORTCUTS_H
+#define SVGTEXTSHORTCUTS_H
+
+#include <QAction>
+
+class QString;
+class KoSvgTextProperties;
+
+class SvgTextShortCuts
+{
+public:
+ static QStringList possibleActions();
+ static bool configureAction(QAction *action, const QString &name);
+
+ static bool actionEnabled(QAction *action, const QList<KoSvgTextProperties> currentProperties);
+
+ static KoSvgTextProperties getModifiedProperties(const QAction *action, QList<KoSvgTextProperties> currentProperties);
+private:
+};
+
+#endif // SVGTEXTSHORTCUTS_H
diff --git a/plugins/tools/svgtexttool/SvgTextTool.cpp b/plugins/tools/svgtexttool/SvgTextTool.cpp
index 389ba46f06..7af77373d4 100644
--- a/plugins/tools/svgtexttool/SvgTextTool.cpp
+++ b/plugins/tools/svgtexttool/SvgTextTool.cpp
@@ -20,6 +20,7 @@
#include "SvgTextEditor.h"
#include "SvgTextRemoveCommand.h"
#include "SvgConvertTextTypeCommand.h"
+#include "SvgTextShortCuts.h"
#include <QPainterPath>
#include <QGridLayout>
@@ -89,6 +90,13 @@ SvgTextTool::SvgTextTool(KoCanvasBase *canvas)
, cursorFlashLimit);
connect(&m_textCursor, SIGNAL(updateCursorDecoration(QRectF)), this, SLOT(slotUpdateCursorDecoration(QRectF)));
+ Q_FOREACH(const QString name, SvgTextShortCuts::possibleActions()) {
+ QAction *a = action(name);
+ if(m_textCursor.registerPropertyAction(a, name)) {
+ qDebug() << "registered" << name << a->shortcut();
+ }
+ }
+
m_base_cursor = QCursor(QPixmap(":/tool_text_basic.xpm"), 7, 7);
m_text_inline_horizontal = QCursor(QPixmap(":/tool_text_inline_horizontal.xpm"), 7, 7);
m_text_inline_vertical = QCursor(QPixmap(":/tool_text_inline_vertical.xpm"), 7, 7);
diff --git a/plugins/tools/svgtexttool/SvgTextToolFactory.cpp b/plugins/tools/svgtexttool/SvgTextToolFactory.cpp
index 0b2f2b929a..3dc19b135a 100644
--- a/plugins/tools/svgtexttool/SvgTextToolFactory.cpp
+++ b/plugins/tools/svgtexttool/SvgTextToolFactory.cpp
@@ -9,9 +9,11 @@
#include "KoSvgTextShape.h"
#include "SvgTextTool.h"
+#include "SvgTextShortCuts.h"
#include <KoIcon.h>
#include <klocalizedstring.h>
+#include <kis_action_registry.h>
SvgTextToolFactory::SvgTextToolFactory()
: KoToolFactoryBase("SvgTextTool")
@@ -32,3 +34,12 @@ KoToolBase *SvgTextToolFactory::createTool(KoCanvasBase *canvas)
return new SvgTextTool(canvas);
}
+QList<QAction *> SvgTextToolFactory::createActionsImpl()
+{
+ QList<QAction *> actions;
+ Q_FOREACH(const QString name, SvgTextShortCuts::possibleActions()) {
+ actions << KisActionRegistry::instance()->makeQAction(name, this);
+ }
+ return actions;
+}
+
diff --git a/plugins/tools/svgtexttool/SvgTextToolFactory.h b/plugins/tools/svgtexttool/SvgTextToolFactory.h
index 4dcd6bda7e..ca3aec76b0 100644
--- a/plugins/tools/svgtexttool/SvgTextToolFactory.h
+++ b/plugins/tools/svgtexttool/SvgTextToolFactory.h
@@ -17,6 +17,9 @@ public:
~SvgTextToolFactory();
KoToolBase *createTool(KoCanvasBase *canvas) override;
+ // KoToolFactoryBase interface
+protected:
+ QList<QAction *> createActionsImpl() override;
};
#endif