-
Notifications
You must be signed in to change notification settings - Fork 0
/
node.cpp
747 lines (637 loc) · 15.6 KB
/
node.cpp
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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
/*
* File: node.cpp
* Author: Rachel Bood
* Date: 2014/11/07
* Version: 1.1
*
* Purpose: creates a node for the users graph
*
* Modification history:
* Feb 8, 2016 (JD):
* (a) Modify setWeight(QString) so that it uses cmr10 if the string
* is numeric. This function is (apparently) called by the
* callback in labelcontroller.cpp when a user edits a label.
* (Is it possible instead to test there, and, if so, should we?)
* (b) drive-by typo and formatting corrections, general cleanup.
* Note that node.lSize is currently written to, never read.
*/
#include "edge.h"
#include "node.h"
#include "canvasview.h"
#include "preview.h"
#include <QTextDocument>
#include <QKeyEvent>
#include <QGraphicsScene>
#include <QGraphicsSceneMouseEvent>
#include <QPainter>
#include <QStyleOption>
#include <QMessageBox>
#include <QDebug>
#include <QMimeData>
#include <QDrag>
#include <QtCore>
static const bool verbose = false;
/*
* Name: Node
* Purpose: Contructor for Node class.
* Arguments: canvas pointer, diameter of node, edge size
* Output: none
* Modifies: Private node variables.
* Returns: none
* Assumptions: none
* Bugs: none
* Notes: none
*/
Node::Node()
{
setFlag(ItemIsMovable);
setFlag(ItemIsSelectable);
setFlag(ItemSendsGeometryChanges);
setFlag(QGraphicsItem::ItemClipsChildrenToShape);
setZValue(2);
nodeID = -1;
choose = 0; // What type of pen style to use when drawing outline.
lSize = 12; // Default font size, in points. CURRENTLY UNUSED.
nodeDiameter = 1;
edgeWeight = 1; // UNUSED IN V 1.1.
rotation = 0;
text = new Label(this);
setHandlesChildEvents(true);
select = false;
QScreen * screen = QGuiApplication::primaryScreen();
logicalDotsPerInchX = screen->logicalDotsPerInchX();
logicalDotsPerInchY = screen->logicalDotsPerInchY();
}
/*
* Name: addEdge
* Purpose: adds an Edge to the pointer QList of edges
* Arguments: an Edge pointer
* Output: none
* Modifies: node
* Returns: none
* Assumptions: none
* Bugs: none...so far
* Notes: none
*/
void Node::addEdge(Edge * edge)
{
edgeList << edge;
edge->adjust();
}
/*
* Name: removeEdge()
* Purpose: Remove an edge from the edgelist.
* Arguments: Edge *
* Output: boolean
* Modifies: edgeList
* Returns: True if edge was removed, otherwise false.
* Assumptions: none
* Bugs: none
* Notes: none
*/
bool Node::removeEdge(Edge * edge)
{
for (int i = 0; i < edgeList.length(); i++)
{
if (edgeList.at(i) == edge)
{
edgeList.removeAt(i);
return true;
}
}
return false;
}
/*
* Name: setDiameter()
* Purpose: Sets the size of the diameter of the node in "logical DPI".
* Arguments: qreal
* Output: none
* Modifies: nodeDiameter
* Returns: none
* Assumptions: none
* Bugs: none
* Notes: The argument diameter is the diameter in inches, therefore
* the value must be converted back to pixels in order for the
* node to be drawn correctly.
*/
void Node::setDiameter(qreal diameter)
{
nodeDiameter = diameter * logicalDotsPerInchX;
update();
}
/*
* Name: getDiameter()
* Purpose: returns diameter of the node (in inches)
* Arguments: none
* Output: QColor
* Modifies: none
* Returns: nodeline
* Assumptions: none
* Bugs: none
* Notes: nodeDiamter is stored in pixels, it needs to be converted back
* to inches before it is returned.
* FUTURE WORK: create multiple getDiameter() functions to return
* different values....or create one function that will return
* the diameter in the desired unit of measurement.
*/
qreal Node::getDiameter()
{
return nodeDiameter / logicalDotsPerInchX;
}
/*
* Name: setEdgeWeight()
* Purpose:
* Arguments:
* Output:
* Modifies:
* Returns:
* Assumptions:
* Bugs:
* Notes:
*/
void Node::setEdgeWeight(qreal aEdgeWeight)
{
edgeWeight = aEdgeWeight;
update();
}
/*
* Name: setRotation()
* Purpose: Sets the rotation of the node.
* Arguments: qreal
* Output: none
* Modifies: rotation
* Returns: none
* Assumptions: none
* Bugs: none
* Notes: none
*/
void Node::setRotation(qreal aRotation)
{
rotation = aRotation;
QGraphicsItem::setRotation(aRotation);
}
/*
* Name: getRotation()
* Purpose: returns the rotation of the node
* Arguments: none
* Output: qreal
* Modifies: none
* Returns: rotation
* Assumptions: none
* Bugs: none
* Notes: none
*/
qreal Node::getRotation()
{
return rotation;
}
/*
* Name: setFillColour()
* Purpose: Sets the fill colour of the node.
* Arguments: QColor
* Output: none
* Modifies: nodeFill
* Returns: none
* Assumptions: none
* Bugs: none
* Notes: none
*/
void Node::setFillColour(QColor fColor)
{
nodeFill = fColor;
update();
}
/*
* Name: getFillColor()
* Purpose: Returns the fill colour of the node.
* Arguments: none
* Output: QColor
* Modifies: none
* Returns: nodeline
* Assumptions: none
* Bugs: none
* Notes: none
*/
QColor Node::getFillColour()
{
return nodeFill;
}
/*
* Name: setLineColour()
* Purpose: Sets the outline colour of the node.
* Arguments: QColor
* Output: none
* Modifies: NodeLine
* Returns: none
* Assumptions: none
* Bugs: none
* Notes: none
*/
void Node::setLineColour(QColor lColor)
{
nodeLine = lColor;
update();
}
/*
* Name: getLineColor()
* Purpose: Returns the outline colour of the node.
* Arguments: none
* Output: QColor
* Modifies: none
* Returns: nodeline
* Assumptions: none
* Bugs: none
* Notes: none
*/
QColor Node::getLineColour()
{
return nodeLine;
}
/*
* Name: findRootParent()
* Purpose: Finds the root parent of the node.
* Arguments: none
* Output: QGraphicsItem
* Modifies: none
* Returns: root
* Assumptions: none
* Bugs: none
* Notes: none
*/
QGraphicsItem * Node::findRootParent()
{
QGraphicsItem * root = this->parentItem();
while (root->parentItem() != 0 && root->parentItem() != nullptr)
root = root->parentItem();
return root;
}
/*
* Name: setID()
* Purpose: Sets the ID of the node.
* Arguments: int
* Output: none
* Modifies: none
* Returns: none
* Assumptions: none
* Bugs: none
* Notes: none
*/
void Node::setID(int id)
{
nodeID = id;
}
/*
* Name: getID()
* Purpose: Returns the ID of the node.
* Arguments: none
* Output: int
* Modifies: none
* Returns: nodeID
* Assumptions: none
* Bugs: none
* Notes: none
*/
int Node::getID()
{
return nodeID;
}
/*
* Name: edges()
* Purpose: Returns the list of edges incident to the node.
* Arguments: none
* Output: QList<Edge *>
* Modifies: none
* Returns: edgeList
* Assumptions: none
* Bugs: none
* Notes: none
*/
QList<Edge *> Node::edges() const
{
return edgeList;
}
/*
* Name: setNodeLabel()
* Purpose: Sets the label of the node.
* Arguments: qreal
* Output: none
* Modifies: The text in the node label (both "text" and "label" fields).
* Returns: none
* Assumptions: none
* Bugs: none
* Notes: none
*/
void Node::setNodeLabel(qreal number)
{
text->setHtml("<font face=\"cmr10\">"
+ QString::number(number) + "</font>");
label = QString::number(number);
}
/*
* Name: setNodeLabel()
* Purpose: Sets the label of the node.
* Arguments: QString, qreal
* Output: none
* Modifies: The text in the node label (both "text" and "label" fields).
* Returns: none
* Assumptions: none
* Bugs: none
* Notes: none
*/
void Node::setNodeLabel(QString aLabel, qreal number)
{
label = aLabel;
text->setHtml("<font face=\"cmmi10\">" + label
+ "</font><sub><font face=\"cmr10\">"
+ QString::number(number) + "</face></sub>");
}
/*
* Name: setNodeLabel()
* Purpose: Sets the label of the node.
* Arguments: QString
* Output: none
* Modifies: The text in the node label (both "text" and "label" fields).
* Returns: none
* Assumptions: none
* Bugs: none
* Notes: This is (apparently) called from the labelcontroller.cpp
* callback, which doesn't distinguish between integer and
* string. So do a test to choose the correct font.
*/
void Node::setNodeLabel(QString aLabel)
{
label = aLabel;
text->setLabel(aLabel);
}
/*
* Name: setNodeLabel()
* Purpose: Sets the label of the node.
* Arguments: QString, qreal, QString
* Output: none
* Modifies: the text in the node's label
* Returns: none
* Assumptions: none
* Bugs: none
* Notes: IS THIS CALLED FROM ANYWHERE?
* IS IT UP-TO-DATE?
*/
void Node::setNodeLabel(QString htmltext, qreal labelSize, QString aLabel)
{
qDebug() << "setNodeLabel(QString, qreal, QString) called!";
label = aLabel;
text->setHtml(htmltext);
QFont font = text->font();
font.setPointSize(labelSize);
text->setFont(font);
lSize = labelSize;
update();
}
/*
* Name: setNodeLabelSize()
* Purpose: Sets the font size of the node's label.
* Arguments: qreal
* Output: none
* Modifies: The text in the node label, lSize.
* Returns: none
* Assumptions: none
* Bugs: none
* Notes: none
*/
void Node::setNodeLabelSize(qreal labelSize)
{
QFont font = text->font();
font.setPointSize(labelSize);
lSize = labelSize;
text->setFont(font);
}
/*
* Name: getLabel()
* Purpose: Returns the string of the label.
* Arguments: none
* Output: QString
* Modifies: none
* Returns: the string contained in the label
* Assumptions: none
* Bugs: none
* Notes: none
*/
QString Node::getLabel() const
{
return label;
}
/*
* Name: getLableSize()
* Purpose: Returns the font size of the label.
* Arguments: none
* Output: qreal
* Modifies: none
* Returns: font size
* Assumptions: none
* Bugs: none
* Notes: none
*/
qreal Node::getLabelSize() const
{
return text->font().pointSizeF();
}
/*
* Name: boundingRect()
* Purpose: Determines the bounding rectangle of the node.
* Arguments: none
* Output: QRectF
* Modifies: none
* Returns: QRectF
* Assumptions: none
* Bugs: none
* Notes: Q: Is adjust some empirical fudge factor?
*/
QRectF Node::boundingRect() const
{
qreal adjust = 2;
return QRectF( (-1 * nodeDiameter / 2) - adjust,
(-1 * nodeDiameter / 2) - adjust,
nodeDiameter + 3 + adjust,
nodeDiameter + 3 + adjust);
}
/*
* Name: chosen()
* Purpose: An int used to determine how to draw the outline of the node.
* Arguments: integer
* Output: none
* Modifies: choose: the integer used in the Node::paint() function.
* Returns: none
* Assumptions: none
* Bugs: none
* Notes: none.
*/
void Node::chosen(int pen_style)
{
choose = pen_style;
update();
}
/*
* Name: editLabel()
* Purpose: Change edit flags to specify if the label is editable.
* Arguments: boolean
* Output: none
* Modifies: ItemIsFocusable, ItemIsSelectable, setHandlesChildEvents flags
* Returns: none
* Assumptions: none
* Bugs: none
* Notes: none
*/
void Node::editLabel(bool edit)
{
setHandlesChildEvents(!edit);
text->setFlag(QGraphicsItem::ItemIsFocusable, edit);
text->setFlag(ItemIsSelectable, edit);
}
/*
* Name: ~Node()
* Purpose: destructor function
* Arguments: none
* Output: none
* Modifies: none
* Returns: none
* Assumptions: none
* Bugs: none
* Notes: none
*/
//Node::~Node()
//{
// emit nodeDeleted();
// foreach(Edge * edge, edgeList)
// delete edge;
// text->setParentItem(nullptr);
// delete text;
// text = nullptr;
// setParentItem(nullptr);
//}
/*
* Name: nodeDeleted()
* Purpose: Originally: send a signal to controllers that the node
* is deleted. Now, obviously none.
* Arguments: none
* Output: none
* Modifies: nothing
* Returns: none
* Assumptions: none
* Bugs: none
* Notes: none
*/
void
Node::nodeDeleted()
{
}
/*
* Name: paint()
* Purpose: Paints a node.
* Arguments: QPainter *, QStyleOptionGraphicsItem *, QWidget *
* Output: A node drawing
* Modifies: The node drawing.
* Returns: Nothing.
* Assumptions: none
* Bugs: none
* Notes: Currently only draws nodes as circles.
*/
void
Node::paint(QPainter * painter, const QStyleOptionGraphicsItem * option,
QWidget * widget)
{
Q_UNUSED(option);
Q_UNUSED(widget);
QColor brushColor;
brushColor = nodeFill;
painter->setBrush(brushColor);
QPen pen;
// TODO: is this code not defunct? It seems to be over-ridden.
if (isSelected())
pen.setStyle(Qt::DotLine);
else
pen.setStyle(Qt::SolidLine);
if (choose == 1)
pen.setStyle(Qt::DotLine);
else if (choose == 2)
pen.setStyle(Qt::DashLine);
else
pen.setStyle(Qt::SolidLine);
pen.setColor(nodeLine);
painter->setPen(pen);
painter->drawEllipse(-1 * nodeDiameter / 2,
-1 * nodeDiameter / 2,
nodeDiameter, nodeDiameter);
text->setPos(this->boundingRect().center().x()
- text->boundingRect().width() / 2.,
this->boundingRect().center().y()
- text->boundingRect().height() / 2.);
}
/*
* Name: itemChange()
* Purpose: Send a signal to the edge objects to re-adjust the location
* of the edges.
* Arguments: GraphicsItemChange, QVariant value
* Output: none
* Modifies: The node's edge list.
* Returns: none
* Assumptions: none
* Bugs: none
* Notes: none
*/
QVariant Node::itemChange(GraphicsItemChange change, const QVariant &value)
{
switch (change)
{
case ItemPositionHasChanged:
if (parentItem() != 0)
{
if (parentItem()->type() == Graph::Type)
{
Graph * graph = qgraphicsitem_cast<Graph*>(parentItem());
Graph * tempGraph = graph;
graph = qgraphicsitem_cast<Graph*>(graph->getRootParent());
this->setParentItem(nullptr);
this->setParentItem(tempGraph);
}
if (verbose)
qDebug() << "node does not have a graph item parent";
}
foreach (Edge * edge, edgeList)
edge->adjust();
break;
case ItemRotationChange:
foreach (Edge * edge, edgeList)
edge->adjust();
break;
default:
break;
};
return QGraphicsItem::itemChange(change, value);
}
/*
* Name: mousePressEvent()
* Purpose:
* Arguments: The event.
* Output:
* Modifies:
* Returns:
* Assumptions:
*/
void Node::mousePressEvent(QGraphicsSceneMouseEvent * event)
{
select = true;
QGraphicsItem::mousePressEvent(event);
}
/*
* Name: mouseReleaseEvent()
* Purpose:
* Arguments:
* Output:
* Modifies:
* Returns:
* Assumptions:
*/
void Node::mouseReleaseEvent(QGraphicsSceneMouseEvent * event)
{
select = false;
QGraphicsItem::mouseReleaseEvent(event);
}