Diagram Scene Example▲
Sélectionnez
/**
**************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the examples of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** 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 The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** BSD License Usage
** Alternatively, you may use this file under the terms of the BSD license
** as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
** * Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** * Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in
** the documentation and/or other materials provided with the
** distribution.
** * Neither the name of The Qt Company Ltd nor the names of its
** contributors may be used to endorse or promote products derived
** from this software without specific prior written permission.
**
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
**
** $QT_END_LICENSE$
**
***************************************************************************
*/
#include
"arrow.h"
#include
"diagramitem.h"
#include
"diagramscene.h"
#include
"diagramtextitem.h"
#include
"mainwindow.h"
#include <QtWidgets>
const
int
InsertTextButton =
10
;
MainWindow::
MainWindow()
{
createActions();
createToolBox();
createMenus();
scene =
new
DiagramScene(itemMenu, this
);
scene-&
gt;setSceneRect(QRectF(0
, 0
, 5000
, 5000
));
connect(scene, SIGNAL(itemInserted(DiagramItem*
)),
this
, SLOT(itemInserted(DiagramItem*
)));
connect(scene, SIGNAL(textInserted(QGraphicsTextItem*
)),
this
, SLOT(textInserted(QGraphicsTextItem*
)));
connect(scene, SIGNAL(itemSelected(QGraphicsItem*
)),
this
, SLOT(itemSelected(QGraphicsItem*
)));
createToolbars();
QHBoxLayout *
layout =
new
QHBoxLayout;
layout-&
gt;addWidget(toolBox);
view =
new
QGraphicsView(scene);
layout-&
gt;addWidget(view);
QWidget *
widget =
new
QWidget;
widget-&
gt;setLayout(layout);
setCentralWidget(widget);
setWindowTitle(tr("Diagramscene"
));
setUnifiedTitleAndToolBarOnMac(true
);
}
void
MainWindow::
backgroundButtonGroupClicked(QAbstractButton *
button)
{
QList&
lt;QAbstractButton *&
gt; buttons =
backgroundButtonGroup-&
gt;buttons();
foreach (QAbstractButton *
myButton, buttons) {
if
(myButton !=
button)
button-&
gt;setChecked(false
);
}
QString text =
button-&
gt;text();
if
(text ==
tr("Blue Grid"
))
scene-&
gt;setBackgroundBrush(QPixmap(":/images/background1.png"
));
else
if
(text ==
tr("White Grid"
))
scene-&
gt;setBackgroundBrush(QPixmap(":/images/background2.png"
));
else
if
(text ==
tr("Gray Grid"
))
scene-&
gt;setBackgroundBrush(QPixmap(":/images/background3.png"
));
else
scene-&
gt;setBackgroundBrush(QPixmap(":/images/background4.png"
));
scene-&
gt;update();
view-&
gt;update();
}
void
MainWindow::
buttonGroupClicked(int
id)
{
QList&
lt;QAbstractButton *&
gt; buttons =
buttonGroup-&
gt;buttons();
foreach (QAbstractButton *
button, buttons) {
if
(buttonGroup-&
gt;button(id) !=
button)
button-&
gt;setChecked(false
);
}
if
(id ==
InsertTextButton) {
scene-&
gt;setMode(DiagramScene::
InsertText);
}
else
{
scene-&
gt;setItemType(DiagramItem::
DiagramType(id));
scene-&
gt;setMode(DiagramScene::
InsertItem);
}
}
void
MainWindow::
deleteItem()
{
foreach (QGraphicsItem *
item, scene-&
gt;selectedItems()) {
if
(item-&
gt;type() ==
Arrow::
Type) {
scene-&
gt;removeItem(item);
Arrow *
arrow =
qgraphicsitem_cast&
lt;Arrow *&
gt;(item);
arrow-&
gt;startItem()-&
gt;removeArrow(arrow);
arrow-&
gt;endItem()-&
gt;removeArrow(arrow);
delete
item;
}
}
foreach (QGraphicsItem *
item, scene-&
gt;selectedItems()) {
if
(item-&
gt;type() ==
DiagramItem::
Type)
qgraphicsitem_cast&
lt;DiagramItem *&
gt;(item)-&
gt;removeArrows();
scene-&
gt;removeItem(item);
delete
item;
}
}
void
MainWindow::
pointerGroupClicked(int
)
{
scene-&
gt;setMode(DiagramScene::
Mode(pointerTypeGroup-&
gt;checkedId()));
}
void
MainWindow::
bringToFront()
{
if
(scene-&
gt;selectedItems().isEmpty())
return
;
QGraphicsItem *
selectedItem =
scene-&
gt;selectedItems().first();
QList&
lt;QGraphicsItem *&
gt; overlapItems =
selectedItem-&
gt;collidingItems();
qreal zValue =
0
;
foreach (QGraphicsItem *
item, overlapItems) {
if
(item-&
gt;zValue() &
gt;=
zValue &
amp;&
amp; item-&
gt;type() ==
DiagramItem::
Type)
zValue =
item-&
gt;zValue() +
0.1
;
}
selectedItem-&
gt;setZValue(zValue);
}
void
MainWindow::
sendToBack()
{
if
(scene-&
gt;selectedItems().isEmpty())
return
;
QGraphicsItem *
selectedItem =
scene-&
gt;selectedItems().first();
QList&
lt;QGraphicsItem *&
gt; overlapItems =
selectedItem-&
gt;collidingItems();
qreal zValue =
0
;
foreach (QGraphicsItem *
item, overlapItems) {
if
(item-&
gt;zValue() &
lt;=
zValue &
amp;&
amp; item-&
gt;type() ==
DiagramItem::
Type)
zValue =
item-&
gt;zValue() -
0.1
;
}
selectedItem-&
gt;setZValue(zValue);
}
void
MainWindow::
itemInserted(DiagramItem *
item)
{
pointerTypeGroup-&
gt;button(int
(DiagramScene::
MoveItem))-&
gt;setChecked(true
);
scene-&
gt;setMode(DiagramScene::
Mode(pointerTypeGroup-&
gt;checkedId()));
buttonGroup-&
gt;button(int
(item-&
gt;diagramType()))-&
gt;setChecked(false
);
}
void
MainWindow::
textInserted(QGraphicsTextItem *
)
{
buttonGroup-&
gt;button(InsertTextButton)-&
gt;setChecked(false
);
scene-&
gt;setMode(DiagramScene::
Mode(pointerTypeGroup-&
gt;checkedId()));
}
void
MainWindow::
currentFontChanged(const
QFont &
amp;)
{
handleFontChange();
}
void
MainWindow::
fontSizeChanged(const
QString &
amp;)
{
handleFontChange();
}
void
MainWindow::
sceneScaleChanged(const
QString &
amp;scale)
{
double
newScale =
scale.left(scale.indexOf(tr("%"
))).toDouble() /
100.0
;
QMatrix oldMatrix =
view-&
gt;matrix();
view-&
gt;resetMatrix();
view-&
gt;translate(oldMatrix.dx(), oldMatrix.dy());
view-&
gt;scale(newScale, newScale);
}
void
MainWindow::
textColorChanged()
{
textAction =
qobject_cast&
lt;QAction *&
gt;(sender());
fontColorToolButton-&
gt;setIcon(createColorToolButtonIcon(
":/images/textpointer.png"
,
qvariant_cast&
lt;QColor&
gt;(textAction-&
gt;data())));
textButtonTriggered();
}
void
MainWindow::
itemColorChanged()
{
fillAction =
qobject_cast&
lt;QAction *&
gt;(sender());
fillColorToolButton-&
gt;setIcon(createColorToolButtonIcon(
":/images/floodfill.png"
,
qvariant_cast&
lt;QColor&
gt;(fillAction-&
gt;data())));
fillButtonTriggered();
}
void
MainWindow::
lineColorChanged()
{
lineAction =
qobject_cast&
lt;QAction *&
gt;(sender());
lineColorToolButton-&
gt;setIcon(createColorToolButtonIcon(
":/images/linecolor.png"
,
qvariant_cast&
lt;QColor&
gt;(lineAction-&
gt;data())));
lineButtonTriggered();
}
void
MainWindow::
textButtonTriggered()
{
scene-&
gt;setTextColor(qvariant_cast&
lt;QColor&
gt;(textAction-&
gt;data()));
}
void
MainWindow::
fillButtonTriggered()
{
scene-&
gt;setItemColor(qvariant_cast&
lt;QColor&
gt;(fillAction-&
gt;data()));
}
void
MainWindow::
lineButtonTriggered()
{
scene-&
gt;setLineColor(qvariant_cast&
lt;QColor&
gt;(lineAction-&
gt;data()));
}
void
MainWindow::
handleFontChange()
{
QFont font =
fontCombo-&
gt;currentFont();
font.setPointSize(fontSizeCombo-&
gt;currentText().toInt());
font.setWeight(boldAction-&
gt;isChecked() ? QFont::
Bold : QFont::
Normal);
font.setItalic(italicAction-&
gt;isChecked());
font.setUnderline(underlineAction-&
gt;isChecked());
scene-&
gt;setFont(font);
}
void
MainWindow::
itemSelected(QGraphicsItem *
item)
{
DiagramTextItem *
textItem =
qgraphicsitem_cast&
lt;DiagramTextItem *&
gt;(item);
QFont font =
textItem-&
gt;font();
fontCombo-&
gt;setCurrentFont(font);
fontSizeCombo-&
gt;setEditText(QString().setNum(font.pointSize()));
boldAction-&
gt;setChecked(font.weight() ==
QFont::
Bold);
italicAction-&
gt;setChecked(font.italic());
underlineAction-&
gt;setChecked(font.underline());
}
void
MainWindow::
about()
{
QMessageBox::
about(this
, tr("About Diagram Scene"
),
tr("The <b>Diagram Scene</b> example shows "
"use of the graphics framework."
));
}
void
MainWindow::
createToolBox()
{
buttonGroup =
new
QButtonGroup(this
);
buttonGroup-&
gt;setExclusive(false
);
connect(buttonGroup, SIGNAL(buttonClicked(int
)),
this
, SLOT(buttonGroupClicked(int
)));
QGridLayout *
layout =
new
QGridLayout;
layout-&
gt;addWidget(createCellWidget(tr("Conditional"
), DiagramItem::
Conditional), 0
, 0
);
layout-&
gt;addWidget(createCellWidget(tr("Process"
), DiagramItem::
Step),0
, 1
);
layout-&
gt;addWidget(createCellWidget(tr("Input/Output"
), DiagramItem::
Io), 1
, 0
);
QToolButton *
textButton =
new
QToolButton;
textButton-&
gt;setCheckable(true
);
buttonGroup-&
gt;addButton(textButton, InsertTextButton);
textButton-&
gt;setIcon(QIcon(QPixmap(":/images/textpointer.png"
)));
textButton-&
gt;setIconSize(QSize(50
, 50
));
QGridLayout *
textLayout =
new
QGridLayout;
textLayout-&
gt;addWidget(textButton, 0
, 0
, Qt::
AlignHCenter);
textLayout-&
gt;addWidget(new
QLabel(tr("Text"
)), 1
, 0
, Qt::
AlignCenter);
QWidget *
textWidget =
new
QWidget;
textWidget-&
gt;setLayout(textLayout);
layout-&
gt;addWidget(textWidget, 1
, 1
);
layout-&
gt;setRowStretch(3
, 10
);
layout-&
gt;setColumnStretch(2
, 10
);
QWidget *
itemWidget =
new
QWidget;
itemWidget-&
gt;setLayout(layout);
backgroundButtonGroup =
new
QButtonGroup(this
);
connect(backgroundButtonGroup, SIGNAL(buttonClicked(QAbstractButton*
)),
this
, SLOT(backgroundButtonGroupClicked(QAbstractButton*
)));
QGridLayout *
backgroundLayout =
new
QGridLayout;
backgroundLayout-&
gt;addWidget(createBackgroundCellWidget(tr("Blue Grid"
),
":/images/background1.png"
), 0
, 0
);
backgroundLayout-&
gt;addWidget(createBackgroundCellWidget(tr("White Grid"
),
":/images/background2.png"
), 0
, 1
);
backgroundLayout-&
gt;addWidget(createBackgroundCellWidget(tr("Gray Grid"
),
":/images/background3.png"
), 1
, 0
);
backgroundLayout-&
gt;addWidget(createBackgroundCellWidget(tr("No Grid"
),
":/images/background4.png"
), 1
, 1
);
backgroundLayout-&
gt;setRowStretch(2
, 10
);
backgroundLayout-&
gt;setColumnStretch(2
, 10
);
QWidget *
backgroundWidget =
new
QWidget;
backgroundWidget-&
gt;setLayout(backgroundLayout);
toolBox =
new
QToolBox;
toolBox-&
gt;setSizePolicy(QSizePolicy(QSizePolicy::
Maximum, QSizePolicy::
Ignored));
toolBox-&
gt;setMinimumWidth(itemWidget-&
gt;sizeHint().width());
toolBox-&
gt;addItem(itemWidget, tr("Basic Flowchart Shapes"
));
toolBox-&
gt;addItem(backgroundWidget, tr("Backgrounds"
));
}
void
MainWindow::
createActions()
{
toFrontAction =
new
QAction(QIcon(":/images/bringtofront.png"
),
tr("Bring to &Front"
), this
);
toFrontAction-&
gt;setShortcut(tr("Ctrl+F"
));
toFrontAction-&
gt;setStatusTip(tr("Bring item to front"
));
connect(toFrontAction, SIGNAL(triggered()), this
, SLOT(bringToFront()));
sendBackAction =
new
QAction(QIcon(":/images/sendtoback.png"
), tr("Send to &Back"
), this
);
sendBackAction-&
gt;setShortcut(tr("Ctrl+T"
));
sendBackAction-&
gt;setStatusTip(tr("Send item to back"
));
connect(sendBackAction, SIGNAL(triggered()), this
, SLOT(sendToBack()));
deleteAction =
new
QAction(QIcon(":/images/delete.png"
), tr("&Delete"
), this
);
deleteAction-&
gt;setShortcut(tr("Delete"
));
deleteAction-&
gt;setStatusTip(tr("Delete item from diagram"
));
connect(deleteAction, SIGNAL(triggered()), this
, SLOT(deleteItem()));
exitAction =
new
QAction(tr("E&xit"
), this
);
exitAction-&
gt;setShortcuts(QKeySequence::
Quit);
exitAction-&
gt;setStatusTip(tr("Quit Scenediagram example"
));
connect(exitAction, SIGNAL(triggered()), this
, SLOT(close()));
boldAction =
new
QAction(tr("Bold"
), this
);
boldAction-&
gt;setCheckable(true
);
QPixmap pixmap(":/images/bold.png"
);
boldAction-&
gt;setIcon(QIcon(pixmap));
boldAction-&
gt;setShortcut(tr("Ctrl+B"
));
connect(boldAction, SIGNAL(triggered()), this
, SLOT(handleFontChange()));
italicAction =
new
QAction(QIcon(":/images/italic.png"
), tr("Italic"
), this
);
italicAction-&
gt;setCheckable(true
);
italicAction-&
gt;setShortcut(tr("Ctrl+I"
));
connect(italicAction, SIGNAL(triggered()), this
, SLOT(handleFontChange()));
underlineAction =
new
QAction(QIcon(":/images/underline.png"
), tr("Underline"
), this
);
underlineAction-&
gt;setCheckable(true
);
underlineAction-&
gt;setShortcut(tr("Ctrl+U"
));
connect(underlineAction, SIGNAL(triggered()), this
, SLOT(handleFontChange()));
aboutAction =
new
QAction(tr("A&bout"
), this
);
aboutAction-&
gt;setShortcut(tr("F1"
));
connect(aboutAction, SIGNAL(triggered()), this
, SLOT(about()));
}
void
MainWindow::
createMenus()
{
fileMenu =
menuBar()-&
gt;addMenu(tr("&File"
));
fileMenu-&
gt;addAction(exitAction);
itemMenu =
menuBar()-&
gt;addMenu(tr("&Item"
));
itemMenu-&
gt;addAction(deleteAction);
itemMenu-&
gt;addSeparator();
itemMenu-&
gt;addAction(toFrontAction);
itemMenu-&
gt;addAction(sendBackAction);
aboutMenu =
menuBar()-&
gt;addMenu(tr("&Help"
));
aboutMenu-&
gt;addAction(aboutAction);
}
void
MainWindow::
createToolbars()
{
editToolBar =
addToolBar(tr("Edit"
));
editToolBar-&
gt;addAction(deleteAction);
editToolBar-&
gt;addAction(toFrontAction);
editToolBar-&
gt;addAction(sendBackAction);
fontCombo =
new
QFontComboBox();
connect(fontCombo, SIGNAL(currentFontChanged(QFont)),
this
, SLOT(currentFontChanged(QFont)));
fontSizeCombo =
new
QComboBox;
fontSizeCombo-&
gt;setEditable(true
);
for
(int
i =
8
; i &
lt; 30
; i =
i +
2
)
fontSizeCombo-&
gt;addItem(QString().setNum(i));
QIntValidator *
validator =
new
QIntValidator(2
, 64
, this
);
fontSizeCombo-&
gt;setValidator(validator);
connect(fontSizeCombo, SIGNAL(currentIndexChanged(QString)),
this
, SLOT(fontSizeChanged(QString)));
fontColorToolButton =
new
QToolButton;
fontColorToolButton-&
gt;setPopupMode(QToolButton::
MenuButtonPopup);
fontColorToolButton-&
gt;setMenu(createColorMenu(SLOT(textColorChanged()), Qt::
black));
textAction =
fontColorToolButton-&
gt;menu()-&
gt;defaultAction();
fontColorToolButton-&
gt;setIcon(createColorToolButtonIcon(":/images/textpointer.png"
, Qt::
black));
fontColorToolButton-&
gt;setAutoFillBackground(true
);
connect(fontColorToolButton, SIGNAL(clicked()),
this
, SLOT(textButtonTriggered()));
fillColorToolButton =
new
QToolButton;
fillColorToolButton-&
gt;setPopupMode(QToolButton::
MenuButtonPopup);
fillColorToolButton-&
gt;setMenu(createColorMenu(SLOT(itemColorChanged()), Qt::
white));
fillAction =
fillColorToolButton-&
gt;menu()-&
gt;defaultAction();
fillColorToolButton-&
gt;setIcon(createColorToolButtonIcon(
":/images/floodfill.png"
, Qt::
white));
connect(fillColorToolButton, SIGNAL(clicked()),
this
, SLOT(fillButtonTriggered()));
lineColorToolButton =
new
QToolButton;
lineColorToolButton-&
gt;setPopupMode(QToolButton::
MenuButtonPopup);
lineColorToolButton-&
gt;setMenu(createColorMenu(SLOT(lineColorChanged()), Qt::
black));
lineAction =
lineColorToolButton-&
gt;menu()-&
gt;defaultAction();
lineColorToolButton-&
gt;setIcon(createColorToolButtonIcon(
":/images/linecolor.png"
, Qt::
black));
connect(lineColorToolButton, SIGNAL(clicked()),
this
, SLOT(lineButtonTriggered()));
textToolBar =
addToolBar(tr("Font"
));
textToolBar-&
gt;addWidget(fontCombo);
textToolBar-&
gt;addWidget(fontSizeCombo);
textToolBar-&
gt;addAction(boldAction);
textToolBar-&
gt;addAction(italicAction);
textToolBar-&
gt;addAction(underlineAction);
colorToolBar =
addToolBar(tr("Color"
));
colorToolBar-&
gt;addWidget(fontColorToolButton);
colorToolBar-&
gt;addWidget(fillColorToolButton);
colorToolBar-&
gt;addWidget(lineColorToolButton);
QToolButton *
pointerButton =
new
QToolButton;
pointerButton-&
gt;setCheckable(true
);
pointerButton-&
gt;setChecked(true
);
pointerButton-&
gt;setIcon(QIcon(":/images/pointer.png"
));
QToolButton *
linePointerButton =
new
QToolButton;
linePointerButton-&
gt;setCheckable(true
);
linePointerButton-&
gt;setIcon(QIcon(":/images/linepointer.png"
));
pointerTypeGroup =
new
QButtonGroup(this
);
pointerTypeGroup-&
gt;addButton(pointerButton, int
(DiagramScene::
MoveItem));
pointerTypeGroup-&
gt;addButton(linePointerButton, int
(DiagramScene::
InsertLine));
connect(pointerTypeGroup, SIGNAL(buttonClicked(int
)),
this
, SLOT(pointerGroupClicked(int
)));
sceneScaleCombo =
new
QComboBox;
QStringList scales;
scales &
lt;&
lt; tr("50%"
) &
lt;&
lt; tr("75%"
) &
lt;&
lt; tr("100%"
) &
lt;&
lt; tr("125%"
) &
lt;&
lt; tr("150%"
);
sceneScaleCombo-&
gt;addItems(scales);
sceneScaleCombo-&
gt;setCurrentIndex(2
);
connect(sceneScaleCombo, SIGNAL(currentIndexChanged(QString)),
this
, SLOT(sceneScaleChanged(QString)));
pointerToolbar =
addToolBar(tr("Pointer type"
));
pointerToolbar-&
gt;addWidget(pointerButton);
pointerToolbar-&
gt;addWidget(linePointerButton);
pointerToolbar-&
gt;addWidget(sceneScaleCombo);
}
QWidget *
MainWindow::
createBackgroundCellWidget(const
QString &
amp;text, const
QString &
amp;image)
{
QToolButton *
button =
new
QToolButton;
button-&
gt;setText(text);
button-&
gt;setIcon(QIcon(image));
button-&
gt;setIconSize(QSize(50
, 50
));
button-&
gt;setCheckable(true
);
backgroundButtonGroup-&
gt;addButton(button);
QGridLayout *
layout =
new
QGridLayout;
layout-&
gt;addWidget(button, 0
, 0
, Qt::
AlignHCenter);
layout-&
gt;addWidget(new
QLabel(text), 1
, 0
, Qt::
AlignCenter);
QWidget *
widget =
new
QWidget;
widget-&
gt;setLayout(layout);
return
widget;
}
QWidget *
MainWindow::
createCellWidget(const
QString &
amp;text, DiagramItem::
DiagramType type)
{
DiagramItem item(type, itemMenu);
QIcon icon(item.image());
QToolButton *
button =
new
QToolButton;
button-&
gt;setIcon(icon);
button-&
gt;setIconSize(QSize(50
, 50
));
button-&
gt;setCheckable(true
);
buttonGroup-&
gt;addButton(button, int
(type));
QGridLayout *
layout =
new
QGridLayout;
layout-&
gt;addWidget(button, 0
, 0
, Qt::
AlignHCenter);
layout-&
gt;addWidget(new
QLabel(text), 1
, 0
, Qt::
AlignCenter);
QWidget *
widget =
new
QWidget;
widget-&
gt;setLayout(layout);
return
widget;
}
QMenu *
MainWindow::
createColorMenu(const
char
*
slot, QColor defaultColor)
{
QList&
lt;QColor&
gt; colors;
colors &
lt;&
lt; Qt::
black &
lt;&
lt; Qt::
white &
lt;&
lt; Qt::
red &
lt;&
lt; Qt::
blue &
lt;&
lt; Qt::
yellow;
QStringList names;
names &
lt;&
lt; tr("black"
) &
lt;&
lt; tr("white"
) &
lt;&
lt; tr("red"
) &
lt;&
lt; tr("blue"
)
&
lt;&
lt; tr("yellow"
);
QMenu *
colorMenu =
new
QMenu(this
);
for
(int
i =
0
; i &
lt; colors.count(); ++
i) {
QAction *
action =
new
QAction(names.at(i), this
);
action-&
gt;setData(colors.at(i));
action-&
gt;setIcon(createColorIcon(colors.at(i)));
connect(action, SIGNAL(triggered()), this
, slot);
colorMenu-&
gt;addAction(action);
if
(colors.at(i) ==
defaultColor)
colorMenu-&
gt;setDefaultAction(action);
}
return
colorMenu;
}
QIcon MainWindow::
createColorToolButtonIcon(const
QString &
amp;imageFile, QColor color)
{
QPixmap pixmap(50
, 80
);
pixmap.fill(Qt::
transparent);
QPainter painter(&
amp;pixmap);
QPixmap image(imageFile);
// Draw icon centred horizontally on button.
QRect target(4
, 0
, 42
, 43
);
QRect source(0
, 0
, 42
, 43
);
painter.fillRect(QRect(0
, 60
, 50
, 80
), color);
painter.drawPixmap(target, image, source);
return
QIcon(pixmap);
}
QIcon MainWindow::
createColorIcon(QColor color)
{
QPixmap pixmap(20
, 20
);
QPainter painter(&
amp;pixmap);
painter.setPen(Qt::
NoPen);
painter.fillRect(QRect(0
, 0
, 20
, 20
), color);
return
QIcon(pixmap);
}