/****************************************************************************
**
** 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() {
float f = 4334261027;
auto f1 = f;
createActions();
createToolBox();
createMenus();
scene = new DiagramScene(itemMenu, this);
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(arrowInserted()),
this, SLOT(backupUndostack()));
connect(scene, SIGNAL(textChanged()),
this, SLOT(backupUndostack()));
connect(scene, SIGNAL(itemSelected(QGraphicsItem*)),
this, SLOT(itemSelected(QGraphicsItem*)));
connect(scene, SIGNAL(scaleChanging(int)),
this, SLOT(sceneScaleZooming(int)));
createToolbars();
QHBoxLayout *layout = new QHBoxLayout;
layout->addWidget(toolBox);
view = new DiagramView(scene);
layout->addWidget(view);
connect(view, SIGNAL(needsUndoBackup()), this, SLOT(backupUndostack()));
QWidget *widget = new QWidget;
widget->setLayout(layout);
setCentralWidget(widget);
setWindowTitle(tr("Diagramscene"));
setUnifiedTitleAndToolBarOnMac(true);
undoStack.backup(QList<QGraphicsItem*>());
}
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();
QAbstractButton* clickedButton = buttonGroup->button(id);
// set other button unchecked
foreach (QAbstractButton *button, buttons) {
if (clickedButton != button)
button->setChecked(false);
}
// simply set objButton unchecked if already checked
if (!clickedButton->isChecked()) {
scene->setMode(DiagramScene::Mode(pointerTypeGroup->checkedId()));
return;
}
if (id == InsertTextButton) {
scene->setMode(DiagramScene::InsertText);
} else {
scene->setItemType(DiagramItem::DiagramType(id));
scene->setMode(DiagramScene::InsertItem);
}
}
void MainWindow::copyItem() {
foreach(QGraphicsItem* p, pasteBoard) {
delete p;
}
pasteBoard = cloneItems(scene->selectedItems());
qDebug() << pasteBoard.size();
}
void MainWindow::pasteItem() {
QList<QGraphicsItem*> pasteBoardCopy(cloneItems(pasteBoard));
foreach(QGraphicsItem* p, scene->items()) p->setSelected(false);
foreach(QGraphicsItem* item, pasteBoard) {
if (item->type() != Arrow::Type) {
item->setPos(item->scenePos() + QPointF(20, 20));
item->setZValue(item->zValue() + 0.1); // raise a little bit
}
scene->addItem(item);
item->setSelected(true);
}
pasteBoard.swap(pasteBoardCopy);
if (!scene->selectedItems().empty())
undoStack.backup(cloneItems(scene->items()));
}
void MainWindow::cutItem() {
copyItem();
deleteItem();
}
void MainWindow::deleteItem() {
bool needsBackup = !scene->selectedItems().empty();
scene->deleteItems(scene->selectedItems());
if (needsBackup)
undoStack.backup(cloneItems(scene->items()));
}
void MainWindow::undo() {
if (undoStack.isEmpty()) return;
// sweep away all items
scene->deleteItems(scene->items());
QList<QGraphicsItem*> undoneItems = cloneItems(undoStack.undo());
foreach(QGraphicsItem* item, undoneItems) {
qDebug() << item << "--------- add item";
scene->addItem(item);
}
// update arrows
foreach(QGraphicsItem* item, undoneItems) {
if (item->type() == Arrow::Type)
qgraphicsitem_cast<Arrow*>(item)->updatePosition();
}
}
void MainWindow::redo() {
if (undoStack.isFull()) return;
scene->deleteItems(scene->items());
QList<QGraphicsItem*> redoneItems = cloneItems(undoStack.redo());
foreach(QGraphicsItem* item, redoneItems) {
scene->addItem(item);
}
// update arrows
foreach(QGraphicsItem* item, redoneItems) {
if (item->type() == Arrow::Type)
qgraphicsitem_cast<Arrow*>(item)->updatePosition();
}
}
void MainWindow::groupItems() {
QGraphicsItemGroup* group = scene->createItemGroup(scene->selectedItems());
group->setFlag(QGraphicsItem::ItemIsMovable, true);
group->setFlag(QGraphicsItem::ItemIsSelectable, true);
group->setFlag(QGraphicsItem::ItemSendsGeometryChanges, true)