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>
32 SkinDocument::SkinDocument(QLabel
* statusLabel
, QWidget
*parent
) :
33 QWidget(parent
), statusLabel(statusLabel
)
40 parseStatus
= tr("Empty Document");
43 SkinDocument::SkinDocument(QLabel
* statusLabel
, QString file
, QWidget
*parent
):
44 QWidget(parent
), fileName(file
), statusLabel(statusLabel
)
48 /* Loading the file */
49 if(QFile::exists(fileName
))
52 fin
.open(QFile::ReadOnly
);
53 editor
->document()->setPlainText(QString(fin
.readAll()));
54 saved
= editor
->document()->toPlainText();
58 /* Setting the title */
59 QStringList decomposed
= fileName
.split('/');
60 title
= decomposed
.last();
63 SkinDocument::~SkinDocument()
69 void SkinDocument::connectPrefs(PreferencesDialog
* prefs
)
71 QObject::connect(prefs
, SIGNAL(accepted()),
72 this, SLOT(settingsChanged()));
73 QObject::connect(prefs
, SIGNAL(accepted()),
74 highlighter
, SLOT(loadSettings()));
77 bool SkinDocument::requestClose()
79 if(editor
->document()->toPlainText() != saved
)
81 /* Spawning the "Are you sure?" dialog */
82 QMessageBox
confirm(this);
83 confirm
.setWindowTitle(tr("Confirm Close"));
84 confirm
.setText(title
+ tr(" has been modified."));
85 confirm
.setInformativeText(tr("Do you want to save your changes?"));
86 confirm
.setStandardButtons(QMessageBox::Save
| QMessageBox::Discard
87 | QMessageBox::Cancel
);
88 confirm
.setDefaultButton(QMessageBox::Save
);
89 int confirmation
= confirm
.exec();
93 case QMessageBox::Save
:
95 /* After calling save, make sure the user actually went through */
96 if(editor
->document()->toPlainText() != saved
)
101 case QMessageBox::Discard
:
104 case QMessageBox::Cancel
:
112 void SkinDocument::setupUI()
114 /* Setting up the text edit */
115 layout
= new QHBoxLayout
;
116 editor
= new QPlainTextEdit(this);
117 editor
->setLineWrapMode(QPlainTextEdit::NoWrap
);
118 layout
->addWidget(editor
);
122 /* Attaching the syntax highlighter */
123 highlighter
= new SkinHighlighter(editor
->document());
125 /* Setting up the model */
126 model
= new ParseTreeModel("");
128 /* Connecting the editor's signal */
129 QObject::connect(editor
, SIGNAL(textChanged()),
130 this, SLOT(codeChanged()));
135 void SkinDocument::settingsChanged()
137 /* Setting the editor colors */
139 settings
.beginGroup("SkinDocument");
141 QColor fg
= settings
.value("fgColor", Qt::black
).value
<QColor
>();
142 QColor bg
= settings
.value("bgColor", Qt::white
).value
<QColor
>();
144 palette
.setColor(QPalette::All
, QPalette::Base
, bg
);
145 palette
.setColor(QPalette::All
, QPalette::Text
, fg
);
146 editor
->setPalette(palette
);
148 errorColor
= QTextCharFormat();
149 QColor highlight
= settings
.value("errorColor", Qt::red
).value
<QColor
>();
150 errorColor
.setBackground(highlight
);
151 errorColor
.setProperty(QTextFormat::FullWidthSelection
, true);
153 /* Setting the font */
154 QFont
def("Monospace");
155 def
.setStyleHint(QFont::TypeWriter
);
156 QFont family
= settings
.value("fontFamily", def
).value
<QFont
>();
157 family
.setPointSize(settings
.value("fontSize", 12).toInt());
158 editor
->setFont(family
);
166 void SkinDocument::codeChanged()
168 parseStatus
= model
->changeTree(editor
->document()->
169 toPlainText().toAscii());
170 statusLabel
->setText(parseStatus
);
172 /* Highlighting if an error was found */
173 if(skin_error_line() > 0)
175 QList
<QTextEdit::ExtraSelection
> highlight
;
176 QTextEdit::ExtraSelection error
;
178 /* Finding the apropriate line */
179 error
.cursor
= QTextCursor(editor
->document()->
180 findBlockByNumber(skin_error_line() - 1));
181 error
.format
= errorColor
;
182 highlight
.append(error
);
184 editor
->setExtraSelections(highlight
);
189 editor
->setExtraSelections(QList
<QTextEdit::ExtraSelection
>());
192 if(editor
->document()->toPlainText() != saved
)
193 emit
titleChanged(title
+ QChar('*'));
195 emit
titleChanged(title
);
198 void SkinDocument::save()
200 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
.last();
215 emit
titleChanged(title
);
219 void SkinDocument::saveAs()
221 /* Determining the directory to open */
222 QString directory
= fileName
;
225 settings
.beginGroup("SkinDocument");
227 directory
= settings
.value("defaultDirectory", "").toString();
229 fileName
= QFileDialog::getSaveFileName(this, tr("Save Document"),
230 directory
, fileFilter());
231 directory
= fileName
;
235 directory
.chop(fileName
.length() - fileName
.lastIndexOf('/') - 1);
236 settings
.setValue("defaultDirectory", directory
);
239 QFile
fout(fileName
);
240 fout
.open(QFile::WriteOnly
);
241 fout
.write(editor
->document()->toPlainText().toAscii());
244 saved
= editor
->document()->toPlainText();
245 QStringList decompose
= fileName
.split('/');
246 title
= decompose
[decompose
.count() - 1];
247 emit
titleChanged(title
);