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 "skindocument.h"
27 #include <QMessageBox>
28 #include <QFileDialog>
30 SkinDocument::SkinDocument(QWidget
*parent
) : QWidget(parent
)
39 SkinDocument::SkinDocument(QString file
, QWidget
*parent
):
40 QWidget(parent
), fileName(file
)
44 /* Loading the file */
45 if(QFile::exists(fileName
))
48 fin
.open(QFile::ReadOnly
);
49 editor
->document()->setPlainText(QString(fin
.readAll()));
50 saved
= editor
->document()->toPlainText();
54 /* Setting the title */
55 QStringList decomposed
= fileName
.split('/');
56 title
= decomposed
.last();
59 SkinDocument::~SkinDocument()
65 void SkinDocument::connectPrefs(PreferencesDialog
* prefs
)
67 QObject::connect(prefs
, SIGNAL(accepted()),
68 this, SLOT(settingsChanged()));
69 QObject::connect(prefs
, SIGNAL(accepted()),
70 highlighter
, SLOT(loadSettings()));
73 bool SkinDocument::requestClose()
75 if(editor
->document()->toPlainText() != saved
)
77 /* Spawning the "Are you sure?" dialog */
78 QMessageBox
confirm(this);
79 confirm
.setWindowTitle(tr("Confirm Close"));
80 confirm
.setText(title
+ tr(" has been modified."));
81 confirm
.setInformativeText(tr("Do you want to save your changes?"));
82 confirm
.setStandardButtons(QMessageBox::Save
| QMessageBox::Discard
83 | QMessageBox::Cancel
);
84 confirm
.setDefaultButton(QMessageBox::Save
);
85 int confirmation
= confirm
.exec();
89 case QMessageBox::Save
:
91 /* After calling save, make sure the user actually went through */
92 if(editor
->document()->toPlainText() != saved
)
97 case QMessageBox::Discard
:
100 case QMessageBox::Cancel
:
108 void SkinDocument::setupUI()
110 /* Setting up the text edit */
111 layout
= new QHBoxLayout
;
112 editor
= new QPlainTextEdit(this);
113 editor
->setLineWrapMode(QPlainTextEdit::NoWrap
);
114 layout
->addWidget(editor
);
118 /* Attaching the syntax highlighter */
119 highlighter
= new SkinHighlighter(editor
->document());
121 /* Setting up the model */
122 model
= new ParseTreeModel("");
124 /* Connecting the editor's signal */
125 QObject::connect(editor
, SIGNAL(textChanged()),
126 this, SLOT(codeChanged()));
131 void SkinDocument::settingsChanged()
133 /* Setting the editor colors */
135 settings
.beginGroup("SkinDocument");
137 QColor fg
= settings
.value("fgColor", Qt::black
).value
<QColor
>();
138 QColor bg
= settings
.value("bgColor", Qt::white
).value
<QColor
>();
140 palette
.setColor(QPalette::All
, QPalette::Base
, bg
);
141 palette
.setColor(QPalette::All
, QPalette::Text
, fg
);
143 editor
->setPalette(palette
);
145 /* Setting the font */
146 QFont family
= settings
.value("fontFamily", QFont()).value
<QFont
>();
147 family
.setPointSize(settings
.value("fontSize", 12).toInt());
148 editor
->setFont(family
);
156 void SkinDocument::codeChanged()
158 model
->changeTree(editor
->document()->toPlainText().toAscii());
160 if(editor
->document()->toPlainText() != saved
)
161 emit
titleChanged(title
+ QChar('*'));
163 emit
titleChanged(title
);
166 void SkinDocument::save()
168 QFile
fout(fileName
);
176 fout
.open(QFile::WriteOnly
);
177 fout
.write(editor
->document()->toPlainText().toAscii());
180 saved
= editor
->document()->toPlainText();
181 QStringList decompose
= fileName
.split('/');
182 title
= decompose
.last();
183 emit
titleChanged(title
);
187 void SkinDocument::saveAs()
189 /* Determining the directory to open */
190 QString directory
= fileName
;
193 settings
.beginGroup("SkinDocument");
195 directory
= settings
.value("defaultDirectory", "").toString();
197 fileName
= QFileDialog::getSaveFileName(this, tr("Save Document"),
198 directory
, fileFilter());
199 directory
= fileName
;
203 directory
.chop(fileName
.length() - fileName
.lastIndexOf('/') - 1);
204 settings
.setValue("defaultDirectory", directory
);
207 QFile
fout(fileName
);
208 fout
.open(QFile::WriteOnly
);
209 fout
.write(editor
->document()->toPlainText().toAscii());
212 saved
= editor
->document()->toPlainText();
213 QStringList decompose
= fileName
.split('/');
214 title
= decompose
[decompose
.count() - 1];
215 emit
titleChanged(title
);