Theme Editor: Changed default font to monospaced, changed organization for the applic...
[kugel-rb.git] / utils / themeeditor / skindocument.cpp
blob3da248a30bcf070f514c4fcd6ef78de23f7c1ed8
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
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"
24 #include <QFile>
25 #include <QSettings>
26 #include <QColor>
27 #include <QMessageBox>
28 #include <QFileDialog>
30 #include <iostream>
32 SkinDocument::SkinDocument(QLabel* statusLabel, QWidget *parent) :
33 QWidget(parent), statusLabel(statusLabel)
35 setupUI();
37 title = "Untitled";
38 fileName = "";
39 saved = "";
40 parseStatus = tr("Empty Document");
43 SkinDocument::SkinDocument(QLabel* statusLabel, QString file, QWidget *parent):
44 QWidget(parent), fileName(file), statusLabel(statusLabel)
46 setupUI();
48 /* Loading the file */
49 if(QFile::exists(fileName))
51 QFile fin(fileName);
52 fin.open(QFile::ReadOnly);
53 editor->document()->setPlainText(QString(fin.readAll()));
54 saved = editor->document()->toPlainText();
55 fin.close();
58 /* Setting the title */
59 QStringList decomposed = fileName.split('/');
60 title = decomposed.last();
63 SkinDocument::~SkinDocument()
65 delete highlighter;
66 delete model;
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();
91 switch(confirmation)
93 case QMessageBox::Save:
94 save();
95 /* After calling save, make sure the user actually went through */
96 if(editor->document()->toPlainText() != saved)
97 return false;
98 else
99 return true;
101 case QMessageBox::Discard:
102 return true;
104 case QMessageBox::Cancel:
105 return false;
109 return true;
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);
120 setLayout(layout);
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()));
132 settingsChanged();
135 void SkinDocument::settingsChanged()
137 /* Setting the editor colors */
138 QSettings settings;
139 settings.beginGroup("SkinDocument");
141 QColor fg = settings.value("fgColor", Qt::black).value<QColor>();
142 QColor bg = settings.value("bgColor", Qt::white).value<QColor>();
143 QPalette palette;
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);
160 editor->repaint();
162 settings.endGroup();
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);
187 else
189 editor->setExtraSelections(QList<QTextEdit::ExtraSelection>());
192 if(editor->document()->toPlainText() != saved)
193 emit titleChanged(title + QChar('*'));
194 else
195 emit titleChanged(title);
198 void SkinDocument::save()
200 QFile fout(fileName);
202 if(!fout.exists())
204 saveAs();
205 return;
208 fout.open(QFile::WriteOnly);
209 fout.write(editor->document()->toPlainText().toAscii());
210 fout.close();
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;
224 QSettings settings;
225 settings.beginGroup("SkinDocument");
226 if(directory == "")
227 directory = settings.value("defaultDirectory", "").toString();
229 fileName = QFileDialog::getSaveFileName(this, tr("Save Document"),
230 directory, fileFilter());
231 directory = fileName;
232 if(fileName == "")
233 return;
235 directory.chop(fileName.length() - fileName.lastIndexOf('/') - 1);
236 settings.setValue("defaultDirectory", directory);
237 settings.endGroup();
239 QFile fout(fileName);
240 fout.open(QFile::WriteOnly);
241 fout.write(editor->document()->toPlainText().toAscii());
242 fout.close();
244 saved = editor->document()->toPlainText();
245 QStringList decompose = fileName.split('/');
246 title = decompose[decompose.count() - 1];
247 emit titleChanged(title);