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
6#include "iconloader_p.h"
12
13#include <QtDesigner/abstractformwindow.h>
14#include <QtDesigner/abstractformwindowcursor.h>
15#include <QtDesigner/abstractformeditor.h>
16#include <QtDesigner/propertysheet.h>
17#include <QtDesigner/abstractintegration.h>
18#include <QtDesigner/abstractsettings.h>
19#include <QtDesigner/qextensionmanager.h>
20
21#include <texteditfindwidget_p.h>
22
23#include <QtWidgets/qcolordialog.h>
24#include <QtWidgets/qdialogbuttonbox.h>
25#include <QtWidgets/qfontdialog.h>
26#include <QtWidgets/qmenu.h>
27#include <QtWidgets/qpushbutton.h>
28#include <QtWidgets/qtoolbar.h>
29#include <QtWidgets/qboxlayout.h>
30
31#include <QtGui/qaction.h>
32#include <QtGui/qevent.h>
33#include <QtGui/qtextdocument.h>
34
35#include <private/qcssparser_p.h>
36
38
39using namespace Qt::StringLiterals;
40
41static constexpr auto styleSheetProperty = "styleSheet"_L1;
42static constexpr auto StyleSheetDialogC = "StyleSheetDialog"_L1;
43static constexpr auto seGeometry = "Geometry"_L1;
44
45namespace qdesigner_internal {
46
65
66// --- StyleSheetEditorDialog
72 m_validityLabel(new QLabel(tr("Valid Style Sheet"))),
73 m_core(core),
74 m_addResourceAction(new QAction(tr("Add Resource..."), this)),
75 m_addGradientAction(new QAction(tr("Add Gradient..."), this)),
76 m_addColorAction(new QAction(tr("Add Color..."), this)),
77 m_addFontAction(new QAction(tr("Add Font..."), this))
78{
79 setWindowTitle(tr("Edit Style Sheet"));
80
86
89
91
93 layout->addWidget(toolBar, 0, 0, 1, 2);
94 layout->addWidget(m_editor, 1, 0, 1, 2);
95 layout->addWidget(m_findWidget, 2, 0, 1, 2);
96 layout->addWidget(m_validityLabel, 3, 0, 1, 1);
97 layout->addWidget(m_buttonBox, 3, 1, 1, 1);
99
103
105 this, [this] { this->slotAddResource(QString()); });
107 this, [this] { this->slotAddGradient(QString()); });
109 this, [this] { this->slotAddColor(QString()); });
111
113
114 const char * const resourceProperties[] = {
115 "background-image",
116 "border-image",
117 "image",
118 nullptr
119 };
120
121 const char * const colorProperties[] = {
122 "color",
123 "background-color",
124 "alternate-background-color",
125 "border-color",
126 "border-top-color",
127 "border-right-color",
128 "border-bottom-color",
129 "border-left-color",
130 "gridline-color",
131 "selection-color",
132 "selection-background-color",
133 nullptr
134 };
135
136 QMenu *resourceActionMenu = new QMenu(this);
137 QMenu *gradientActionMenu = new QMenu(this);
138 QMenu *colorActionMenu = new QMenu(this);
139
144 }
145
149 this, [this, colorPropertyName] { this->slotAddColor(colorPropertyName); });
151 this, [this, colorPropertyName] { this->slotAddGradient(colorPropertyName); } );
152 }
153
157
158
165
167
170
173
175}
176
185
192
194{
202 delete menu;
203}
204
206{
208 if (!path.isEmpty())
209 insertCssProperty(property, "url("_L1 + path + u')');
210}
211
213{
214 bool ok;
216 if (ok)
218}
219
221{
222 const QColor color = QColorDialog::getColor(0xffffffff, this, QString(), QColorDialog::ShowAlphaChannel);
223 if (!color.isValid())
224 return;
225
227
228 if (color.alpha() == 255) {
229 colorStr = QString::asprintf("rgb(%d, %d, %d)",
230 color.red(), color.green(), color.blue());
231 } else {
232 colorStr = QString::asprintf("rgba(%d, %d, %d, %d)",
233 color.red(), color.green(), color.blue(),
234 color.alpha());
235 }
236
238}
239
241{
242 bool ok;
243 QFont font = QFontDialog::getFont(&ok, this);
244 if (ok) {
246 if (font.weight() != QFont::Normal)
247 fontStr += QString::number(font.weight()) + u' ';
248
249 switch (font.style()) {
250 case QFont::StyleItalic:
251 fontStr += "italic "_L1;
252 break;
253 case QFont::StyleOblique:
254 fontStr += "oblique "_L1;
255 break;
256 default:
257 break;
258 }
260 fontStr += "pt \""_L1;
261 fontStr += font.family();
262 fontStr += u'"';
263
264 insertCssProperty(u"font"_s, fontStr);
266 if (font.underline())
267 decoration += "underline"_L1;
268 if (font.strikeOut()) {
269 if (!decoration.isEmpty())
270 decoration += u' ';
271 decoration += "line-through"_L1;
272 }
273 insertCssProperty(u"text-decoration"_s, decoration);
274 }
275}
276
278{
279 if (!value.isEmpty()) {
281 if (!name.isEmpty()) {
285
286 // Simple check to see if we're in a selector scope
290 const bool inSelector = !opening.isNull() && (closing.isNull() ||
293 if (m_editor->textCursor().block().length() != 1)
294 insertion += u'\n';
295 if (inSelector)
296 insertion += u'\t';
297 insertion += name;
298 insertion += ": "_L1;
299 insertion += value;
300 insertion += u';';
303 } else {
305 }
306 }
307}
308
310{
311 m_core->integration()->emitHelpRequested(u"qtwidgets"_s,
312 u"stylesheet-reference.html"_s);
313}
314
315// See QDialog::keyPressEvent()
316static inline bool isEnter(const QKeyEvent *e)
317{
318 const bool isEnter = e->key() == Qt::Key_Enter;
319 const bool isReturn = e->key() == Qt::Key_Return;
320 return (e->modifiers() == Qt::KeyboardModifiers() && (isEnter || isReturn))
321 || (e->modifiers().testFlag(Qt::KeypadModifier) && isEnter);
322}
323
325{
326 // As long as the find widget is visible, suppress the default button
327 // behavior (close on Enter) of QDialog.
328 if (!(m_findWidget->isVisible() && isEnter(e)))
330}
331
336
338{
339 return m_editor->toPlainText();
340}
341
343{
345}
346
348{
351 if (parser.parse(&sheet))
352 return true;
353 QCss::Parser parser2("* { "_L1 + styleSheet + '}'_L1);
354 return parser2.parse(&sheet);
355}
356
358{
361 if (valid) {
362 m_validityLabel->setText(tr("Valid Style Sheet"));
363 m_validityLabel->setStyleSheet(u"color: green"_s);
364 } else {
365 m_validityLabel->setText(tr("Invalid Style Sheet"));
366 m_validityLabel->setStyleSheet(u"color: red"_s);
367 }
368}
369
370// --- StyleSheetPropertyEditorDialog
393
395{
396 const PropertySheetStringValue value(text(), false);
398}
399
400} // namespace qdesigner_internal
401
402QT_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