diff --git a/src/plugins/qmldesigner/components/formeditor/backgroundaction.cpp b/src/plugins/qmldesigner/components/formeditor/backgroundaction.cpp new file mode 100644 index 00000000000..c1fe4e37e5e --- /dev/null +++ b/src/plugins/qmldesigner/components/formeditor/backgroundaction.cpp @@ -0,0 +1,96 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of Qt Creator. +** +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, 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, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +****************************************************************************/ + +#include "backgroundaction.h" + +#include +#include + +namespace QmlDesigner { + +BackgroundAction::BackgroundAction(QObject *parent) : + QWidgetAction(parent) +{ +} + +QIcon iconForColor(const QColor &color) { + const int size = 16; + QImage image(size, size, QImage::Format_ARGB32); + image.fill(0); + QPainter p(&image); + + p.fillRect(2, 2, size - 4, size - 4, Qt::black); + + if (color.alpha() == 0) { + const int miniSize = (size - 8) / 2; + p.fillRect(4, 4, miniSize, miniSize, Qt::white); + p.fillRect(miniSize + 4, miniSize + 4, miniSize, miniSize, Qt::white); + } else { + p.fillRect(4, 4, size - 8, size - 8, color); + } + return QPixmap::fromImage(image); +} + +QWidget *BackgroundAction::createWidget(QWidget *parent) +{ + QComboBox *comboBox = new QComboBox(parent); + comboBox->setFixedWidth(42); + + for (int i = 0; i < colors().count(); ++i) { + comboBox->addItem(tr("")); + comboBox->setItemIcon(i, iconForColor((colors().at(i)))); + } + + comboBox->setCurrentIndex(0); + connect(comboBox, SIGNAL(currentIndexChanged(int)), SLOT(emitBackgroundChanged(int))); + + comboBox->setProperty("hideborder", true); + return comboBox; +} + +void BackgroundAction::emitBackgroundChanged(int index) +{ + if (index < colors().count()) + emit backgroundChanged(colors().at(index)); +} + +QList BackgroundAction::colors() +{ + static QColor alphaZero(Qt::transparent); + static QList colorList = QList() << alphaZero + << QColor(Qt::black) + << QColor(Qt::darkGray) + << QColor(Qt::lightGray) + << QColor(Qt::white); + + + return colorList; +} + +} // namespace QmlDesigner diff --git a/src/plugins/qmldesigner/components/formeditor/backgroundaction.h b/src/plugins/qmldesigner/components/formeditor/backgroundaction.h new file mode 100644 index 00000000000..65eb16bc64b --- /dev/null +++ b/src/plugins/qmldesigner/components/formeditor/backgroundaction.h @@ -0,0 +1,64 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of Qt Creator. +** +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, 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, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +****************************************************************************/ + +#ifndef QMLDESIGNER_BACKGROUNDACTION_H +#define QMLDESIGNER_BACKGROUNDACTION_H + +#include + +namespace QmlDesigner { + +class BackgroundAction : public QWidgetAction +{ + enum BackgroundType { + CheckboardBackground, + WhiteBackground, + BlackBackground + }; + + Q_OBJECT +public: + explicit BackgroundAction(QObject *parent); + +signals: + void backgroundChanged(const QColor &color); + +protected: + QWidget *createWidget(QWidget *parent); + +private slots: + void emitBackgroundChanged(int index); + +private: + static QList colors(); +}; + +} // namespace QmlDesigner + +#endif // QMLDESIGNER_BACKGROUNDACTION_H diff --git a/src/plugins/qmldesigner/components/formeditor/formeditor.pri b/src/plugins/qmldesigner/components/formeditor/formeditor.pri index 8968c2f597c..1bb4f11d5e1 100644 --- a/src/plugins/qmldesigner/components/formeditor/formeditor.pri +++ b/src/plugins/qmldesigner/components/formeditor/formeditor.pri @@ -35,7 +35,8 @@ SOURCES += formeditoritem.cpp \ anchorindicatorgraphicsitem.cpp \ bindingindicator.cpp \ bindingindicatorgraphicsitem.cpp \ - contentnoteditableindicator.cpp + contentnoteditableindicator.cpp \ + backgroundaction.cpp HEADERS += formeditorscene.h \ formeditorwidget.h \ formeditoritem.h \ @@ -72,5 +73,6 @@ HEADERS += formeditorscene.h \ anchorindicatorgraphicsitem.h \ bindingindicator.h \ bindingindicatorgraphicsitem.h \ - contentnoteditableindicator.h + contentnoteditableindicator.h \ + backgroundaction.h RESOURCES += formeditor.qrc diff --git a/src/plugins/qmldesigner/components/formeditor/formeditorgraphicsview.cpp b/src/plugins/qmldesigner/components/formeditor/formeditorgraphicsview.cpp index daad8028bb0..569bb86f87f 100644 --- a/src/plugins/qmldesigner/components/formeditor/formeditorgraphicsview.cpp +++ b/src/plugins/qmldesigner/components/formeditor/formeditorgraphicsview.cpp @@ -54,16 +54,7 @@ FormEditorGraphicsView::FormEditorGraphicsView(QWidget *parent) : setAutoFillBackground(true); setBackgroundRole(QPalette::Window); - const int checkerbordSize= 20; - QPixmap tilePixmap(checkerbordSize * 2, checkerbordSize * 2); - tilePixmap.fill(Qt::white); - QPainter tilePainter(&tilePixmap); - QColor color(220, 220, 220); - tilePainter.fillRect(0, 0, checkerbordSize, checkerbordSize, color); - tilePainter.fillRect(checkerbordSize, checkerbordSize, checkerbordSize, checkerbordSize, color); - tilePainter.end(); - - setBackgroundBrush(tilePixmap); + activateCheckboardBackground(); viewport()->setMouseTracking(true); } @@ -126,6 +117,25 @@ QRectF FormEditorGraphicsView::rootItemRect() const return m_rootItemRect; } +void FormEditorGraphicsView::activateCheckboardBackground() +{ + const int checkerbordSize= 20; + QPixmap tilePixmap(checkerbordSize * 2, checkerbordSize * 2); + tilePixmap.fill(Qt::white); + QPainter tilePainter(&tilePixmap); + QColor color(220, 220, 220); + tilePainter.fillRect(0, 0, checkerbordSize, checkerbordSize, color); + tilePainter.fillRect(checkerbordSize, checkerbordSize, checkerbordSize, checkerbordSize, color); + tilePainter.end(); + + setBackgroundBrush(tilePixmap); +} + +void FormEditorGraphicsView::activateColoredBackground(const QColor &color) +{ + setBackgroundBrush(color); +} + void FormEditorGraphicsView::drawBackground(QPainter *painter, const QRectF &rectangle) { painter->save(); diff --git a/src/plugins/qmldesigner/components/formeditor/formeditorgraphicsview.h b/src/plugins/qmldesigner/components/formeditor/formeditorgraphicsview.h index be1e86273af..37c28ec2aa8 100644 --- a/src/plugins/qmldesigner/components/formeditor/formeditorgraphicsview.h +++ b/src/plugins/qmldesigner/components/formeditor/formeditorgraphicsview.h @@ -44,6 +44,9 @@ public: void setRootItemRect(const QRectF &rect); QRectF rootItemRect() const; + void activateCheckboardBackground(); + void activateColoredBackground(const QColor &color); + protected: void drawBackground(QPainter *painter, const QRectF &rect); void wheelEvent(QWheelEvent *event); diff --git a/src/plugins/qmldesigner/components/formeditor/formeditorwidget.cpp b/src/plugins/qmldesigner/components/formeditor/formeditorwidget.cpp index 21d4c187934..c44938111d5 100644 --- a/src/plugins/qmldesigner/components/formeditor/formeditorwidget.cpp +++ b/src/plugins/qmldesigner/components/formeditor/formeditorwidget.cpp @@ -41,6 +41,7 @@ #include #include #include +#include #include @@ -128,6 +129,11 @@ FormEditorWidget::FormEditorWidget(FormEditorView *view) fillLayout->addWidget(m_toolBox.data()); m_toolBox->setLeftSideActions(upperActions); + m_backgroundAction = new BackgroundAction(m_toolActionGroup.data()); + connect(m_backgroundAction.data(), &BackgroundAction::backgroundChanged, this, &FormEditorWidget::changeBackgound); + addAction(m_backgroundAction.data()); + upperActions.append(m_backgroundAction.data()); + m_toolBox->addRightSideAction(m_backgroundAction.data()); m_zoomAction = new ZoomAction(m_toolActionGroup.data()); connect(m_zoomAction.data(), SIGNAL(zoomLevelChanged(double)), SLOT(setZoomLevel(double))); @@ -177,6 +183,14 @@ void FormEditorWidget::changeRootItemHeight(const QString &heighText) m_formEditorView->rootModelNode().setAuxiliaryData("height", QVariant()); } +void FormEditorWidget::changeBackgound(const QColor &color) +{ + if (color.alpha() == 0) + m_graphicsView->activateCheckboardBackground(); + else + m_graphicsView->activateColoredBackground(color); +} + void FormEditorWidget::resetNodeInstanceView() { m_formEditorView->setCurrentStateNode(m_formEditorView->rootModelNode()); diff --git a/src/plugins/qmldesigner/components/formeditor/formeditorwidget.h b/src/plugins/qmldesigner/components/formeditor/formeditorwidget.h index a44cd4daf22..2265145dfb5 100644 --- a/src/plugins/qmldesigner/components/formeditor/formeditorwidget.h +++ b/src/plugins/qmldesigner/components/formeditor/formeditorwidget.h @@ -42,6 +42,7 @@ namespace QmlDesigner { class ZoomAction; class LineEditAction; +class BackgroundAction; class FormEditorView; class FormEditorScene; class FormEditorGraphicsView; @@ -88,6 +89,7 @@ private slots: void setZoomLevel(double zoomLevel); void changeRootItemWidth(const QString &widthText); void changeRootItemHeight(const QString &heightText); + void changeBackgound(const QColor &color); void resetNodeInstanceView(); private: @@ -104,6 +106,7 @@ private: QPointer m_selectOnlyContentItemsAction; QPointer m_rootWidthAction; QPointer m_rootHeightAction; + QPointer m_backgroundAction; QPointer m_resetAction; };