-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwidgetcontrol.cpp
More file actions
165 lines (134 loc) · 4.19 KB
/
widgetcontrol.cpp
File metadata and controls
165 lines (134 loc) · 4.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include "widgetcontrol.h"
#include "logiccontrol.h"
#include "brainnode.h"
#include "brainarc.h"
#include "mainwindow.h"
#include <QMenu>
const QColor WidgetControl::m_paperColor(248,248,255); // Set background color
WidgetControl::WidgetControl(MainWindow *parent)
: QGraphicsView(parent)
// , m_parent(parent)
{
m_scene = new QGraphicsScene(this);
m_scene->setItemIndexMethod(QGraphicsScene::NoIndex);
m_scene->setSceneRect(-400, -400, 800, 800);
setScene(m_scene);
m_logic = new LogicControl(this);
// Right Click Menu, menu content and connect signal and slots
m_RightMenu = new QMenu;
m_AddNode = new QAction(tr("Add Node"), this);
connect(m_AddNode, SIGNAL(triggered()), m_logic, SLOT(insertNode()));
m_RightMenu->addAction(m_AddNode);
m_RemoveNode = new QAction(tr("Remove Node"), this);
connect(m_RemoveNode, SIGNAL(triggered()), m_logic, SLOT(removeNode()));
m_RightMenu->addAction(m_RemoveNode);
m_AddArc = new QAction(tr("Add Arc"), this);
connect(m_AddArc, SIGNAL(triggered()), m_logic, SLOT(addEdge()));
m_RightMenu->addAction(m_AddArc);
m_RemoveArc = new QAction(tr("Remove Arc"), this);
connect(m_RemoveArc, SIGNAL(triggered()), m_logic, SLOT(removeEdge()));
m_RightMenu->addAction(m_RemoveArc);
m_NodeColor = new QAction(tr("Edit Node Color"), this);
connect(m_NodeColor, SIGNAL(triggered()), m_logic, SLOT(nodeColor()));
m_RightMenu->addAction(m_NodeColor);
m_TextColor = new QAction(tr("Edit Text Color"),this);
connect(m_TextColor, SIGNAL(triggered()), m_logic, SLOT(nodeTextColor()));
m_RightMenu->addAction(m_TextColor);
}
void WidgetControl::newScene()
{
m_logic->removeAllNodes();
m_logic->addFirstNode();
this->show();
}
void WidgetControl::closeScene()
{
m_logic->removeAllNodes();
this->hide();
}
LogicControl *WidgetControl::logicControl() const
{
return m_logic;
}
void WidgetControl::zoomIn()
{
scale(1 + 0.2, 1 + 0.2);
}
void WidgetControl::zoomOut()
{
scale(1 - 0.2, 1 - 0.2);
}
/* Key Press Event will be dealt with here, creating shortcut */
void WidgetControl::keyPressEvent(QKeyEvent *event)
{
if (event->key() == Qt::Key_Plus)
{
zoomIn();
return;
}
if (event->key() == Qt::Key_Minus)
{
zoomOut();
return;
}
if (event->key() == Qt::Key_Insert){
m_logic->insertNode();
}
if (event->key() == Qt::Key_Delete){
m_logic->removeNode();
}
if (event->key() == Qt::Key_A){
m_logic->addEdge();
}
if (event->key() == Qt::Key_D){
m_logic->removeEdge();
}
QGraphicsView::keyPressEvent(event); //继续保留QGraphicsView原有的键盘点击事件.
}
// Create Right Click Menu
void WidgetControl::mousePressEvent(QMouseEvent *event){
if (event->button() == Qt::RightButton)
{
m_RightMenu->exec(event->globalPos());
}
//继续保留QGraphicsView原有的鼠标点击事件.
QGraphicsView::mousePressEvent(event);
}
void WidgetControl::mouseDoubleClickEvent(QMouseEvent *event){
if(event->button() == Qt::LeftButton){
m_logic->nodeEdited();
}
if(event->button() == Qt::RightButton){
m_logic->insertNode();
}
QGraphicsView::mouseDoubleClickEvent(event);
}
void WidgetControl::wheelEvent(QWheelEvent *event)
{
if (Qt::ControlModifier & event->modifiers()){ // Press control + wheel modify the node
if (event->delta() > 0) m_logic->scaleUp();
else m_logic->scaleDown();
}
else { // modify the graph
if (event->delta() > 0) zoomIn();
else zoomOut();
}
}
void WidgetControl::drawBackground(QPainter *painter, const QRectF &rect)
{
Q_UNUSED(rect);
painter->fillRect(m_scene->sceneRect(), WidgetControl::m_paperColor); // Set background color
painter->setBrush(Qt::NoBrush);
painter->drawRect(m_scene->sceneRect());
}
void WidgetControl::scaleView(qreal factor)
{
// don't allow to scale up/down too much
// qreal viewScale = transform().m11() + 1 + factor;
// if (viewScale < qreal(1) || viewScale > qreal(10))
// {
// emit notification(tr("Too much zooming."));
// return;
// }
scale(1 + factor, 1 + factor);
}