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, 450);
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 textData.enabled = true;
71 m_model->textData.set(textData);
72 if (m_quickWidget->rootObject()) {
73 m_quickWidget->rootObject()->setProperty("presetTitle", m_currentResource->name());
74 m_quickWidget->rootObject()->setProperty("presetDescription", m_currentResource->description());
75 m_quickWidget->rootObject()->setProperty("presetSample", m_currentResource->sampleSvg());
76 m_quickWidget->rootObject()->setProperty("presetSampleAlignment", variantFromAlignment(m_currentResource->alignSample()));
77 m_quickWidget->rootObject()->setProperty("styleType", m_currentResource->styleType());
78
79 m_quickWidget->rootObject()->setProperty("beforeSample", m_currentResource->beforeText());
80 m_quickWidget->rootObject()->setProperty("sampleText", m_currentResource->sampleText());
81 m_quickWidget->rootObject()->setProperty("afterSample", m_currentResource->afterText());
82 const QSizeF paragraphSampleSize = m_currentResource->paragraphSampleSize();
83 m_quickWidget->rootObject()->setProperty("sampleWidth", paragraphSampleSize.width());
84 m_quickWidget->rootObject()->setProperty("sampleHeight", paragraphSampleSize.height());
85 const int storedPPI = m_currentResource->storedPPIResolution();
86 m_quickWidget->rootObject()->setProperty("makePixelRelative", storedPPI > 0);
87 }
88 m_blockUpdates = false;
90}
91
93{
96 const QString title = m_quickWidget->rootObject()->property("presetTitle").toString();
97 const QString description = m_quickWidget->rootObject()->property("presetDescription").toString();
98 m_currentResource->setName(title);
99 m_currentResource->setDescription(description);
100 }
101 return m_currentResource;
102}
103
105{
106 if (m_quickWidget->rootObject()) {
107 m_quickWidget->rootObject()->setProperty("canvasDPI", dpi);
108 }
109}
110
112{
114 const QString before = m_quickWidget->rootObject()->property("beforeSample").toString();
115 const QString sample = m_quickWidget->rootObject()->property("sampleText").toString();
116 const QString after = m_quickWidget->rootObject()->property("afterSample").toString();
117 const QString styleType = m_quickWidget->rootObject()->property("styleType").toString();
118
119 const int sampleHeight = m_quickWidget->rootObject()->property("sampleHeight").toInt();
120 const int sampleWidth = m_quickWidget->rootObject()->property("sampleWidth").toInt();
121 const QSizeF sampleSize = QSizeF(sampleWidth, sampleHeight);
122
123
124 bool shouldUpdateSample = (before != m_currentResource->beforeText()
125 || sample != m_currentResource->sampleText()
126 || after != m_currentResource->afterText())
127 || sampleSize != m_currentResource->paragraphSampleSize();
128
129 KoSvgTextPropertyData textData = m_model->textData.get();
130
131 if (m_currentResource->properties() != textData.commonProperties || shouldUpdateSample) {
132 m_currentResource->setStyleType(styleType);
133 m_currentResource->setBeforeText(before);
134 m_currentResource->setSampleText(sample);
135 m_currentResource->setAfterText(after);
136
137 if (styleType == "paragraph") {
138 m_currentResource->setParagraphSampleSize(sampleSize);
139 }
140
141 m_currentResource->setProperties(textData.commonProperties);
142 m_currentResource->updateThumbnail();
143 m_quickWidget->rootObject()->setProperty("presetSample", m_currentResource->sampleSvg());
144 m_quickWidget->rootObject()->setProperty("presetSampleAlignment", variantFromAlignment(m_currentResource->alignSample()));
146 }
147 }
148}
149
151{
152 QColor c = QColorDialog::getColor(oldColor);
153 return c.isValid()? c: oldColor;
154}
155
157 if (!m_quickWidget->rootObject()) return;
158 bool isDirty = m_currentResource->isDirty();
159 const QString title = m_quickWidget->rootObject()->property("presetTitle").toString();
160 if (!isDirty && m_quickWidget->rootObject()) {
161 const QString description = m_quickWidget->rootObject()->property("presetDescription").toString();
162 if (title != m_currentResource->name() || description != m_currentResource->description()) {
163 isDirty = true;
164 }
165 }
166 this->button(KoDialog::Ok)->setEnabled(isDirty && !title.isEmpty());
167}
168
170 //Whenever we make it pixel relative, we'll set the resolution to 72 dpi.
171 if (m_blockUpdates) return;
172 if (!m_quickWidget->rootObject()) return;
173 const bool makePixelRelative = m_quickWidget->rootObject()->property("makePixelRelative").toBool();
174 const int storedPPI = m_currentResource->storedPPIResolution();
175 const int canvasPPI = m_quickWidget->rootObject()->property("canvasDPI").toInt();
176 KoSvgTextPropertyData textData = m_model->textData.get();
177 if (storedPPI > 0 && !makePixelRelative) {
178 const double scale = double(storedPPI)/double(canvasPPI);
179 textData.commonProperties.scaleAbsoluteValues(scale, scale);
180 m_currentResource->setStoredPPIResolution(0);
181 } else if(storedPPI == 0 && makePixelRelative) {
182 const double scale = double(canvasPPI)/double(72);
183 textData.commonProperties.scaleAbsoluteValues(scale, scale);
184 m_currentResource->setStoredPPIResolution(72);
185 }
186 m_model->textData.set(textData);
188}
QVariant variantFromAlignment(Qt::Alignment align)
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.