This repository has been archived by the owner on Nov 9, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
SettingsWidget.cpp
214 lines (170 loc) · 6.09 KB
/
SettingsWidget.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
// Copyright (C) 2012-2015, Ali Baharev
// All rights reserved.
// This code is published under the GNU Lesser General Public License.
#include <QDebug>
#include <QWidget>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QLabel>
#include <QFile>
#include <QFileInfo>
#include "SettingsWidget.hpp"
#include "OptionWidget.hpp"
#include "Option.hpp"
SettingsWidget::SettingsWidget(QWidget* mainWindow) : QWidget(mainWindow) {
QFrame* left = new QFrame(this);
QFrame* right = new QFrame(this);
leftColumn = new QVBoxLayout(left);
rightColumn = new QVBoxLayout(right);
int n_groups = numberOfGroups();
int left_size = (n_groups+1) / 2;
addInvisibleRunModeWidget();
fillColumn(leftColumn, 1, left_size); // Skip the run mode group
fillColumn(rightColumn, left_size, n_groups);
left->setFrameStyle(QFrame::WinPanel | QFrame::Raised);
right->setFrameStyle(QFrame::WinPanel | QFrame::Raised);
panel = new QHBoxLayout(this);
panel->addWidget(left);
panel->addWidget(right);
panel->addStretch(1);
setLayout(panel);
foreach (OptionWidget* widget, optionWidgets)
cliKeyToOptionWidget[widget->cliKey()] = widget;
// TODO The well group hack: disabled these options
// in field mode.
collectWellGroupWidgets();
disableWellGroup(true);
}
void SettingsWidget::fillColumn(QVBoxLayout* col, int group_first, int group_end) {
const QVector<OptionGroup>& optionGroups = getOptionGroups();
for (int i=group_first; i<group_end; ++i) {
QString name = optionGroups.at(i).first;
col->addWidget(new QLabel("<b>"+name+"</b><hr/>"));
QVector<Option> options = optionGroups.at(i).second;
foreach (Option opt, options) {
opt.checkConsistency();
OptionWidget* optWidget = new OptionWidget(this, opt);
optionWidgets.push_back(optWidget);
col->addWidget(optWidget);
}
if (i!=group_end-1) {
QFrame* hline = new QFrame();
hline->setFrameShape(QFrame::HLine);
col->addWidget(hline);
}
}
col->addStretch(1);
}
void SettingsWidget::reset_defaults() {
foreach (OptionWidget* widget , optionWidgets) {
widget->selectDefault();
}
}
//-----------------------------------------------------------------------------
// Hacks concerning the run mode
void SettingsWidget::addInvisibleRunModeWidget() {
OptionWidget* optWidget = new OptionWidget(this, getOptionGroups().at(0).second.at(0));
optWidget->setVisible(false);
Q_ASSERT(optionWidgets.empty());
optionWidgets.push_back(optWidget);
}
void SettingsWidget::collectWellGroupWidgets() {
// count the first and the last index of the well
// group options in optionWidgets
const QVector<OptionGroup>& optionGroups = getOptionGroups();
int begin = 0;
int well_group_index = wellGroupIndex();
for (int i=0; i<well_group_index; ++i)
begin += optionGroups.at(i).second.size();
int end = begin + optionGroups.at(well_group_index).second.size();
for (int i=begin; i<end; ++i)
wellGroup.push_back(optionWidgets.at(i));
}
void SettingsWidget::setRunMode(bool isWell) {
// Assumes that index 0 is no, and 1 is yes, compare with
// Option.cpp mode_opts[]
optionWidgets.at(0)->setCurrentIndex(isWell ? 1 : 0);
disableWellGroup(!isWell);
}
bool SettingsWidget::isWell() const {
// Compare with setRunMode() just above
return optionWidgets.at(0)->getCurrentIndex() == 1;
}
void SettingsWidget::disableWellGroup(bool flag) {
foreach (OptionWidget* widget , wellGroup) {
widget->setForbidden(flag);
}
}
//-----------------------------------------------------------------------------
void SettingsWidget::newProjectSelected(const QString& newProjectPath,
const QString& newProjectName)
{
projectPath = newProjectPath;
projectName = newProjectName;
bool loadedCleanly = tryLoadSettings();
emit newSettingsFileLoaded(isWell(), loadedCleanly);
}
// returns: loadedCleanly
bool SettingsWidget::tryLoadSettings() {
reset_defaults();
setFileName = projectPath + "/" + projectName + ".set";
QFileInfo setFile = QFileInfo(setFileName);
if (setFile.exists() && setFile.isFile()) {
qDebug() << "Corresponding .set file found";
return loadSettings();
}
else {
qDebug() << setFileName << " not found";
return false;
}
}
void SettingsWidget::writeSettings() {
QFile setFile(setFileName);
if (!setFile.open(QIODevice::WriteOnly | QIODevice::Text)) {
return;
}
QTextStream out(&setFile);
foreach (OptionWidget* widget , optionWidgets) {
QString line = widget->selection2CLI();
out << line << '\n';
}
qDebug() << "Settings dumped";
}
// return: loadedCleanly
bool SettingsWidget::loadSettings() {
QFile file(setFileName);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
qDebug() << "Failed to open " << setFileName;
return false;
}
bool isOK = true;
QTextStream in(&file);
while (!in.atEnd()) {
// Compare with Option::toCLIString() where the file is written
QString line = in.readLine();
const QStringList opt = line.split(":\t", QString::SkipEmptyParts);
if (opt.size()!=2) {
isOK = false;
qDebug() << "Failed to split into key-value pair:" << line;
continue;
}
const QString& cliKey = opt.at(0);
if (!cliKeyToOptionWidget.contains(cliKey)) {
isOK = false;
qDebug() << "Key" << cliKey << "not found for line:" << line;
continue;
}
OptionWidget* widget = cliKeyToOptionWidget.value(cliKey);
const QString& value = opt.at(1);
int pos = widget->indexOf(value);
if (pos >= 0)
widget->setCurrentIndex(pos);
else {
isOK = false;
qDebug() << "Value" << value << "not found for line:" << line;
}
}
if (isOK)
qDebug() << "The file" << setFileName << "loaded cleanly";
return isOK;
}