forked from qt-creator/qt-creator
Add combo box to change the background of the form editor
Task-number: QTCREATORBUG-11352 Change-Id: I73cca4ff86005d929a26af83a1765e9b7a313952 Reviewed-by: Alessandro Portale <alessandro.portale@theqtcompany.com>
This commit is contained in:
committed by
Thomas Hartmann
parent
fa5d4b92a7
commit
0ebbcc5615
@@ -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 <QComboBox>
|
||||
#include <QPainter>
|
||||
|
||||
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<QColor> BackgroundAction::colors()
|
||||
{
|
||||
static QColor alphaZero(Qt::transparent);
|
||||
static QList<QColor> colorList = QList<QColor>() << alphaZero
|
||||
<< QColor(Qt::black)
|
||||
<< QColor(Qt::darkGray)
|
||||
<< QColor(Qt::lightGray)
|
||||
<< QColor(Qt::white);
|
||||
|
||||
|
||||
return colorList;
|
||||
}
|
||||
|
||||
} // namespace QmlDesigner
|
@@ -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 <QWidgetAction>
|
||||
|
||||
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<QColor> colors();
|
||||
};
|
||||
|
||||
} // namespace QmlDesigner
|
||||
|
||||
#endif // QMLDESIGNER_BACKGROUNDACTION_H
|
@@ -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
|
||||
|
@@ -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();
|
||||
|
@@ -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);
|
||||
|
@@ -41,6 +41,7 @@
|
||||
#include <formeditorscene.h>
|
||||
#include <formeditorview.h>
|
||||
#include <lineeditaction.h>
|
||||
#include <backgroundaction.h>
|
||||
|
||||
#include <utils/fileutils.h>
|
||||
|
||||
@@ -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());
|
||||
|
@@ -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<QAction> m_selectOnlyContentItemsAction;
|
||||
QPointer<LineEditAction> m_rootWidthAction;
|
||||
QPointer<LineEditAction> m_rootHeightAction;
|
||||
QPointer<BackgroundAction> m_backgroundAction;
|
||||
QPointer<QAction> m_resetAction;
|
||||
};
|
||||
|
||||
|
Reference in New Issue
Block a user