Theme Editor: Added line numbering in the text editor, thanks to some code from Nokia...
[kugel-rb.git] / utils / themeeditor / skindocument.cpp
blobf7c902f30f9eebbd44da9e00b5666db9bcd0ec6d
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 editor->setTextCursor(QTextCursor(editor->document()->begin()));
56 fin.close();
59 /* Setting the title */
60 QStringList decomposed = fileName.split('/');
61 title = decomposed.last();
64 SkinDocument::~SkinDocument()
66 delete highlighter;
67 delete model;
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();
92 switch(confirmation)
94 case QMessageBox::Save:
95 save();
96 /* After calling save, make sure the user actually went through */
97 if(editor->document()->toPlainText() != saved)
98 return false;
99 else
100 return true;
102 case QMessageBox::Discard:
103 return true;
105 case QMessageBox::Cancel:
106 return false;
110 return true;
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);
121 setLayout(layout);
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()));
133 settingsChanged();
136 void SkinDocument::settingsChanged()
138 /* Setting the editor colors */
139 QSettings settings;
140 settings.beginGroup("SkinDocument");
142 QColor fg = settings.value("fgColor", Qt::black).value<QColor>();
143 QColor bg = settings.value("bgColor", Qt::white).value<QColor>();
144 QPalette palette;
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);
161 editor->repaint();
163 settings.endGroup();
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);
188 else
190 editor->setExtraSelections(QList<QTextEdit::ExtraSelection>());
193 if(editor->document()->toPlainText() != saved)
194 emit titleChanged(title + QChar('*'));
195 else
196 emit titleChanged(title);
199 void SkinDocument::save()
201 QFile fout(fileName);
203 if(!fout.exists())
205 saveAs();
206 return;
209 fout.open(QFile::WriteOnly);
210 fout.write(editor->document()->toPlainText().toAscii());
211 fout.close();
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;
225 QSettings settings;
226 settings.beginGroup("SkinDocument");
227 if(directory == "")
228 directory = settings.value("defaultDirectory", "").toString();
230 fileName = QFileDialog::getSaveFileName(this, tr("Save Document"),
231 directory, fileFilter());
232 directory = fileName;
233 if(fileName == "")
234 return;
236 directory.chop(fileName.length() - fileName.lastIndexOf('/') - 1);
237 settings.setValue("defaultDirectory", directory);
238 settings.endGroup();
240 QFile fout(fileName);
241 fout.open(QFile::WriteOnly);
242 fout.write(editor->document()->toPlainText().toAscii());
243 fout.close();
245 saved = editor->document()->toPlainText();
246 QStringList decompose = fileName.split('/');
247 title = decompose[decompose.count() - 1];
248 emit titleChanged(title);