1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
|
/****************************************************************************
**
** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the Qt Designer of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this
** file. Please review the following information to ensure the GNU Lesser
** General Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU General
** Public License version 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of this
** file. Please review the following information to ensure the GNU General
** Public License version 3.0 requirements will be met:
** http://www.gnu.org/copyleft/gpl.html.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "stylesheeteditor_p.h"
#include "csshighlighter_p.h"
#include "iconselector_p.h"
#include "qtgradientmanager.h"
#include "qtgradientviewdialog.h"
#include "qtgradientutils.h"
#include "qdesigner_integration_p.h"
#include "qdesigner_utils_p.h"
#include "abstractsettings_p.h"
#include <QtDesigner/QDesignerFormWindowInterface>
#include <QtDesigner/QDesignerFormWindowCursorInterface>
#include <QtDesigner/QDesignerFormEditorInterface>
#include <QtDesigner/QDesignerPropertySheetExtension>
#include <QtDesigner/QExtensionManager>
#include <QtCore/QSignalMapper>
#include <QtGui/QAction>
#include <QtGui/QColorDialog>
#include <QtGui/QDialogButtonBox>
#include <QtGui/QFontDialog>
#include <QtGui/QMenu>
#include <QtGui/QPushButton>
#include <QtGui/QTextDocument>
#include <QtGui/QToolBar>
#include <QtGui/QVBoxLayout>
#include "private/qcssparser_p.h"
QT_BEGIN_NAMESPACE
static const char *styleSheetProperty = "styleSheet";
static const char *StyleSheetDialogC = "StyleSheetDialog";
static const char *Geometry = "Geometry";
namespace qdesigner_internal {
StyleSheetEditor::StyleSheetEditor(QWidget *parent)
: QTextEdit(parent)
{
setTabStopWidth(fontMetrics().width(QLatin1Char(' '))*4);
setAcceptRichText(false);
new CssHighlighter(document());
}
// --- StyleSheetEditorDialog
StyleSheetEditorDialog::StyleSheetEditorDialog(QDesignerFormEditorInterface *core, QWidget *parent, Mode mode):
QDialog(parent),
m_buttonBox(new QDialogButtonBox(QDialogButtonBox::Ok|QDialogButtonBox::Cancel|QDialogButtonBox::Help)),
m_editor(new StyleSheetEditor),
m_validityLabel(new QLabel(tr("Valid Style Sheet"))),
m_core(core),
m_addResourceAction(new QAction(tr("Add Resource..."), this)),
m_addGradientAction(new QAction(tr("Add Gradient..."), this)),
m_addColorAction(new QAction(tr("Add Color..."), this)),
m_addFontAction(new QAction(tr("Add Font..."), this))
{
setWindowTitle(tr("Edit Style Sheet"));
setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
connect(m_buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
connect(m_buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
connect(m_buttonBox, SIGNAL(helpRequested()), this, SLOT(slotRequestHelp()));
m_buttonBox->button(QDialogButtonBox::Help)->setShortcut(QKeySequence::HelpContents);
connect(m_editor, SIGNAL(textChanged()), this, SLOT(validateStyleSheet()));
QToolBar *toolBar = new QToolBar;
QGridLayout *layout = new QGridLayout;
layout->addWidget(toolBar, 0, 0, 1, 2);
layout->addWidget(m_editor, 1, 0, 1, 2);
layout->addWidget(m_validityLabel, 2, 0, 1, 1);
layout->addWidget(m_buttonBox, 2, 1, 1, 1);
setLayout(layout);
m_editor->setContextMenuPolicy(Qt::CustomContextMenu);
connect(m_editor, SIGNAL(customContextMenuRequested(QPoint)),
this, SLOT(slotContextMenuRequested(QPoint)));
QSignalMapper *resourceActionMapper = new QSignalMapper(this);
QSignalMapper *gradientActionMapper = new QSignalMapper(this);
QSignalMapper *colorActionMapper = new QSignalMapper(this);
resourceActionMapper->setMapping(m_addResourceAction, QString());
gradientActionMapper->setMapping(m_addGradientAction, QString());
colorActionMapper->setMapping(m_addColorAction, QString());
connect(m_addResourceAction, SIGNAL(triggered()), resourceActionMapper, SLOT(map()));
connect(m_addGradientAction, SIGNAL(triggered()), gradientActionMapper, SLOT(map()));
connect(m_addColorAction, SIGNAL(triggered()), colorActionMapper, SLOT(map()));
connect(m_addFontAction, SIGNAL(triggered()), this, SLOT(slotAddFont()));
m_addResourceAction->setEnabled(mode == ModePerForm);
const char * const resourceProperties[] = {
"background-image",
"border-image",
"image",
0
};
const char * const colorProperties[] = {
"color",
"background-color",
"alternate-background-color",
"border-color",
"border-top-color",
"border-right-color",
"border-bottom-color",
"border-left-color",
"gridline-color",
"selection-color",
"selection-background-color",
0
};
QMenu *resourceActionMenu = new QMenu(this);
QMenu *gradientActionMenu = new QMenu(this);
QMenu *colorActionMenu = new QMenu(this);
for (int resourceProperty = 0; resourceProperties[resourceProperty]; ++resourceProperty) {
QAction *action = resourceActionMenu->addAction(QLatin1String(resourceProperties[resourceProperty]));
connect(action, SIGNAL(triggered()), resourceActionMapper, SLOT(map()));
resourceActionMapper->setMapping(action, QLatin1String(resourceProperties[resourceProperty]));
}
for (int colorProperty = 0; colorProperties[colorProperty]; ++colorProperty) {
QAction *gradientAction = gradientActionMenu->addAction(QLatin1String(colorProperties[colorProperty]));
QAction *colorAction = colorActionMenu->addAction(QLatin1String(colorProperties[colorProperty]));
connect(gradientAction, SIGNAL(triggered()), gradientActionMapper, SLOT(map()));
connect(colorAction, SIGNAL(triggered()), colorActionMapper, SLOT(map()));
gradientActionMapper->setMapping(gradientAction, QLatin1String(colorProperties[colorProperty]));
colorActionMapper->setMapping(colorAction, QLatin1String(colorProperties[colorProperty]));
}
connect(resourceActionMapper, SIGNAL(mapped(QString)), this, SLOT(slotAddResource(QString)));
connect(gradientActionMapper, SIGNAL(mapped(QString)), this, SLOT(slotAddGradient(QString)));
connect(colorActionMapper, SIGNAL(mapped(QString)), this, SLOT(slotAddColor(QString)));
m_addResourceAction->setMenu(resourceActionMenu);
m_addGradientAction->setMenu(gradientActionMenu);
m_addColorAction->setMenu(colorActionMenu);
toolBar->addAction(m_addResourceAction);
toolBar->addAction(m_addGradientAction);
toolBar->addAction(m_addColorAction);
toolBar->addAction(m_addFontAction);
m_editor->setFocus();
QDesignerSettingsInterface *settings = core->settingsManager();
settings->beginGroup(QLatin1String(StyleSheetDialogC));
if (settings->contains(QLatin1String(Geometry)))
restoreGeometry(settings->value(QLatin1String(Geometry)).toByteArray());
settings->endGroup();
}
StyleSheetEditorDialog::~StyleSheetEditorDialog()
{
QDesignerSettingsInterface *settings = m_core->settingsManager();
settings->beginGroup(QLatin1String(StyleSheetDialogC));
settings->setValue(QLatin1String(Geometry), saveGeometry());
settings->endGroup();
}
void StyleSheetEditorDialog::setOkButtonEnabled(bool v)
{
m_buttonBox->button(QDialogButtonBox::Ok)->setEnabled(v);
if (QPushButton *applyButton = m_buttonBox->button(QDialogButtonBox::Apply))
applyButton->setEnabled(v);
}
void StyleSheetEditorDialog::slotContextMenuRequested(const QPoint &pos)
{
QMenu *menu = m_editor->createStandardContextMenu();
menu->addSeparator();
menu->addAction(m_addResourceAction);
menu->addAction(m_addGradientAction);
menu->exec(mapToGlobal(pos));
delete menu;
}
void StyleSheetEditorDialog::slotAddResource(const QString &property)
{
const QString path = IconSelector::choosePixmapResource(m_core, m_core->resourceModel(), QString(), this);
if (!path.isEmpty())
insertCssProperty(property, QString(QLatin1String("url(%1)")).arg(path));
}
void StyleSheetEditorDialog::slotAddGradient(const QString &property)
{
bool ok;
const QGradient grad = QtGradientViewDialog::getGradient(&ok, m_core->gradientManager(), this);
if (ok)
insertCssProperty(property, QtGradientUtils::styleSheetCode(grad));
}
void StyleSheetEditorDialog::slotAddColor(const QString &property)
{
const QColor color = QColorDialog::getColor(0xffffffff, this, QString(), QColorDialog::ShowAlphaChannel);
if (!color.isValid())
return;
QString colorStr;
if (color.alpha() == 255) {
colorStr = QString(QLatin1String("rgb(%1, %2, %3)")).arg(
color.red()).arg(color.green()).arg(color.blue());
} else {
colorStr = QString(QLatin1String("rgba(%1, %2, %3, %4)")).arg(
color.red()).arg(color.green()).arg(color.blue()).arg(color.alpha());
}
insertCssProperty(property, colorStr);
}
void StyleSheetEditorDialog::slotAddFont()
{
bool ok;
QFont font = QFontDialog::getFont(&ok, this);
if (ok) {
QString fontStr;
if (font.weight() != QFont::Normal) {
fontStr += QString::number(font.weight());
fontStr += QLatin1Char(' ');
}
switch (font.style()) {
case QFont::StyleItalic:
fontStr += QLatin1String("italic ");
break;
case QFont::StyleOblique:
fontStr += QLatin1String("oblique ");
break;
default:
break;
}
fontStr += QString::number(font.pointSize());
fontStr += QLatin1String("pt \"");
fontStr += font.family();
fontStr += QLatin1Char('"');
insertCssProperty(QLatin1String("font"), fontStr);
QString decoration;
if (font.underline())
decoration += QLatin1String("underline");
if (font.strikeOut()) {
if (!decoration.isEmpty())
decoration += QLatin1Char(' ');
decoration += QLatin1String("line-through");
}
insertCssProperty(QLatin1String("text-decoration"), decoration);
}
}
void StyleSheetEditorDialog::insertCssProperty(const QString &name, const QString &value)
{
if (!value.isEmpty()) {
QTextCursor cursor = m_editor->textCursor();
if (!name.isEmpty()) {
cursor.beginEditBlock();
cursor.removeSelectedText();
cursor.movePosition(QTextCursor::EndOfLine);
// Simple check to see if we're in a selector scope
const QTextDocument *doc = m_editor->document();
const QTextCursor closing = doc->find(QLatin1String("}"), cursor, QTextDocument::FindBackward);
const QTextCursor opening = doc->find(QLatin1String("{"), cursor, QTextDocument::FindBackward);
const bool inSelector = !opening.isNull() && (closing.isNull() ||
closing.position() < opening.position());
QString insertion;
if (m_editor->textCursor().block().length() != 1)
insertion += QLatin1Char('\n');
if (inSelector)
insertion += QLatin1Char('\t');
insertion += name;
insertion += QLatin1String(": ");
insertion += value;
insertion += QLatin1Char(';');
cursor.insertText(insertion);
cursor.endEditBlock();
} else {
cursor.insertText(value);
}
}
}
void StyleSheetEditorDialog::slotRequestHelp()
{
QDesignerIntegration::requestHelp(m_core, QLatin1String("qt"),
QLatin1String("stylesheet-reference.html"));
}
QDialogButtonBox * StyleSheetEditorDialog::buttonBox() const
{
return m_buttonBox;
}
QString StyleSheetEditorDialog::text() const
{
return m_editor->toPlainText();
}
void StyleSheetEditorDialog::setText(const QString &t)
{
m_editor->setText(t);
}
bool StyleSheetEditorDialog::isStyleSheetValid(const QString &styleSheet)
{
QCss::Parser parser(styleSheet);
QCss::StyleSheet sheet;
if (parser.parse(&sheet))
return true;
QString fullSheet = QLatin1String("* { ");
fullSheet += styleSheet;
fullSheet += QLatin1Char('}');
QCss::Parser parser2(fullSheet);
return parser2.parse(&sheet);
}
void StyleSheetEditorDialog::validateStyleSheet()
{
const bool valid = isStyleSheetValid(m_editor->toPlainText());
setOkButtonEnabled(valid);
if (valid) {
m_validityLabel->setText(tr("Valid Style Sheet"));
m_validityLabel->setStyleSheet(QLatin1String("color: green"));
} else {
m_validityLabel->setText(tr("Invalid Style Sheet"));
m_validityLabel->setStyleSheet(QLatin1String("color: red"));
}
}
// --- StyleSheetPropertyEditorDialog
StyleSheetPropertyEditorDialog::StyleSheetPropertyEditorDialog(QWidget *parent,
QDesignerFormWindowInterface *fw,
QWidget *widget):
StyleSheetEditorDialog(fw->core(), parent),
m_fw(fw),
m_widget(widget)
{
Q_ASSERT(m_fw != 0);
QPushButton *apply = buttonBox()->addButton(QDialogButtonBox::Apply);
QObject::connect(apply, SIGNAL(clicked()), this, SLOT(applyStyleSheet()));
QObject::connect(buttonBox(), SIGNAL(accepted()), this, SLOT(applyStyleSheet()));
QDesignerPropertySheetExtension *sheet =
qt_extension<QDesignerPropertySheetExtension*>(m_fw->core()->extensionManager(), m_widget);
Q_ASSERT(sheet != 0);
const int index = sheet->indexOf(QLatin1String(styleSheetProperty));
const PropertySheetStringValue value = qvariant_cast<PropertySheetStringValue>(sheet->property(index));
setText(value.value());
}
void StyleSheetPropertyEditorDialog::applyStyleSheet()
{
const PropertySheetStringValue value(text(), false);
m_fw->cursor()->setWidgetProperty(m_widget, QLatin1String(styleSheetProperty), QVariant::fromValue(value));
}
} // namespace qdesigner_internal
QT_END_NAMESPACE
|