This repository was archived by the owner on Jan 21, 2022. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathpythoncomparedialog.cpp
More file actions
169 lines (144 loc) · 5.29 KB
/
Copy pathpythoncomparedialog.cpp
File metadata and controls
169 lines (144 loc) · 5.29 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
/*
* pythoncomparedialog.cpp
*
* Created on: Nov 4, 2017
* Author:
*/
#include "pythoncomparedialog.h"
#include <fstream>
#include <QDirIterator>
#include <QFileDialog>
#include <QGridLayout>
#include <QLabel>
#include <QMessageBox>
#include <QPushButton>
PythonCompareDialog::PythonCompareDialog(QWidget* parent,
const QString& scriptPath,
const QString& osiFile1,
const QString& osiFile2)
: QDialog(parent),
osiFile1_(new QLineEdit(osiFile1, this)),
osiFile2_(new QLineEdit(osiFile2, this)),
scripts_(new QComboBox(this)),
logWin_(new QTextEdit(this)),
scriptPath_(scriptPath)
{
setWindowTitle(tr("Python Compare"));
setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
QGridLayout* mainGridLayout = new QGridLayout(this);
// File 1
mainGridLayout->addWidget(new QLabel(tr("OSI File 1:"), this), 0, 0);
mainGridLayout->addWidget(osiFile1_, 0, 1);
QPushButton* pBrowse1 = new QPushButton(tr("..."), this);
pBrowse1->setMaximumWidth(30);
connect(pBrowse1, &QPushButton::clicked, this, &PythonCompareDialog::OnBrowseFile1);
mainGridLayout->addWidget(pBrowse1, 0, 2);
// File 2
mainGridLayout->addWidget(new QLabel(tr("OSI File 2:"), this), 1, 0);
mainGridLayout->addWidget(osiFile2_, 1, 1);
QPushButton* pBrowse2 = new QPushButton(tr("..."), this);
pBrowse2->setMaximumWidth(30);
connect(pBrowse2, &QPushButton::clicked, this, &PythonCompareDialog::OnBrowseFile2);
mainGridLayout->addWidget(pBrowse2, 1, 2);
QHBoxLayout* hBoxLayout = new QHBoxLayout();
hBoxLayout->addWidget(new QLabel(tr("Python Scripts:"), this));
// combobox selection
InitScriptsSelection();
hBoxLayout->addWidget(scripts_, 1);
// run button
QPushButton* pRun = new QPushButton(tr("Run"), this);
connect(pRun, &QPushButton::clicked, this, &PythonCompareDialog::OnRun);
pRun->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Minimum);
hBoxLayout->addWidget(pRun, 1);
// export button
QPushButton* pExport = new QPushButton(tr("Export"), this);
connect(pExport, &QPushButton::clicked, this, &PythonCompareDialog::OnExport);
pExport->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Minimum);
hBoxLayout->addWidget(pExport, 1);
mainGridLayout->addLayout(hBoxLayout, 2, 0, 1, 2);
logWin_->setMinimumSize(400, 200);
logWin_->setReadOnly(true);
mainGridLayout->addWidget(logWin_, 3, 0, 1, 2);
}
void PythonCompareDialog::OnBrowseFile1()
{
QString fileName = osiFile1_->text();
fileName = QFileDialog::getOpenFileName(this, tr("Load file"), fileName, tr("Text Files (*.txt)"));
if (!fileName.isEmpty())
{
osiFile1_->setText(fileName);
osiFile1_->setToolTip(fileName);
}
}
void PythonCompareDialog::OnBrowseFile2()
{
QString fileName = osiFile2_->text();
fileName = QFileDialog::getOpenFileName(this, tr("Open OSI GroundTruth Files"), "", tr("All Files (*)"));
if (!fileName.isEmpty())
{
osiFile2_->setText(fileName);
osiFile2_->setToolTip(fileName);
}
}
void PythonCompareDialog::OnRun()
{
static const std::string tmpLog = "python_compare_log.txt";
remove(tmpLog.c_str());
logWin_->clear();
QString script = scripts_->currentText();
if (script.isEmpty() == false && osiFile1_->text().isEmpty() == false && osiFile2_->text().isEmpty() == false)
{
std::string command = "python " + scriptPath_.toStdString() + "Resources/Python/";
command += script.toStdString();
command += " " + osiFile1_->text().toStdString();
command += " " + osiFile2_->text().toStdString();
command += " >> " + tmpLog;
system(command.c_str());
std::ifstream pLog(tmpLog.c_str());
if (pLog.is_open())
{
logWin_->append(tr("---- Start Running ") + script + tr(" ----"));
while (!pLog.good())
{
std::string s;
getline(pLog, s);
logWin_->append(QString::fromStdString(s));
}
logWin_->append(tr("---- End of Running ----"));
}
pLog.close();
}
else
{
QString errMsg;
if (script.isEmpty())
errMsg += "No python script found!\n";
if (osiFile1_->text().isEmpty())
errMsg += "OSI file 1 is empty!\n";
if (osiFile2_->text().isEmpty())
errMsg += "OSI file 2 is empty!\n";
QMessageBox::critical(nullptr, "Error", errMsg, QMessageBox::Ok, QMessageBox::Ok);
}
}
void PythonCompareDialog::OnExport()
{
QString fileNamePyLog =
QFileDialog::getSaveFileName(this, tr("Save Python Validation Results"), "", tr("All Files (*)"));
std::ofstream pythonLog(fileNamePyLog.toStdString().c_str());
if (pythonLog.is_open())
{
pythonLog << logWin_->toPlainText().toStdString();
}
pythonLog.close();
}
void PythonCompareDialog::InitScriptsSelection()
{
QDirIterator it("application/adp/bmw/tools/simulation/lime_osi/osi_visualizer/resources/Python/",
QStringList() << "*.py",
QDir::Files);
while (it.hasNext())
{
it.next();
scripts_->addItem(it.fileName());
}
}