source: trunk/tools/designer/src/lib/shared/stylesheeteditor.cpp@ 5

Last change on this file since 5 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

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