| 1 | /**************************************************************************** | 
|---|
| 2 | ** | 
|---|
| 3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). | 
|---|
| 4 | ** Contact: Qt Software Information (qt-info@nokia.com) | 
|---|
| 5 | ** | 
|---|
| 6 | ** This file is part of the examples of the Qt Toolkit. | 
|---|
| 7 | ** | 
|---|
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ | 
|---|
| 9 | ** Commercial Usage | 
|---|
| 10 | ** Licensees holding valid Qt Commercial licenses may use this file in | 
|---|
| 11 | ** accordance with the Qt Commercial License Agreement provided with the | 
|---|
| 12 | ** Software or, alternatively, in accordance with the terms contained in | 
|---|
| 13 | ** a written agreement between you and Nokia. | 
|---|
| 14 | ** | 
|---|
| 15 | ** GNU Lesser General Public License Usage | 
|---|
| 16 | ** Alternatively, this file may be used under the terms of the GNU Lesser | 
|---|
| 17 | ** General Public License version 2.1 as published by the Free Software | 
|---|
| 18 | ** Foundation and appearing in the file LICENSE.LGPL included in the | 
|---|
| 19 | ** packaging of this file.  Please review the following information to | 
|---|
| 20 | ** ensure the GNU Lesser General Public License version 2.1 requirements | 
|---|
| 21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | 
|---|
| 22 | ** | 
|---|
| 23 | ** In addition, as a special exception, Nokia gives you certain | 
|---|
| 24 | ** additional rights. These rights are described in the Nokia Qt LGPL | 
|---|
| 25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this | 
|---|
| 26 | ** 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 are unsure which license is appropriate for your use, please | 
|---|
| 37 | ** contact the sales department at qt-sales@nokia.com. | 
|---|
| 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 |  | 
|---|
| 50 | const int InsertTextButton = 10; | 
|---|
| 51 |  | 
|---|
| 52 | //! [0] | 
|---|
| 53 | MainWindow::MainWindow() | 
|---|
| 54 | { | 
|---|
| 55 | createActions(); | 
|---|
| 56 | createToolBox(); | 
|---|
| 57 | createMenus(); | 
|---|
| 58 |  | 
|---|
| 59 | scene = new DiagramScene(itemMenu); | 
|---|
| 60 | scene->setSceneRect(QRectF(0, 0, 5000, 5000)); | 
|---|
| 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 *))); | 
|---|
| 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")); | 
|---|
| 79 | } | 
|---|
| 80 | //! [0] | 
|---|
| 81 |  | 
|---|
| 82 | //! [1] | 
|---|
| 83 | void MainWindow::backgroundButtonGroupClicked(QAbstractButton *button) | 
|---|
| 84 | { | 
|---|
| 85 | QList<QAbstractButton *> buttons = backgroundButtonGroup->buttons(); | 
|---|
| 86 | foreach (QAbstractButton *myButton, buttons) { | 
|---|
| 87 | if (myButton != button) | 
|---|
| 88 | button->setChecked(false); | 
|---|
| 89 | } | 
|---|
| 90 | QString text = button->text(); | 
|---|
| 91 | if (text == tr("Blue Grid")) | 
|---|
| 92 | scene->setBackgroundBrush(QPixmap(":/images/background1.png")); | 
|---|
| 93 | else if (text == tr("White Grid")) | 
|---|
| 94 | scene->setBackgroundBrush(QPixmap(":/images/background2.png")); | 
|---|
| 95 | else if (text == tr("Gray Grid")) | 
|---|
| 96 | scene->setBackgroundBrush(QPixmap(":/images/background3.png")); | 
|---|
| 97 | else | 
|---|
| 98 | scene->setBackgroundBrush(QPixmap(":/images/background4.png")); | 
|---|
| 99 |  | 
|---|
| 100 | scene->update(); | 
|---|
| 101 | view->update(); | 
|---|
| 102 | } | 
|---|
| 103 | //! [1] | 
|---|
| 104 |  | 
|---|
| 105 | //! [2] | 
|---|
| 106 | void MainWindow::buttonGroupClicked(int id) | 
|---|
| 107 | { | 
|---|
| 108 | QList<QAbstractButton *> buttons = buttonGroup->buttons(); | 
|---|
| 109 | foreach (QAbstractButton *button, buttons) { | 
|---|
| 110 | if (buttonGroup->button(id) != button) | 
|---|
| 111 | button->setChecked(false); | 
|---|
| 112 | } | 
|---|
| 113 | if (id == InsertTextButton) { | 
|---|
| 114 | scene->setMode(DiagramScene::InsertText); | 
|---|
| 115 | } else { | 
|---|
| 116 | scene->setItemType(DiagramItem::DiagramType(id)); | 
|---|
| 117 | scene->setMode(DiagramScene::InsertItem); | 
|---|
| 118 | } | 
|---|
| 119 | } | 
|---|
| 120 | //! [2] | 
|---|
| 121 |  | 
|---|
| 122 | //! [3] | 
|---|
| 123 | void MainWindow::deleteItem() | 
|---|
| 124 | { | 
|---|
| 125 | foreach (QGraphicsItem *item, scene->selectedItems()) { | 
|---|
| 126 | if (item->type() == DiagramItem::Type) { | 
|---|
| 127 | qgraphicsitem_cast<DiagramItem *>(item)->removeArrows(); | 
|---|
| 128 | } | 
|---|
| 129 | scene->removeItem(item); | 
|---|
| 130 | } | 
|---|
| 131 | } | 
|---|
| 132 | //! [3] | 
|---|
| 133 |  | 
|---|
| 134 | //! [4] | 
|---|
| 135 | void MainWindow::pointerGroupClicked(int) | 
|---|
| 136 | { | 
|---|
| 137 | scene->setMode(DiagramScene::Mode(pointerTypeGroup->checkedId())); | 
|---|
| 138 | } | 
|---|
| 139 | //! [4] | 
|---|
| 140 |  | 
|---|
| 141 | //! [5] | 
|---|
| 142 | void MainWindow::bringToFront() | 
|---|
| 143 | { | 
|---|
| 144 | if (scene->selectedItems().isEmpty()) | 
|---|
| 145 | return; | 
|---|
| 146 |  | 
|---|
| 147 | QGraphicsItem *selectedItem = scene->selectedItems().first(); | 
|---|
| 148 | QList<QGraphicsItem *> overlapItems = selectedItem->collidingItems(); | 
|---|
| 149 |  | 
|---|
| 150 | qreal zValue = 0; | 
|---|
| 151 | foreach (QGraphicsItem *item, overlapItems) { | 
|---|
| 152 | if (item->zValue() >= zValue && | 
|---|
| 153 | item->type() == DiagramItem::Type) | 
|---|
| 154 | zValue = item->zValue() + 0.1; | 
|---|
| 155 | } | 
|---|
| 156 | selectedItem->setZValue(zValue); | 
|---|
| 157 | } | 
|---|
| 158 | //! [5] | 
|---|
| 159 |  | 
|---|
| 160 | //! [6] | 
|---|
| 161 | void MainWindow::sendToBack() | 
|---|
| 162 | { | 
|---|
| 163 | if (scene->selectedItems().isEmpty()) | 
|---|
| 164 | return; | 
|---|
| 165 |  | 
|---|
| 166 | QGraphicsItem *selectedItem = scene->selectedItems().first(); | 
|---|
| 167 | QList<QGraphicsItem *> overlapItems = selectedItem->collidingItems(); | 
|---|
| 168 |  | 
|---|
| 169 | qreal zValue = 0; | 
|---|
| 170 | foreach (QGraphicsItem *item, overlapItems) { | 
|---|
| 171 | if (item->zValue() <= zValue && | 
|---|
| 172 | item->type() == DiagramItem::Type) | 
|---|
| 173 | zValue = item->zValue() - 0.1; | 
|---|
| 174 | } | 
|---|
| 175 | selectedItem->setZValue(zValue); | 
|---|
| 176 | } | 
|---|
| 177 | //! [6] | 
|---|
| 178 |  | 
|---|
| 179 | //! [7] | 
|---|
| 180 | void MainWindow::itemInserted(DiagramItem *item) | 
|---|
| 181 | { | 
|---|
| 182 | pointerTypeGroup->button(int(DiagramScene::MoveItem))->setChecked(true); | 
|---|
| 183 | scene->setMode(DiagramScene::Mode(pointerTypeGroup->checkedId())); | 
|---|
| 184 | buttonGroup->button(int(item->diagramType()))->setChecked(false); | 
|---|
| 185 | } | 
|---|
| 186 | //! [7] | 
|---|
| 187 |  | 
|---|
| 188 | //! [8] | 
|---|
| 189 | void MainWindow::textInserted(QGraphicsTextItem *) | 
|---|
| 190 | { | 
|---|
| 191 | buttonGroup->button(InsertTextButton)->setChecked(false); | 
|---|
| 192 | scene->setMode(DiagramScene::Mode(pointerTypeGroup->checkedId())); | 
|---|
| 193 | } | 
|---|
| 194 | //! [8] | 
|---|
| 195 |  | 
|---|
| 196 | //! [9] | 
|---|
| 197 | void MainWindow::currentFontChanged(const QFont &) | 
|---|
| 198 | { | 
|---|
| 199 | handleFontChange(); | 
|---|
| 200 | } | 
|---|
| 201 | //! [9] | 
|---|
| 202 |  | 
|---|
| 203 | //! [10] | 
|---|
| 204 | void MainWindow::fontSizeChanged(const QString &) | 
|---|
| 205 | { | 
|---|
| 206 | handleFontChange(); | 
|---|
| 207 | } | 
|---|
| 208 | //! [10] | 
|---|
| 209 |  | 
|---|
| 210 | //! [11] | 
|---|
| 211 | void MainWindow::sceneScaleChanged(const QString &scale) | 
|---|
| 212 | { | 
|---|
| 213 | double newScale = scale.left(scale.indexOf(tr("%"))).toDouble() / 100.0; | 
|---|
| 214 | QMatrix oldMatrix = view->matrix(); | 
|---|
| 215 | view->resetMatrix(); | 
|---|
| 216 | view->translate(oldMatrix.dx(), oldMatrix.dy()); | 
|---|
| 217 | view->scale(newScale, newScale); | 
|---|
| 218 | } | 
|---|
| 219 | //! [11] | 
|---|
| 220 |  | 
|---|
| 221 | //! [12] | 
|---|
| 222 | void MainWindow::textColorChanged() | 
|---|
| 223 | { | 
|---|
| 224 | textAction = qobject_cast<QAction *>(sender()); | 
|---|
| 225 | fontColorToolButton->setIcon(createColorToolButtonIcon( | 
|---|
| 226 | ":/images/textpointer.png", | 
|---|
| 227 | qVariantValue<QColor>(textAction->data()))); | 
|---|
| 228 | textButtonTriggered(); | 
|---|
| 229 | } | 
|---|
| 230 | //! [12] | 
|---|
| 231 |  | 
|---|
| 232 | //! [13] | 
|---|
| 233 | void MainWindow::itemColorChanged() | 
|---|
| 234 | { | 
|---|
| 235 | fillAction = qobject_cast<QAction *>(sender()); | 
|---|
| 236 | fillColorToolButton->setIcon(createColorToolButtonIcon( | 
|---|
| 237 | ":/images/floodfill.png", | 
|---|
| 238 | qVariantValue<QColor>(fillAction->data()))); | 
|---|
| 239 | fillButtonTriggered(); | 
|---|
| 240 | } | 
|---|
| 241 | //! [13] | 
|---|
| 242 |  | 
|---|
| 243 | //! [14] | 
|---|
| 244 | void MainWindow::lineColorChanged() | 
|---|
| 245 | { | 
|---|
| 246 | lineAction = qobject_cast<QAction *>(sender()); | 
|---|
| 247 | lineColorToolButton->setIcon(createColorToolButtonIcon( | 
|---|
| 248 | ":/images/linecolor.png", | 
|---|
| 249 | qVariantValue<QColor>(lineAction->data()))); | 
|---|
| 250 | lineButtonTriggered(); | 
|---|
| 251 | } | 
|---|
| 252 | //! [14] | 
|---|
| 253 |  | 
|---|
| 254 | //! [15] | 
|---|
| 255 | void MainWindow::textButtonTriggered() | 
|---|
| 256 | { | 
|---|
| 257 | scene->setTextColor(qVariantValue<QColor>(textAction->data())); | 
|---|
| 258 | } | 
|---|
| 259 | //! [15] | 
|---|
| 260 |  | 
|---|
| 261 | //! [16] | 
|---|
| 262 | void MainWindow::fillButtonTriggered() | 
|---|
| 263 | { | 
|---|
| 264 | scene->setItemColor(qVariantValue<QColor>(fillAction->data())); | 
|---|
| 265 | } | 
|---|
| 266 | //! [16] | 
|---|
| 267 |  | 
|---|
| 268 | //! [17] | 
|---|
| 269 | void MainWindow::lineButtonTriggered() | 
|---|
| 270 | { | 
|---|
| 271 | scene->setLineColor(qVariantValue<QColor>(lineAction->data())); | 
|---|
| 272 | } | 
|---|
| 273 | //! [17] | 
|---|
| 274 |  | 
|---|
| 275 | //! [18] | 
|---|
| 276 | void MainWindow::handleFontChange() | 
|---|
| 277 | { | 
|---|
| 278 | QFont font = fontCombo->currentFont(); | 
|---|
| 279 | font.setPointSize(fontSizeCombo->currentText().toInt()); | 
|---|
| 280 | font.setWeight(boldAction->isChecked() ? QFont::Bold : QFont::Normal); | 
|---|
| 281 | font.setItalic(italicAction->isChecked()); | 
|---|
| 282 | font.setUnderline(underlineAction->isChecked()); | 
|---|
| 283 |  | 
|---|
| 284 | scene->setFont(font); | 
|---|
| 285 | } | 
|---|
| 286 | //! [18] | 
|---|
| 287 |  | 
|---|
| 288 | //! [19] | 
|---|
| 289 | void MainWindow::itemSelected(QGraphicsItem *item) | 
|---|
| 290 | { | 
|---|
| 291 | DiagramTextItem *textItem = | 
|---|
| 292 | qgraphicsitem_cast<DiagramTextItem *>(item); | 
|---|
| 293 |  | 
|---|
| 294 | QFont font = textItem->font(); | 
|---|
| 295 | QColor color = textItem->defaultTextColor(); | 
|---|
| 296 | fontCombo->setCurrentFont(font); | 
|---|
| 297 | fontSizeCombo->setEditText(QString().setNum(font.pointSize())); | 
|---|
| 298 | boldAction->setChecked(font.weight() == QFont::Bold); | 
|---|
| 299 | italicAction->setChecked(font.italic()); | 
|---|
| 300 | underlineAction->setChecked(font.underline()); | 
|---|
| 301 | } | 
|---|
| 302 | //! [19] | 
|---|
| 303 |  | 
|---|
| 304 | //! [20] | 
|---|
| 305 | void MainWindow::about() | 
|---|
| 306 | { | 
|---|
| 307 | QMessageBox::about(this, tr("About Diagram Scene"), | 
|---|
| 308 | tr("The <b>Diagram Scene</b> example shows " | 
|---|
| 309 | "use of the graphics framework.")); | 
|---|
| 310 | } | 
|---|
| 311 | //! [20] | 
|---|
| 312 |  | 
|---|
| 313 | //! [21] | 
|---|
| 314 | void MainWindow::createToolBox() | 
|---|
| 315 | { | 
|---|
| 316 | buttonGroup = new QButtonGroup; | 
|---|
| 317 | buttonGroup->setExclusive(false); | 
|---|
| 318 | connect(buttonGroup, SIGNAL(buttonClicked(int)), | 
|---|
| 319 | this, SLOT(buttonGroupClicked(int))); | 
|---|
| 320 | QGridLayout *layout = new QGridLayout; | 
|---|
| 321 | layout->addWidget(createCellWidget(tr("Conditional"), | 
|---|
| 322 | DiagramItem::Conditional), 0, 0); | 
|---|
| 323 | layout->addWidget(createCellWidget(tr("Process"), | 
|---|
| 324 | DiagramItem::Step),0, 1); | 
|---|
| 325 | layout->addWidget(createCellWidget(tr("Input/Output"), | 
|---|
| 326 | DiagramItem::Io), 1, 0); | 
|---|
| 327 | //! [21] | 
|---|
| 328 |  | 
|---|
| 329 | QToolButton *textButton = new QToolButton; | 
|---|
| 330 | textButton->setCheckable(true); | 
|---|
| 331 | buttonGroup->addButton(textButton, InsertTextButton); | 
|---|
| 332 | textButton->setIcon(QIcon(QPixmap(":/images/textpointer.png") | 
|---|
| 333 | .scaled(30, 30))); | 
|---|
| 334 | textButton->setIconSize(QSize(50, 50)); | 
|---|
| 335 | QGridLayout *textLayout = new QGridLayout; | 
|---|
| 336 | textLayout->addWidget(textButton, 0, 0, Qt::AlignHCenter); | 
|---|
| 337 | textLayout->addWidget(new QLabel(tr("Text")), 1, 0, Qt::AlignCenter); | 
|---|
| 338 | QWidget *textWidget = new QWidget; | 
|---|
| 339 | textWidget->setLayout(textLayout); | 
|---|
| 340 | layout->addWidget(textWidget, 1, 1); | 
|---|
| 341 |  | 
|---|
| 342 | layout->setRowStretch(3, 10); | 
|---|
| 343 | layout->setColumnStretch(2, 10); | 
|---|
| 344 |  | 
|---|
| 345 | QWidget *itemWidget = new QWidget; | 
|---|
| 346 | itemWidget->setLayout(layout); | 
|---|
| 347 |  | 
|---|
| 348 | backgroundButtonGroup = new QButtonGroup; | 
|---|
| 349 | connect(backgroundButtonGroup, SIGNAL(buttonClicked(QAbstractButton *)), | 
|---|
| 350 | this, SLOT(backgroundButtonGroupClicked(QAbstractButton *))); | 
|---|
| 351 |  | 
|---|
| 352 | QGridLayout *backgroundLayout = new QGridLayout; | 
|---|
| 353 | backgroundLayout->addWidget(createBackgroundCellWidget(tr("Blue Grid"), | 
|---|
| 354 | ":/images/background1.png"), 0, 0); | 
|---|
| 355 | backgroundLayout->addWidget(createBackgroundCellWidget(tr("White Grid"), | 
|---|
| 356 | ":/images/background2.png"), 0, 1); | 
|---|
| 357 | backgroundLayout->addWidget(createBackgroundCellWidget(tr("Gray Grid"), | 
|---|
| 358 | ":/images/background3.png"), 1, 0); | 
|---|
| 359 | backgroundLayout->addWidget(createBackgroundCellWidget(tr("No Grid"), | 
|---|
| 360 | ":/images/background4.png"), 1, 1); | 
|---|
| 361 |  | 
|---|
| 362 | backgroundLayout->setRowStretch(2, 10); | 
|---|
| 363 | backgroundLayout->setColumnStretch(2, 10); | 
|---|
| 364 |  | 
|---|
| 365 | QWidget *backgroundWidget = new QWidget; | 
|---|
| 366 | backgroundWidget->setLayout(backgroundLayout); | 
|---|
| 367 |  | 
|---|
| 368 |  | 
|---|
| 369 | //! [22] | 
|---|
| 370 | toolBox = new QToolBox; | 
|---|
| 371 | toolBox->setSizePolicy(QSizePolicy(QSizePolicy::Maximum, QSizePolicy::Ignored)); | 
|---|
| 372 | toolBox->setMinimumWidth(itemWidget->sizeHint().width()); | 
|---|
| 373 | toolBox->addItem(itemWidget, tr("Basic Flowchart Shapes")); | 
|---|
| 374 | toolBox->addItem(backgroundWidget, tr("Backgrounds")); | 
|---|
| 375 | } | 
|---|
| 376 | //! [22] | 
|---|
| 377 |  | 
|---|
| 378 | //! [23] | 
|---|
| 379 | void MainWindow::createActions() | 
|---|
| 380 | { | 
|---|
| 381 | toFrontAction = new QAction(QIcon(":/images/bringtofront.png"), | 
|---|
| 382 | tr("Bring to &Front"), this); | 
|---|
| 383 | toFrontAction->setShortcut(tr("Ctrl+F")); | 
|---|
| 384 | toFrontAction->setStatusTip(tr("Bring item to front")); | 
|---|
| 385 | connect(toFrontAction, SIGNAL(triggered()), | 
|---|
| 386 | this, SLOT(bringToFront())); | 
|---|
| 387 | //! [23] | 
|---|
| 388 |  | 
|---|
| 389 | sendBackAction = new QAction(QIcon(":/images/sendtoback.png"), | 
|---|
| 390 | tr("Send to &Back"), this); | 
|---|
| 391 | sendBackAction->setShortcut(tr("Ctrl+B")); | 
|---|
| 392 | sendBackAction->setStatusTip(tr("Send item to back")); | 
|---|
| 393 | connect(sendBackAction, SIGNAL(triggered()), | 
|---|
| 394 | this, SLOT(sendToBack())); | 
|---|
| 395 |  | 
|---|
| 396 | deleteAction = new QAction(QIcon(":/images/delete.png"), | 
|---|
| 397 | tr("&Delete"), this); | 
|---|
| 398 | deleteAction->setShortcut(tr("Delete")); | 
|---|
| 399 | deleteAction->setStatusTip(tr("Delete item from diagram")); | 
|---|
| 400 | connect(deleteAction, SIGNAL(triggered()), | 
|---|
| 401 | this, SLOT(deleteItem())); | 
|---|
| 402 |  | 
|---|
| 403 | exitAction = new QAction(tr("E&xit"), this); | 
|---|
| 404 | exitAction->setShortcut(tr("Ctrl+X")); | 
|---|
| 405 | exitAction->setStatusTip(tr("Quit Scenediagram example")); | 
|---|
| 406 | connect(exitAction, SIGNAL(triggered()), this, SLOT(close())); | 
|---|
| 407 |  | 
|---|
| 408 | boldAction = new QAction(tr("Bold"), this); | 
|---|
| 409 | boldAction->setCheckable(true); | 
|---|
| 410 | QPixmap pixmap(":/images/bold.png"); | 
|---|
| 411 | boldAction->setIcon(QIcon(pixmap)); | 
|---|
| 412 | boldAction->setShortcut(tr("Ctrl+B")); | 
|---|
| 413 | connect(boldAction, SIGNAL(triggered()), | 
|---|
| 414 | this, SLOT(handleFontChange())); | 
|---|
| 415 |  | 
|---|
| 416 | italicAction = new QAction(QIcon(":/images/italic.png"), | 
|---|
| 417 | tr("Italic"), this); | 
|---|
| 418 | italicAction->setCheckable(true); | 
|---|
| 419 | italicAction->setShortcut(tr("Ctrl+I")); | 
|---|
| 420 | connect(italicAction, SIGNAL(triggered()), | 
|---|
| 421 | this, SLOT(handleFontChange())); | 
|---|
| 422 |  | 
|---|
| 423 | underlineAction = new QAction(QIcon(":/images/underline.png"), | 
|---|
| 424 | tr("Underline"), this); | 
|---|
| 425 | underlineAction->setCheckable(true); | 
|---|
| 426 | underlineAction->setShortcut(tr("Ctrl+U")); | 
|---|
| 427 | connect(underlineAction, SIGNAL(triggered()), | 
|---|
| 428 | this, SLOT(handleFontChange())); | 
|---|
| 429 |  | 
|---|
| 430 | aboutAction = new QAction(tr("A&bout"), this); | 
|---|
| 431 | aboutAction->setShortcut(tr("Ctrl+B")); | 
|---|
| 432 | connect(aboutAction, SIGNAL(triggered()), | 
|---|
| 433 | this, SLOT(about())); | 
|---|
| 434 | } | 
|---|
| 435 |  | 
|---|
| 436 | //! [24] | 
|---|
| 437 | void MainWindow::createMenus() | 
|---|
| 438 | { | 
|---|
| 439 | fileMenu = menuBar()->addMenu(tr("&File")); | 
|---|
| 440 | fileMenu->addAction(exitAction); | 
|---|
| 441 |  | 
|---|
| 442 | itemMenu = menuBar()->addMenu(tr("&Item")); | 
|---|
| 443 | itemMenu->addAction(deleteAction); | 
|---|
| 444 | itemMenu->addSeparator(); | 
|---|
| 445 | itemMenu->addAction(toFrontAction); | 
|---|
| 446 | itemMenu->addAction(sendBackAction); | 
|---|
| 447 |  | 
|---|
| 448 | aboutMenu = menuBar()->addMenu(tr("&Help")); | 
|---|
| 449 | aboutMenu->addAction(aboutAction); | 
|---|
| 450 | } | 
|---|
| 451 | //! [24] | 
|---|
| 452 |  | 
|---|
| 453 | //! [25] | 
|---|
| 454 | void MainWindow::createToolbars() | 
|---|
| 455 | { | 
|---|
| 456 | //! [25] | 
|---|
| 457 | editToolBar = addToolBar(tr("Edit")); | 
|---|
| 458 | editToolBar->addAction(deleteAction); | 
|---|
| 459 | editToolBar->addAction(toFrontAction); | 
|---|
| 460 | editToolBar->addAction(sendBackAction); | 
|---|
| 461 |  | 
|---|
| 462 | fontCombo = new QFontComboBox(); | 
|---|
| 463 | fontSizeCombo = new QComboBox(); | 
|---|
| 464 | connect(fontCombo, SIGNAL(currentFontChanged(const QFont &)), | 
|---|
| 465 | this, SLOT(currentFontChanged(const QFont &))); | 
|---|
| 466 |  | 
|---|
| 467 | fontSizeCombo = new QComboBox; | 
|---|
| 468 | fontSizeCombo->setEditable(true); | 
|---|
| 469 | for (int i = 8; i < 30; i = i + 2) | 
|---|
| 470 | fontSizeCombo->addItem(QString().setNum(i)); | 
|---|
| 471 | QIntValidator *validator = new QIntValidator(2, 64, this); | 
|---|
| 472 | fontSizeCombo->setValidator(validator); | 
|---|
| 473 | connect(fontSizeCombo, SIGNAL(currentIndexChanged(const QString &)), | 
|---|
| 474 | this, SLOT(fontSizeChanged(const QString &))); | 
|---|
| 475 |  | 
|---|
| 476 | fontColorToolButton = new QToolButton; | 
|---|
| 477 | fontColorToolButton->setPopupMode(QToolButton::MenuButtonPopup); | 
|---|
| 478 | fontColorToolButton->setMenu(createColorMenu(SLOT(textColorChanged()), | 
|---|
| 479 | Qt::black)); | 
|---|
| 480 | textAction = fontColorToolButton->menu()->defaultAction(); | 
|---|
| 481 | fontColorToolButton->setIcon(createColorToolButtonIcon( | 
|---|
| 482 | ":/images/textpointer.png", Qt::black)); | 
|---|
| 483 | fontColorToolButton->setAutoFillBackground(true); | 
|---|
| 484 | connect(fontColorToolButton, SIGNAL(clicked()), | 
|---|
| 485 | this, SLOT(textButtonTriggered())); | 
|---|
| 486 |  | 
|---|
| 487 | //! [26] | 
|---|
| 488 | fillColorToolButton = new QToolButton; | 
|---|
| 489 | fillColorToolButton->setPopupMode(QToolButton::MenuButtonPopup); | 
|---|
| 490 | fillColorToolButton->setMenu(createColorMenu(SLOT(itemColorChanged()), | 
|---|
| 491 | Qt::white)); | 
|---|
| 492 | fillAction = fillColorToolButton->menu()->defaultAction(); | 
|---|
| 493 | fillColorToolButton->setIcon(createColorToolButtonIcon( | 
|---|
| 494 | ":/images/floodfill.png", Qt::white)); | 
|---|
| 495 | connect(fillColorToolButton, SIGNAL(clicked()), | 
|---|
| 496 | this, SLOT(fillButtonTriggered())); | 
|---|
| 497 | //! [26] | 
|---|
| 498 |  | 
|---|
| 499 | lineColorToolButton = new QToolButton; | 
|---|
| 500 | lineColorToolButton->setPopupMode(QToolButton::MenuButtonPopup); | 
|---|
| 501 | lineColorToolButton->setMenu(createColorMenu(SLOT(lineColorChanged()), | 
|---|
| 502 | Qt::black)); | 
|---|
| 503 | lineAction = lineColorToolButton->menu()->defaultAction(); | 
|---|
| 504 | lineColorToolButton->setIcon(createColorToolButtonIcon( | 
|---|
| 505 | ":/images/linecolor.png", Qt::black)); | 
|---|
| 506 | connect(lineColorToolButton, SIGNAL(clicked()), | 
|---|
| 507 | this, SLOT(lineButtonTriggered())); | 
|---|
| 508 |  | 
|---|
| 509 | textToolBar = addToolBar(tr("Font")); | 
|---|
| 510 | textToolBar->addWidget(fontCombo); | 
|---|
| 511 | textToolBar->addWidget(fontSizeCombo); | 
|---|
| 512 | textToolBar->addAction(boldAction); | 
|---|
| 513 | textToolBar->addAction(italicAction); | 
|---|
| 514 | textToolBar->addAction(underlineAction); | 
|---|
| 515 |  | 
|---|
| 516 | colorToolBar = addToolBar(tr("Color")); | 
|---|
| 517 | colorToolBar->addWidget(fontColorToolButton); | 
|---|
| 518 | colorToolBar->addWidget(fillColorToolButton); | 
|---|
| 519 | colorToolBar->addWidget(lineColorToolButton); | 
|---|
| 520 |  | 
|---|
| 521 | QToolButton *pointerButton = new QToolButton; | 
|---|
| 522 | pointerButton->setCheckable(true); | 
|---|
| 523 | pointerButton->setChecked(true); | 
|---|
| 524 | pointerButton->setIcon(QIcon(":/images/pointer.png")); | 
|---|
| 525 | QToolButton *linePointerButton = new QToolButton; | 
|---|
| 526 | linePointerButton->setCheckable(true); | 
|---|
| 527 | linePointerButton->setIcon(QIcon(":/images/linepointer.png")); | 
|---|
| 528 |  | 
|---|
| 529 | pointerTypeGroup = new QButtonGroup; | 
|---|
| 530 | pointerTypeGroup->addButton(pointerButton, int(DiagramScene::MoveItem)); | 
|---|
| 531 | pointerTypeGroup->addButton(linePointerButton, | 
|---|
| 532 | int(DiagramScene::InsertLine)); | 
|---|
| 533 | connect(pointerTypeGroup, SIGNAL(buttonClicked(int)), | 
|---|
| 534 | this, SLOT(pointerGroupClicked(int))); | 
|---|
| 535 |  | 
|---|
| 536 | sceneScaleCombo = new QComboBox; | 
|---|
| 537 | QStringList scales; | 
|---|
| 538 | scales << tr("50%") << tr("75%") << tr("100%") << tr("125%") << tr("150%"); | 
|---|
| 539 | sceneScaleCombo->addItems(scales); | 
|---|
| 540 | sceneScaleCombo->setCurrentIndex(2); | 
|---|
| 541 | connect(sceneScaleCombo, SIGNAL(currentIndexChanged(const QString &)), | 
|---|
| 542 | this, SLOT(sceneScaleChanged(const QString &))); | 
|---|
| 543 |  | 
|---|
| 544 | pointerToolbar = addToolBar(tr("Pointer type")); | 
|---|
| 545 | pointerToolbar->addWidget(pointerButton); | 
|---|
| 546 | pointerToolbar->addWidget(linePointerButton); | 
|---|
| 547 | pointerToolbar->addWidget(sceneScaleCombo); | 
|---|
| 548 | //! [27] | 
|---|
| 549 | } | 
|---|
| 550 | //! [27] | 
|---|
| 551 |  | 
|---|
| 552 | //! [28] | 
|---|
| 553 | QWidget *MainWindow::createBackgroundCellWidget(const QString &text, | 
|---|
| 554 | const QString &image) | 
|---|
| 555 | { | 
|---|
| 556 | QToolButton *button = new QToolButton; | 
|---|
| 557 | button->setText(text); | 
|---|
| 558 | button->setIcon(QIcon(image)); | 
|---|
| 559 | button->setIconSize(QSize(50, 50)); | 
|---|
| 560 | button->setCheckable(true); | 
|---|
| 561 | backgroundButtonGroup->addButton(button); | 
|---|
| 562 |  | 
|---|
| 563 | QGridLayout *layout = new QGridLayout; | 
|---|
| 564 | layout->addWidget(button, 0, 0, Qt::AlignHCenter); | 
|---|
| 565 | layout->addWidget(new QLabel(text), 1, 0, Qt::AlignCenter); | 
|---|
| 566 |  | 
|---|
| 567 | QWidget *widget = new QWidget; | 
|---|
| 568 | widget->setLayout(layout); | 
|---|
| 569 |  | 
|---|
| 570 | return widget; | 
|---|
| 571 | } | 
|---|
| 572 | //! [28] | 
|---|
| 573 |  | 
|---|
| 574 | //! [29] | 
|---|
| 575 | QWidget *MainWindow::createCellWidget(const QString &text, | 
|---|
| 576 | DiagramItem::DiagramType type) | 
|---|
| 577 | { | 
|---|
| 578 |  | 
|---|
| 579 | DiagramItem item(type, itemMenu); | 
|---|
| 580 | QIcon icon(item.image()); | 
|---|
| 581 |  | 
|---|
| 582 | QToolButton *button = new QToolButton; | 
|---|
| 583 | button->setIcon(icon); | 
|---|
| 584 | button->setIconSize(QSize(50, 50)); | 
|---|
| 585 | button->setCheckable(true); | 
|---|
| 586 | buttonGroup->addButton(button, int(type)); | 
|---|
| 587 |  | 
|---|
| 588 | QGridLayout *layout = new QGridLayout; | 
|---|
| 589 | layout->addWidget(button, 0, 0, Qt::AlignHCenter); | 
|---|
| 590 | layout->addWidget(new QLabel(text), 1, 0, Qt::AlignCenter); | 
|---|
| 591 |  | 
|---|
| 592 | QWidget *widget = new QWidget; | 
|---|
| 593 | widget->setLayout(layout); | 
|---|
| 594 |  | 
|---|
| 595 | return widget; | 
|---|
| 596 | } | 
|---|
| 597 | //! [29] | 
|---|
| 598 |  | 
|---|
| 599 | //! [30] | 
|---|
| 600 | QMenu *MainWindow::createColorMenu(const char *slot, QColor defaultColor) | 
|---|
| 601 | { | 
|---|
| 602 | QList<QColor> colors; | 
|---|
| 603 | colors << Qt::black << Qt::white << Qt::red << Qt::blue << Qt::yellow; | 
|---|
| 604 | QStringList names; | 
|---|
| 605 | names << tr("black") << tr("white") << tr("red") << tr("blue") | 
|---|
| 606 | << tr("yellow"); | 
|---|
| 607 |  | 
|---|
| 608 | QMenu *colorMenu = new QMenu; | 
|---|
| 609 | for (int i = 0; i < colors.count(); ++i) { | 
|---|
| 610 | QAction *action = new QAction(names.at(i), this); | 
|---|
| 611 | action->setData(colors.at(i)); | 
|---|
| 612 | action->setIcon(createColorIcon(colors.at(i))); | 
|---|
| 613 | connect(action, SIGNAL(triggered()), | 
|---|
| 614 | this, slot); | 
|---|
| 615 | colorMenu->addAction(action); | 
|---|
| 616 | if (colors.at(i) == defaultColor) { | 
|---|
| 617 | colorMenu->setDefaultAction(action); | 
|---|
| 618 | } | 
|---|
| 619 | } | 
|---|
| 620 | return colorMenu; | 
|---|
| 621 | } | 
|---|
| 622 | //! [30] | 
|---|
| 623 |  | 
|---|
| 624 | //! [31] | 
|---|
| 625 | QIcon MainWindow::createColorToolButtonIcon(const QString &imageFile, | 
|---|
| 626 | QColor color) | 
|---|
| 627 | { | 
|---|
| 628 | QPixmap pixmap(50, 80); | 
|---|
| 629 | pixmap.fill(Qt::transparent); | 
|---|
| 630 | QPainter painter(&pixmap); | 
|---|
| 631 | QPixmap image(imageFile); | 
|---|
| 632 | QRect target(0, 0, 50, 60); | 
|---|
| 633 | QRect source(0, 0, 42, 42); | 
|---|
| 634 | painter.fillRect(QRect(0, 60, 50, 80), color); | 
|---|
| 635 | painter.drawPixmap(target, image, source); | 
|---|
| 636 |  | 
|---|
| 637 | return QIcon(pixmap); | 
|---|
| 638 | } | 
|---|
| 639 | //! [31] | 
|---|
| 640 |  | 
|---|
| 641 | //! [32] | 
|---|
| 642 | QIcon MainWindow::createColorIcon(QColor color) | 
|---|
| 643 | { | 
|---|
| 644 | QPixmap pixmap(20, 20); | 
|---|
| 645 | QPainter painter(&pixmap); | 
|---|
| 646 | painter.setPen(Qt::NoPen); | 
|---|
| 647 | painter.fillRect(QRect(0, 0, 20, 20), color); | 
|---|
| 648 |  | 
|---|
| 649 | return QIcon(pixmap); | 
|---|
| 650 | } | 
|---|
| 651 | //! [32] | 
|---|