Theme Editor: Implemented document load functionality
[kugel-rb.git] / utils / themeeditor / skindocument.cpp
blobefb16eeae102f3cbd1eeb49fd1016c14a1291795
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 <QMessageBox>
27 #include <QFileDialog>
29 SkinDocument::SkinDocument(QWidget *parent) : QWidget(parent)
31 setupUI();
33 title = "Untitled";
34 fileName = "";
35 saved = "";
38 SkinDocument::SkinDocument(QString file, QWidget *parent):
39 QWidget(parent), fileName(file)
41 setupUI();
43 /* Loading the file */
44 if(QFile::exists(fileName))
46 QFile fin(fileName);
47 fin.open(QFile::ReadOnly);
48 editor->document()->setPlainText(QString(fin.readAll()));
49 saved = editor->document()->toPlainText();
50 fin.close();
53 /* Setting the title */
54 QStringList decomposed = fileName.split('/');
55 title = decomposed.last();
58 SkinDocument::~SkinDocument()
60 delete highlighter;
61 delete model;
64 bool SkinDocument::requestClose()
66 if(editor->document()->toPlainText() != saved)
68 /* Spawning the "Are you sure?" dialog */
69 QMessageBox confirm(this);
70 confirm.setWindowTitle(tr("Confirm Close"));
71 confirm.setText(title + tr(" has been modified."));
72 confirm.setInformativeText(tr("Do you want to save your changes?"));
73 confirm.setStandardButtons(QMessageBox::Save | QMessageBox::Discard
74 | QMessageBox::Cancel);
75 confirm.setDefaultButton(QMessageBox::Save);
76 int confirmation = confirm.exec();
78 switch(confirmation)
80 case QMessageBox::Save:
81 save();
82 /* After calling save, make sure the user actually went through */
83 if(editor->document()->toPlainText() != saved)
84 return false;
85 else
86 return true;
88 case QMessageBox::Discard:
89 return true;
91 case QMessageBox::Cancel:
92 return false;
96 return true;
99 void SkinDocument::setupUI()
101 /* Setting up the text edit */
102 layout = new QHBoxLayout;
103 editor = new QPlainTextEdit(this);
104 layout->addWidget(editor);
106 setLayout(layout);
108 /* Attaching the syntax highlighter */
109 highlighter = new SkinHighlighter(QColor(0,180,0), QColor(255,0,0),
110 QColor(0,0,255), QColor(120,120,120),
111 editor->document());
113 /* Setting up the model */
114 model = new ParseTreeModel("");
116 /* Connecting the editor's signal */
117 QObject::connect(editor, SIGNAL(textChanged()),
118 this, SLOT(codeChanged()));
121 void SkinDocument::codeChanged()
123 model->changeTree(editor->document()->toPlainText().toAscii());
125 if(editor->document()->toPlainText() != saved)
126 emit titleChanged(title + QChar('*'));
127 else
128 emit titleChanged(title);
131 void SkinDocument::save()
133 QFile fout(fileName);
135 if(!fout.exists())
137 saveAs();
138 return;
141 fout.open(QFile::WriteOnly);
142 fout.write(editor->document()->toPlainText().toAscii());
143 fout.close();
145 saved = editor->document()->toPlainText();
146 QStringList decompose = fileName.split('/');
147 title = decompose.last();
148 emit titleChanged(title);
152 void SkinDocument::saveAs()
154 /* Determining the directory to open */
155 QString directory = fileName;
157 QSettings settings;
158 settings.beginGroup("SkinDocument");
159 if(directory == "")
160 directory = settings.value("defaultDirectory", "").toString();
162 fileName = QFileDialog::getSaveFileName(this, tr("Save Document"),
163 directory, fileFilter());
164 directory = fileName;
165 if(fileName == "")
166 return;
168 directory.chop(fileName.length() - fileName.lastIndexOf('/') - 1);
169 settings.setValue("defaultDirectory", directory);
170 settings.endGroup();
172 QFile fout(fileName);
173 fout.open(QFile::WriteOnly);
174 fout.write(editor->document()->toPlainText().toAscii());
175 fout.close();
177 saved = editor->document()->toPlainText();
178 QStringList decompose = fileName.split('/');
179 title = decompose[decompose.count() - 1];
180 emit titleChanged(title);