Theme Editor: Made text editor scroll horizontally
[kugel-rb.git] / utils / themeeditor / skindocument.cpp
blob80a4fa330df202a9c1d5b854341be57a6650ff6b
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 SkinDocument::SkinDocument(QWidget *parent) : QWidget(parent)
32 setupUI();
34 title = "Untitled";
35 fileName = "";
36 saved = "";
39 SkinDocument::SkinDocument(QString file, QWidget *parent):
40 QWidget(parent), fileName(file)
42 setupUI();
44 /* Loading the file */
45 if(QFile::exists(fileName))
47 QFile fin(fileName);
48 fin.open(QFile::ReadOnly);
49 editor->document()->setPlainText(QString(fin.readAll()));
50 saved = editor->document()->toPlainText();
51 fin.close();
54 /* Setting the title */
55 QStringList decomposed = fileName.split('/');
56 title = decomposed.last();
59 SkinDocument::~SkinDocument()
61 delete highlighter;
62 delete model;
65 void SkinDocument::connectPrefs(PreferencesDialog* prefs)
67 QObject::connect(prefs, SIGNAL(accepted()),
68 this, SLOT(settingsChanged()));
69 QObject::connect(prefs, SIGNAL(accepted()),
70 highlighter, SLOT(loadSettings()));
73 bool SkinDocument::requestClose()
75 if(editor->document()->toPlainText() != saved)
77 /* Spawning the "Are you sure?" dialog */
78 QMessageBox confirm(this);
79 confirm.setWindowTitle(tr("Confirm Close"));
80 confirm.setText(title + tr(" has been modified."));
81 confirm.setInformativeText(tr("Do you want to save your changes?"));
82 confirm.setStandardButtons(QMessageBox::Save | QMessageBox::Discard
83 | QMessageBox::Cancel);
84 confirm.setDefaultButton(QMessageBox::Save);
85 int confirmation = confirm.exec();
87 switch(confirmation)
89 case QMessageBox::Save:
90 save();
91 /* After calling save, make sure the user actually went through */
92 if(editor->document()->toPlainText() != saved)
93 return false;
94 else
95 return true;
97 case QMessageBox::Discard:
98 return true;
100 case QMessageBox::Cancel:
101 return false;
105 return true;
108 void SkinDocument::setupUI()
110 /* Setting up the text edit */
111 layout = new QHBoxLayout;
112 editor = new QPlainTextEdit(this);
113 editor->setLineWrapMode(QPlainTextEdit::NoWrap);
114 layout->addWidget(editor);
116 setLayout(layout);
118 /* Attaching the syntax highlighter */
119 highlighter = new SkinHighlighter(editor->document());
121 /* Setting up the model */
122 model = new ParseTreeModel("");
124 /* Connecting the editor's signal */
125 QObject::connect(editor, SIGNAL(textChanged()),
126 this, SLOT(codeChanged()));
128 settingsChanged();
131 void SkinDocument::settingsChanged()
133 /* Setting the editor colors */
134 QSettings settings;
135 settings.beginGroup("SkinDocument");
137 QColor fg = settings.value("fgColor", Qt::black).value<QColor>();
138 QColor bg = settings.value("bgColor", Qt::white).value<QColor>();
139 QPalette palette;
140 palette.setColor(QPalette::All, QPalette::Base, bg);
141 palette.setColor(QPalette::All, QPalette::Text, fg);
143 editor->setPalette(palette);
145 /* Setting the font */
146 QFont family = settings.value("fontFamily", QFont()).value<QFont>();
147 family.setPointSize(settings.value("fontSize", 12).toInt());
148 editor->setFont(family);
150 editor->repaint();
152 settings.endGroup();
156 void SkinDocument::codeChanged()
158 model->changeTree(editor->document()->toPlainText().toAscii());
160 if(editor->document()->toPlainText() != saved)
161 emit titleChanged(title + QChar('*'));
162 else
163 emit titleChanged(title);
166 void SkinDocument::save()
168 QFile fout(fileName);
170 if(!fout.exists())
172 saveAs();
173 return;
176 fout.open(QFile::WriteOnly);
177 fout.write(editor->document()->toPlainText().toAscii());
178 fout.close();
180 saved = editor->document()->toPlainText();
181 QStringList decompose = fileName.split('/');
182 title = decompose.last();
183 emit titleChanged(title);
187 void SkinDocument::saveAs()
189 /* Determining the directory to open */
190 QString directory = fileName;
192 QSettings settings;
193 settings.beginGroup("SkinDocument");
194 if(directory == "")
195 directory = settings.value("defaultDirectory", "").toString();
197 fileName = QFileDialog::getSaveFileName(this, tr("Save Document"),
198 directory, fileFilter());
199 directory = fileName;
200 if(fileName == "")
201 return;
203 directory.chop(fileName.length() - fileName.lastIndexOf('/') - 1);
204 settings.setValue("defaultDirectory", directory);
205 settings.endGroup();
207 QFile fout(fileName);
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[decompose.count() - 1];
215 emit titleChanged(title);