Krita Source Code Documentation
Loading...
Searching...
No Matches
CssStylePresetEditDialog.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2025 Wolthera van Hövell tot Westerflier <griffinvalley@gmail.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
7#include <QQmlEngine>
8#include <QQmlContext>
9#include <QQuickItem>
10#include <QColorDialog>
11#include <QPushButton>
12#include <KoResourcePaths.h>
13#include <KLocalizedContext>
14#include <KLocalizedString>
15#include <KoFontRegistry.h>
16#include "FontAxesModel.h"
17#include "FontStyleModel.h"
18
19QVariant variantFromAlignment(Qt::Alignment align) {
20 return QVariant(static_cast<Qt::Alignment::Int>(align));
21}
22
24 :KoDialog(parent)
25 , m_model(new KoSvgTextPropertiesModel())
26
27{
28 setMinimumSize(600, 400);
29 setModal(true);
30
33 m_quickWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
34
35 m_quickWidget->setPalette(this->palette());
36 this->setWindowTitle(i18nc("@title:window", "Edit Style Preset"));
37
38 QStringList wellFormedBCPNames;
39 Q_FOREACH (const QString langCode, KLocalizedString::languages()) {
40 wellFormedBCPNames.append(langCode.split("_").join("-"));
41 }
42
43 connect(m_model, SIGNAL(textPropertyChanged()),
44 this, SLOT(slotUpdateTextProperties()));
45
46 m_quickWidget->setSource(QUrl("qrc:/CssStylePresetEdit.qml"));
47
48 if (!m_quickWidget->errors().empty()) {
49 qWarning() << "Errors in " << windowTitle() << ":" << m_quickWidget->errors();
50 } else {
51 m_quickWidget->rootObject()->setProperty("textProperties", QVariant::fromValue(m_model));
52 m_quickWidget->rootObject()->setProperty("locales", QVariant::fromValue(wellFormedBCPNames));
53 }
54}
55
61
63{
64 m_blockUpdates = true;
65 m_currentResource = resource;
66 KoSvgTextProperties properties = m_currentResource->properties();
67 KoSvgTextPropertyData textData;
69 textData.commonProperties = properties;
70 m_model->textData.set(textData);
71 if (m_quickWidget->rootObject()) {
72 m_quickWidget->rootObject()->setProperty("presetTitle", m_currentResource->name());
73 m_quickWidget->rootObject()->setProperty("presetDescription", m_currentResource->description());
74 m_quickWidget->rootObject()->setProperty("presetSample", m_currentResource->sampleSvg());
75 m_quickWidget->rootObject()->setProperty("presetSampleAlignment", variantFromAlignment(m_currentResource->alignSample()));
76 m_quickWidget->rootObject()->setProperty("styleType", m_currentResource->styleType());
77
78 m_quickWidget->rootObject()->setProperty("beforeSample", m_currentResource->beforeText());
79 m_quickWidget->rootObject()->setProperty("sampleText", m_currentResource->sampleText());
80 m_quickWidget->rootObject()->setProperty("afterSample", m_currentResource->afterText());
81 const int storedPPI = m_currentResource->storedPPIResolution();
82 m_quickWidget->rootObject()->setProperty("makePixelRelative", storedPPI > 0);
83 }
84 m_blockUpdates = false;
86}
87
89{
92 const QString title = m_quickWidget->rootObject()->property("presetTitle").toString();
93 const QString description = m_quickWidget->rootObject()->property("presetDescription").toString();
94 m_currentResource->setName(title);
95 m_currentResource->setDescription(description);
96 }
97 return m_currentResource;
98}
99
101{
102 if (m_quickWidget->rootObject()) {
103 m_quickWidget->rootObject()->setProperty("canvasDPI", dpi);
104 }
105}
106
108{
110 const QString before = m_quickWidget->rootObject()->property("beforeSample").toString();
111 const QString sample = m_quickWidget->rootObject()->property("sampleText").toString();
112 const QString after = m_quickWidget->rootObject()->property("afterSample").toString();
113 const QString styleType = m_quickWidget->rootObject()->property("styleType").toString();
114
115 bool shouldUpdateSample = (before != m_currentResource->beforeText()
116 || sample != m_currentResource->sampleText()
117 || after != m_currentResource->afterText());
118
119 KoSvgTextPropertyData textData = m_model->textData.get();
120
121 if (m_currentResource->properties() != textData.commonProperties || shouldUpdateSample) {
122 m_currentResource->setStyleType(styleType);
123 m_currentResource->setBeforeText(before);
124 m_currentResource->setSampleText(sample);
125 m_currentResource->setAfterText(after);
126 m_currentResource->setProperties(textData.commonProperties);
127 m_currentResource->updateThumbnail();
128 m_quickWidget->rootObject()->setProperty("presetSample", m_currentResource->sampleSvg());
129 m_quickWidget->rootObject()->setProperty("presetSampleAlignment", variantFromAlignment(m_currentResource->alignSample()));
131 }
132 }
133}
134
136{
137 QColor c = QColorDialog::getColor(oldColor);
138 return c.isValid()? c: oldColor;
139}
140
142 if (!m_quickWidget->rootObject()) return;
143 bool isDirty = m_currentResource->isDirty();
144 const QString title = m_quickWidget->rootObject()->property("presetTitle").toString();
145 if (!isDirty && m_quickWidget->rootObject()) {
146 const QString description = m_quickWidget->rootObject()->property("presetDescription").toString();
147 if (title != m_currentResource->name() || description != m_currentResource->description()) {
148 isDirty = true;
149 }
150 }
151 this->button(KoDialog::Ok)->setEnabled(isDirty && !title.isEmpty());
152}
153
155 //Whenever we make it pixel relative, we'll set the resolution to 72 dpi.
156 if (m_blockUpdates) return;
157 if (!m_quickWidget->rootObject()) return;
158 const bool makePixelRelative = m_quickWidget->rootObject()->property("makePixelRelative").toBool();
159 const int storedPPI = m_currentResource->storedPPIResolution();
160 const int canvasPPI = m_quickWidget->rootObject()->property("canvasDPI").toInt();
161 KoSvgTextPropertyData textData = m_model->textData.get();
162 if (storedPPI > 0 && !makePixelRelative) {
163 const double scale = double(storedPPI)/double(canvasPPI);
164 textData.commonProperties.scaleAbsoluteValues(scale, scale);
165 m_currentResource->setStoredPPIResolution(0);
166 } else if(storedPPI == 0 && makePixelRelative) {
167 const double scale = double(canvasPPI)/double(72);
168 textData.commonProperties.scaleAbsoluteValues(scale, scale);
169 m_currentResource->setStoredPPIResolution(72);
170 }
171 m_model->textData.set(textData);
173}
QVariant variantFromAlignment(Qt::Alignment align)
connect(this, SIGNAL(optionsChanged()), this, SLOT(saveOptions()))
CssStylePresetEditDialog(QWidget *parent=nullptr)
QColor modalColorDialog(QColor oldColor)
void setCurrentResource(KoCssStylePresetSP resource)
KoSvgTextPropertiesModel * m_model
The KisQQuickWidget class.
A dialog base class with standard buttons and predefined layouts.
Definition KoDialog.h:116
QPushButton * button(ButtonCode id) const
Definition KoDialog.cpp:591
void setMainWidget(QWidget *widget)
Definition KoDialog.cpp:354
@ Ok
Show Ok button. (this button accept()s the dialog; result set to QDialog::Accepted)
Definition KoDialog.h:127
The KoSvgTextPropertiesModel class.
lager::cursor< KoSvgTextPropertyData > textData
void scaleAbsoluteValues(const double scaleInline=1.0, const double scaleBlock=1.0)
scaleAbsoluteValues This scales all absolute values stored in these text properties....
rgba palette[MAX_PALETTE]
Definition palette.c:35
The KoSvgTextPropertyData struct.
KoSvgTextProperties commonProperties
The properties common between all the selected text.
KoSvgTextProperties inheritedProperties
The properties that are inherited, so that widgets may be set correctly.