summaryrefslogtreecommitdiff
path: root/utils/themeeditor/gui/configdocument.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'utils/themeeditor/gui/configdocument.cpp')
-rw-r--r--utils/themeeditor/gui/configdocument.cpp267
1 files changed, 267 insertions, 0 deletions
diff --git a/utils/themeeditor/gui/configdocument.cpp b/utils/themeeditor/gui/configdocument.cpp
new file mode 100644
index 0000000000..a897d3b9e3
--- /dev/null
+++ b/utils/themeeditor/gui/configdocument.cpp
@@ -0,0 +1,267 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2010 Robert Bieber
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
16 *
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
19 *
20 ****************************************************************************/
21
22#include "projectmodel.h"
23#include "configdocument.h"
24#include "ui_configdocument.h"
25
26#include <QMessageBox>
27#include <QFile>
28#include <QSettings>
29#include <QFileDialog>
30
31ConfigDocument::ConfigDocument(QMap<QString, QString>& settings, QString file,
32 QWidget *parent)
33 : TabContent(parent),
34 ui(new Ui::ConfigDocument),
35 filePath(file)
36{
37 ui->setupUi(this);
38
39 /* Populating the known keys list */
40 QFile fin(":/resources/configkeys");
41 fin.open(QFile::ReadOnly);
42
43 QStringList* container = &primaryKeys;
44 while(!fin.atEnd())
45 {
46 QString current = QString(fin.readLine());
47 if(current == "-\n")
48 container = &secondaryKeys;
49 else if(current != "\n")
50 container->append(current.trimmed());
51 }
52
53 QMap<QString, QString>::iterator i;
54 for(i = settings.begin(); i != settings.end(); i++)
55 if(i.key() != "themebase")
56 addRow(i.key(), i.value());
57
58 saved = toPlainText();
59
60 QObject::connect(ui->addKeyButton, SIGNAL(pressed()),
61 this, SLOT(addClicked()));
62}
63
64ConfigDocument::~ConfigDocument()
65{
66 delete ui;
67}
68
69void ConfigDocument::changeEvent(QEvent *e)
70{
71 QWidget::changeEvent(e);
72 switch (e->type()) {
73 case QEvent::LanguageChange:
74 ui->retranslateUi(this);
75 break;
76 default:
77 break;
78 }
79}
80
81QString ConfigDocument::title() const
82{
83 QStringList decompose = filePath.split("/");
84 return decompose.last();
85}
86
87void ConfigDocument::save()
88{
89 QFile fout(filePath);
90
91 if(!fout.exists())
92 {
93 saveAs();
94 return;
95 }
96
97 fout.open(QFile::WriteOnly);
98 fout.write(toPlainText().toAscii());
99 fout.close();
100
101 saved = toPlainText();
102 emit titleChanged(title());
103
104}
105
106void ConfigDocument::saveAs()
107{
108 /* Determining the directory to open */
109 QString directory = filePath;
110
111 QSettings settings;
112 settings.beginGroup("ProjectModel");
113 if(directory == "")
114 directory = settings.value("defaultDirectory", "").toString();
115
116 filePath = QFileDialog::getSaveFileName(this, tr("Save Document"),
117 directory,
118 ProjectModel::fileFilter());
119 directory = filePath;
120 if(filePath == "")
121 return;
122
123 directory.chop(filePath.length() - filePath.lastIndexOf('/') - 1);
124 settings.setValue("defaultDirectory", directory);
125 settings.endGroup();
126
127 QFile fout(filePath);
128 fout.open(QFile::WriteOnly);
129 fout.write(toPlainText().toAscii());
130 fout.close();
131
132 saved = toPlainText();
133 emit titleChanged(title());
134 emit configFileChanged(file());
135
136}
137
138bool ConfigDocument::requestClose()
139{
140 if(toPlainText() != saved)
141 {
142 /* Spawning the "Are you sure?" dialog */
143 QMessageBox confirm(this);
144 confirm.setWindowTitle(tr("Confirm Close"));
145 confirm.setText(title() + tr(" has been modified."));
146 confirm.setInformativeText(tr("Do you want to save your changes?"));
147 confirm.setStandardButtons(QMessageBox::Save | QMessageBox::Discard
148 | QMessageBox::Cancel);
149 confirm.setDefaultButton(QMessageBox::Save);
150 int confirmation = confirm.exec();
151
152 switch(confirmation)
153 {
154 case QMessageBox::Save:
155 save();
156 /* After calling save, make sure the user actually went through */
157 if(toPlainText() != saved)
158 return false;
159 else
160 return true;
161
162 case QMessageBox::Discard:
163 return true;
164
165 case QMessageBox::Cancel:
166 return false;
167 }
168 }
169 return true;
170}
171
172QString ConfigDocument::toPlainText() const
173{
174 QString buffer = "";
175
176 for(int i = 0; i < keys.count(); i++)
177 {
178 buffer += keys[i]->currentText();
179 buffer += ":";
180 buffer += values[i]->text();
181 buffer += "\n";
182 }
183
184 return buffer;
185}
186
187void ConfigDocument::addRow(QString key, QString value)
188{
189 QHBoxLayout* layout = new QHBoxLayout();
190 QComboBox* keyEdit = new QComboBox(this);
191 QLineEdit* valueEdit = new QLineEdit(value, this);
192 QPushButton* delButton = new QPushButton(tr("-"), this);
193 QLabel* label = new QLabel(":");
194
195 /* Loading the combo box options */
196 keyEdit->setInsertPolicy(QComboBox::InsertAlphabetically);
197 keyEdit->setEditable(true);
198 keyEdit->addItems(primaryKeys);
199 keyEdit->insertSeparator(keyEdit->count());
200 keyEdit->addItems(secondaryKeys);
201 if(keyEdit->findText(key) != -1)
202 keyEdit->setCurrentIndex(keyEdit->findText(key));
203 else
204 keyEdit->setEditText(key);
205
206 layout->addWidget(keyEdit);
207 layout->addWidget(label);
208 layout->addWidget(valueEdit);
209 layout->addWidget(delButton);
210
211 delButton->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Fixed);
212 delButton->setMaximumWidth(35);
213
214 QObject::connect(delButton, SIGNAL(clicked()),
215 this, SLOT(deleteClicked()));
216 QObject::connect(keyEdit, SIGNAL(currentIndexChanged(QString)),
217 this, SLOT(textChanged()));
218 QObject::connect(keyEdit, SIGNAL(textChanged(QString)),
219 this, SLOT(textChanged()));
220 QObject::connect(valueEdit, SIGNAL(textChanged(QString)),
221 this, SLOT(textChanged()));
222
223 ui->configBoxes->addLayout(layout);
224
225 containers.append(layout);
226 keys.append(keyEdit);
227 values.append(valueEdit);
228 deleteButtons.append(delButton);
229 labels.append(label);
230
231}
232
233void ConfigDocument::deleteClicked()
234{
235 QPushButton* button = dynamic_cast<QPushButton*>(sender());
236 int row = deleteButtons.indexOf(button);
237
238 deleteButtons[row]->deleteLater();
239 keys[row]->deleteLater();
240 values[row]->deleteLater();
241 containers[row]->deleteLater();
242 labels[row]->deleteLater();
243
244 deleteButtons.removeAt(row);
245 keys.removeAt(row);
246 values.removeAt(row);
247 containers.removeAt(row);
248 labels.removeAt(row);
249
250 if(saved != toPlainText())
251 emit titleChanged(title() + "*");
252 else
253 emit titleChanged(title());
254}
255
256void ConfigDocument::addClicked()
257{
258 addRow(tr("Key"), tr("Value"));
259}
260
261void ConfigDocument::textChanged()
262{
263 if(toPlainText() != saved)
264 emit titleChanged(title() + "*");
265 else
266 emit titleChanged(title());
267}