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

Refactor Style Preset resource to not contain a TextShape.

Wolthera van Hövell tot Westerflier20fcff4559b9
diff --git a/libs/flake/resources/KoCssStylePreset.cpp b/libs/flake/resources/KoCssStylePreset.cpp
index 7be013568b..ae9008bdfb 100644
--- a/libs/flake/resources/KoCssStylePreset.cpp
+++ b/libs/flake/resources/KoCssStylePreset.cpp
@@ -34,13 +34,14 @@ const QString SAMPLE_PLACEHOLDER = i18nc("info:placeholder", "Style Sample");
struct KoCssStylePreset::Private {
Private()
- : shape(new KoSvgTextShape())
{
- shape->insertText(0, SAMPLE_PLACEHOLDER);
}
~Private() {}
- QScopedPointer<KoSvgTextShape> shape;
+ KoSvgTextProperties properties;
+ QString beforeText;
+ QString sample = SAMPLE_PLACEHOLDER;
+ QString afterText;
};
KoCssStylePreset::KoCssStylePreset(const QString &filename)
@@ -58,8 +59,12 @@ KoCssStylePreset::KoCssStylePreset(const KoCssStylePreset &rhs)
: KoResource(rhs)
, d(new Private())
{
- d->shape.reset(dynamic_cast<KoSvgTextShape*>(rhs.d->shape.data()->cloneShape()));
+ d->properties = rhs.d->properties;
+ d->sample = rhs.d->sample;
+ d->afterText = rhs.d->afterText;
+ d->beforeText = rhs.d->beforeText;
setDescription(rhs.description());
+ updateThumbnail();
setValid(true);
}
@@ -70,10 +75,7 @@ KoCssStylePreset::~KoCssStylePreset()
KoSvgTextProperties KoCssStylePreset::properties(int ppi, bool removeKraProps) const
{
- const QVector<int> treeIndex = d->shape->findTreeIndexForPropertyId(KoSvgTextProperties::KraTextStyleType);
-
- if (treeIndex.isEmpty()) return KoSvgTextProperties();
- KoSvgTextProperties props = d->shape->propertiesForTreeIndex(treeIndex);
+ KoSvgTextProperties props = d->properties;
const int storedPPI = storedPPIResolution();
if (storedPPI > 0 && ppi > 0) {
const double scale = double(storedPPI)/double(ppi);
@@ -96,17 +98,14 @@ KoSvgTextProperties KoCssStylePreset::properties(int ppi, bool removeKraProps) c
void KoCssStylePreset::setProperties(const KoSvgTextProperties &properties)
{
- const QVector<int> treeIndex = d->shape->findTreeIndexForPropertyId(KoSvgTextProperties::KraTextStyleType);
- if (!treeIndex.isEmpty() && d->shape->propertiesForTreeIndex(treeIndex) == properties) return;
- if (treeIndex.isEmpty()) {
- KoSvgTextProperties modifiedProps = properties;
- modifiedProps.setProperty(KoSvgTextProperties::KraTextStyleType, STYLE_TYPE_PARAGRAPH);
- d->shape->setPropertiesAtPos(-1, modifiedProps);
- } else {
- d->shape->setPropertiesAtTreeIndex(treeIndex, properties);
- }
- updateThumbnail();
+ if (d->properties == properties)
+ return;
+ d->properties = properties;
+ QStringList fonts = d->properties.property(KoSvgTextProperties::FontFamiliesId).toStringList();
+ //TODO: Apparantly we cannot remove metadata, only set it to nothing...
+ addMetaData(PRIMARY_FONT_FAMILY, fonts.value(0));
setValid(true);
+ setDirty(true);
}
QString KoCssStylePreset::description() const
@@ -115,12 +114,11 @@ QString KoCssStylePreset::description() const
return m[DESCRIPTION].toString();
}
-void KoCssStylePreset::setDescription(QString description)
+void KoCssStylePreset::setDescription(const QString &desc)
{
- d->shape->setAdditionalAttribute(DESC, description);
QMap<QString, QVariant> m = metadata();
- if (m[DESCRIPTION].toString() == description) return;
- addMetaData(DESCRIPTION, description);
+ if (m[DESCRIPTION].toString() == desc) return;
+ addMetaData(DESCRIPTION, desc);
setDirty(true);
}
@@ -132,27 +130,35 @@ QString KoCssStylePreset::styleType() const
void KoCssStylePreset::setStyleType(const QString &type)
{
+ QMap<QString, QVariant> m = metadata();
+ if (m[STYLE_TYPE].toString() == type) return;
addMetaData(STYLE_TYPE, type);
+ setDirty(true);
}
QString KoCssStylePreset::sampleText() const
{
- const QVector<int> treeIndex = d->shape->findTreeIndexForPropertyId(KoSvgTextProperties::KraTextStyleType);
- if (treeIndex.isEmpty()) return QString();
- QPair<int, int> pos = d->shape->findRangeForTreeIndex(treeIndex);
- pos.first = d->shape->indexForPos(pos.first);
- pos.second = d->shape->indexForPos(pos.second);
- return d->shape->plainText().mid(pos.first, pos.second-pos.first);
+ return d->sample;
+}
+
+void KoCssStylePreset::setSampleText(const QString &text)
+{
+ if (d->sample == text) return;
+ d->sample = text;
+ setDirty(true);
}
-void KoCssStylePreset::setSampleText(const QString &sample, const KoSvgTextProperties &props, const QString &before, const QString &after)
+KoShape* KoCssStylePreset::generateSampleShape() const
{
- KoSvgTextProperties modifiedProps = props;
+ KoSvgTextProperties modifiedProps = d->properties;
+ const QString sample = d->sample;
+ const QString after = d->afterText;
+ const QString before = d->beforeText;
+ KoSvgTextShape *textShape = new KoSvgTextShape();
KoSvgTextShape *sampleText = new KoSvgTextShape();
sampleText->insertText(0, sample.isEmpty()? name().isEmpty()? SAMPLE_PLACEHOLDER: name(): sample);
const QString type = styleType().isEmpty()? STYLE_TYPE_CHARACTER: styleType();
- setStyleType(type);
bool removeParagraph = type == STYLE_TYPE_CHARACTER;
@@ -188,10 +194,6 @@ void KoCssStylePreset::setSampleText(const QString &sample, const KoSvgTextPrope
sampleText->setPropertiesAtPos(-1, modifiedProps);
- QStringList fonts = modifiedProps.property(KoSvgTextProperties::FontFamiliesId).toStringList();
- //TODO: Apparantly we cannot remove metadata, only set it to nothing...
- addMetaData(PRIMARY_FONT_FAMILY, fonts.value(0));
-
if (type == STYLE_TYPE_PARAGRAPH) {
// For paragraph we'll add a shape, as those will allow wrapping,
// without being part of the properties like inline-size is.
@@ -205,7 +207,7 @@ void KoCssStylePreset::setSampleText(const QString &sample, const KoSvgTextPrope
sampleText->setShapesInside({inlineShape});
sampleText->relayout();
- d->shape.reset(sampleText);
+ textShape = sampleText;
} else {
/// For character the sample always needs to be set to be a child to
/// ensure that the character property doesn't include the default props.
@@ -223,15 +225,10 @@ void KoCssStylePreset::setSampleText(const QString &sample, const KoSvgTextPrope
}
newShape->insertRichText(newShape->posForIndex(before.size()), sampleText);
-
- d->shape.reset(newShape);
-
+ textShape = newShape;
}
- d->shape->cleanUp();
- updateThumbnail();
- setValid(true);
- setDirty(true);
+ return textShape;
}
Qt::Alignment KoCssStylePreset::alignSample() const
@@ -252,7 +249,7 @@ void KoCssStylePreset::updateAlignSample()
Qt::AlignmentFlag hComponent = Qt::AlignHCenter;
Qt::AlignmentFlag vComponent = Qt::AlignVCenter;
- const KoSvgTextProperties props = properties();
+ const KoSvgTextProperties props = d->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());
@@ -347,22 +344,26 @@ void KoCssStylePreset::updateAlignSample()
QString KoCssStylePreset::beforeText() const
{
- // handle paragraph.
- const QVector<int> treeIndex = d->shape->findTreeIndexForPropertyId(KoSvgTextProperties::KraTextStyleType);
- QPair<int, int> pos = d->shape->findRangeForTreeIndex(treeIndex);
- if (treeIndex.isEmpty()) return QString();
- pos.first = d->shape->indexForPos(pos.first);
- return d->shape->plainText().mid(0, pos.first);
+ return d->beforeText;
+}
+
+void KoCssStylePreset::setBeforeText(const QString &text)
+{
+ if (d->beforeText == text) return;
+ d->beforeText = text;
+ setDirty(true);
}
QString KoCssStylePreset::afterText() const
{
- // handle paragraph
- const QVector<int> treeIndex = d->shape->findTreeIndexForPropertyId(KoSvgTextProperties::KraTextStyleType);
- QPair<int, int> pos = d->shape->findRangeForTreeIndex(treeIndex);
- if (treeIndex.isEmpty()) return QString();
- pos.second = d->shape->indexForPos(pos.second);
- return d->shape->plainText().mid(pos.second);
+ return d->afterText;
+}
+
+void KoCssStylePreset::setAfterText(const QString &text)
+{
+ if (d->afterText == text) return;
+ d->afterText = text;
+ setDirty(true);
}
QString KoCssStylePreset::sampleSvg() const
@@ -382,6 +383,7 @@ void KoCssStylePreset::setStoredPPIResolution(const int ppi)
addMetaData(STORED_PPI, ppi);
}
+
KoResourceSP KoCssStylePreset::clone() const
{
return KoResourceSP(new KoCssStylePreset(*this));
@@ -418,14 +420,12 @@ bool KoCssStylePreset::loadFromDevice(QIODevice *dev, KisResourcesInterfaceSP re
Q_FOREACH(KoShape *shape, shapes) {
KoSvgTextShape *textShape = dynamic_cast<KoSvgTextShape*>(shape);
if (textShape) {
- d->shape.reset(textShape);
- setName(d->shape->additionalAttribute(TITLE));
- addMetaData(DESCRIPTION, d->shape->additionalAttribute(DESC));
- QVector<int> treeIndex = d->shape->findTreeIndexForPropertyId(KoSvgTextProperties::KraTextStyleType);
- qDebug() << "style found at..." << treeIndex;
+ setName(textShape->additionalAttribute(TITLE));
+ addMetaData(DESCRIPTION, textShape->additionalAttribute(DESC));
+ QVector<int> treeIndex = textShape->findTreeIndexForPropertyId(KoSvgTextProperties::KraTextStyleType);
QString styleType = STYLE_TYPE_PARAGRAPH;
if (!treeIndex.isEmpty()) {
- KoSvgTextProperties props = d->shape->propertiesForTreeIndex(treeIndex);
+ KoSvgTextProperties props = textShape->propertiesForTreeIndex(treeIndex);
styleType = props.property(KoSvgTextProperties::KraTextStyleType).toString();
if (props.hasProperty(KoSvgTextProperties::KraTextStyleResolution)) {
addMetaData(STORED_PPI, props.property(KoSvgTextProperties::KraTextStyleResolution));
@@ -434,11 +434,19 @@ bool KoCssStylePreset::loadFromDevice(QIODevice *dev, KisResourcesInterfaceSP re
QStringList fonts = props.property(KoSvgTextProperties::FontFamiliesId).toStringList();
addMetaData(PRIMARY_FONT_FAMILY, fonts.value(0));
}
+ setProperties(props);
+
+ QPair<int, int> pos = textShape->findRangeForTreeIndex(treeIndex);
+ pos.first = textShape->indexForPos(pos.first);
+ pos.second = textShape->indexForPos(pos.second);
+
+ d->sample = textShape->plainText().mid(pos.first, pos.second-pos.first);
+ d->beforeText = textShape->plainText().mid(0, pos.first);
+ d->afterText = textShape->plainText().mid(pos.second);
}
addMetaData(STYLE_TYPE, styleType);
- qDebug() << "type..." << styleType;
updateThumbnail();
setValid(true);
return true;
@@ -451,14 +459,15 @@ bool KoCssStylePreset::loadFromDevice(QIODevice *dev, KisResourcesInterfaceSP re
bool KoCssStylePreset::saveToDevice(QIODevice *dev) const
{
- if (!d->shape) return false;
+ KoShape *shape = generateSampleShape();
+ if (!shape) return false;
QMap<QString, QVariant> m = metadata();
- d->shape->setAdditionalAttribute(DESC, m[DESCRIPTION].toString());
- d->shape->setAdditionalAttribute(TITLE, name());
+ shape->setAdditionalAttribute(DESC, m[DESCRIPTION].toString());
+ shape->setAdditionalAttribute(TITLE, name());
- SvgWriter writer({d->shape.data()});
- return writer.save(*dev, d->shape->boundingRect().size());
+ SvgWriter writer({shape});
+ return writer.save(*dev, shape->boundingRect().size());
}
QString KoCssStylePreset::defaultFileExtension() const
@@ -479,17 +488,19 @@ QString generateSVG(const KoSvgTextShape *shape) {
void KoCssStylePreset::updateThumbnail()
{
+ KoSvgTextShape *shape = dynamic_cast<KoSvgTextShape*>(generateSampleShape());
+ if (!shape) return;
QImage img(256,
256,
QImage::Format_ARGB32);
img.fill(Qt::white);
KoShapePainter painter;
- painter.setShapes({d->shape.data()});
+ painter.setShapes({shape});
painter.paint(img);
/// generate SVG sample.
- addMetaData(SAMPLE_SVG, generateSVG(d->shape.data()));
+ addMetaData(SAMPLE_SVG, generateSVG(shape));
updateAlignSample();
setImage(img);
diff --git a/libs/flake/resources/KoCssStylePreset.h b/libs/flake/resources/KoCssStylePreset.h
index 697880cbeb..2b10fea625 100644
--- a/libs/flake/resources/KoCssStylePreset.h
+++ b/libs/flake/resources/KoCssStylePreset.h
@@ -30,11 +30,13 @@ public:
/// The actual text properties.
KoSvgTextProperties properties(int ppi = 72, bool removeKraProps = false) const;
+
+ /// set the properties. Call updateThumbnail to update the sample.
void setProperties(const KoSvgTextProperties &properties);
/// The description associated with this style.
QString description() const;
- void setDescription(QString description);
+ void setDescription(const QString &desc);
/// Set the style type, type is either "paragraph" or "character".
QString styleType() const;
@@ -43,12 +45,21 @@ public:
/// The sample text that is being styled by this preset.
QString sampleText() const;
+ /// set the sample. Call updateThumbnail to update the sample.
+ void setSampleText(const QString &text);
+
/// The text displayed before the sample. Only relevant when in Character mode.
QString beforeText() const;
+ /// set the before text. Call updateThumbnail to update the sample.
+ void setBeforeText(const QString &text);
+
/// The text displayed after the sample, only relevant when in character mode.
QString afterText() const;
+ /// set the after text. Call updateThumbnail to update the sample.
+ void setAfterText(const QString &text);
+
/// Returns the sample svg metadata. Use updateThumbnail to update it.
QString sampleSvg() const;
@@ -63,14 +74,10 @@ public:
void setStoredPPIResolution(const int ppi);
/**
- * @brief setSampleText
- * This allows setting the visible sample text. In effect, this reconstructs the internal text
- * with before, styled and after text.
- * @param before -- unstyled before text, this is only set when the styletype is character.
- * @param sample -- text of the styled sample.
- * @param after -- unstyled after text, this is only set when the styletype is character.
+ * @brief generateSampleShape
+ * This generates the sample textshape from the properties and sample text(s).
*/
- void setSampleText(const QString &sample, const KoSvgTextProperties &properties, const QString &before = "", const QString &after = "");
+ KoShape* generateSampleShape() const;
/// Determines the prefered sample alignment based on the text properties. It's set up so that
/// the alignment anchor of the text is shown.
@@ -87,6 +94,7 @@ public:
QString primaryFontFamily() 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 6c545e8e5c..5e8c8e9fb6 100644
--- a/plugins/dockers/textproperties/CssStylePresetEditDialog.cpp
+++ b/plugins/dockers/textproperties/CssStylePresetEditDialog.cpp
@@ -133,7 +133,11 @@ void CssStylePresetEditDialog::slotUpdateTextProperties()
if (m_currentResource->properties() != textData.commonProperties || shouldUpdateSample) {
m_currentResource->setStyleType(styleType);
- m_currentResource->setSampleText(sample, textData.commonProperties, before, after);
+ m_currentResource->setBeforeText(before);
+ m_currentResource->setSampleText(sample);
+ m_currentResource->setAfterText(after);
+ m_currentResource->setProperties(textData.commonProperties);
+ m_currentResource->updateThumbnail();
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/TextPropertiesDock.cpp b/plugins/dockers/textproperties/TextPropertiesDock.cpp
index 19162a28cb..8e59c3df2d 100644
--- a/plugins/dockers/textproperties/TextPropertiesDock.cpp
+++ b/plugins/dockers/textproperties/TextPropertiesDock.cpp
@@ -278,6 +278,8 @@ bool TextPropertiesDock::createNewPresetFromSettings()
KoSvgTextPropertyData textData = d->textModel->textData.get();
preset->setProperties(textData.commonProperties);
preset->setName(i18nc("@info:placeholder", "New Style Preset"));
+ preset->setStyleType("character");
+ preset->updateThumbnail();
CssStylePresetEditDialog *dialog = new CssStylePresetEditDialog(this);
dialog->setDpi(d->currentDpi);