#include <QGraphicsScene>
#include <QGraphicsView>
#include <QColorDialog>
#include <QMenu>
#include <QClipboard>
#include <QMenuBar>
#include <QToolBar>
#include "diagramwindow.h"
#include "link.h"
#include "node.h"
#include "propertiesdialog.h"
DiagramWindow::DiagramWindow()
{
scene = new QGraphicsScene(0, 0, 600, 500);
view = new QGraphicsView;
view->setScene(scene);
view->setDragMode(QGraphicsView::RubberBandDrag);
view->setRenderHints(QPainter::Antialiasing
| QPainter::TextAntialiasing);
view->setContextMenuPolicy(Qt::ActionsContextMenu);
setCentralWidget(view);
minZ = 0;
maxZ = 0;
seqNumber = 0;
createActions();
createMenus();
createToolBars();
connect(scene, SIGNAL(selectionChanged()),
this, SLOT(updateActions()));
setWindowTitle(tr("Diagram"));
updateActions();
}
void DiagramWindow::addNode()
{
Node *node = new Node;
node->setText(tr("Node %1").arg(seqNumber + 1));
setupNode(node);
}
void DiagramWindow::addLink()
{
NodePair nodes = selectedNodePair();
if (nodes == NodePair())
return;
Link *link = new Link(nodes.first, nodes.second);
scene->addItem(link);
}
void DiagramWindow::del()
{
QList<QGraphicsItem *> items = scene->selectedItems();
QMutableListIterator<QGraphicsItem *> i(items);
while (i.hasNext())
{
Link *link = dynamic_cast<Link *>(i.next());
if (link)
{
delete link;
i.remove();
}
}
qDeleteAll(items);
}
void DiagramWindow::cut()
{
Node *node = selectedNode();
if (!node)
return;
copy();
delete node;
}
void DiagramWindow::copy()
{
Node *node = selectedNode();
if (!node)
return;
QString str = QString("Node %1 %2 %3 %4")
.arg(node->textColor().name())
.arg(node->outlineColor().name())
.arg(node->backgroundColor().name())
.arg(node->text());
QApplication::clipboard()->setText(str);
}
void DiagramWindow::paste()
{
QString str = QApplication::clipboard()->text();
QStringList parts = str.split(" ");
if (parts.count() >= 5 && parts.first() == "Node")
{
Node *node = new Node;
node->setText(QStringList(parts.mid(4)).join(" "));
node->setTextColor(QColor(parts[1]));
node->setOutlineColor(QColor(parts[2]));
node->setBackgroundColor(QColor(parts[3]));
setupNode(node);
}
}
void DiagramWindow::bringToFront()
{
++maxZ;
setZValue(maxZ);
}
void DiagramWindow::sendToBack()
{
--minZ;
setZValue(minZ);
}
void DiagramWindow::properties()
{
Node *node = selectedNode();
Link *link = selectedLink();
if (node)
{
PropertiesDialog dialog(node, this);
dialog.exec();
}
else if (link)
{
QColor color = QColorDialog::getColor(link->color(), this);
if (color.isValid())
link->setColor(color);
}
}
void DiagramWindow::updateActions()
{
bool hasSelection = !scene->selectedItems().isEmpty();
bool isNode = (selectedNode() != 0);
bool isNodePair = (selectedNodePair() != NodePair());
cutAction->setEnabled(isNode);
copyAction->setEnabled(isNode);
addLinkAction->setEnabled(isNodePair);
deleteAction->setEnabled(hasSelection);
bringToFrontAction->setEnabled(isNode);
sendToBackAction->setEnabled(isNode);
propertiesAction->setEnabled(isNode);
foreach (QAction *action, view->actions())
view->removeAction(action);
foreach (QAction *action, editMenu->actions())
{
if (action->isEnabled())
view->addAction(action);
}
}
void DiagramWindow::createActions()
{
exitAction = new QAction(tr("E&xit"), this);
exitAction->setShortcut(tr("Ctrl+Q"));
connect(exitAction, SIGNAL(triggered()), this, SLOT(close()));
addNodeAction = new QAction(tr("Add &Node"), this);
addNodeAction->setIcon(QIcon(":/images/node.png"));
addNodeAction->setShortcut(tr("Ctrl+N"));
connect(addNodeAction, SIGNAL(triggered()), this, SLOT(addNode()));
addLinkAction = new QAction(tr("Add &Link"), this);
addLinkAction->setIcon(QIcon(":/images/link.png"));
addLinkAction->setShortcut(tr("Ctrl+L"));
connect(addLinkAction, SIGNAL(triggered()), this, SLOT(addLink()));
deleteAction = new QAction(tr("&Delete"), this);
deleteAction->setIcon(QIcon(":/images/delete.png"));
deleteAction->setShortcut(tr("Del"));
connect(deleteAction, SIGNAL(triggered()), this, SLOT(del()));
cutAction = new QAction(tr("Cu&t"), this);
cutAction->setIcon(QIcon(":/images/cut.png"));
cutAction->setShortcut(tr("Ctrl+X"));
connect(cutAction, SIGNAL(triggered()), this, SLOT(cut()));
copyAction = new QAction(tr("&Copy"), this);
copyAction->setIcon(QIcon(":/images/copy.png"));
copyAction->setShortcut(tr("Ctrl+C"));
connect(copyAction, SIGNAL(triggered()), this, SLOT(copy()));
pasteAction = new QAction(tr("&Paste"), this);
pasteAction->setIcon(QIcon(":/images/paste.png"));
pasteAction->setShortcut(tr("Ctrl+V"));
connect(pasteAction, SIGNAL(triggered()), this, SLOT(paste()));
bringToFrontAction = new QAction(tr("Bring to &Front"), this);
bringToFrontAction->setIcon(QIcon(":/images/bringtofront.png"));
connect(bringToFrontAction, SIGNAL(triggered()),
this, SLOT(bringToFront()));
sendToBackAction = new QAction(tr("&Send to Back"), this);
sendToBackAction->setIcon(QIcon(":/images/sendtoback.png"));
connect(sendToBackAction, SIGNAL(triggered()),
this, SLOT(sendToBack()));
propertiesAction = new QAction(tr("P&roperties..."), this);
connect(propertiesAction, SIGNAL(triggered()),
this, SLOT(properties()));
}
void DiagramWindow::createMenus()
{
fileMenu = menuBar()->addMenu(tr("&File"));
fileMenu->addAction(exitAction);
editMenu = menuBar()->addMenu(tr("&Edit"));
editMenu->addAction(addNodeAction);
editMenu->addAction(addLinkAction);
editMenu->addAction(deleteAction);
editMenu->addSeparator();
editMenu->addAction(cutAction);
editMenu->addAction(copyAction);
editMenu->addAction(pasteAction);
editMenu->addSeparator();
editMenu->addAction(bringToFrontAction);
editMenu->addAction(sendToBackAction);
editMenu->addSeparator();
editMenu->addAction(propertiesAction);
}
void DiagramWindow::createToolBars()
{
editToolBar = addToolBar(tr("Edit"));
editToolBar->addAction(addNodeAction);
editToolBar->addAction(addLinkAction);
editToolBar->addAction(deleteAction);
editToolBar->addSeparator();
editToolBar->addAction(cutAction);
editToolBar->addAction(copyAction);
editToolBar->addAction(pasteAction);
editToolBar->addSeparator();
editToolBar->addAction(bringToFrontAction);
editToolBar->addAction(sendToBackAction);
}
void DiagramWindow::setZValue(int z)
{
Node *node = selectedNode();
if (node)
node->setZValue(z);
}
void DiagramWindow::setupNode(Node *node)
{
node->setPos(QPoint(80 + (100 * (seqNumber % 5)),
80 + (50 * ((seqNumber / 5) % 7))));
scene->addItem(node);
++seqNumber;
scene->clearSelection();
node->setSelected(true);
bringToFront();
}
Node *DiagramWindow::selectedNode() const
{
QList<QGraphicsItem *> items = scene->selectedItems();
if (items.count() == 1)
{
return dynamic_cast<Node *>(items.first());
}
else
{
return 0;
}
}
Link *DiagramWindow::selectedLink() const
{
QList<QGraphicsItem *> items = scene->selectedItems();
if (items.count() == 1)
{
return dynamic_cast<Link *>(items.first());
}
else
{
return 0;
}
}
DiagramWindow::NodePair DiagramWindow::selectedNodePair() const
{
QList<QGraphicsItem *> items = scene->selectedItems();
if (items.coun
没有合适的资源?快使用搜索试试~ 我知道了~
qt 绘制流程图示例程序——Diagram
共22个文件
png:9个
cpp:5个
h:4个
2星 需积分: 47 276 下载量 167 浏览量
2019-11-03
07:35:59
上传
评论 11
收藏 16KB RAR 举报
温馨提示
qt 绘制流程图示例程序,实现了节点的添加,以及节点之间连续操作
资源推荐
资源详情
资源评论
收起资源包目录
Diagram.rar (22个子文件)
Diagram
link.cpp 2KB
diagramwindow.h 1KB
images
cut.png 537B
copy.png 485B
link.png 208B
node.png 212B
bringtofront.png 293B
colors.png 1KB
sendtoback.png 318B
paste.png 907B
delete.png 833B
node.h 1KB
node.cpp 3KB
propertiesdialog.h 686B
propertiesdialog.ui 8KB
main.cpp 189B
propertiesdialog.cpp 2KB
link.h 599B
diagramwindow.cpp 8KB
Diagram.pro.user 43KB
Diagram.pro 1KB
resources.qrc 353B
共 22 条
- 1
资源评论
- JeffChong01252024-02-23是真的毛用没有,根本就不是预览图那样 #毫无价值
- qq_447306362021-04-16关键点就是连线那块,但这块作者做的属实不行
- lanhaitun2020-10-25骗子,积分花了,就只有几行没有用的代码_东门吹雪2020-12-31什么才叫有用呢,直接拿过来用吗?你想的也太天真的,说了只是示例程序,只是做参考而已
- AllIn!!!2020-10-13已搞成功,有用
- 迷茫的根号七2020-04-08东西还行,可以拿来学习,不过这个连线真的是非常糟糕
_东门吹雪
- 粉丝: 197
- 资源: 8
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功