-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathqmapperdbscene.cpp
More file actions
401 lines (325 loc) · 11.8 KB
/
qmapperdbscene.cpp
File metadata and controls
401 lines (325 loc) · 11.8 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
#include "qmapperdbscene.h"
QMapperDbScene::QMapperDbScene(QObject *parent) : QGraphicsScene(parent), activeLayer(this, 3)
{
dbModel = NULL;
addItem(&tempPathItem);
tempPathItem.setPen(QPen(Qt::red, 2));
//addItem(activeLayer.getLayerItems());
tempPathItem.setVisible(false);
}
QMapperDbScene::~QMapperDbScene()
{
removeItem(activeLayer.getLayerItems());
}
void QMapperDbScene::mouseDoubleClicked()
{
qDebug() <<"dbl";
activeLayer.setVisible(!activeLayer.getIsVisible());
update();
//updateScene();
}
//void QMapperDbScene::mouseMoveEvent(QGraphicsSceneMouseEvent *e)
//{
// qDebug() <<"dbscene mmove " << e->pos();
//}
void QMapperDbScene::mouseDropped(QPointF pos)
{
qDebug() << "dbScene dropped @ " << pos;
tempPathItem.setVisible(false);
}
void QMapperDbScene::mouseDragged(QPointF pos)
{ //NOT USED
tempPathItem.setVisible(true);
qDebug() <<"dbScene dragged @ " << pos;
mapPtDst = pos;
mapPtDst.setY( mapPtDst.y());
updateTempPath();
//updateScene();
}
void QMapperDbScene::mouseDropped(QPointF src, QPointF dst)
{
qDebug() <<"dbScene DROP from " << src <<" to " << dst;
//undo hovered render on all
for (int i=0; i<sigs.size(); ++i)
{
sigs.at(i)->setHovered(false);
}
tempPathItem.setVisible(false);
QPointF dst_mod(dst.x()+src.x(), dst.y()+src.y());
int srcIdx = getIndexOfSigNear(src, 10);
int dstIdx = getIndexOfSigNear(dst_mod, 10);
qDebug() << "make map from " << srcIdx <<" to " <<dstIdx;
if ( (srcIdx != -1) & (dstIdx != -1) )
{
if (srcIdx != dstIdx) {// one more
addMap(srcIdx, dstIdx, true);
//IF WE WANT TO APPLY ANOTHER LAYER, do this:
//activeLayer.addMap(srcIdx, dstIdx);
}
}
//new method using layers:
// (actually other layers are passive, so we'd never do it. but if we ever did...)
//activeLayer.setAllHovered(false);
}
void QMapperDbScene::mouseDragged(QPointF src, QPointF dst)
{
//soooo ... many... hacks....
//Note: lets not do this to other layers, except to "working" layer
// that interacts with UI.
tempPathItem.setVisible(true);
qDebug() <<"dbScene DRAG from " << src <<" to " << dst;
//first, get the location of where we should be drawing
mapPtSrc = src;
mapPtSrc.setX(mapPtSrc.x()+MAPPER_SCENE_ITEM_W);
mapPtSrc.setY(mapPtSrc.y()+MAPPER_SCENE_ITEM_H/2);
mapPtSrc.setY(mapPtSrc.y());
mapPtDst = dst;
mapPtDst.setY( mapPtDst.y() +src.y()); //SEE TODO BELOW
mapPtDst.setX( mapPtDst.x() +src.x());
//find index of source object in list
//note this is the "modified" point!
int srcIdx = getIndexOfSigNear(mapPtSrc, 10);
//then, check if we've moved over an item...
int hoverIdx = getIndexOfSigNear(mapPtDst, 10);
//and if so, set it to render with selected graphic
//IF WE WANT TO APPLY ANOTHER LAYER, do this:
//activeLayer.setAllHovered(false);
//activeLayer.setTempHover(srcIdx, hoverIdx);
//old:
for (int i=0; i<sigs.size(); ++i)
{
if ((i == hoverIdx) && (srcIdx != hoverIdx))
{
sigs.at(i)->setHovered(true);
}
else
{ //unfortunately we have to also set everything else to
// non-hovered, unless we have some sort of exit
// mechanism, would would require a similar kind of search
// or better manangement of the child items.
// TODO: this potentially motivates the child objects (CustomRect)
// to hold onto an external index? this way we can simply emit
// the src and dst object indicies...
// THE MORE I THINK ABOUT IT THE MORE THIS MAKES SENSE
// (e.g. stupid hacks for relative mouse move/drag positions
// of child objects betwen source/destination, as above...)
sigs.at(i)->setHovered(false);
}
}
//make end of temporary arrow at the centre of receiving object
// if available, and make sure its not itself
if ( (hoverIdx != -1) && (hoverIdx != srcIdx))
{
mapPtDst.setX(sigs.at(hoverIdx)->boundingRectAbs().left());
mapPtDst.setY(sigs.at(hoverIdx)->boundingRectAbs().top() + MAPPER_SCENE_ITEM_H/2);
}
//draw map path
updateTempPath();
//IF WE WANT TO APPLY ANOTHER LAYER, do this:
//activeLayer.updateTempPath();
//updateScene();
}
void QMapperDbScene::addMap(int src_idx, int dst_idx, bool ifExistsRemove)
{
int existingIdx = mapExists(src_idx, dst_idx);
if ( (ifExistsRemove) && ( existingIdx!= -1) ) {
qDebug() <<"removing existing map # " << existingIdx;
mapSrcIdxs.erase(mapSrcIdxs.begin()+existingIdx);
mapDstIdxs.erase(mapDstIdxs.begin()+existingIdx);
}
else {
mapSrcIdxs.push_back(src_idx);
mapDstIdxs.push_back(dst_idx);
qDebug() <<"Main Scene added map from" <<src_idx << " to " << dst_idx;
}
redrawMapPaths();
//if we want to add to other layer:
//activeLayer.addMap(src_idx, dst_idx);
}
int QMapperDbScene::mapExists(int src_idx, int dst_idx)
{
int found_src = -1;
for (int i=0; i<mapSrcIdxs.size(); ++i)
{
if (mapSrcIdxs.at(i) == src_idx)
{
found_src = i;
if (mapDstIdxs.at(i) == dst_idx)
return i;
}
}
//not found
return -1;
}
void QMapperDbScene::updateTempPath()
{
//temporary line (while dragging)
QPainterPath tempPath;
tempPath.moveTo(mapPtSrc);
tempPath.cubicTo(mapPtSrc.x()+MAPPER_SCENE_CURVE, mapPtSrc.y(), mapPtDst.x()-MAPPER_SCENE_CURVE, mapPtDst.y(), mapPtDst.x(), mapPtDst.y());
//tempPath.lineTo(mapPtDst);
//addPath(tempPath);
tempPathItem.setPath(tempPath);
}
void QMapperDbScene::updateMapPaths()
{
//todo: don't need to store these explicitly
mapSrcs.clear();
mapDsts.clear();
for (int i=0; i < mapSrcIdxs.size(); ++i)
{
int src_idx = mapSrcIdxs.at(i);
int dst_idx = mapDstIdxs.at(i);
QPointF src_pt( (sigs.at(src_idx)->boundingRectAbs().right()),
(sigs.at(src_idx)->boundingRectAbs().top() + MAPPER_SCENE_ITEM_H/2));
QPointF dst_pt( (sigs.at(dst_idx)->boundingRectAbs().left()),
(sigs.at(dst_idx)->boundingRectAbs().top() + MAPPER_SCENE_ITEM_H/2));
mapSrcs.push_back(src_pt);
mapDsts.push_back(dst_pt);
}
//maps list
for (int i=0; i<mapSrcs.size(); ++i) //no bounds checking at all yet...
{
QGraphicsPathItem* pathItem = new QGraphicsPathItem();
QPainterPath path;
path.moveTo(mapSrcs.at(i));
//todo: use the PointF version which looks much neater...
path.cubicTo(mapSrcs.at(i).x()+MAPPER_SCENE_CURVE, mapSrcs.at(i).y(), mapDsts.at(i).x()-MAPPER_SCENE_CURVE, mapDsts.at(i).y(), mapDsts.at(i).x(), mapDsts.at(i).y());
pathItem->setPath(path);
mapPathItems.push_back(pathItem);
addItem(pathItem);
}
}
void QMapperDbScene::removeMapPaths()
{
for (int i=0; i<mapPathItems.size(); ++i)
{
removeItem(mapPathItems.at(i));
}
while (mapPathItems.size())
{
QGraphicsItem* item = mapPathItems.at(mapPathItems.size()-1);
delete item;
mapPathItems.pop_back();
}
}
void QMapperDbScene::redrawMapPaths()
{
removeMapPaths();
updateMapPaths();
}
void QMapperDbScene::updateScene()
{
//note: in the current implementation we need to remove
// owned child items explicitly which release ownership
// of the items from the scene; otherwise we have issues on exit.
// should fix this by reworking the class composition...
//also, once the children are put in a group, for some reason
// the UI signals don't get connected which explains why for now we
// have almost identical code in this class and the scenelayer class
// for updating of the scene... ideally a single class should suffice...
// activeLayer.setMapperDbModel(dbModel);
activeLayer.updateLayer();
removeItem(activeLayer.getLayerItems());
// set of drawings for the entire scene:
if (dbModel != NULL)
{
//qDebug() <<"reinit scene from dbModel...";
//remove stuff
while (sigs.size())
{
removeItem(sigs.at(sigs.size()-1));
delete sigs.at(sigs.size()-1);
sigs.pop_back();
}
removeMapPaths();
removeItem(&tempPathItem);//note: find way to avoid this bit
clear(); //probably won't need this since we've manually removed everything...
//add stuff back
activeLayer.getLayerItems()->setZValue(MAPPER_SCENE_MIDDLE_LAYER);
activeLayer.setOffset(15);
activeLayer.setDrawText(false);
QColor activeLayerCol(200, 200, 0);
activeLayer.setRectColour(activeLayerCol);
activeLayer.setAlpha(100);
addItem(activeLayer.getLayerItems());
addItem(&tempPathItem);
tempPathItem.setVisible(false);
sigs.clear();
int inputOffsetY = 0;
int outputOffsetY = 0;
for (int i=0; i<dbModel->getNumSigs(); ++i)
{
QString devname = dbModel->getSigDevName(i);
QString signame = dbModel->getSigName(i);
CustomRect * sigrect;
int offsetY;
int offsetX;
if (dbModel->isOutputSig(i))
{
offsetX = 0;
outputOffsetY += MAPPER_SCENE_ITEM_H + MAPPER_SCENE_SPACER;
offsetY = outputOffsetY;
}
else
{
offsetX = MAPPER_SCENE_ITEM_W*2 + MAPPER_SCENE_SPACER*2;
inputOffsetY += MAPPER_SCENE_ITEM_H + MAPPER_SCENE_SPACER;
offsetY = inputOffsetY;
}
sigrect = new CustomRect(offsetX, offsetY, devname, signame);
sigrect->setFillColor(QColor(128, 128, 128, 255));
//QObject::connect(sigrect, SIGNAL(mouseDragSig(QPointF)), this, SLOT(mouseDragged(QPointF)));
QObject::connect(sigrect, SIGNAL(mouseDragSig(QPointF, QPointF)), this, SLOT(mouseDragged(QPointF, QPointF)));
//QObject::connect(sigrect, SIGNAL(mouseDropSig(QPointF)), this, SLOT(mouseDropped(QPointF)));
QObject::connect(sigrect, SIGNAL(mouseDropSig(QPointF, QPointF)), this, SLOT(mouseDropped(QPointF, QPointF)));
//QObject::connect(sigrect, SIGNAL(mousePressSig()), this, SLOT(mousePressed()));
QObject::connect(sigrect, SIGNAL(rectMovedSig()), this, SLOT(devsigMoved()));
QObject::connect(sigrect, SIGNAL(mouseDoubleClickSig()), this, SLOT(mouseDoubleClicked()));
sigs.push_back(sigrect);
addItem(sigrect);
sigrect->setZValue(MAPPER_SCENE_BOTTOM_LAYER);
}
//load the map objects
for (int i=0; i<dbModel->getMapSrcs().size(); ++i)
{
addMap(dbModel->getMapSrcs().at(i), dbModel->getMapDsts().at(i));
}
updateMapPaths();
updateTempPath();
//updateMaps();
}
}
void QMapperDbScene::setMapperDbModel(QMapperDbModel* model)
{
dbModel = model;
}
void QMapperDbScene::setMapperDbModelActive(QMapperDbModel* model)
{
activeLayer.setMapperDbModel(model);
}
int QMapperDbScene::getIndexOfSigNear(QPointF pos, float len)
{
int foundIdx = -1;
for (int i=0; i<sigs.size(); ++i)
{
QRectF dragHitRect(pos.x()-len/2, pos.y()-len/2, len, len);
if (dragHitRect.intersects(sigs.at(i)->boundingRectAbs()))
{
qDebug() << "hit sig # " << i;
foundIdx = i;
break; //assume no overlapping boxes...
}
}
return foundIdx;
}
void QMapperDbScene::devsigMoved()
{
qDebug () <<" need to redraw!";
redrawMapPaths();
activeLayer.redrawMapPaths();
}
void QMapperDbScene::setAlpha(int alpha)
{
setForegroundBrush(QColor(255, 255, 255, alpha));
}