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++

Ensure paragraphs get a nice preview for Text Property Style Presets.

Wolthera van Hövell tot Westerflierea7e63d9de98
diff --git a/libs/flake/resources/KoCssStylePreset.cpp b/libs/flake/resources/KoCssStylePreset.cpp
index 724272ef64..8f347c9f83 100644
--- a/libs/flake/resources/KoCssStylePreset.cpp
+++ b/libs/flake/resources/KoCssStylePreset.cpp
@@ -22,6 +22,7 @@ const QString TITLE = "title";
const QString DESCRIPTION = "description";
const QString DESC = "desc";
const QString SAMPLE_SVG = "sample_svg";
+const QString SAMPLE_ALIGN = "sample_align";
const QString STYLE_TYPE = "style_type";
const QString STYLE_TYPE_PARAGRAPH = "paragraph";
@@ -65,7 +66,7 @@ KoCssStylePreset::~KoCssStylePreset()
}
-KoSvgTextProperties KoCssStylePreset::properties()
+KoSvgTextProperties KoCssStylePreset::properties() const
{
const QVector<int> treeIndex = d->shape->findTreeIndexForPropertyId(KoSvgTextProperties::KraTextStyleType);
qDebug() << "searching properties" << treeIndex;
@@ -103,7 +104,7 @@ void KoCssStylePreset::setDescription(QString description)
QString KoCssStylePreset::styleType() const
{
QMap<QString, QVariant> m = metadata();
- return m[STYLE_TYPE].toString();
+ return m.value(STYLE_TYPE, STYLE_TYPE_PARAGRAPH).toString();
}
void KoCssStylePreset::setStyleType(const QString &type)
@@ -143,6 +144,10 @@ void KoCssStylePreset::setSampleText(const QString &sample, const KoSvgTextPrope
modifiedProps.removeProperty(p);
}
}
+ // Always remove inline size, it is shape-specific.
+ if (p == KoSvgTextProperties::InlineSizeId) {
+ modifiedProps.removeProperty(p);
+ }
}
// This one is added after removing, because otherwise, the type is removed...
modifiedProps.setProperty(KoSvgTextProperties::KraTextStyleType, type);
@@ -150,34 +155,151 @@ void KoCssStylePreset::setSampleText(const QString &sample, const KoSvgTextPrope
sampleText->setPropertiesAtPos(-1, modifiedProps);
if (type == STYLE_TYPE_PARAGRAPH) {
- d->shape.reset(sampleText);
- }
+ KoPathShape *inlineShape = new KoPathShape();
+ inlineShape->moveTo(QPointF(0, 0));
+ inlineShape->lineTo(QPointF(120, 0));
+ inlineShape->lineTo(QPointF(120, 120));
+ inlineShape->lineTo(QPointF(0, 120));
+ inlineShape->lineTo(QPointF(0, 0));
+ inlineShape->close();
+ sampleText->setShapesInside({inlineShape});
+ sampleText->relayout();
- if (before.isEmpty() && after.isEmpty()) {
d->shape.reset(sampleText);
} else {
- KoSvgTextShape *newShape = new KoSvgTextShape();
- KoSvgTextProperties paraProps = KoSvgTextProperties::defaultProperties();
- // Set whitespace rule to pre-wrap.
- paraProps.setProperty(KoSvgTextProperties::TextCollapseId, KoSvgText::Preserve);
- paraProps.setProperty(KoSvgTextProperties::TextWrapId, KoSvgText::Wrap);
- newShape->setPropertiesAtPos(-1, paraProps);
- if (!after.isEmpty()) {
- newShape->insertText(0, after);
- }
- if (!before.isEmpty()) {
- newShape->insertText(0, before);
- }
- newShape->insertRichText(newShape->posForIndex(before.size()), sampleText);
- d->shape.reset(newShape);
+ if (before.isEmpty() && after.isEmpty()) {
+ d->shape.reset(sampleText);
+ } else {
+ KoSvgTextShape *newShape = new KoSvgTextShape();
+ KoSvgTextProperties paraProps = KoSvgTextProperties::defaultProperties();
+ // Set whitespace rule to pre-wrap.
+ paraProps.setProperty(KoSvgTextProperties::TextCollapseId, KoSvgText::Preserve);
+ paraProps.setProperty(KoSvgTextProperties::TextWrapId, KoSvgText::Wrap);
+ newShape->setPropertiesAtPos(-1, paraProps);
+ if (!after.isEmpty()) {
+ newShape->insertText(0, after);
+ }
+ if (!before.isEmpty()) {
+ newShape->insertText(0, before);
+ }
+
+ newShape->insertRichText(newShape->posForIndex(before.size()), sampleText);
+ d->shape.reset(newShape);
+ }
}
+
d->shape->debugParsing();
// Don't d->shape->cleanup() here, as it *can* lead to the sample getting cleaned up.
updateThumbnail();
setValid(true);
}
+Qt::Alignment KoCssStylePreset::alignSample() const
+{
+ QMap<QString, QVariant> m = metadata();
+ QVariant v = m.value(SAMPLE_ALIGN, static_cast<Qt::Alignment::Int>(Qt::AlignHCenter | Qt::AlignVCenter));
+ return static_cast<Qt::Alignment>(v.value<Qt::Alignment::Int>());
+}
+
+void KoCssStylePreset::updateAlignSample()
+{
+ Qt::AlignmentFlag hComponent = Qt::AlignHCenter;
+ Qt::AlignmentFlag vComponent = Qt::AlignVCenter;
+
+ const KoSvgTextProperties props = properties();
+ const QString type = styleType().isEmpty()? props.property(KoSvgTextProperties::KraTextStyleType).toString(): styleType();
+ if (type == STYLE_TYPE_PARAGRAPH) {
+ const KoSvgText::WritingMode mode = KoSvgText::WritingMode(props.propertyOrDefault(KoSvgTextProperties::WritingModeId).toInt());
+ const KoSvgText::Direction dir = KoSvgText::Direction(props.propertyOrDefault(KoSvgTextProperties::DirectionId).toInt());
+ const bool textAlignLast = props.hasProperty(KoSvgTextProperties::TextAlignLastId);
+ if (props.hasProperty(KoSvgTextProperties::TextAlignAllId) || textAlignLast) {
+ const KoSvgText::TextAlign align = textAlignLast? KoSvgText::TextAlign(props.property(KoSvgTextProperties::TextAlignLastId).toInt())
+ : KoSvgText::TextAlign(props.property(KoSvgTextProperties::TextAlignAllId).toInt());
+
+ if (mode == KoSvgText::HorizontalTB) {
+ vComponent = Qt::AlignTop;
+ if (align == KoSvgText::AlignStart || align == KoSvgText::AlignLastAuto) {
+ if (dir == KoSvgText::DirectionLeftToRight) {
+ hComponent = Qt::AlignLeft;
+ } else {
+ hComponent = Qt::AlignRight;
+ }
+ } else if (align == KoSvgText::AlignEnd) {
+ if (dir == KoSvgText::DirectionLeftToRight) {
+ hComponent = Qt::AlignRight;
+ } else {
+ hComponent = Qt::AlignLeft;
+ }
+ } else if (align == KoSvgText::AlignLeft) {
+ hComponent = Qt::AlignLeft;
+ } else if (align == KoSvgText::AlignRight) {
+ hComponent = Qt::AlignRight;
+ }
+ } else {
+ hComponent = mode == KoSvgText::VerticalRL? Qt::AlignRight: Qt::AlignLeft;
+ if (align == KoSvgText::AlignStart || align == KoSvgText::AlignLastAuto) {
+ if (dir == KoSvgText::DirectionLeftToRight) {
+ vComponent = Qt::AlignTop;
+ } else {
+ vComponent = Qt::AlignBottom;
+ }
+ } else if (align == KoSvgText::AlignEnd) {
+ if (dir == KoSvgText::DirectionLeftToRight) {
+ vComponent = Qt::AlignBottom;
+ } else {
+ vComponent = Qt::AlignTop;
+ }
+ } else if (align == KoSvgText::AlignLeft) {
+ vComponent = Qt::AlignTop;
+ } else if (align == KoSvgText::AlignRight) {
+ vComponent = Qt::AlignBottom;
+ }
+ }
+ } else {
+ const KoSvgText::TextAnchor anchor = KoSvgText::TextAnchor(props.propertyOrDefault(KoSvgTextProperties::TextAnchorId).toInt());
+
+ if (mode == KoSvgText::HorizontalTB) {
+ vComponent = Qt::AlignTop;
+ if (anchor == KoSvgText::AnchorStart) {
+ if (dir == KoSvgText::DirectionLeftToRight) {
+ hComponent = Qt::AlignLeft;
+ } else {
+ hComponent = Qt::AlignRight;
+ }
+ } else if (anchor == KoSvgText::AnchorEnd) {
+ if (dir == KoSvgText::DirectionLeftToRight) {
+ hComponent = Qt::AlignRight;
+ } else {
+ hComponent = Qt::AlignLeft;
+ }
+ } else {
+ hComponent = Qt::AlignHCenter;
+ }
+ } else {
+ hComponent = mode == KoSvgText::VerticalRL? Qt::AlignRight: Qt::AlignLeft;
+ if (anchor == KoSvgText::AnchorStart) {
+ if (dir == KoSvgText::DirectionLeftToRight) {
+ vComponent = Qt::AlignTop;
+ } else {
+ vComponent = Qt::AlignBottom;
+ }
+ } else if (anchor == KoSvgText::AnchorEnd) {
+ if (dir == KoSvgText::DirectionLeftToRight) {
+ vComponent = Qt::AlignBottom;
+ } else {
+ vComponent = Qt::AlignTop;
+ }
+ } else {
+ vComponent = Qt::AlignVCenter;
+ }
+ }
+ }
+ }
+
+ addMetaData(SAMPLE_ALIGN, static_cast<Qt::Alignment::Int>(hComponent | vComponent));
+}
+
QString KoCssStylePreset::beforeText() const
{
// handle paragraph.
@@ -303,6 +425,7 @@ void KoCssStylePreset::updateThumbnail()
/// generate SVG sample.
addMetaData(SAMPLE_SVG, generateSVG(d->shape.data()));
+ updateAlignSample();
setImage(img);
}
diff --git a/libs/flake/resources/KoCssStylePreset.h b/libs/flake/resources/KoCssStylePreset.h
index e28682ec96..b2571371f3 100644
--- a/libs/flake/resources/KoCssStylePreset.h
+++ b/libs/flake/resources/KoCssStylePreset.h
@@ -29,7 +29,7 @@ public:
~KoCssStylePreset();
/// The actual text properties.
- KoSvgTextProperties properties();
+ KoSvgTextProperties properties() const;
void setProperties(const KoSvgTextProperties &properties);
/// The description associated with this style.
@@ -62,6 +62,11 @@ public:
*/
void setSampleText(const QString &sample, const KoSvgTextProperties &properties, const QString &before = "", const QString &after = "");
+ /// Determines the prefered sample alignment based on the text properties. It's set up so that
+ /// the alignment anchor of the text is shown.
+ Qt::Alignment alignSample() const;
+
+ void updateAlignSample();
// KoResource interface
public:
KoResourceSP clone() const override;
diff --git a/plugins/dockers/textproperties/CssStylePresetEditDialog.cpp b/plugins/dockers/textproperties/CssStylePresetEditDialog.cpp
index 6023f65440..f3413ab104 100644
--- a/plugins/dockers/textproperties/CssStylePresetEditDialog.cpp
+++ b/plugins/dockers/textproperties/CssStylePresetEditDialog.cpp
@@ -15,6 +15,10 @@
#include "FontAxesModel.h"
#include "FontStyleModel.h"
+QVariant variantFromAlignment(Qt::Alignment align) {
+ return QVariant(static_cast<Qt::Alignment::Int>(align));
+}
+
CssStylePresetEditDialog::CssStylePresetEditDialog(QWidget *parent)
:KoDialog(parent)
, m_model(new KoSvgTextPropertiesModel())
@@ -71,7 +75,6 @@ CssStylePresetEditDialog::~CssStylePresetEditDialog()
void CssStylePresetEditDialog::setCurrentResource(KoCssStylePresetSP resource)
{
- if (resource == m_currentResource) return;
m_blockUpdates = true;
m_currentResource = resource;
KoSvgTextProperties properties = m_currentResource->properties();
@@ -82,6 +85,7 @@ void CssStylePresetEditDialog::setCurrentResource(KoCssStylePresetSP resource)
m_quickWidget->rootObject()->setProperty("presetTitle", m_currentResource->name());
m_quickWidget->rootObject()->setProperty("presetDescription", m_currentResource->description());
m_quickWidget->rootObject()->setProperty("presetSample", m_currentResource->sampleSvg());
+ m_quickWidget->rootObject()->setProperty("presetSampleAlignment", variantFromAlignment(m_currentResource->alignSample()));
m_quickWidget->rootObject()->setProperty("styleType", m_currentResource->styleType());
m_quickWidget->rootObject()->setProperty("beforeSample", m_currentResource->beforeText());
@@ -127,6 +131,7 @@ void CssStylePresetEditDialog::slotUpdateTextProperties()
m_currentResource->setStyleType(styleType);
m_currentResource->setSampleText(sample, textData.commonProperties, before, after);
m_quickWidget->rootObject()->setProperty("presetSample", m_currentResource->sampleSvg());
+ m_quickWidget->rootObject()->setProperty("presetSampleAlignment", variantFromAlignment(m_currentResource->alignSample()));
QMetaObject::invokeMethod(m_quickWidget->rootObject(), "setProperties");
}
}
diff --git a/plugins/dockers/textproperties/qml/CssStylePresetEdit.qml b/plugins/dockers/textproperties/qml/CssStylePresetEdit.qml
index c9db331a15..832fa1fc48 100644
--- a/plugins/dockers/textproperties/qml/CssStylePresetEdit.qml
+++ b/plugins/dockers/textproperties/qml/CssStylePresetEdit.qml
@@ -15,7 +15,8 @@ Control {
property alias presetTitle: presetTitleField.text;
property alias presetDescription: presetDescriptionArea.text;
property alias presetSample: presetSampleLabel.svgData;
- property string styleType: "paragraph";
+ property alias presetSampleAlignment: presetSampleLabel.alignment;
+ property string styleType: "character"; // Defaults to character because the first index in the style combobox is character.
property double canvasDPI: 72.0;
property alias beforeSample: beforeSampleField.text;
@@ -56,7 +57,14 @@ Control {
Layout.columnSpan: 2;
foregroundColor: palette.text;
fullColor: false;
- Layout.preferredHeight: presetTitleField.height*2;
+
+ scalingType: styleType === "paragraph"? KoShapeQtQuickLabel.FitWidth: KoShapeQtQuickLabel.Fit;
+ Layout.preferredHeight: presetTitleField.height*4;
+ padding: presetTitleField.height;
+
+ Frame {
+ anchors.fill: parent;
+ }
}
Label {
@@ -119,9 +127,11 @@ Control {
Layout.fillWidth: true;
TextField {
id: beforeSampleField;
- Layout.fillWidth: true;
+ Layout.fillWidth: visible? true: false;
+ Layout.preferredWidth: visible? implicitWidth: 0;
placeholderText: i18nc("info:placeholder", "Before...");
onFocusChanged: if (!activeFocus) mainWindow.slotUpdateTextProperties();
+ visible: styleType !== "paragraph";
}
TextField {
id: sampleField;
@@ -131,9 +141,11 @@ Control {
}
TextField {
id: afterSampleField;
- Layout.fillWidth: true;
+ Layout.fillWidth: visible? true: false;
+ Layout.preferredWidth: visible? implicitWidth: 0;
placeholderText: i18nc("info:placeholder", "... After");
onFocusChanged: if (!activeFocus) mainWindow.slotUpdateTextProperties();
+ visible: styleType !== "paragraph";
}
}
}
diff --git a/plugins/dockers/textproperties/qml/TextProperties.qml b/plugins/dockers/textproperties/qml/TextProperties.qml
index 3e9882f78c..8b72a56c44 100644
--- a/plugins/dockers/textproperties/qml/TextProperties.qml
+++ b/plugins/dockers/textproperties/qml/TextProperties.qml
@@ -96,7 +96,9 @@ Rectangle {
contentItem: KoShapeQtQuickLabel {
implicitHeight: nameLabel.height * 4;
padding: nameLabel.height;
-
+ alignment: presetDelegate.meta.sample_align;
+ scalingType: presetDelegate.meta.style_type === "paragraph"?
+ KoShapeQtQuickLabel.FitWidth: KoShapeQtQuickLabel.Fit;
svgData: presetDelegate.meta.sample_svg;
foregroundColor: presetDelegate.highlighted? palette.highlightedText: palette.text;