1 /**
2 * projectM-qt -- Qt4 based projectM GUI
3 * Copyright (C)2003-2004 projectM Team
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 * See 'LICENSE.txt' included within this release
19 *
20 */
21
22 #include "qpreseteditordialog.hpp"
23 #include <QFile>
24 #include <QMessageBox>
25 #include <QKeyEvent>
26
QPresetEditorDialog(QProjectMWidget * widget,QWidget * parent,Qt::WindowFlags f)27 QPresetEditorDialog::QPresetEditorDialog(QProjectMWidget * widget, QWidget * parent, Qt::WindowFlags f): QDialog(parent,f), _qprojectMWidget(widget) {
28 _ui.setupUi(this);
29
30 QHBoxLayout * hboxLayout = new QHBoxLayout();
31
32 hboxLayout->addWidget(_ui.layoutWidget);
33 this->setLayout(hboxLayout);
34
35 connect(_ui.buttonBox, SIGNAL(clicked(QAbstractButton*)), this, SLOT(buttonBoxHandler(QAbstractButton*)));
36
37 }
38
setPreset(QString url,int rowIndex)39 void QPresetEditorDialog::setPreset(QString url, int rowIndex) {
40
41 disconnect(_ui.presetTextEdit, 0, this, 0);
42
43 m_index = rowIndex;
44
45 m_presetUrl = url;
46 _ui.presetTextEdit->loadPresetText(url);
47
48 this->setWindowTitle(QString("Preset Editor - %1 [*]").arg(url));
49 this->setWindowModified(false);
50
51 connect(_ui.presetTextEdit, SIGNAL(textChanged()), this, SLOT(updateWindowTitle()));
52 connect(_ui.presetTextEdit, SIGNAL(applyRequested()), this, SLOT(saveAndNotify()), Qt::DirectConnection);
53 }
54
55
updateWindowTitle()56 void QPresetEditorDialog::updateWindowTitle() {
57 this->setWindowModified(true);
58 }
59
presetUrl() const60 const QString & QPresetEditorDialog::presetUrl() const {
61 return m_presetUrl;
62 }
63
revertBuffer()64 void QPresetEditorDialog::revertBuffer() {
65 setPreset(m_presetUrl, m_index);
66 }
67
saveFile()68 void QPresetEditorDialog::saveFile() {
69 QFile qfile(presetUrl());
70
71 if (!qfile.open(QIODevice::ReadWrite | QIODevice::Text)) {
72 QMessageBox::warning (0, "Preset File Error", QString(tr("There was a problem trying to save the preset \"%1\". You may not have permission to write to the file.")).arg(presetUrl()));
73 return ;
74 }
75
76 QTextStream textStream(&qfile);
77
78 textStream << _ui.presetTextEdit->toPlainText();
79
80 textStream.flush();
81 this->setWindowModified(false);
82 }
83
saveAndNotify()84 void QPresetEditorDialog::saveAndNotify() {
85
86 qDebug() << "save and notify";
87 saveFile();
88
89 emit(presetModified(m_index));
90 }
91
buttonBoxHandler(QAbstractButton * button)92 void QPresetEditorDialog::buttonBoxHandler(QAbstractButton * button) {
93 switch (_ui.buttonBox->standardButton(button)) {
94 case QDialogButtonBox::Close:
95 this->hide();
96 break;
97 case QDialogButtonBox::Apply:
98 saveAndNotify();
99 break;
100 case QDialogButtonBox::Reset:
101 revertBuffer();
102 break;
103 default:
104 break;
105 }
106 }
107
108