-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
235 lines (204 loc) · 7.48 KB
/
mainwindow.cpp
File metadata and controls
235 lines (204 loc) · 7.48 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
#include "mainwindow.h"
#include <QString>
#include <filesystem>
MainWindow::MainWindow(QWidget *parent): QMainWindow(parent) {
setWindowTitle(tr("Utopia"));
setMenu();
QString save_results = "";
QString pathOpenfile = "";
}
MainWindow::~MainWindow(){
delete menu;
}
void MainWindow::openFile(const QString &path) {
QString fileName = path;
if (fileName.isNull()) {
std::filesystem::path filePath(shellVariable1.toStdString());
std::filesystem::path fullPath = filePath / "test/data/ril/test.ril";
fileName = QFileDialog::getOpenFileName(this, tr("Open File"), QString::fromStdString(fullPath.string()), tr("RIL Files (*.ril)"));
}
if (!fileName.isEmpty()) {
edit = new CodeEditor(this);
setCentralWidget(edit);
edit->setWindowTitle(QObject::tr(""));
edit->setFont(font);
edit->show();
QFile file(fileName);
pathOpenfile = fileName;
if (file.exists() && file.open(QFile::ReadOnly | QFile::Text)) {
edit->setPlainText(file.readAll());
file.close();
return;
}
else {
qDebug() << "Failed to open file: ";
}
}
else {
qDebug() << "No file selected: ";
}
}
void MainWindow::save() {
QString fileName = pathOpenfile;
if (fileName != "") {
QFile file(fileName);
if (!file.open(QIODevice::WriteOnly)) {
qDebug() << "the file did not open";
} else {
QTextStream stream(&file);
stream << edit->toPlainText();
stream.flush();
file.close();
}
}
}
void MainWindow::SaveAs() {
std::filesystem::path filePath(shellVariable1.toStdString());
std::filesystem::path fullPath = filePath / "test/data/ril/test.ril";
QString fileName = QFileDialog::getSaveFileName(this, tr("Save as"), QString::fromStdString(fullPath.string()),tr("Text file (*.txt );; RIL file (*.ril )"));
if (fileName != "") {
QFile file(fileName);
if (!file.open(QIODevice::WriteOnly)) {
qDebug() << "the file did not open";
} else {
QTextStream stream(&file);
stream << edit->toPlainText();
stream.flush();
file.close();
}
}
}
void MainWindow::runUtopia() {
utopiaRun = new windowRun(this);
utopiaRun->show();
connect(utopiaRun, &windowRun::fileSelected, this, &MainWindow::openFileinWindow);
}
void MainWindow::loadGraph(QString filename, QMap<QString, QVector<QString>> &adjList){
QFile file(filename);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
qDebug() << "Failed to open file: ";
return;
}
QXmlStreamReader xmlReader(&file);
QString currentElement;
QString source, target;
while (!xmlReader.atEnd() && !xmlReader.hasError()) {
QXmlStreamReader::TokenType token = xmlReader.readNext();
if (token == QXmlStreamReader::StartElement) {
if (xmlReader.name().compare(QString("node")) == 0) {
currentElement = xmlReader.attributes().value("id").toString();
adjList[currentElement] = QVector<QString>();
}
if (xmlReader.name().compare(QString("edge")) == 0){
source = xmlReader.attributes().value("source").toString();
target = xmlReader.attributes().value("target").toString();
adjList[source].append(target);
adjList[target].append(source);
}
}
}
file.close();
}
void MainWindow::displayGraph(QMap<QString, QVector<QString>> &adjList) {
GVC_t *gvc;
Agraph_t *g;
Agnode_t *n, *m;
Agedge_t *e;
gvc = gvContext();
char graphName[] = "G";
g = agopen(graphName, Agdirected, nullptr);
QMap<QString, Agnode_t*> nodes;
for (const QString& key : adjList.keys()) {
n = agnode(g, NULL, TRUE);
agsafeset(n, const_cast<char*>("label"), const_cast<char*>(key.toUtf8().constData()), const_cast<char*>(""));
nodes[key] = n;
}
for (const QString& key : adjList.keys()) {
for (const QString& neighbor : adjList[key]) {
n = nodes[key];
m = nodes[neighbor];
e = agedge(g, n, m, NULL, TRUE);
}
}
gvLayout(gvc, g, "dot");
char *renderData;
unsigned int renderDataLength;
gvRenderData(gvc, g, "png", &renderData, &renderDataLength);
QImage image;
image.loadFromData((uchar*)renderData, renderDataLength);
QGraphicsScene *scene = new QGraphicsScene();
QGraphicsPixmapItem *item = new QGraphicsPixmapItem(QPixmap::fromImage(image));
scene->addItem(item);
QGraphicsView *view = new QGraphicsView(scene);
view->setWindowTitle("Graph");
view->show();
gvFreeRenderData(renderData);
gvFreeLayout(gvc, g);
agclose(g);
gvFreeContext(gvc);
}
void MainWindow::exportResults() {
QMap<QString, QVector<QString>> adjList;
std::filesystem::path filePath(shellVariable1.toStdString());
std::filesystem::path fullPath = filePath / "test/data/ril/test.ril";
QStringList filters;
filters << "XML Files (*.xml)" << "Verilog Files (*.v)" << "Bench Files (*bench)";
QString filterString = filters.join(";;");
QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"), QString::fromStdString(fullPath.string()), filterString); // choose a file
if (!fileName.isEmpty()) {
if (fileName.endsWith(".xml", Qt::CaseInsensitive)) {
QStringList libraryFilters;
libraryFilters << "GraphViz" << "BasicViz" <<"FpgaViz";
QString selectedLibraryFilter = QInputDialog::getItem(this, tr("Select Library"), tr("Select a library:"), libraryFilters);
if (!selectedLibraryFilter.isEmpty()) {
if (selectedLibraryFilter == libraryFilters[0]) {
loadGraph(fileName, adjList);
displayGraph(adjList);
} else if (selectedLibraryFilter == libraryFilters[1]) {
bench = new windowBench(this);
bench->filename = fileName;
bench->show();
} else if (selectedLibraryFilter == libraryFilters[2]) {
const QProcessEnvironment env3 = QProcessEnvironment::systemEnvironment();
const QString shellVariable3 = env3.value("FpgaViz");
QString program = shellVariable3;
QStringList arguments;
QString fileNameRes = fileName + "_FLG";
arguments << fileName << fileNameRes ;
QProcess::startDetached(program, arguments);
qDebug() << QString("Starting process: %1 %2").arg(program).arg(arguments.join(" "));
bench = new windowBench(this);
bench->filename = fileNameRes;
bench->show();
}
} else {
qDebug() << "No library selected";
}
} else if (fileName.endsWith(".v", Qt::CaseInsensitive)) {
utopiaRun = new windowRun(this);
utopiaRun->b = fileName;
utopiaRun->text = " "; // here will be the option which need to generate xml file
utopiaRun->onBtnRunUtopiaClicked(); // run Utopia
} else if (fileName.endsWith(".bench", Qt::CaseInsensitive)) {
bench = new windowBench(this);
bench->filename = fileName;
bench->show();
}
} else {
qDebug() << "No file selected";
}
}
void MainWindow::openFileinWindow(const QString &filePath) {
if (!filePath.isEmpty()) {
openFile(filePath);
}
}
void MainWindow::setMenu(){
menu = menuBar()->addMenu(tr("&File"));
menu->addAction(tr("Open file"), this, SLOT(openFile()), QKeySequence(tr("Ctrl+N","File|New")));
menu->addAction(tr("Save file"), this, SLOT(save()), QKeySequence(tr("Ctrl+S","File|Save")));
menu->addAction(tr("Save as file"), this, SLOT(SaveAs()), QKeySequence(tr("Ctrl+T","File|Save as")));
menu->addAction(tr("Export"), this, SLOT(exportResults()), QKeySequence(tr("Ctrl+E","File|Export")));
menu = menuBar()->addMenu(tr("&Run"));
menu->addAction(tr("Run"), this, SLOT(runUtopia()), QKeySequence(tr("Ctrl+R","File|New")));
}