1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2010 Robert Bieber
12 * This program is free software; 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 optiyouon) 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
, ProjectModel
* project
,
33 DeviceState
* device
, QWidget
*parent
)
34 :TabContent(parent
), statusLabel(statusLabel
),
35 project(project
), device(device
)
39 titleText
= "Untitled";
42 parseStatus
= tr("Empty document");
46 SkinDocument::SkinDocument(QLabel
* statusLabel
, QString file
,
47 ProjectModel
* project
, DeviceState
* device
,
49 :TabContent(parent
), fileName(file
),
50 statusLabel(statusLabel
), project(project
),
56 /* Loading the file */
57 if(QFile::exists(fileName
))
60 fin
.open(QFile::ReadOnly
);
61 editor
->document()->setPlainText(QString(fin
.readAll()));
62 saved
= editor
->document()->toPlainText();
63 editor
->setTextCursor(QTextCursor(editor
->document()->begin()));
67 /* Setting the title */
68 QStringList decomposed
= fileName
.split('/');
69 titleText
= decomposed
.last();
72 SkinDocument::~SkinDocument()
74 highlighter
->deleteLater();
78 void SkinDocument::connectPrefs(PreferencesDialog
* prefs
)
80 QObject::connect(prefs
, SIGNAL(accepted()),
81 this, SLOT(settingsChanged()));
82 QObject::connect(prefs
, SIGNAL(accepted()),
83 highlighter
, SLOT(loadSettings()));
86 bool SkinDocument::requestClose()
88 /* Storing the response in blockUpdate will also block updates to the
89 status bar if the tab is being closed */
90 if(editor
->document()->toPlainText() != saved
)
92 /* Spawning the "Are you sure?" dialog */
93 QMessageBox
confirm(this);
94 confirm
.setWindowTitle(tr("Confirm Close"));
95 confirm
.setText(titleText
+ tr(" has been modified."));
96 confirm
.setInformativeText(tr("Do you want to save your changes?"));
97 confirm
.setStandardButtons(QMessageBox::Save
| QMessageBox::Discard
98 | QMessageBox::Cancel
);
99 confirm
.setDefaultButton(QMessageBox::Save
);
100 int confirmation
= confirm
.exec();
104 case QMessageBox::Save
:
106 /* After calling save, make sure the user actually went through */
107 if(editor
->document()->toPlainText() != saved
)
113 case QMessageBox::Discard
:
117 case QMessageBox::Cancel
:
128 void SkinDocument::setupUI()
130 /* Setting up the text edit */
131 layout
= new QHBoxLayout
;
132 editor
= new CodeEditor(this);
133 editor
->setLineWrapMode(QPlainTextEdit::NoWrap
);
134 layout
->addWidget(editor
);
138 /* Attaching the syntax highlighter */
139 highlighter
= new SkinHighlighter(editor
->document());
141 /* Setting up the model */
142 model
= new ParseTreeModel("");
144 /* Connecting the editor's signal */
145 QObject::connect(editor
, SIGNAL(textChanged()),
146 this, SLOT(codeChanged()));
147 QObject::connect(editor
, SIGNAL(cursorPositionChanged()),
148 this, SLOT(cursorChanged()));
150 /* Connecting to device setting changes */
151 QObject::connect(device
, SIGNAL(settingsChanged()),
152 this, SLOT(deviceChanged()));
154 /* Attaching the find/replace dialog */
155 findReplace
= new FindReplaceDialog(this);
156 findReplace
->setModal(false);
157 findReplace
->setTextEdit(editor
);
163 void SkinDocument::settingsChanged()
165 /* Setting the editor colors */
167 settings
.beginGroup("SkinDocument");
169 QColor fg
= settings
.value("fgColor", Qt::black
).value
<QColor
>();
170 QColor bg
= settings
.value("bgColor", Qt::white
).value
<QColor
>();
172 palette
.setColor(QPalette::All
, QPalette::Base
, bg
);
173 palette
.setColor(QPalette::All
, QPalette::Text
, fg
);
174 editor
->setPalette(palette
);
176 QColor highlight
= settings
.value("errorColor", Qt::red
).value
<QColor
>();
177 editor
->setErrorColor(highlight
);
179 /* Setting the font */
180 QFont
def("Monospace");
181 def
.setStyleHint(QFont::TypeWriter
);
182 QFont family
= settings
.value("fontFamily", def
).value
<QFont
>();
183 family
.setPointSize(settings
.value("fontSize", 12).toInt());
184 editor
->setFont(family
);
192 void SkinDocument::cursorChanged()
194 if(editor
->isError(editor
->textCursor().blockNumber() + 1))
196 QTextCursor line
= editor
->textCursor();
197 line
.movePosition(QTextCursor::StartOfLine
);
198 line
.movePosition(QTextCursor::EndOfLine
, QTextCursor::KeepAnchor
);
199 skin_parse(line
.selectedText().toAscii());
200 if(skin_error_line() > 0)
201 parseStatus
= tr("Error on line ") +
202 QString::number(line
.blockNumber() + 1) + tr(": ") +
203 skin_error_message();
204 statusLabel
->setText(parseStatus
);
206 else if(editor
->hasErrors())
208 parseStatus
= tr("Errors in document");
209 statusLabel
->setText(parseStatus
);
213 emit
lineChanged(editor
->textCursor().blockNumber() + 1);
218 void SkinDocument::codeChanged()
223 if(editor
->document()->isEmpty())
225 parseStatus
= tr("Empty document");
226 statusLabel
->setText(parseStatus
);
230 editor
->clearErrors();
231 parseStatus
= model
->changeTree(editor
->document()->
232 toPlainText().toAscii());
233 if(skin_error_line() > 0)
234 parseStatus
= tr("Errors in document");
235 statusLabel
->setText(parseStatus
);
237 /* Highlighting if an error was found */
238 if(skin_error_line() > 0)
240 editor
->addError(skin_error_line());
242 /* Now we're going to attempt parsing again at each line, until we find
243 one that won't error out*/
244 QTextDocument
doc(editor
->document()->toPlainText());
246 while(skin_error_line() > 0 && !doc
.isEmpty())
248 QTextCursor
rest(&doc
);
250 for(int i
= 0; i
< skin_error_line(); i
++)
251 rest
.movePosition(QTextCursor::NextBlock
,
252 QTextCursor::KeepAnchor
);
253 if(skin_error_line() == doc
.blockCount())
254 rest
.movePosition(QTextCursor::End
, QTextCursor::KeepAnchor
);
256 rest
.removeSelectedText();
257 base
+= skin_error_line();
259 skin_parse(doc
.toPlainText().toAscii());
261 if(skin_error_line() > 0)
262 editor
->addError(base
+ skin_error_line());
267 if(editor
->document()->toPlainText() != saved
)
268 emit
titleChanged(titleText
+ QChar('*'));
270 emit
titleChanged(titleText
);
272 model
->render(project
, device
, &fileName
);
278 void SkinDocument::save()
280 QFile
fout(fileName
);
288 fout
.open(QFile::WriteOnly
);
289 fout
.write(editor
->document()->toPlainText().toAscii());
292 saved
= editor
->document()->toPlainText();
293 QStringList decompose
= fileName
.split('/');
294 titleText
= decompose
.last();
295 emit
titleChanged(titleText
);
301 void SkinDocument::saveAs()
303 /* Determining the directory to open */
304 QString directory
= fileName
;
307 settings
.beginGroup("SkinDocument");
309 directory
= settings
.value("defaultDirectory", "").toString();
311 fileName
= QFileDialog::getSaveFileName(this, tr("Save Document"),
312 directory
, fileFilter());
313 directory
= fileName
;
317 directory
.chop(fileName
.length() - fileName
.lastIndexOf('/') - 1);
318 settings
.setValue("defaultDirectory", directory
);
321 QFile
fout(fileName
);
322 fout
.open(QFile::WriteOnly
);
323 fout
.write(editor
->document()->toPlainText().toAscii());
326 saved
= editor
->document()->toPlainText();
327 QStringList decompose
= fileName
.split('/');
328 titleText
= decompose
[decompose
.count() - 1];
329 emit
titleChanged(titleText
);
335 QString
SkinDocument::findSetting(QString key
, QString fallback
)
340 return project
->getSetting(key
, fallback
);