Theme Editor: Stopped combo boxes in configuration editor from scrolling on mouse...
[kugel-rb.git] / utils / themeeditor / gui / configdocument.cpp
blob0962484ba9675f27b570a650462f97c6a69e4ca5
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2010 Robert Bieber
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.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
22 #include "projectmodel.h"
23 #include "configdocument.h"
24 #include "ui_configdocument.h"
26 #include <QMessageBox>
27 #include <QFile>
28 #include <QSettings>
29 #include <QFileDialog>
31 ConfigDocument::ConfigDocument(QMap<QString, QString>& settings, QString file,
32 QWidget *parent)
33 : TabContent(parent),
34 ui(new Ui::ConfigDocument),
35 filePath(file)
37 ui->setupUi(this);
39 /* Populating the known keys list */
40 QFile fin(":/resources/configkeys");
41 fin.open(QFile::ReadOnly);
43 QStringList* container = &primaryKeys;
44 while(!fin.atEnd())
46 QString current = QString(fin.readLine());
47 if(current == "-\n")
48 container = &secondaryKeys;
49 else if(current != "\n")
50 container->append(current.trimmed());
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());
58 saved = toPlainText();
60 QObject::connect(ui->addKeyButton, SIGNAL(pressed()),
61 this, SLOT(addClicked()));
64 ConfigDocument::~ConfigDocument()
66 delete ui;
69 void ConfigDocument::changeEvent(QEvent *e)
71 QWidget::changeEvent(e);
72 switch (e->type()) {
73 case QEvent::LanguageChange:
74 ui->retranslateUi(this);
75 break;
76 default:
77 break;
81 QString ConfigDocument::title() const
83 QStringList decompose = filePath.split("/");
84 return decompose.last();
87 void ConfigDocument::save()
89 QFile fout(filePath);
91 if(!fout.exists())
93 saveAs();
94 return;
97 fout.open(QFile::WriteOnly);
98 fout.write(toPlainText().toAscii());
99 fout.close();
101 saved = toPlainText();
102 emit titleChanged(title());
103 emit configFileChanged(file());
106 void ConfigDocument::saveAs()
108 /* Determining the directory to open */
109 QString directory = filePath;
111 QSettings settings;
112 settings.beginGroup("ProjectModel");
113 if(directory == "")
114 directory = settings.value("defaultDirectory", "").toString();
116 filePath = QFileDialog::getSaveFileName(this, tr("Save Document"),
117 directory,
118 ProjectModel::fileFilter());
119 directory = filePath;
120 if(filePath == "")
121 return;
123 directory.chop(filePath.length() - filePath.lastIndexOf('/') - 1);
124 settings.setValue("defaultDirectory", directory);
125 settings.endGroup();
127 QFile fout(filePath);
128 fout.open(QFile::WriteOnly);
129 fout.write(toPlainText().toAscii());
130 fout.close();
132 saved = toPlainText();
133 emit titleChanged(title());
134 emit configFileChanged(file());
138 bool ConfigDocument::requestClose()
140 if(toPlainText() != saved)
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();
152 switch(confirmation)
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;
162 case QMessageBox::Discard:
163 return true;
165 case QMessageBox::Cancel:
166 return false;
169 return true;
172 QString ConfigDocument::toPlainText() const
174 QString buffer = "";
176 for(int i = 0; i < keys.count(); i++)
178 buffer += keys[i]->currentText();
179 buffer += ":";
180 buffer += values[i]->text();
181 buffer += "\n";
184 return buffer;
187 void ConfigDocument::addRow(QString key, QString value)
189 QHBoxLayout* layout = new QHBoxLayout();
190 NoScrollCombo* keyEdit = new NoScrollCombo(this);
191 QLineEdit* valueEdit = new QLineEdit(value, this);
192 QPushButton* delButton = new QPushButton(tr("-"), this);
193 QLabel* label = new QLabel(":");
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);
207 layout->addWidget(keyEdit);
208 layout->addWidget(label);
209 layout->addWidget(valueEdit);
210 layout->addWidget(delButton);
212 delButton->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Fixed);
213 delButton->setMaximumWidth(35);
215 QObject::connect(delButton, SIGNAL(clicked()),
216 this, SLOT(deleteClicked()));
217 QObject::connect(keyEdit, SIGNAL(currentIndexChanged(QString)),
218 this, SLOT(textChanged()));
219 QObject::connect(keyEdit, SIGNAL(textChanged(QString)),
220 this, SLOT(textChanged()));
221 QObject::connect(valueEdit, SIGNAL(textChanged(QString)),
222 this, SLOT(textChanged()));
224 ui->configBoxes->addLayout(layout);
226 containers.append(layout);
227 keys.append(keyEdit);
228 values.append(valueEdit);
229 deleteButtons.append(delButton);
230 labels.append(label);
234 void ConfigDocument::deleteClicked()
236 QPushButton* button = dynamic_cast<QPushButton*>(sender());
237 int row = deleteButtons.indexOf(button);
239 deleteButtons[row]->deleteLater();
240 keys[row]->deleteLater();
241 values[row]->deleteLater();
242 containers[row]->deleteLater();
243 labels[row]->deleteLater();
245 deleteButtons.removeAt(row);
246 keys.removeAt(row);
247 values.removeAt(row);
248 containers.removeAt(row);
249 labels.removeAt(row);
251 if(saved != toPlainText())
252 emit titleChanged(title() + "*");
253 else
254 emit titleChanged(title());
257 void ConfigDocument::addClicked()
259 addRow(tr("Key"), tr("Value"));
262 void ConfigDocument::textChanged()
264 if(toPlainText() != saved)
265 emit titleChanged(title() + "*");
266 else
267 emit titleChanged(title());