-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathggraphicsproxy.cpp
More file actions
144 lines (116 loc) · 4.68 KB
/
ggraphicsproxy.cpp
File metadata and controls
144 lines (116 loc) · 4.68 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
#include "ggraphicsproxy.h"
#include <QPainter>
#include <QDebug>
#include "gpad.h"
#include "gmovesmodel.h"
#include "gmovenode.h"
#include "gmovearcnode.h"
#include "gmoveline.h"
#include "gmovearc.h"
GGraphicsProxy::GGraphicsProxy(GMovesModel *model, QObject *parent)
: QGraphicsScene(parent),
mModel(model),
mMargin(10.0)
{
setItemIndexMethod(QGraphicsScene::NoIndex);
setSceneRect(-mMargin, -mMargin, 205.0 + mMargin * 2, 205.0 + mMargin * 2);
// connect(mModel, SIGNAL(changed(int, int)), this, SLOT(update(int, int)));
connect(mModel, SIGNAL(modelReset()), this, SLOT(resetData()));
connect(mModel, SIGNAL(layoutChanged()), this, SLOT(layoutChanged()));
connect(mModel, SIGNAL(rowsInserted(const QModelIndex &, int, int)),
this, SLOT(rowsInserted(const QModelIndex &, int, int)));
connect(mModel, SIGNAL(rowsAboutToBeRemoved(const QModelIndex &, int, int)),
this, SLOT(rowsAboutToBeRemoved(const QModelIndex &, int, int)));
connect(mModel, SIGNAL(dataChanged(const QModelIndex &, const QModelIndex &, const QVector<int> &)),
this, SLOT(dataChanged(const QModelIndex &, const QModelIndex &, const QVector<int> &)));
}
GGraphicsProxy::~GGraphicsProxy()
{
}
//void GGraphicsProxy::updateData(int top, int bottom)
//{
//}
void GGraphicsProxy::resetData()
{
clear();
mIndexToNodes.clear();
GMoveNode* prevNode = NULL;
for (int i = 0; i < mModel->rowCount(); ++i) {
QPersistentModelIndex index = mModel->index(i, 0);
bool visible = index.data(GPad::VisibilityRole).toBool();
GMoveNode* node = new GMoveNode(index);
node->setVisible(visible);
addItem(node);
if (index.data(GPad::MoveArcDirectionRole).toInt() == 0) {
addItem(new GMoveLine(prevNode, node));
} else {
GMoveArcNode* arcCenter = new GMoveArcNode(index);
arcCenter->setVisible(visible);
addItem(arcCenter);
mIndexToArcNodes.insert(index, arcCenter);
addItem(new GMoveArc(prevNode, node, arcCenter));
}
mIndexToNodes.insert(index, node);
prevNode = node;
}
}
void GGraphicsProxy::layoutChanged()
{
qDebug() << __PRETTY_FUNCTION__;
}
void GGraphicsProxy::rowsInserted(const QModelIndex &parent, int start, int end)
{
qDebug() << __PRETTY_FUNCTION__;
}
void GGraphicsProxy::rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end)
{
qDebug() << __PRETTY_FUNCTION__;
}
void GGraphicsProxy::dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &roles)
{
if (!(roles.empty() || mIndexToNodes.isEmpty())) {
int min = topLeft.row() == -1 ? 0 : qMin(topLeft.row(), bottomRight.row());
int max = bottomRight.row() == -1 ? mModel->rowCount() - 1 : qMax(topLeft.row(), bottomRight.row());
if (roles.contains(GPad::VisibilityRole)) {
//qDebug() << __PRETTY_FUNCTION__ << "VisibilityRole" << min << max;
for (int i = min; i <= max; ++i) {
QPersistentModelIndex index = mModel->index(i, 0);
mIndexToNodes.value(index)->setVisible(index.data(GPad::VisibilityRole).toBool());
if (mIndexToArcNodes.contains(index)) {
mIndexToArcNodes.value(index)->setVisible(index.data(GPad::VisibilityRole).toBool());
}
}
}
if (roles.contains(GPad::SelectionRole)) {
// qDebug() << __PRETTY_FUNCTION__ << "SelectionRole";
for (int i = min; i <= max; ++i) {
QPersistentModelIndex index = mModel->index(i, 0);
bool v = mModel->data(index, GPad::SelectionRole).toBool();
mIndexToNodes.value(index)->setSelected(v);
if (mIndexToArcNodes.contains(index)) {
mIndexToArcNodes.value(index)->setSelected(v);
}
}
}
}
update(sceneRect());
}
void GGraphicsProxy::drawBackground(QPainter *painter, const QRectF &rect)
{
//TODO: Move to view
QRectF border = sceneRect().adjusted(mMargin, mMargin, -mMargin, -mMargin);
qreal step = 10.0;
qreal x = border.left() + step;
qreal y = border.top() + step;
painter->setPen(QPen(Qt::lightGray, 0));
while (x < border.right()) {
painter->drawLine(x, 0, x, border.bottom());
x += step;
}
while (y < border.bottom()) {
painter->drawLine(0, y, border.right(), y);
y += step;
}
painter->setPen(QPen(Qt::lightGray, 1, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
painter->drawRect(border);
}