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
11
12#include <QtDesigner/abstractformwindow.h>
13#include <QtDesigner/abstractformwindowcursor.h>
14#include <QtDesigner/abstractformeditor.h>
15#include <QtDesigner/propertysheet.h>
16#include <QtDesigner/abstractintegration.h>
17#include <QtDesigner/abstractsettings.h>
18#include <QtDesigner/qextensionmanager.h>
19
20#include <texteditfindwidget_p.h>
21
22#include <QtWidgets/qcolordialog.h>
23#include <QtWidgets/qdialogbuttonbox.h>
24#include <QtWidgets/qfontdialog.h>
25#include <QtWidgets/qmenu.h>
26#include <QtWidgets/qpushbutton.h>
27#include <QtWidgets/qtoolbar.h>
28#include <QtWidgets/qboxlayout.h>
29
30#include <QtGui/qaction.h>
31#include <QtGui/qevent.h>
32#include <QtGui/qtextdocument.h>
33
34#include <private/qcssparser_p.h>
35
37
38using namespace Qt::StringLiterals;
39
40static constexpr auto styleSheetProperty = "styleSheet"_L1;
41static constexpr auto StyleSheetDialogC = "StyleSheetDialog"_L1;
42static constexpr auto seGeometry = "Geometry"_L1;
43
44namespace qdesigner_internal {
45
69
70// --- StyleSheetEditorDialog
76 m_validityLabel(new QLabel(tr("Valid Style Sheet"))),
77 m_core(core),
78 m_addResourceAction(new QAction(tr("Add Resource..."), this)),
79 m_addGradientAction(new QAction(tr("Add Gradient..."), this)),
80 m_addColorAction(new QAction(tr("Add Color..."), this)),
81 m_addFontAction(new QAction(tr("Add Font..."), this))
82{
83 setWindowTitle(tr("Edit Style Sheet"));
84
90
93
95
97 layout->addWidget(toolBar, 0, 0, 1, 2);
98 layout->addWidget(m_editor, 1, 0, 1, 2);
99 layout->addWidget(m_findWidget, 2, 0, 1, 2);
100 layout->addWidget(m_validityLabel, 3, 0, 1, 1);
101 layout->addWidget(m_buttonBox, 3, 1, 1, 1);
103
107
109 this, [this] { this->slotAddResource(QString()); });
111 this, [this] { this->slotAddGradient(QString()); });
113 this, [this] { this->slotAddColor(QString()); });
115
117
118 const char * const resourceProperties[] = {
119 "background-image",
120 "border-image",
121 "image",
122 nullptr
123 };
124
125 const char * const colorProperties[] = {
126 "color",
127 "background-color",
128 "alternate-background-color",
129 "border-color",
130 "border-top-color",
131 "border-right-color",
132 "border-bottom-color",
133 "border-left-color",
134 "gridline-color",
135 "selection-color",
136 "selection-background-color",
137 nullptr
138 };
139
140 QMenu *resourceActionMenu = new QMenu(this);
141 QMenu *gradientActionMenu = new QMenu(this);
142 QMenu *colorActionMenu = new QMenu(this);
143
148 }
149
153 this, [this, colorPropertyName] { this->slotAddColor(colorPropertyName); });
155 this, [this, colorPropertyName] { this->slotAddGradient(colorPropertyName); } );
156 }
157
161
162
169
171
174
177
179}
180
189
196
198{
206 delete menu;
207}
208
210{
212 if (!path.isEmpty())
213 insertCssProperty(property, "url("_L1 + path + u')');
214}
215
217{
218 bool ok;
220 if (ok)
222}
223
225{
226 const QColor color = QColorDialog::getColor(0xffffffff, this, QString(), QColorDialog::ShowAlphaChannel);
227 if (!color.isValid())
228 return;
229
231
232 if (color.alpha() == 255) {
233 colorStr = QString::asprintf("rgb(%d, %d, %d)",
234 color.red(), color.green(), color.blue());
235 } else {
236 colorStr = QString::asprintf("rgba(%d, %d, %d, %d)",
237 color.red(), color.green(), color.blue(),
238 color.alpha());
239 }
240
242}
243
245{
246 bool ok;
247 QFont font = QFontDialog::getFont(&ok, this);
248 if (ok) {
250 if (font.weight() != QFont::Normal)
251 fontStr += QString::number(font.weight()) + u' ';
252
253 switch (font.style()) {
254 case QFont::StyleItalic:
255 fontStr += "italic "_L1;
256 break;
257 case QFont::StyleOblique:
258 fontStr += "oblique "_L1;
259 break;
260 default:
261 break;
262 }
264 fontStr += "pt \""_L1;
265 fontStr += font.family();
266 fontStr += u'"';
267
268 insertCssProperty(u"font"_s, fontStr);
270 if (font.underline())
271 decoration += "underline"_L1;
272 if (font.strikeOut()) {
273 if (!decoration.isEmpty())
274 decoration += u' ';
275 decoration += "line-through"_L1;
276 }
277 insertCssProperty(u"text-decoration"_s, decoration);
278 }
279}
280
282{
283 if (!value.isEmpty()) {
285 if (!name.isEmpty()) {
289
290 // Simple check to see if we're in a selector scope
294 const bool inSelector = !opening.isNull() && (closing.isNull() ||
297 if (m_editor->textCursor().block().length() != 1)
298 insertion += u'\n';
299 if (inSelector)
300 insertion += u'\t';
301 insertion += name;
302 insertion += ": "_L1;
303 insertion += value;
304 insertion += u';';
307 } else {
309 }
310 }
311}
312
314{
315 m_core->integration()->emitHelpRequested(u"qtwidgets"_s,
316 u"stylesheet-reference.html"_s);
317}
318
319// See QDialog::keyPressEvent()
320static inline bool isEnter(const QKeyEvent *e)
321{
322 const bool isEnter = e->key() == Qt::Key_Enter;
323 const bool isReturn = e->key() == Qt::Key_Return;
324 return (e->modifiers() == Qt::KeyboardModifiers() && (isEnter || isReturn))
325 || (e->modifiers().testFlag(Qt::KeypadModifier) && isEnter);
326}
327
329{
330 // As long as the find widget is visible, suppress the default button
331 // behavior (close on Enter) of QDialog.
332 if (!(m_findWidget->isVisible() && isEnter(e)))
334}
335
340
342{
343 return m_editor->toPlainText();
344}
345
347{
349}
350
352{
355 if (parser.parse(&sheet))
356 return true;
357 QCss::Parser parser2("* { "_L1 + styleSheet + '}'_L1);
358 return parser2.parse(&sheet);
359}
360
362{
365 if (valid) {
366 m_validityLabel->setText(tr("Valid Style Sheet"));
367 m_validityLabel->setStyleSheet(u"color: green"_s);
368 } else {
369 m_validityLabel->setText(tr("Invalid Style Sheet"));
370 m_validityLabel->setStyleSheet(u"color: red"_s);
371 }
372}
373
374// --- StyleSheetPropertyEditorDialog
397
399{
400 const PropertySheetStringValue value(text(), false);
402}
403
404} // namespace qdesigner_internal
405
406QT_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