#include <QtWidgets>
#include "orgchartdialog.h"
#include "scene.h"
#include "node.h"
#include "link.h"
#include "insertlinkdialog.h"
#include "nodeinfodialog.h"
#include "aboutdialog.h"
OrgChartDialog::OrgChartDialog(QWidget *parent) :
QDialog(parent)
{
this->setWindowFlags(Qt::Window);
this->resize(1268, 744);
initOrgChartDialog();
createActions();
initViewActions();
}
OrgChartDialog::~OrgChartDialog()
{
qDeleteAll(m_nodeQueue);
m_nodeQueue.clear();
}
void OrgChartDialog::initOrgChartDialog()
{
m_scene = new Scene(0, 0, 50000, 60000);
connect(m_scene, SIGNAL(updateContextMenu()), this, SLOT(onSceneUpdateContextMenu()));
m_view = new QGraphicsView(this);
m_view->setScene(m_scene);
m_view->setDragMode(QGraphicsView::RubberBandDrag);
m_view->setContextMenuPolicy(Qt::ActionsContextMenu);
QHBoxLayout *hLayout = new QHBoxLayout();
hLayout->addWidget(m_view);
this->setLayout(hLayout);
}
void OrgChartDialog::createActions()
{
m_topMenu = new QMenu(this);
m_actAdd = new QAction("Add Node", this);
m_actAdd->setShortcut(tr("Ctrl+N"));
connect(m_actAdd, SIGNAL(triggered(bool)), this, SLOT(onAdd()));
m_topMenu->addAction(m_actAdd);
m_actDel = new QAction("Delete", this);
m_actDel->setShortcut(tr("Del"));
connect(m_actDel, SIGNAL(triggered(bool)), this, SLOT(onDelete()));
m_topMenu->addAction(m_actDel);
m_topMenu->addSeparator();
m_actCut = new QAction("Cut", this);
m_actCut->setShortcut(tr("Ctrl+X"));
connect(m_actCut, SIGNAL(triggered(bool)), this, SLOT(onCut()));
m_topMenu->addAction(m_actCut);
m_actCopy = new QAction("Copy", this);
m_actCopy->setShortcut(tr("Ctrl+C"));
connect(m_actCopy, SIGNAL(triggered(bool)), this, SLOT(onCopy()));
m_topMenu->addAction(m_actCopy);
m_actPaste = new QAction("Paste", this);
m_actPaste->setShortcut(tr("Ctrl+V"));
connect(m_actPaste, SIGNAL(triggered(bool)), this, SLOT(onPaste()));
m_topMenu->addAction(m_actPaste);
m_topMenu->addSeparator();
m_actInsertLink = new QAction("Insert Link", this);
m_actInsertLink->setShortcut(tr("Ctrl+K"));
connect(m_actInsertLink, SIGNAL(triggered(bool)), this, SLOT(onInsertLink()));
m_topMenu->addAction(m_actInsertLink);
m_actInsertBgImage = new QAction("Insert Background Image", this);
connect(m_actInsertBgImage, SIGNAL(triggered(bool)), this, SLOT(onInsertBgImage()));
m_topMenu->addAction(m_actInsertBgImage);
m_topMenu->addSeparator();
m_actProperty = new QAction("Property", this);
connect(m_actProperty, SIGNAL(triggered(bool)), this, SLOT(onProperty()));
m_topMenu->addAction(m_actProperty);
m_topMenu->addSeparator();
m_actAbout = new QAction("About", this);
m_actAbout->setShortcut(tr("F1"));
connect(m_actAbout, SIGNAL(triggered(bool)), this, SLOT(onAbout()));
m_topMenu->addAction(m_actAbout);
}
void OrgChartDialog::initViewActions()
{
QList<QAction *> acts = m_topMenu->actions();
for (int i = 0; i < acts.count(); i++) {
QAction *act = acts.at(i);
if (act) {
m_view->addAction(act);
}
}
}
Scene *OrgChartDialog::getOrgChartDialogScene()
{
return m_scene;
}
Node *OrgChartDialog::getSelectedNode()
{
QList<QGraphicsItem *> items = m_scene->selectedItems();
if (items.count() == 1) {
return dynamic_cast<Node *>(items.first());
}
else {
return 0;
}
}
void OrgChartDialog::adjustNodes(Node *fromNode, int nodeSpace)
{
if (!fromNode)
return;
QStack<Node *> nodeStack;
nodeStack.push(fromNode);
while (!nodeStack.isEmpty()) {
Node *topNode = nodeStack.top();
nodeStack.pop();
QList<Link *> linksTo = topNode->getLinksTo();
for (int i = 0; i < linksTo.size(); i++) {
Link *link = linksTo.at(i);
if (link) {
Node *toNode = link->getToNode();
if (toNode) {
if (!toNode->getLinksTo().isEmpty()) {
nodeStack.push(toNode);
}
toNode->setBasePoint(QPointF(toNode->x() - nodeSpace, toNode->y()));
toNode->setPos(toNode->getBasePoint().x(), toNode->getBasePoint().y());
link->adjust();
}
}
}
}
}
void OrgChartDialog::adjustNodesByLinkFrom(Link *linkFrom, int nodeX)
{
if (linkFrom) {
Node *fromNode = linkFrom->getFromNode();
if (fromNode) {
QList<Link *> linksTo = fromNode->getLinksTo();
for (int i = 0; i < linksTo.size(); i++) {
Link *link = linksTo.at(i);
if (link) {
Node *toNode = link->getToNode();
if (toNode) {
if (toNode->x() < nodeX) {
toNode->setBasePoint(QPointF(toNode->x() + 70, toNode->y()));
toNode->setPos(toNode->getBasePoint());
adjustNodes(toNode, -70);
}
else {
toNode->setBasePoint(QPointF(toNode->x() - 70, toNode->y()));
toNode->setPos(toNode->getBasePoint());
adjustNodes(toNode, +70);
}
}
link->adjust();
}
}
}
}
}
void OrgChartDialog::itemsMove()
{
Node *selectedNode = getSelectedNode();
if (selectedNode) {
if (!selectedNode->getLinksTo().isEmpty()) {
int moveX = selectedNode->x() - selectedNode->getBasePoint().x();
int moveY = selectedNode->y() - selectedNode->getBasePoint().y();
QStack<Node *> nodeStack;
nodeStack.push(selectedNode);
while (!nodeStack.isEmpty()) {
Node *node = nodeStack.top();
nodeStack.pop();
if (node) {
QList<Link *> linksTo = node->getLinksTo();
for (int i = 0; i < linksTo.size(); i++) {
Link *link = linksTo.at(i);
if (link) {
Node *toNode = link->getToNode();
if (toNode) {
if (!toNode->getLinksTo().isEmpty()) {
nodeStack.push(toNode);
}
toNode->setBasePoint(QPointF(toNode->x() + moveX, toNode->y() + moveY));
toNode->setPos(toNode->getBasePoint().x(), toNode->getBasePoint().y());
link->adjust();
}
}
}
}
}
}
}
}
void OrgChartDialog::onSceneUpdateContextMenu()
{
bool hasSelection = !m_scene->selectedItems().isEmpty();
bool isItemsEmpty = m_scene->items().isEmpty();
bool isNode = (getSelectedNode() != 0);
bool isQueueEmpty = m_nodeQueue.isEmpty();
m_actAdd->setEnabled(isNode || isItemsEmpty);
m_actDel->setEnabled(hasSelection);
m_actCut->setEnabled(isNode);
m_actCopy->setEnabled(isNode);
m_actPaste->setEnabled(!isQueueEmpty);
m_actInsertLink->setEnabled(isNode);
m_actInsertBgImage->setEnabled(isNode);
m_actProperty->setEnabled(isNode);
}
void OrgChartDialog::onAdd()
{
Node *node = new Node(this);
node->setText("Node");
QList<QGraphicsItem *> items = m_scene->items();
if (items.isEmpty()) {
node->setPos(m_scene->width() / 2.0, m_scene->height() / 2
Qt实现高级组织结构图
4星 · 超过85%的资源 需积分: 42 64 浏览量
2019-08-01
17:41:12
上传
评论 2
收藏 13KB RAR 举报
wgj22068
- 粉丝: 10
- 资源: 45
最新资源
- 基于C++实现二叉树的创建,遍历,添加,查找与删除
- 基于C语言实现二叉树的基本操作
- 毕业设计基于STM32的测量温度与压力的数据处理设计C语言完整源码+论文.zip
- 基于MATLAB的PCA算法人脸识别项目源码+GUI界面+说明文档.zip
- 基于STM32的测量温度与压力的数据处理设计源码+论文(毕业设计).zip
- Vision Transformer 网络对不同氨气氧气浓度轨迹RAS 图像数据集的分类,包含训练权重和数据集、迁移学习
- 基于C51带字库LCD12864(ST7920)的keil工程源码,只支持8位并口通讯(不支持串口),可显示中文.zip
- 基于SI4463射频模块433MD-SMA无线模块软硬件技术资料及(SI4463)IC技术资料文档.zip
- (GPS+北斗+GSM)HLK-GS2503模块软硬件开发资料包硬件参考设计(原理图PCB)+技术文档资料.zip
- 基于BERT+Biaffine结构的关系抽取模型源码+文档说明.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈