-
Notifications
You must be signed in to change notification settings - Fork 0
/
ngav.cpp
242 lines (202 loc) · 6.4 KB
/
ngav.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
// Copyright 2020-2023 VMware, Inc.
// SPDX-License-Identifier: MIT
#include <QSettings>
#include <QMessageBox>
#include <QFile>
#include <QProcess>
#include <QComboBox>
#include <QStandardPaths>
#include "ngav.h"
#include "ui_ngav.h"
#include "difftool.h"
#include "copy.h"
#include "policies.h"
const int max_inst_display = 2;
Ngav::Ngav(QWidget *parent) :
QWidget(parent),
ui(new Ui::Ngav)
{
ui->setupUi(this);
ui->checkBox_show_logs->setVisible(false);
start();
}
Ngav::~Ngav()
{
delete ui;
}
void Ngav::on_pushButton_connect_clicked()
{
// clear policy in selected policy table
ui->tableWidget_selection->clearContents();
ui->tableWidget_selection->setRowCount(0);
updateButtons();
emit refresh();
}
void Ngav::on_checkBox_show_logs_stateChanged(int checked)
{
if (checked) {
ui->logTextEdit->setVisible(true);
} else {
ui->logTextEdit->setVisible(false);
}
}
void Ngav::log(const QString & text)
{
ui->checkBox_show_logs->setVisible(true);
ui->checkBox_show_logs->setChecked(true);
ui->logTextEdit->append(text);
}
void Ngav::on_pushButton_compare_clicked()
{
QSettings settings;
QString program(settings.value("diff_tool", "").toString());
if (QFile(program).exists() == false) {
ui->checkBox_show_logs->setVisible(true);
ui->checkBox_show_logs->setChecked(true);
ui->logTextEdit->append(tr("Please set a diff tool"));
return;
}
QProcess *myProcess = new QProcess(this);
QStringList arguments;
for (int row=0; row < ui->tableWidget_selection->rowCount(); row++)
{
QString f;
f += QStandardPaths::writableLocation(QStandardPaths::TempLocation);
f += QString("/%1_%2.json")
.arg(ui->tableWidget_selection->item(row, 0)->text())
.arg(ui->tableWidget_selection->item(row, 1)->text());
arguments << f;
}
myProcess->start(program, arguments);
}
QStringList Ngav::get_instances_list()
{
QStringList inst_list;
QSettings settings;
int size = settings.beginReadArray("instances");
for (int i = 0; i < size; ++i) {
settings.setArrayIndex(i);
inst_list << settings.value("name").toString();
}
settings.endArray();
return inst_list;
}
void Ngav::add_pol(const QString & a, const QString & b)
{
int row = ui->tableWidget_selection->rowCount() + 1;
ui->tableWidget_selection->setRowCount(row--);
QTableWidgetItem *newItem_inst = new QTableWidgetItem(a);
QTableWidgetItem *newItem_pol = new QTableWidgetItem(b);
ui->tableWidget_selection->setItem(row, 0, newItem_inst);
ui->tableWidget_selection->setItem(row, 1, newItem_pol);
updateButtons();
ui->tableWidget_selection->resizeColumnToContents(0);
}
void Ngav::del_pol(const QString & a, const QString & b)
{
for (int i = 0; i < ui->tableWidget_selection->rowCount(); i++) {
if ((a == ui->tableWidget_selection->item(i, 0)->text())
&& (b == ui->tableWidget_selection->item(i, 1)->text())) {
ui->tableWidget_selection->removeRow(i);
updateButtons();
return;
}
}
log("Can't delete, not found" + a + " -- " + b);
updateButtons();
}
void Ngav::updateButtons()
{
ui->pushButton_unselect->setEnabled(true);
ui->pushButton_copy->setEnabled(true);
int rows = ui->tableWidget_selection->rowCount();
switch (rows) {
case 0:
ui->pushButton_compare->setEnabled(false);
ui->pushButton_copy->setEnabled(false);
ui->pushButton_unselect->setEnabled(false);
break;
case 1:
ui->pushButton_compare->setEnabled(false);
break;
case 2:
case 3:
ui->pushButton_compare->setEnabled(true);
break;
default:
ui->pushButton_compare->setEnabled(false);
}
}
void Ngav::on_pushButton_unselect_clicked()
{
emit unselect_all();
ui->tableWidget_selection->clearContents();
ui->tableWidget_selection->setRowCount(0);
}
void Ngav::start()
{
ui->logTextEdit->setVisible(false);
ui->tableWidget_selection->horizontalHeader()->setStretchLastSection(true);
ui->pushButton_compare->setToolTip(tr("Max 3 files"));
updateButtons();
inst_list = get_instances_list();
int n = qMin(inst_list.size(), max_inst_display);
// Create n instances !
for (int i=0; i < n; i++)
{
QVBoxLayout *layout = new QVBoxLayout();
QComboBox * qcb = new QComboBox(this);
Policies * pol = new Policies(this);
Widgets.append(qcb);
Widgets.append(pol);
for (int inst=0; inst < inst_list.size(); inst++) {
qcb->addItem(inst_list[inst]);
}
qcb->setCurrentIndex(i);
layout->addWidget(qcb);
layout->addWidget(pol);
ui->horizontalLayout_policies->addLayout(layout);
// connect combobox to policies...
connect(qcb, SIGNAL(currentTextChanged(const QString &)),
pol, SLOT(set_instance_name(const QString &)));
// connect policies to Ngav
connect(pol, SIGNAL(pol_selected(const QString &, const QString &)),
this, SLOT(add_pol(const QString &, const QString &)));
connect(pol, SIGNAL(pol_deselected(const QString &, const QString &)),
this, SLOT(del_pol(const QString &, const QString &)));
connect(pol, SIGNAL(log(const QString &)), this, SLOT(log(const QString &)));
// connect Ngav to policies
connect(this, SIGNAL(unselect_all()), pol, SLOT(unselect_all()));
connect(this, SIGNAL(refresh()), pol, SLOT(refresh()));
pol->set_instance_name(inst_list[i]);
}
}
void Ngav::clear()
{
foreach(QWidget * child, Widgets)
{
delete child;
}
Widgets.clear();
}
void Ngav::on_pushButton_copy_clicked()
{
Copy *copy_window = new Copy(this);
connect(copy_window, SIGNAL(log(const QString &)), this, SLOT(log(const QString &)));
copy_window->setModal(true);
copy_window->set_inst_list(inst_list);
QStringList arguments;
for (int row=0; row < ui->tableWidget_selection->rowCount(); row++)
{
copy_window->add_pol(ui->tableWidget_selection->item(row, 0)->text(),
ui->tableWidget_selection->item(row, 1)->text());
}
copy_window->exec();
on_pushButton_connect_clicked();
}
void Ngav::on_pushButton_difftool_clicked()
{
DiffTool *d = new DiffTool(this);
d->setModal(true);
d->show();
}