-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlabprofiledialog.cpp
More file actions
309 lines (267 loc) · 10.6 KB
/
labprofiledialog.cpp
File metadata and controls
309 lines (267 loc) · 10.6 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
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
#include "labprofiledialog.h"
#include "ui_labprofiledialog.h"
LabProfileDialog::LabProfileDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::LabProfileDialog)
{
ui->setupUi(this);
QDir appDir = QApplication::applicationDirPath();
settings = new QSettings(appDir.absoluteFilePath("settings.ini"),QSettings::IniFormat);
profWidget = new ProfileTreeWidget(this);
ui->parameterLayout->addWidget(profWidget);
profiles = new QMap<QString,QSettings*>();
loadProfiles();
loadPreDefined();
}
LabProfileDialog::~LabProfileDialog()
{
delete ui;
}
void LabProfileDialog::loadProfiles()
{
QDir profileDir(QApplication::applicationDirPath());
profileDir.cd("profiles");
profileDir.setNameFilters(QStringList("*.ini"));
profileDir.setFilter(QDir::Files);
QStringList profileFileNames = profileDir.entryList();
if(profileFileNames.length() > 0) {
for(QString fn : profileFileNames) {
QSettings *s = new QSettings(profileDir.absoluteFilePath(fn),QSettings::IniFormat);
QString profName = s->value("profilename",fn).toString();
ui->profileListWidget->addItem(profName);
profiles->insert(profName,s);
}
QString defaultProfile = settings->value("selectedProfile").toString();
if(defaultProfile != "") {
selectLabProfile(defaultProfile);
} else {
selectLabProfile(ui->profileListWidget->item(0)->text());
}
}
}
void LabProfileDialog::on_profileListWidget_itemDoubleClicked(QListWidgetItem *item)
{
selectLabProfile(item->text());
}
void LabProfileDialog::selectLabProfile(QString profileName) {
if(selectedProfile == profileName)
return;
qDebug() << "switching to profile: " << profileName;
// save old profile
if(selectedProfile != "" & selectedProfile != profileName)
saveProfile();
QList<QListWidgetItem*> oldItem = ui->profileListWidget->findItems(selectedProfile,Qt::MatchExactly);
if(oldItem.length()>0) {
QFont f = oldItem.at(0)->font();
f.setBold(false);
oldItem.at(0)->setFont(f);
}
QList<QListWidgetItem*> pList = ui->profileListWidget->findItems(profileName,Qt::MatchExactly);
if(pList.length()>0) {
QFont ff = pList.at(0)->font();
ff.setBold(true);
pList.at(0)->setFont(ff);
}
// Logic to load data into tree widget
QSettings* s = profiles->value(profileName);
if(s == nullptr) {
qDebug() << "selectProfile: failed to get settings for profileName " << profileName;
return;
}
selectedProfile = profileName;
settings->setValue("selectedProfile",selectedProfile);
ui->profileLabel->setText("Profile: "+profileName);
profWidget->clear();
if(!s->childGroups().contains("tables")) {
qDebug() << "profile does not contain tables";
return;
}
s->beginGroup("tables");
QStringList tables = s->childGroups();
for(QString table : tables) {
qDebug() << "loading table: " << table;
QTreeWidgetItem *tItem = new QTreeWidgetItem(profWidget);
tItem->setText(0,table);
tItem->setData(0,Qt::UserRole,"table");
s->beginGroup(table);
tItem->setData(0,0x0101,s->value("orientation").toString());
QStringList order = s->value("order").toStringList();
s->beginGroup("parameters");
for(QString param : order) {
QTreeWidgetItem *pItem = new QTreeWidgetItem(tItem);
pItem->setText(0,param);
pItem->setData(0,Qt::UserRole,"param");
QStringList codes = s->value(param).toStringList();
for(QString code : codes){
QTreeWidgetItem *cItem = new QTreeWidgetItem(pItem);
cItem->setText(0,code);
cItem->setData(0,Qt::UserRole,"code");
QIcon icon(":/icons/codes.png");
cItem->setIcon(0,icon);
pItem->addChild(cItem);
}
QIcon vial(":/icons/vial.png");
pItem->setIcon(0,vial);
tItem->addChild(pItem);
}
s->endGroup();
QIcon tIcon(":/icons/table.png");
tItem->setIcon(0,tIcon);
profWidget->addTopLevelItem(tItem);
s->endGroup();
}
s->endGroup();
}
bool LabProfileDialog::createProfileFile(QString profileName)
{
QDir profileDir(QApplication::applicationDirPath());
profileDir.cd("profiles");
profileDir.setNameFilters(QStringList("*.ini"));
profileDir.setFilter(QDir::Files);
QStringList profileFileNames = profileDir.entryList();
if(profileFileNames.contains(profileName+".ini")) {
qDebug() << "Profile already exists";
return false;
}
qDebug() << "createProfileFile: adding new file for profilename: " << profileName << " filedir: "
<< profileDir.absoluteFilePath(profileName);
QSettings *s = new QSettings(profileDir.absoluteFilePath(profileName+".ini"),QSettings::IniFormat);
s->setValue("version","2.0");
s->setValue("profilename",profileName);
s->sync();
profiles->insert(profileName,s);
return true;
}
void LabProfileDialog::saveProfile() {
if(selectedProfile.isEmpty()) {
qDebug() << "no profiles to save!";
return;
}
QSettings* s = profiles->value(selectedProfile);
if(s == nullptr) {
qDebug() << "could not find settings for profile: " << selectedProfile;
return;
}
s->beginGroup("tables");
for(int t = 0; t < profWidget->topLevelItemCount(); t++) {
QTreeWidgetItem *topItem = profWidget->topLevelItem(t);
QString orientation = topItem->data(0,0x0101).toString();
qDebug() << "saveProfile: saving table " << topItem->text(0)
<< " with orientation " << orientation
<< " childCount is " << topItem->childCount();
QStringList order;
s->beginGroup(topItem->text(0));
s->beginGroup("parameters");
for(int c = 0; c < topItem->childCount(); c++) {
QTreeWidgetItem *param = topItem->child(c);
order.append(param->text(0));
QStringList codes;
for(int i = 0; i < param->childCount(); i++) {
codes.append(param->child(i)->text(0));
}
s->setValue(param->text(0),codes);
}
s->endGroup();
s->setValue("order",order);
s->setValue("orientation",orientation);
s->endGroup();
}
s->endGroup();
}
void LabProfileDialog::saveParameters() {
}
void LabProfileDialog::loadPreDefined()
{
QDir profileDir(QApplication::applicationDirPath());
profileDir.cd("profiles");
profileDir.setNameFilters(QStringList("*.txt"));
profileDir.setFilter(QDir::Files);
QStringList profileFileNames = profileDir.entryList();
QList<QString> addedCodes;
if(profileFileNames.length() > 0) {
for(QString fn : profileFileNames) {
QFile f = QFile(profileDir.absoluteFilePath(fn));
f.open(QFile::ReadOnly);
while(!f.atEnd()) {
QString line = QString(f.readLine());
line = line.replace("\r","").replace("\n","");
QStringList split = line.split("\t");
//qDebug() << "split is: " << split;
if(split.length()<2)
continue;
QString code = split.at(0);
QString name = split.at(1);
if(addedCodes.contains(code)) {
qDebug() << "code already added: " << code;
continue;
}
addedCodes.append(code);
QTreeWidgetItem *pItem = new QTreeWidgetItem(ui->preDefinedTreeWidget);
pItem->setText(0,name);
pItem->setData(0,Qt::UserRole,"param");
QStringList codes(code);
for(QString code : codes){
QTreeWidgetItem *cItem = new QTreeWidgetItem(pItem);
cItem->setText(0,code);
cItem->setData(0,Qt::UserRole,"code");
QIcon icon(":/icons/codes.png");
cItem->setIcon(0,icon);
pItem->addChild(cItem);
}
QIcon vial(":/icons/vial.png");
pItem->setIcon(0,vial);
ui->preDefinedTreeWidget->addTopLevelItem(pItem);
}
}
}
}
void LabProfileDialog::on_profileListWidget_customContextMenuRequested(const QPoint &pos)
{
QListWidgetItem* item = ui->profileListWidget->itemAt(pos);
qDebug() << "Profile List: Custom Context Menu requested at position: " << pos;
QMenu *menu = new QMenu(this);
if(item==nullptr) {
qDebug() << "item is nullptr";
QAction *addChild = new QAction(tr("Add profile"), this);
connect(addChild, &QAction::triggered, this, [=]() {
bool ok;
QString param = QInputDialog::getText(this, tr("Enter profile name"),tr("Name:"),QLineEdit::Normal,"",&ok);
if(ok && !param.isEmpty()) {
qDebug() << "adding new profile: " << param;
if(createProfileFile(param)) {
QListWidgetItem* item = new QListWidgetItem(ui->profileListWidget);
item->setText(param);
ui->profileListWidget->addItem(item);
if(selectedProfile.isEmpty()) {
qDebug() << "no previous profile selected, selecting newly created profile: " << param;
selectLabProfile(param);
}
} else {
qDebug() << "failed to add new profile: reason exists";
}
}
});
menu->addAction(addChild);
} else {
QAction *removeItem = new QAction(tr("Delete profile"),this);
removeItem->setStatusTip(tr("Removes the selected profile"));
connect(removeItem, &QAction::triggered, this, [=]() {
delete item;
});
menu->addAction(removeItem);
}
menu->popup(ui->profileListWidget->viewport()->mapToGlobal(pos));
}
void LabProfileDialog::on_okButton_clicked()
{
saveProfile();
saveParameters();
this->accept();
}
void LabProfileDialog::on_cancelButton_clicked()
{
this->reject();
}
void LabProfileDialog::on_predefinedTreeWidget_customContextMenuRequested(const QPoint &pos)
{
}