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++
Make convert text type controllable via a button group.
Wolthera van Hövell tot Westerflier44dc06a22545
diff --git a/libs/flake/text/KoSvgTextShape.cpp b/libs/flake/text/KoSvgTextShape.cppindex 67d0e5a0ff..537cdf9643 100644--- a/libs/flake/text/KoSvgTextShape.cpp+++ b/libs/flake/text/KoSvgTextShape.cpp@@ -1286,7 +1286,7 @@ void KoSvgTextShape::notifyMarkupChanged()}}-void KoSvgTextShape::convertCharTransformsToPreformatted(const bool makeInlineSize)+void KoSvgTextShape::convertCharTransformsToPreformatted(bool makeInlineSize){const int inlineSize = writingMode() == KoSvgText::HorizontalTB? outlineRect().width(): outlineRect().height();d->applyWhiteSpace(d->textData, true);@@ -1296,7 +1296,8 @@ void KoSvgTextShape::convertCharTransformsToPreformatted(const bool makeInlineSiKoSvgTextProperties props = this->propertiesForPos(-1);if (makeInlineSize) {KoSvgText::AutoValue val;- val.customValue = inlineSize;+ // Using QCeil here because otherwise the text will layout too tight.+ val.customValue = qCeil(inlineSize);val.isAuto = false;if (!props.hasProperty(KoSvgTextProperties::InlineSizeId)) {props.setProperty(KoSvgTextProperties::InlineSizeId, QVariant::fromValue(val));@@ -1723,6 +1724,26 @@ KoShape * KoSvgTextShape::textOutline() constreturn shape;}+KoSvgTextShape::TextType KoSvgTextShape::textType() const+{+ KoSvgText::AutoValue inlineSize = d->textData.childBegin()->properties.propertyOrDefault(KoSvgTextProperties::InlineSizeId).value<KoSvgText::AutoValue>();+ if (!d->shapesInside.isEmpty()) {+ return TextType::TextInShape;+ } else if (!inlineSize.isAuto) {+ return TextType::InlineWrap;+ } else {+ bool textSpaceCollapse = false;+ for (auto it = d->textData.depthFirstTailBegin(); it != d->textData.depthFirstTailEnd(); it++) {+ KoSvgText::TextSpaceCollapse collapse = KoSvgText::TextSpaceCollapse(it->properties.propertyOrDefault(KoSvgTextProperties::TextCollapseId).toInt());+ if (collapse == KoSvgText::Collapse || collapse == KoSvgText::PreserveSpaces) {+ textSpaceCollapse = true;+ break;+ }+ }+ return textSpaceCollapse? TextType::PrePositionedText: TextType::PreformattedText;+ }+}+void KoSvgTextShape::setShapesInside(QList<KoShape *> shapesInside){d->shapesInside = shapesInside;diff --git a/libs/flake/text/KoSvgTextShape.h b/libs/flake/text/KoSvgTextShape.hindex 28afda1198..9f11dd8ff4 100644--- a/libs/flake/text/KoSvgTextShape.h+++ b/libs/flake/text/KoSvgTextShape.h@@ -65,6 +65,26 @@ public:*/KoShape * textOutline() const;+ enum TextType {+ PrePositionedText = 0, ///< SVG 1.1 text, white spaces are collapsed+ ///< or only spaces are preserved. Identical to PreformattedText otherwise.+ PreformattedText, ///< No wrapping, but preserves spaces and linebreaks.+ ///< Text-on-Path falls under this or PrePositionedText depending on collapse of lines.+ InlineWrap, ///< Uses inline size to wrap and preserves spaces.+ TextInShape ///< Uses shape-inside to wrap and preserves spaces.+ };+ /**+ * @brief textType+ * This enum gives an indication of what kind of text this shape is.+ * The different text types are a bit blurry as SVG allows a mixutre to+ * provide fallback information. When such fallback is in place, this will+ * only return the primary text type, that is, TextInShape before InlineWrap+ * and InlineWrap before PrePositioned/Preformatted.+ * This primarily exists to give UI feedback.+ * @return estimated TextType+ */+ TextType textType() const;+/*** @brief setShapesInside* @param shapesInside the list of shapes to make up the content area.@@ -442,13 +462,16 @@ public:/*** @brief convertCharTransformsToPreformatted- *+ * Converts the text to a preformatted SVG 2.0 text.+ * This changes the textType();* @param makeInlineSize -- whether to have inline size applied.*/- void convertCharTransformsToPreformatted(const bool makeInlineSize = false);+ void convertCharTransformsToPreformatted(bool makeInlineSize = false);/*** @brief setCharacterTransformsFromLayout+ * Converts the text to a prepositioned SVG 1.1 text.+ * This changes the textType();*/void setCharacterTransformsFromLayout();diff --git a/plugins/tools/svgtexttool/SvgConvertTextTypeCommand.cpp b/plugins/tools/svgtexttool/SvgConvertTextTypeCommand.cppindex 8dffeebfdc..3ce2f12f0a 100644--- a/plugins/tools/svgtexttool/SvgConvertTextTypeCommand.cpp+++ b/plugins/tools/svgtexttool/SvgConvertTextTypeCommand.cpp@@ -5,10 +5,11 @@*/#include "SvgConvertTextTypeCommand.h"-SvgConvertTextTypeCommand::SvgConvertTextTypeCommand(KoSvgTextShape *shape, ConversionType type, KUndo2Command *parent)+SvgConvertTextTypeCommand::SvgConvertTextTypeCommand(KoSvgTextShape *shape, ConversionType type, int pos, KUndo2Command *parent): KUndo2Command(parent), m_shape(shape), m_conversionType(type)+ , m_pos(pos){setText(kundo2_i18n("Convert Text Type"));}@@ -17,6 +18,7 @@ void SvgConvertTextTypeCommand::redo(){QRectF updateRect = m_shape->boundingRect();m_textData = m_shape->getMemento();+ const int oldIndex = qMax(0, m_shape->indexForPos(m_pos));if (m_conversionType == ToPreFormatted) {m_shape->convertCharTransformsToPreformatted(false);@@ -27,11 +29,13 @@ void SvgConvertTextTypeCommand::redo()}m_shape->updateAbsolute( updateRect| m_shape->boundingRect());+ const int pos = m_shape->posForIndex(oldIndex, false, false);+ m_shape->notifyCursorPosChanged(pos, pos);}void SvgConvertTextTypeCommand::undo(){QRectF updateRect = m_shape->boundingRect();- m_shape->setMemento(m_textData, 0, 0);+ m_shape->setMemento(m_textData, m_pos, m_pos);m_shape->updateAbsolute( updateRect| m_shape->boundingRect());}diff --git a/plugins/tools/svgtexttool/SvgConvertTextTypeCommand.h b/plugins/tools/svgtexttool/SvgConvertTextTypeCommand.hindex 5b728a680b..b574639fcc 100644--- a/plugins/tools/svgtexttool/SvgConvertTextTypeCommand.h+++ b/plugins/tools/svgtexttool/SvgConvertTextTypeCommand.h@@ -28,7 +28,7 @@ public:ToCharTransforms ///< This applies SVG 1.1 char transforms and sets whitespace to normal.};- SvgConvertTextTypeCommand(KoSvgTextShape *shape, ConversionType type, KUndo2Command *parent = 0);+ SvgConvertTextTypeCommand(KoSvgTextShape *shape, ConversionType type, int pos, KUndo2Command *parent = 0);~SvgConvertTextTypeCommand() override = default;void redo() override;@@ -43,6 +43,7 @@ private:KoSvgTextShapeMementoSP m_textData;ConversionType m_conversionType;+ int m_pos;};#endif // SVGCONVERTTEXTTYPECOMMAND_Hdiff --git a/plugins/tools/svgtexttool/SvgTextTool.cpp b/plugins/tools/svgtexttool/SvgTextTool.cppindex e58a8c1972..0f6dcbb8ef 100644--- a/plugins/tools/svgtexttool/SvgTextTool.cpp+++ b/plugins/tools/svgtexttool/SvgTextTool.cpp@@ -108,6 +108,9 @@ SvgTextTool::~SvgTextTool()m_glyphPalette->close();}delete m_defAlignment;+ delete m_defWritingMode;+ delete m_defDirection;+ delete m_convertType;}void SvgTextTool::activate(const QSet<KoShape *> &shapes)@@ -270,9 +273,13 @@ QWidget *SvgTextTool::createOptionWidget()connect(optionUi.btnEditSvg, SIGNAL(clicked(bool)), SLOT(showEditorSvgSource()));connect(optionUi.btnGlyphPalette, SIGNAL(clicked(bool)), SLOT(showGlyphPalette()));- connect(optionUi.convertPreFormatBtn, SIGNAL(clicked(bool)), this, SLOT(slotConvertToPreformatted()));- connect(optionUi.convertInlineBtn, SIGNAL(clicked(bool)), this, SLOT(slotConvertToInlineSize()));- connect(optionUi.convertToSvg1_1, SIGNAL(clicked(bool)), this, SLOT(slotConvertToSVGCharTransforms()));+ m_convertType = new QButtonGroup();+ m_convertType->setExclusive(true);+ m_convertType->addButton(optionUi.convertPreFormatBtn, KoSvgTextShape::PreformattedText);+ m_convertType->addButton(optionUi.convertInlineBtn, KoSvgTextShape::InlineWrap);+ m_convertType->addButton(optionUi.convertToSvg1_1, KoSvgTextShape::PrePositionedText);+ connect(m_convertType, SIGNAL(idClicked(int)), this, SLOT(slotConvertType(int)));+ slotTextTypeUpdated();return optionWidget;}@@ -471,6 +478,7 @@ void SvgTextTool::slotShapeSelectionChanged()setTextMode(false);}}+ slotTextTypeUpdated();}void SvgTextTool::copy() const@@ -541,26 +549,37 @@ void SvgTextTool::slotUpdateCursorDecoration(QRectF updateRect)}}-void SvgTextTool::slotConvertToPreformatted() {- if (selectedShape()) {- SvgConvertTextTypeCommand *cmd = new SvgConvertTextTypeCommand(selectedShape(), SvgConvertTextTypeCommand::ToPreFormatted);- canvas()->addCommand(cmd);- }-}--void SvgTextTool::slotConvertToInlineSize()+void SvgTextTool::slotTextTypeUpdated(){+ if (!m_convertType) return;if (selectedShape()) {- SvgConvertTextTypeCommand *cmd = new SvgConvertTextTypeCommand(selectedShape(), SvgConvertTextTypeCommand::ToInlineSize);- canvas()->addCommand(cmd);+ KoSvgTextShape::TextType type = selectedShape()->textType();+ if (m_convertType->button(type)) {+ m_convertType->button(type)->setChecked(true);+ } else {+ Q_FOREACH(QAbstractButton *button, m_convertType->buttons()) {+ m_convertType->button(type)->setChecked(false);+ }+ }}}-void SvgTextTool::slotConvertToSVGCharTransforms()-{+void SvgTextTool::slotConvertType(int index) {if (selectedShape()) {- SvgConvertTextTypeCommand *cmd = new SvgConvertTextTypeCommand(selectedShape(), SvgConvertTextTypeCommand::ToCharTransforms);- canvas()->addCommand(cmd);+ if (index == selectedShape()->textType()) return;+ SvgConvertTextTypeCommand::ConversionType type = SvgConvertTextTypeCommand::ToPreFormatted;+ if (index == KoSvgTextShape::InlineWrap) {+ type = SvgConvertTextTypeCommand::ToInlineSize;+ } else if (index == KoSvgTextShape::PrePositionedText) {+ type = SvgConvertTextTypeCommand::ToCharTransforms;+ }+ KUndo2Command *parentCommand = new KUndo2Command();+ new KoKeepShapesSelectedCommand({selectedShape()}, {}, canvas()->selectedShapesProxy(), KisCommandUtils::FlipFlopCommand::State::INITIALIZING, parentCommand);+ SvgConvertTextTypeCommand *cmd = new SvgConvertTextTypeCommand(selectedShape(), type, m_textCursor.getPos(), parentCommand);+ parentCommand->setText(cmd->text());+ new KoKeepShapesSelectedCommand({}, {selectedShape()}, canvas()->selectedShapesProxy(), KisCommandUtils::FlipFlopCommand::State::FINALIZING, parentCommand);+ canvas()->addCommand(parentCommand);+ slotTextTypeUpdated();}}diff --git a/plugins/tools/svgtexttool/SvgTextTool.h b/plugins/tools/svgtexttool/SvgTextTool.hindex 1117d0d95d..55b874acc4 100644--- a/plugins/tools/svgtexttool/SvgTextTool.h+++ b/plugins/tools/svgtexttool/SvgTextTool.h@@ -156,9 +156,8 @@ private Q_SLOTS:*/void slotUpdateCursorDecoration(QRectF updateRect);- void slotConvertToPreformatted();- void slotConvertToInlineSize();- void slotConvertToSVGCharTransforms();+ void slotTextTypeUpdated();+ void slotConvertType(int index);private:@@ -187,6 +186,7 @@ private:QButtonGroup *m_defAlignment {nullptr};QButtonGroup *m_defWritingMode {nullptr};QButtonGroup *m_defDirection {nullptr};+ QButtonGroup *m_convertType {nullptr};KConfigGroup m_configGroup;SvgTextCursor m_textCursor;KisSignalAutoConnectionsStore m_canvasConnections;@@ -195,6 +195,7 @@ private:QPainterPath m_hoveredShapeHighlightRect;Ui_WdgSvgTextOptionWidget optionUi;+ bool m_optionUiInitialized {false};QCursor m_base_cursor;QCursor m_text_inline_horizontal;diff --git a/plugins/tools/svgtexttool/WdgSvgTextOptionWidget.ui b/plugins/tools/svgtexttool/WdgSvgTextOptionWidget.uiindex 320a9d6dfe..3b5f83434c 100644--- a/plugins/tools/svgtexttool/WdgSvgTextOptionWidget.ui+++ b/plugins/tools/svgtexttool/WdgSvgTextOptionWidget.ui@@ -11,7 +11,7 @@<x>0</x><y>0</y><width>452</width>- <height>435</height>+ <height>463</height></rect></property><layout class="QGridLayout" name="gridLayout">@@ -336,27 +336,36 @@<item row="4" column="0"><widget class="QGroupBox" name="groupBoxConvert"><property name="title">- <string>Convert...</string>+ <string>Text Type</string></property><layout class="QVBoxLayout" name="verticalLayout_3"><item><widget class="QPushButton" name="convertPreFormatBtn"><property name="text">- <string>Convert to Preformatted</string>+ <string>Preformatted</string>+ </property>+ <property name="checkable">+ <bool>true</bool></property></widget></item><item><widget class="QPushButton" name="convertInlineBtn"><property name="text">- <string>Convert to Inline Wrapped</string>+ <string>Inline Wrapped</string>+ </property>+ <property name="checkable">+ <bool>true</bool></property></widget></item><item><widget class="QPushButton" name="convertToSvg1_1"><property name="text">- <string>Convert to SVG 1.1</string>+ <string>Prepositioned</string>+ </property>+ <property name="checkable">+ <bool>true</bool></property></widget></item>