source: trunk/examples/graphicsview/diagramscene/mainwindow.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: 20.7 KB
RevLine 
[2]1/****************************************************************************
2**
[651]3** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
[561]4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
[2]6**
7** This file is part of the examples 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**
[561]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.
[2]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**
[561]36** If you have questions regarding the use of this file, please contact
37** Nokia at [email protected].
[2]38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include <QtGui>
43#include <QLabel>
44
45#include "mainwindow.h"
46#include "diagramitem.h"
47#include "diagramscene.h"
48#include "diagramtextitem.h"
49
50const int InsertTextButton = 10;
51
52//! [0]
53MainWindow::MainWindow()
54{
55 createActions();
56 createToolBox();
57 createMenus();
58
59 scene = new DiagramScene(itemMenu);
60 scene->setSceneRect(QRectF(0, 0, 5000, 5000));
[561]61 connect(scene, SIGNAL(itemInserted(DiagramItem*)),
62 this, SLOT(itemInserted(DiagramItem*)));
63 connect(scene, SIGNAL(textInserted(QGraphicsTextItem*)),
64 this, SLOT(textInserted(QGraphicsTextItem*)));
65 connect(scene, SIGNAL(itemSelected(QGraphicsItem*)),
66 this, SLOT(itemSelected(QGraphicsItem*)));
[2]67 createToolbars();
68
69 QHBoxLayout *layout = new QHBoxLayout;
70 layout->addWidget(toolBox);
71 view = new QGraphicsView(scene);
72 layout->addWidget(view);
73
74 QWidget *widget = new QWidget;
75 widget->setLayout(layout);
76
77 setCentralWidget(widget);
78 setWindowTitle(tr("Diagramscene"));
[561]79 setUnifiedTitleAndToolBarOnMac(true);
[2]80}
81//! [0]
82
83//! [1]
84void MainWindow::backgroundButtonGroupClicked(QAbstractButton *button)
85{
86 QList<QAbstractButton *> buttons = backgroundButtonGroup->buttons();
87 foreach (QAbstractButton *myButton, buttons) {
88 if (myButton != button)
89 button->setChecked(false);
90 }
91 QString text = button->text();
92 if (text == tr("Blue Grid"))
93 scene->setBackgroundBrush(QPixmap(":/images/background1.png"));
94 else if (text == tr("White Grid"))
95 scene->setBackgroundBrush(QPixmap(":/images/background2.png"));
96 else if (text == tr("Gray Grid"))
97 scene->setBackgroundBrush(QPixmap(":/images/background3.png"));
98 else
99 scene->setBackgroundBrush(QPixmap(":/images/background4.png"));
100
101 scene->update();
102 view->update();
103}
104//! [1]
105
106//! [2]
107void MainWindow::buttonGroupClicked(int id)
108{
109 QList<QAbstractButton *> buttons = buttonGroup->buttons();
110 foreach (QAbstractButton *button, buttons) {
111 if (buttonGroup->button(id) != button)
112 button->setChecked(false);
113 }
114 if (id == InsertTextButton) {
115 scene->setMode(DiagramScene::InsertText);
116 } else {
117 scene->setItemType(DiagramItem::DiagramType(id));
118 scene->setMode(DiagramScene::InsertItem);
119 }
120}
121//! [2]
122
123//! [3]
124void MainWindow::deleteItem()
125{
126 foreach (QGraphicsItem *item, scene->selectedItems()) {
127 if (item->type() == DiagramItem::Type) {
128 qgraphicsitem_cast<DiagramItem *>(item)->removeArrows();
129 }
130 scene->removeItem(item);
131 }
132}
133//! [3]
134
135//! [4]
136void MainWindow::pointerGroupClicked(int)
137{
138 scene->setMode(DiagramScene::Mode(pointerTypeGroup->checkedId()));
139}
140//! [4]
141
142//! [5]
143void MainWindow::bringToFront()
144{
145 if (scene->selectedItems().isEmpty())
146 return;
147
148 QGraphicsItem *selectedItem = scene->selectedItems().first();
149 QList<QGraphicsItem *> overlapItems = selectedItem->collidingItems();
150
151 qreal zValue = 0;
152 foreach (QGraphicsItem *item, overlapItems) {
153 if (item->zValue() >= zValue &&
154 item->type() == DiagramItem::Type)
155 zValue = item->zValue() + 0.1;
156 }
157 selectedItem->setZValue(zValue);
158}
159//! [5]
160
161//! [6]
162void MainWindow::sendToBack()
163{
164 if (scene->selectedItems().isEmpty())
165 return;
166
167 QGraphicsItem *selectedItem = scene->selectedItems().first();
168 QList<QGraphicsItem *> overlapItems = selectedItem->collidingItems();
169
170 qreal zValue = 0;
171 foreach (QGraphicsItem *item, overlapItems) {
172 if (item->zValue() <= zValue &&
173 item->type() == DiagramItem::Type)
174 zValue = item->zValue() - 0.1;
175 }
176 selectedItem->setZValue(zValue);
177}
178//! [6]
179
180//! [7]
181void MainWindow::itemInserted(DiagramItem *item)
182{
183 pointerTypeGroup->button(int(DiagramScene::MoveItem))->setChecked(true);
184 scene->setMode(DiagramScene::Mode(pointerTypeGroup->checkedId()));
185 buttonGroup->button(int(item->diagramType()))->setChecked(false);
186}
187//! [7]
188
189//! [8]
190void MainWindow::textInserted(QGraphicsTextItem *)
191{
192 buttonGroup->button(InsertTextButton)->setChecked(false);
193 scene->setMode(DiagramScene::Mode(pointerTypeGroup->checkedId()));
194}
195//! [8]
196
197//! [9]
198void MainWindow::currentFontChanged(const QFont &)
199{
200 handleFontChange();
201}
202//! [9]
203
204//! [10]
205void MainWindow::fontSizeChanged(const QString &)
206{
207 handleFontChange();
208}
209//! [10]
210
211//! [11]
212void MainWindow::sceneScaleChanged(const QString &scale)
213{
214 double newScale = scale.left(scale.indexOf(tr("%"))).toDouble() / 100.0;
215 QMatrix oldMatrix = view->matrix();
216 view->resetMatrix();
217 view->translate(oldMatrix.dx(), oldMatrix.dy());
218 view->scale(newScale, newScale);
219}
220//! [11]
221
222//! [12]
223void MainWindow::textColorChanged()
224{
225 textAction = qobject_cast<QAction *>(sender());
226 fontColorToolButton->setIcon(createColorToolButtonIcon(
227 ":/images/textpointer.png",
228 qVariantValue<QColor>(textAction->data())));
229 textButtonTriggered();
230}
231//! [12]
232
233//! [13]
234void MainWindow::itemColorChanged()
235{
236 fillAction = qobject_cast<QAction *>(sender());
237 fillColorToolButton->setIcon(createColorToolButtonIcon(
238 ":/images/floodfill.png",
239 qVariantValue<QColor>(fillAction->data())));
240 fillButtonTriggered();
241}
242//! [13]
243
244//! [14]
245void MainWindow::lineColorChanged()
246{
247 lineAction = qobject_cast<QAction *>(sender());
248 lineColorToolButton->setIcon(createColorToolButtonIcon(
249 ":/images/linecolor.png",
250 qVariantValue<QColor>(lineAction->data())));
251 lineButtonTriggered();
252}
253//! [14]
254
255//! [15]
256void MainWindow::textButtonTriggered()
257{
258 scene->setTextColor(qVariantValue<QColor>(textAction->data()));
259}
260//! [15]
261
262//! [16]
263void MainWindow::fillButtonTriggered()
264{
265 scene->setItemColor(qVariantValue<QColor>(fillAction->data()));
266}
267//! [16]
268
269//! [17]
270void MainWindow::lineButtonTriggered()
271{
272 scene->setLineColor(qVariantValue<QColor>(lineAction->data()));
273}
274//! [17]
275
276//! [18]
277void MainWindow::handleFontChange()
278{
279 QFont font = fontCombo->currentFont();
280 font.setPointSize(fontSizeCombo->currentText().toInt());