-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig_object.cpp
217 lines (183 loc) · 6.84 KB
/
config_object.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
#include "config_object.h"
#include "mainwindow.h"
#include "ui_configdialog.h"
ConfigObject::ConfigObject(QObject *parent)
: QObject{parent}
{
qDebug() << "Config object constructor entered";
QStringList strList = QStandardPaths::standardLocations(QStandardPaths::HomeLocation);
long size = strList.size();
if ( size > 1 ) {
// Something strange happened - this function should return $HOME
qDebug() << "ConfigObject constructor encountered multiple home locations - exiting";
QApplication::exit(3);
}
file.setFileName(strList.at(0) + QString("/.macrr/macremote-config.ini"));
qDebug() << "File name set to " << file.fileName();
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
qDebug() << "ConfigObject constructor failed opening filename";
QApplication::quit();
}
QTextStream in(&file);
while (!in.atEnd()) {
QString line = in.readLine();
process_line(line);
}
file.close();
// open_config_dialog();
}
void ConfigObject::process_line(QString s) {
/* Add key/value pair to our map object */
QString tmp_key = s.section(',', 0 , 0);
QString tmp_value = s.section(',', 1);
configMap.insert(tmp_key, tmp_value);
qDebug() << "process_line: " << "tmp_key = " << tmp_key << " tmp_value = " << tmp_value;
}
ConfigObject::~ConfigObject() {
qDebug() << "ConfigObject destructor called";
delete pDialog;
}
void ConfigObject::open_config_dialog() {
qDebug() << "ConfigObject::open_config_dialog(): open_config_dialog entered";
pDialog = new ConfigDialog();
pDialog->set_conf_obj_pointer(static_cast<QObject *>(this));
// Populate the ComboBox with the keys from the configMap
QMap<QString, QString>::const_iterator i = configMap.constBegin();
int index = 0;
while (i != configMap.constEnd()) {
pDialog->ui->config_ComboBox->insertItem(index, i.key());
++i; ++index;
}
pDialog->ui->config_ComboBox->setCurrentText("Rig File");
pDialog->show();
get_value_from_key();
}
void ConfigObject::get_value_from_key() {
#if 0
// Display the entire map
QMap<QString, QString>::const_iterator i = configMap.constBegin();
int j = 0;
while (i != configMap.constEnd()) {
qDebug() << "Map[" << j << "]:" << i.key() << ": " << i.value();
++i; j++;
}
#endif
// Get the key currently being displayed by the Combo Box
current_key = pDialog->ui->config_ComboBox->currentText();
qDebug() << "key currently being displayed by the Combo Box: " << current_key;
// Find the key in the map to get it's corresponding value - second parm is default if key not found
current_value = configMap.value(current_key, "<no value>");
// Populate the value in the text edit field
pDialog->ui->config_LineEdit->setText("");
pDialog->ui->config_LineEdit->insert(current_value);
qDebug() << "ConfigObject::get_value_from_key(): current_key= " << current_key << "current_value= " << current_value;
return;
}
void ConfigObject::key_value_changed(int index) {
qDebug() << "key_value_changed signalled to ConfigObject: index = " << index;
get_value_from_key();
}
void ConfigObject::store_new_key_value() {
QString key = pDialog->ui->config_ComboBox->currentText();
if ( key.isEmpty() ) {
// Do not store an empty key/value pair
return;
}
QString s = pDialog->ui->config_LineEdit->text();
configMap.insert(current_key, s);
qDebug() << "ConfigObject::store_new_key_value() - current key = " << current_key << "New value = " << configMap.value(current_key);
// Now write the new map to disk
write_map_to_disk();
}
void ConfigObject::delete_key_value() {
QString key = pDialog->ui->config_ComboBox->currentText();
qDebug() << "ConfigObject::delete_key_value() - key is " << key;
long sz = configMap.remove(key);
if ( sz != 1 ) {
qDebug() << "ConfigObject::delete_key_value() failed";
QApplication::quit();
}
// Clear the combo box - the key value has been deleted
pDialog->ui->config_ComboBox->clear();
qDebug() << "ConfigObject::delete_key_value() - deleted " << sz << " key/value pair";
pDialog->ui->messageLabel->setText("Key/Value pair deleted");
debug_display_map();
write_map_to_disk();
}
void ConfigObject::debug_display_map() {
// Print the current map
QMap<QString, QString>::const_iterator i = configMap.constBegin();
int j = 0;
while (i != configMap.constEnd()) {
qDebug() << "Map[" << j << "]:" << i.key() << ": " << i.value();
++i; ++j;
}
}
void ConfigObject::write_map_to_disk() {
// Serialize the map to the disk file
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
qDebug() << "ConfigObject constructor failed opening filename";
QApplication::quit();
}
QTextStream out(&file);
QMap<QString, QString>::const_iterator i = configMap.constBegin();
int j = 0;
while (i != configMap.constEnd()) {
out << i.key() << ',' << i.value() << Qt::endl;
qDebug() << "Map[" << j << "]:" << i.key() << ": " << i.value();
++i; ++j;
}
file.close();
}
QString ConfigObject::get_value_from_key(QString &key) {
return configMap.value(key);
}
/*
*****************************************************
**************** Class ConfigDialog *****************
*****************************************************
*/
ConfigDialog::ConfigDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::ConfigDialog)
{
ui->setupUi(this);
qDebug() << "ConfigDialog constructor entered - parent pointer = " << parent;
}
ConfigDialog::~ConfigDialog()
{
delete ui;
}
void ConfigDialog::set_conf_obj_pointer(QObject *p) {
config_obj_p = p;
}
void ConfigDialog::on_config_buttonBox_clicked()
{
// This is the "Cancel" button in the config dialog
qDebug() << "on_config_buttonBox_clicked() - exiting";
ConfigObject *cop = static_cast<ConfigObject *>(config_obj_p);
cop->debug_display_map();
}
void ConfigDialog::on_config_LineEdit_textEdited(const QString &arg1)
{
// Generated when any text is edited in the line edit widget
qDebug() << "on_config_LineEdit_textEdited(const QString &arg1) entered: " << arg1;
}
void ConfigDialog::on_config_ComboBox_currentIndexChanged(int index)
{
// Selection has been made in the config dialog "Key" Combo Box
ConfigObject *cop = static_cast<ConfigObject *>(config_obj_p);
cop->key_value_changed(index);
}
void ConfigDialog::on_config_buttonBox_accepted()
{
// Store the new value to this map (key) entry
qDebug() << "User pressed OK";
ConfigObject *cop = static_cast<ConfigObject *>(config_obj_p);
cop->store_new_key_value();
}
void ConfigDialog::on_delete_key_pbutton_clicked()
{
ConfigObject *cop = static_cast<ConfigObject *>(config_obj_p);
cop->delete_key_value();
}