Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
stylesheeteditor.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
3// Qt-Security score:significant reason:default
4
7#include "iconloader_p.h"
13
14#include <QtDesigner/abstractformwindow.h>
15#include <QtDesigner/abstractformwindowcursor.h>
16#include <QtDesigner/abstractformeditor.h>
17#include <QtDesigner/propertysheet.h>
18#include <QtDesigner/abstractintegration.h>
19#include <QtDesigner/abstractsettings.h>
20#include <QtDesigner/qextensionmanager.h>
21
22#include <texteditfindwidget_p.h>
23
24#include <QtWidgets/qcolordialog.h>
25#include <QtWidgets/qdialogbuttonbox.h>
26#include <QtWidgets/qfontdialog.h>
27#include <QtWidgets/qmenu.h>
28#include <QtWidgets/qpushbutton.h>
29#include <QtWidgets/qtoolbar.h>
30#include <QtWidgets/qboxlayout.h>
31
32#include <QtGui/qaction.h>
33#include <QtGui/qevent.h>
34#include <QtGui/qtextdocument.h>
35
36#include <private/qcssparser_p.h>
37
39
40using namespace Qt::StringLiterals;
41
42static constexpr auto styleSheetProperty = "styleSheet"_L1;
43static constexpr auto StyleSheetDialogC = "StyleSheetDialog"_L1;
44static constexpr auto seGeometry = "Geometry"_L1;
45
46namespace qdesigner_internal {
47
66
67// --- StyleSheetEditorDialog
73 m_validityLabel(new QLabel(tr("Valid Style Sheet"))),
74 m_core(core),
75 m_addResourceAction(new QAction(tr("Add Resource..."), this)),
76 m_addGradientAction(new QAction(tr("Add Gradient..."), this)),
77 m_addColorAction(new QAction(tr("Add Color..."), this)),
78 m_addFontAction(new QAction(tr("Add Font..."), this))
79{
80 setWindowTitle(tr("Edit Style Sheet"));
81
87
90
92
94 layout->addWidget(toolBar, 0, 0, 1, 2);
95 layout->addWidget(m_editor, 1, 0, 1, 2);
96 layout->addWidget(m_findWidget, 2, 0, 1, 2);
97 layout->addWidget(m_validityLabel, 3, 0, 1, 1);
98 layout->addWidget(m_buttonBox, 3, 1, 1, 1);
100
104
106 this, [this] { this->slotAddResource(QString()); });
108 this, [this] { this->slotAddGradient(QString()); });
110 this, [this] { this->slotAddColor(QString()); });
112
114
115 const char * const resourceProperties[] = {
116 "background-image",
117 "border-image",
118 "image",
119 nullptr
120 };
121
122 const char * const colorProperties[] = {
123 "color",
124 "background-color",
125 "alternate-background-color",
126 "border-color",
127 "border-top-color",
128 "border-right-color",
129 "border-bottom-color",
130 "border-left-color",
131 "gridline-color",
132 "selection-color",
133 "selection-background-color",
134 nullptr
135 };
136
137 QMenu *resourceActionMenu = new QMenu(this);
138 QMenu *gradientActionMenu = new QMenu(this);
139 QMenu *colorActionMenu = new QMenu(this);
140
145 }
146
150 this, [this, colorPropertyName] { this->slotAddColor(colorPropertyName); });
152 this, [this, colorPropertyName] { this->slotAddGradient(colorPropertyName); } );
153 }
154
158
159
166
168
171
174
176}
177
186
193
195{
203 delete menu;
204}
205
207{
209 if (!path.isEmpty())
210 insertCssProperty(property, "url("_L1 + path + u')');
211}
212
214{
215 bool ok;
217 if (ok)
219}
220
222{
223 const QColor color = QColorDialog::getColor(0xffffffff, this, QString(), QColorDialog::ShowAlphaChannel);
224 if (!color.isValid())
225 return;
226
228
229 if (color.alpha() == 255) {
230 colorStr = QString::asprintf("rgb(%d, %d, %d)",
231 color.red(), color.green(), color.blue());
232 } else {
233 colorStr = QString::asprintf("rgba(%d, %d, %d, %d)",
234 color.red(), color.green(), color.blue(),
235 color.alpha());
236 }
237
239}
240
242{
243 bool ok;
244 QFont font = QFontDialog::getFont(&ok, this);
245 if (ok) {
247 if (font.weight() != QFont::Normal)
248 fontStr += QString::number(font.weight()) + u' ';
249
250 switch (font.style()) {
251 case QFont::StyleItalic:
252 fontStr += "italic "_L1;
253 break;
254 case QFont::StyleOblique:
255 fontStr += "oblique "_L1;
256 break;
257 default:
258 break;
259 }
261 fontStr += "pt \""_L1;
262 fontStr += font.family();
263 fontStr += u'"';
264
265 insertCssProperty(u"font"_s, fontStr);
267 if (font.underline())
268 decoration += "underline"_L1;
269 if (font.strikeOut()) {
270 if (!decoration.isEmpty())
271 decoration += u' ';
272 decoration += "line-through"_L1;
273 }
274 insertCssProperty(u"text-decoration"_s, decoration);
275 }
276}
277
279{
280 if (!value.isEmpty()) {
282 if (!name.isEmpty()) {
286
287 // Simple check to see if we're in a selector scope
291 const bool inSelector = !opening.isNull() && (closing.isNull() ||
294 if (m_editor->textCursor().block().length() != 1)
295 insertion += u'\n';
296 if (inSelector)
297 insertion += u'\t';
298 insertion += name;
299 insertion += ": "_L1;
300 insertion += value;
301 insertion += u';';
304 } else {
306 }
307 }
308}
309
311{
312 m_core->integration()->emitHelpRequested(u"qtwidgets"_s,
313 u"stylesheet-reference.html"_s);
314}
315
316// See QDialog::keyPressEvent()
317static inline bool isEnter(const QKeyEvent *e)
318{
319 const bool isEnter = e->key() == Qt::Key_Enter;
320 const bool isReturn = e->key() == Qt::Key_Return;
321 return (e->modifiers() == Qt::KeyboardModifiers() && (isEnter || isReturn))
322 || (e->modifiers().testFlag(Qt::KeypadModifier) && isEnter);
323}
324
326{
327 // As long as the find widget is visible, suppress the default button
328 // behavior (close on Enter) of QDialog.
329 if (!(m_findWidget->isVisible() && isEnter(e)))
331}
332
337
339{
340 return m_editor->toPlainText();
341}
342
344{
346}
347
349{
352 if (parser.parse(&sheet))
353 return true;
354 QCss::Parser parser2("* { "_L1 + styleSheet + '}'_L1);
355 return parser2.parse(&sheet);
356}
357
359{
362 if (valid) {
363 m_validityLabel->setText(tr("Valid Style Sheet"));
364 m_validityLabel->setStyleSheet(u"color: green"_s);
365 } else {
366 m_validityLabel->setText(tr("Invalid Style Sheet"));
367 m_validityLabel->setStyleSheet(u"color: red"_s);
368 }
369}
370
371// --- StyleSheetPropertyEditorDialog
394
396{
397 const PropertySheetStringValue value(text(), false);
399}
400
401} // namespace qdesigner_internal
402
403QT_END_NAMESPACE
Combined button and popup list for selecting options.
Auxiliary methods to store/retrieve settings.
static bool isEnter(const QKeyEvent *e)
static constexpr auto styleSheetProperty
static constexpr auto seGeometry
static constexpr auto StyleSheetDialogC