1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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>
29 #include <QFileDialog>
31 ConfigDocument::ConfigDocument(QMap
<QString
, QString
>& settings
, QString file
,
34 ui(new Ui::ConfigDocument
),
39 /* Populating the known keys list */
40 QFile
fin(":/resources/configkeys");
41 fin
.open(QFile::ReadOnly
);
43 QStringList
* container
= &primaryKeys
;
46 QString current
= QString(fin
.readLine());
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()
69 void ConfigDocument::changeEvent(QEvent
*e
)
71 QWidget::changeEvent(e
);
73 case QEvent::LanguageChange
:
74 ui
->retranslateUi(this);
81 QString
ConfigDocument::title() const
83 QStringList decompose
= filePath
.split("/");
84 return decompose
.last();
87 void ConfigDocument::save()
97 fout
.open(QFile::WriteOnly
);
98 fout
.write(toPlainText().toAscii());
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
;
112 settings
.beginGroup("ProjectModel");
114 directory
= settings
.value("defaultDirectory", "").toString();
116 filePath
= QFileDialog::getSaveFileName(this, tr("Save Document"),
118 ProjectModel::fileFilter());
119 directory
= filePath
;
123 directory
.chop(filePath
.length() - filePath
.lastIndexOf('/') - 1);
124 settings
.setValue("defaultDirectory", directory
);
127 QFile
fout(filePath
);
128 fout
.open(QFile::WriteOnly
);
129 fout
.write(toPlainText().toAscii());
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();
154 case QMessageBox::Save
:
156 /* After calling save, make sure the user actually went through */
157 if(toPlainText() != saved
)
162 case QMessageBox::Discard
:
165 case QMessageBox::Cancel
:
172 QString
ConfigDocument::toPlainText() const
176 for(int i
= 0; i
< keys
.count(); i
++)
178 buffer
+= keys
[i
]->currentText();
180 buffer
+= values
[i
]->text();
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
));
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
);
247 values
.removeAt(row
);
248 containers
.removeAt(row
);
249 labels
.removeAt(row
);
251 if(saved
!= toPlainText())
252 emit
titleChanged(title() + "*");
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() + "*");
267 emit
titleChanged(title());