forked from liamnegron3/COP3530_Project3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraphicelementdialog.cpp
More file actions
114 lines (94 loc) · 4.11 KB
/
Copy pathgraphicelementdialog.cpp
File metadata and controls
114 lines (94 loc) · 4.11 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
#include "graphicelementdialog.h"
#include "ui_graphicelementdialog.h"
GraphicElementDialog::GraphicElementDialog(vector<pair<string,string>> &speciesNames,QString display,pair<string,string> _parentSpecies, QWidget *parent)
: QDialog(parent)
, ui(new Ui::GraphicElementDialog)
{
ui->setupUi(this);
scene = new QGraphicsScene(this);
ui->graphicsView->setScene(scene);
parentSpecies = _parentSpecies;
if(display == "Ancestor Tree")
NodalAncestorPath(scene,speciesNames);
else
RelatedSpecies(scene,speciesNames);
}
void GraphicElementDialog::NodalAncestorPath(QGraphicsScene *scene,vector<pair<string,string>> &ancestorPath)
{
//set base position
int x = 0;
int y = 0;
//add root
SpeciesNode* root = new SpeciesNode(ancestorPath[0].first.c_str(),ancestorPath[0].second.c_str());
root->setPos(QPointF(x,y));
scene->addItem(root);
//add rest of the species
for(unsigned int i = 1; i < ancestorPath.size(); i++)
{
SpeciesNode *child = new SpeciesNode(ancestorPath[i].first.c_str(),ancestorPath[i].second.c_str());
QLineF sceneLine = QLineF(40, 40, 80, 80);
//from Qt docs: https://doc.qt.io/qt-5/qtwidgets-graphicsview-diagramscene-example.html#arrow-class-definition
qreal arrowSize = 20;
double angle = std::atan2(-sceneLine.dy(), sceneLine.dx());
QPointF arrowP1 = sceneLine.p1() + QPointF(sin(angle + M_PI / 3) * arrowSize,
cos(angle + M_PI / 3) * arrowSize);
QPointF arrowP2 = sceneLine.p1() + QPointF(sin(angle + M_PI - M_PI / 3) * arrowSize,
cos(angle + M_PI - M_PI / 3) * arrowSize);
QPolygonF sceneArrowHead;
sceneArrowHead.clear();
sceneArrowHead << sceneLine.p1() << arrowP1 << arrowP2;
QGraphicsLineItem *line = scene->addLine(sceneLine);
QGraphicsPolygonItem *arrowHead = scene->addPolygon(sceneArrowHead);
int oldx = x;
int oldy = y;
x+=100;
y+=150;
child->setPos(QPointF(x,y));
root->addEdge(line,arrowHead,false);
child->addEdge(line,arrowHead,true);
scene->addItem(child);
child->itemChange(QGraphicsItem::ItemPositionChange,QPointF(x+1,y+1));
root->itemChange(QGraphicsItem::ItemPositionChange,QPointF(oldx+1,oldy+1));
root = child;
}
}
void GraphicElementDialog::RelatedSpecies(QGraphicsScene *scene,vector<pair<string,string>> &siblings)
{
//set base position
int x = 0;
int y = 0;
//add root
SpeciesNode* root = new SpeciesNode(parentSpecies.first.c_str(),parentSpecies.second.c_str());
root->setPos(QPointF(x,y));
scene->addItem(root);
//add rest of the species
for(unsigned int i = 0; i < siblings.size(); i++)
{
SpeciesNode *child = new SpeciesNode(siblings[i].first.c_str(),siblings[i].second.c_str());
QLineF sceneLine = QLineF(40, 40, 80, 80);
//from Qt docs: https://doc.qt.io/qt-5/qtwidgets-graphicsview-diagramscene-example.html#arrow-class-definition
qreal arrowSize = 20;
double angle = std::atan2(-sceneLine.dy(), sceneLine.dx());
QPointF arrowP1 = sceneLine.p1() + QPointF(sin(angle + M_PI / 3) * arrowSize,
cos(angle + M_PI / 3) * arrowSize);
QPointF arrowP2 = sceneLine.p1() + QPointF(sin(angle + M_PI - M_PI / 3) * arrowSize,
cos(angle + M_PI - M_PI / 3) * arrowSize);
QPolygonF sceneArrowHead;
sceneArrowHead.clear();
sceneArrowHead << sceneLine.p1() << arrowP1 << arrowP2;
QGraphicsLineItem *line = scene->addLine(sceneLine);
QGraphicsPolygonItem *arrowHead = scene->addPolygon(sceneArrowHead);
x+=100;
y+=100;
child->setPos(QPointF(x,y));
scene->addItem(child);
root->addEdge(line,arrowHead,false);
child->addEdge(line,arrowHead,true);
child->itemChange(QGraphicsItem::ItemPositionChange,QPointF(x+1,y+1));
}
}
GraphicElementDialog::~GraphicElementDialog()
{
delete node;
delete ui;
}