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

Last change on this file since 651 was 651, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.6.2 sources.

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