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();
55 editor
->setTextCursor(QTextCursor(editor
->document()->begin()));
59 /* Setting the title */
60 QStringList decomposed
= fileName
.split('/');
61 title
= decomposed
.last();
64 SkinDocument::~SkinDocument()
70 void SkinDocument::connectPrefs(PreferencesDialog
* prefs
)
72 QObject::connect(prefs
, SIGNAL(accepted()),
73 this, SLOT(settingsChanged()));
74 QObject::connect(prefs
, SIGNAL(accepted()),
75 highlighter
, SLOT(loadSettings()));
78 bool SkinDocument::requestClose()
80 if(editor
->document()->toPlainText() != saved
)
82 /* Spawning the "Are you sure?" dialog */
83 QMessageBox
confirm(this);
84 confirm
.setWindowTitle(tr("Confirm Close"));
85 confirm
.setText(title
+ tr(" has been modified."));
86 confirm
.setInformativeText(tr("Do you want to save your changes?"));
87 confirm
.setStandardButtons(QMessageBox::Save
| QMessageBox::Discard
88 | QMessageBox::Cancel
);
89 confirm
.setDefaultButton(QMessageBox::Save
);
90 int confirmation
= confirm
.exec();
94 case QMessageBox::Save
:
96 /* After calling save, make sure the user actually went through */
97 if(editor
->document()->toPlainText() != saved
)
102 case QMessageBox::Discard
:
105 case QMessageBox::Cancel
:
113 void SkinDocument::setupUI()
115 /* Setting up the text edit */
116 layout
= new QHBoxLayout
;
117 editor
= new CodeEditor(this);
118 editor
->setLineWrapMode(QPlainTextEdit::NoWrap
);
119 layout
->addWidget(editor
);
123 /* Attaching the syntax highlighter */
124 highlighter
= new SkinHighlighter(editor
->document());
126 /* Setting up the model */
127 model
= new ParseTreeModel("");
129 /* Connecting the editor's signal */
130 QObject::connect(editor
, SIGNAL(textChanged()),
131 this, SLOT(codeChanged()));
136 void SkinDocument::settingsChanged()
138 /* Setting the editor colors */
140 settings
.beginGroup("SkinDocument");
142 QColor fg
= settings
.value("fgColor", Qt::black
).value
<QColor
>();
143 QColor bg
= settings
.value("bgColor", Qt::white
).value
<QColor
>();
145 palette
.setColor(QPalette::All
, QPalette::Base
, bg
);
146 palette
.setColor(QPalette::All
, QPalette::Text
, fg
);
147 editor
->setPalette(palette
);
149 errorColor
= QTextCharFormat();
150 QColor highlight
= settings
.value("errorColor", Qt::red
).value
<QColor
>();
151 errorColor
.setBackground(highlight
);
152 errorColor
.setProperty(QTextFormat::FullWidthSelection
, true);
154 /* Setting the font */
155 QFont
def("Monospace");
156 def
.setStyleHint(QFont::TypeWriter
);
157 QFont family
= settings
.value("fontFamily", def
).value
<QFont
>();
158 family
.setPointSize(settings
.value("fontSize", 12).toInt());
159 editor
->setFont(family
);
167 void SkinDocument::codeChanged()
169 parseStatus
= model
->changeTree(editor
->document()->
170 toPlainText().toAscii());
171 statusLabel
->setText(parseStatus
);
173 /* Highlighting if an error was found */
174 if(skin_error_line() > 0)
176 QList
<QTextEdit::ExtraSelection
> highlight
;
177 QTextEdit::ExtraSelection error
;
179 /* Finding the apropriate line */
180 error
.cursor
= QTextCursor(editor
->document()->
181 findBlockByNumber(skin_error_line() - 1));
182 error
.format
= errorColor
;
183 highlight
.append(error
);
185 editor
->setExtraSelections(highlight
);
190 editor
->setExtraSelections(QList
<QTextEdit::ExtraSelection
>());
193 if(editor
->document()->toPlainText() != saved
)
194 emit
titleChanged(title
+ QChar('*'));
196 emit
titleChanged(title
);
199 void SkinDocument::save()
201 QFile
fout(fileName
);
209 fout
.open(QFile::WriteOnly
);
210 fout
.write(editor
->document()->toPlainText().toAscii());
213 saved
= editor
->document()->toPlainText();
214 QStringList decompose
= fileName
.split('/');
215 title
= decompose
.last();
216 emit
titleChanged(title
);
220 void SkinDocument::saveAs()
222 /* Determining the directory to open */
223 QString directory
= fileName
;
226 settings
.beginGroup("SkinDocument");
228 directory
= settings
.value("defaultDirectory", "").toString();
230 fileName
= QFileDialog::getSaveFileName(this, tr("Save Document"),
231 directory
, fileFilter());
232 directory
= fileName
;
236 directory
.chop(fileName
.length() - fileName
.lastIndexOf('/') - 1);
237 settings
.setValue("defaultDirectory", directory
);
240 QFile
fout(fileName
);
241 fout
.open(QFile::WriteOnly
);
242 fout
.write(editor
->document()->toPlainText().toAscii());
245 saved
= editor
->document()->toPlainText();
246 QStringList decompose
= fileName
.split('/');
247 title
= decompose
[decompose
.count() - 1];
248 emit
titleChanged(title
);