📄 mainwindow.cpp
字号:
/******************************************************************************** Copyright (C) 2007-2007 Trolltech ASA. All rights reserved.**** This file is part of the example classes of the Qt Toolkit.**** This file may be used under the terms of the GNU General Public** License version 2.0 as published by the Free Software Foundation** and appearing in the file LICENSE.GPL included in the packaging of** this file. Please review the following information to ensure GNU** General Public Licensing requirements will be met:** http://trolltech.com/products/qt/licenses/licensing/opensource/**** If you are unsure which license is appropriate for your use, please** review the following information:** http://trolltech.com/products/qt/licenses/licensing/licensingoverview** or contact the sales department at sales@trolltech.com.**** In addition, as a special exception, Trolltech gives you certain** additional rights. These rights are described in the Trolltech GPL** Exception version 1.0, which can be found at** http://www.trolltech.com/products/qt/gplexception/ and in the file** GPL_EXCEPTION.txt in this package.**** In addition, as a special exception, Trolltech, as the sole copyright** holder for Qt Designer, grants users of the Qt/Eclipse Integration** plug-in the right for the Qt/Eclipse Integration to link to** functionality provided by Qt Designer and its related libraries.**** Trolltech reserves all rights not expressly granted herein.**** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.******************************************************************************/#include <QtGui>#include "mainwindow.h"#include "diagramitem.h"#include "diagramscene.h"#include "diagramtextitem.h"const int InsertTextButton = 10;MainWindow::MainWindow(){ createActions(); createToolBox(); createMenus(); scene = new DiagramScene(itemMenu); scene->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->addWidget(toolBox); view = new QGraphicsView(scene); layout->addWidget(view); QWidget *widget = new QWidget; widget->setLayout(layout); setCentralWidget(widget); setWindowTitle(tr("Diagramscene"));}void MainWindow::backgroundButtonGroupClicked(QAbstractButton *button){ QList<QAbstractButton *> buttons = backgroundButtonGroup->buttons(); foreach (QAbstractButton *myButton, buttons) { if (myButton != button) button->setChecked(false); } QString text = button->text(); if (text == tr("Blue Grid")) scene->setBackgroundBrush(QPixmap(":/images/background1.png")); else if (text == tr("White Grid")) scene->setBackgroundBrush(QPixmap(":/images/background2.png")); else if (text == tr("Gray Grid")) scene->setBackgroundBrush(QPixmap(":/images/background3.png")); else scene->setBackgroundBrush(QPixmap(":/images/background4.png")); scene->update(); view->update();}void MainWindow::buttonGroupClicked(int id){ QList<QAbstractButton *> buttons = buttonGroup->buttons(); foreach (QAbstractButton *button, buttons) { if (buttonGroup->button(id) != button) button->setChecked(false); } if (id == InsertTextButton) { scene->setMode(DiagramScene::InsertText); } else { scene->setItemType(DiagramItem::DiagramType(id)); scene->setMode(DiagramScene::InsertItem); }}void MainWindow::deleteItem(){ foreach (QGraphicsItem *item, scene->selectedItems()) { if (item->type() == DiagramItem::Type) { qgraphicsitem_cast<DiagramItem *>(item)->removeArrows(); } scene->removeItem(item); }}void MainWindow::pointerGroupClicked(int){ scene->setMode(DiagramScene::Mode(pointerTypeGroup->checkedId()));}void MainWindow::bringToFront(){ if (scene->selectedItems().isEmpty()) return; QGraphicsItem *selectedItem = scene->selectedItems().first(); QList<QGraphicsItem *> overlapItems = selectedItem->collidingItems(); qreal zValue = 0; foreach (QGraphicsItem *item, overlapItems) { if (item->zValue() >= zValue && item->type() == DiagramItem::Type) zValue = item->zValue() + 0.1; } selectedItem->setZValue(zValue);}void MainWindow::sendToBack(){ if (scene->selectedItems().isEmpty()) return; QGraphicsItem *selectedItem = scene->selectedItems().first(); QList<QGraphicsItem *> overlapItems = selectedItem->collidingItems(); qreal zValue = 0; foreach (QGraphicsItem *item, overlapItems) { if (item->zValue() <= zValue && item->type() == DiagramItem::Type) zValue = item->zValue() - 0.1; } selectedItem->setZValue(zValue);}void MainWindow::itemInserted(DiagramItem *item){ scene->setMode(DiagramScene::Mode(pointerTypeGroup->checkedId())); buttonGroup->button(int(item->diagramType()))->setChecked(false);}void MainWindow::textInserted(QGraphicsTextItem *){ buttonGroup->button(InsertTextButton)->setChecked(false); scene->setMode(DiagramScene::Mode(pointerTypeGroup->checkedId()));}void MainWindow::currentFontChanged(const QFont &){ handleFontChange();}void MainWindow::fontSizeChanged(const QString &){ handleFontChange();}void MainWindow::sceneScaleChanged(const QString &scale){ double newScale = scale.left(scale.indexOf(tr("%"))).toDouble() / 100.0; QMatrix oldMatrix = view->matrix(); view->resetMatrix(); view->translate(oldMatrix.dx(), oldMatrix.dy()); view->scale(newScale, newScale);}void MainWindow::textColorChanged(){ textAction = qobject_cast<QAction *>(sender()); fontColorToolButton->setIcon(createColorToolButtonIcon( ":/images/textpointer.png", qVariantValue<QColor>(textAction->data()))); textButtonTriggered();}void MainWindow::itemColorChanged(){ fillAction = qobject_cast<QAction *>(sender()); fillColorToolButton->setIcon(createColorToolButtonIcon( ":/images/floodfill.png", qVariantValue<QColor>(fillAction->data()))); fillButtonTriggered();}void MainWindow::lineColorChanged(){ lineAction = qobject_cast<QAction *>(sender()); lineColorToolButton->setIcon(createColorToolButtonIcon( ":/images/linecolor.png", qVariantValue<QColor>(lineAction->data()))); lineButtonTriggered();}void MainWindow::textButtonTriggered(){ scene->setTextColor(qVariantValue<QColor>(textAction->data()));}void MainWindow::fillButtonTriggered(){ scene->setItemColor(qVariantValue<QColor>(fillAction->data()));}void MainWindow::lineButtonTriggered(){ scene->setLineColor(qVariantValue<QColor>(lineAction->data()));}void MainWindow::handleFontChange(){ QFont font = fontCombo->currentFont(); font.setPointSize(fontSizeCombo->currentText().toInt()); font.setWeight(boldAction->isChecked() ? QFont::Bold : QFont::Normal); font.setItalic(italicAction->isChecked()); font.setUnderline(underlineAction->isChecked()); scene->setFont(font);}void MainWindow::itemSelected(QGraphicsItem *item){ DiagramTextItem *textItem = qgraphicsitem_cast<DiagramTextItem *>(item); QFont font = textItem->font(); QColor color = textItem->defaultTextColor(); fontCombo->setCurrentFont(font); fontSizeCombo->setEditText(QString().setNum(font.pointSize())); boldAction->setChecked(font.weight() == QFont::Bold); italicAction->setChecked(font.italic()); underlineAction->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; buttonGroup->setExclusive(false); connect(buttonGroup, SIGNAL(buttonClicked(int)), this, SLOT(buttonGroupClicked(int))); QGridLayout *layout = new QGridLayout; layout->addWidget(createCellWidget(tr("Conditional"), DiagramItem::Conditional), 0, 0); layout->addWidget(createCellWidget(tr("Proccess"), DiagramItem::Step),0, 1); layout->addWidget(createCellWidget(tr("Input/Output"), DiagramItem::Io), 1, 0); QToolButton *textButton = new QToolButton; textButton->setCheckable(true); buttonGroup->addButton(textButton, InsertTextButton); textButton->setIcon(QIcon(QPixmap(":/images/textpointer.png") .scaled(30, 30))); textButton->setIconSize(QSize(50, 50)); QGridLayout *textLayout = new QGridLayout; textLayout->addWidget(textButton, 0, 0, Qt::AlignHCenter); textLayout->addWidget(new QLabel(tr("Text")), 1, 0, Qt::AlignCenter); QWidget *textWidget = new QWidget; textWidget->setLayout(textLayout); layout->addWidget(textWidget, 1, 1);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -