Theme Editor: Began working on open document functionality (still incomplete), fixed...
[kugel-rb.git] / utils / themeeditor / skindocument.cpp
blob469401d07b5e87c7a5caebc0af8f516dd1af18ce
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 <QTimer>
26 #include <QSettings>
27 #include <QMessageBox>
28 #include <QFileDialog>
30 SkinDocument::SkinDocument(QWidget *parent) :
31 QWidget(parent)
33 setupUI();
35 title = "Untitled";
36 fileName = "";
37 saved = "";
40 SkinDocument::~SkinDocument()
42 delete highlighter;
43 delete model;
46 bool SkinDocument::requestClose()
48 if(editor->document()->toPlainText() != saved)
50 /* Spawning the "Are you sure?" dialog */
51 QMessageBox confirm(this);
52 confirm.setWindowTitle(tr("Confirm Close"));
53 confirm.setText(title + tr(" has been modified."));
54 confirm.setInformativeText(tr("Do you want to save your changes?"));
55 confirm.setStandardButtons(QMessageBox::Save | QMessageBox::Discard
56 | QMessageBox::Cancel);
57 confirm.setDefaultButton(QMessageBox::Save);
58 int confirmation = confirm.exec();
60 switch(confirmation)
62 case QMessageBox::Save:
63 save();
64 /* After calling save, make sure the user actually went through */
65 if(editor->document()->toPlainText() != saved)
66 return false;
67 else
68 return true;
70 case QMessageBox::Discard:
71 return true;
73 case QMessageBox::Cancel:
74 return false;
78 return true;
81 void SkinDocument::setupUI()
83 /* Setting up the text edit */
84 layout = new QHBoxLayout;
85 editor = new QPlainTextEdit(this);
86 layout->addWidget(editor);
88 setLayout(layout);
90 /* Attaching the syntax highlighter */
91 highlighter = new SkinHighlighter(QColor(0,180,0), QColor(255,0,0),
92 QColor(0,0,255), QColor(120,120,120),
93 editor->document());
95 /* Setting up the model */
96 model = new ParseTreeModel("");
98 /* Connecting the editor's signal */
99 QObject::connect(editor, SIGNAL(textChanged()),
100 this, SLOT(codeChanged()));
103 void SkinDocument::codeChanged()
105 model->changeTree(editor->document()->toPlainText().toAscii());
107 if(editor->document()->toPlainText() != saved)
108 emit titleChanged(title + QChar('*'));
109 else
110 emit titleChanged(title);
113 void SkinDocument::save()
115 QFile fout(fileName);
117 if(!fout.exists())
119 saveAs();
120 return;
123 fout.open(QFile::WriteOnly);
124 fout.write(editor->document()->toPlainText().toAscii());
125 fout.close();
127 saved = editor->document()->toPlainText();
128 QStringList decompose = fileName.split('/');
129 title = decompose[decompose.count() - 1];
130 emit titleChanged(title);
134 void SkinDocument::saveAs()
136 /* Determining the directory to open */
137 QString directory = fileName;
139 QSettings settings;
140 settings.beginGroup("SkinDocument");
141 if(directory == "")
142 directory = settings.value("defaultDirectory", "").toString();
144 fileName = QFileDialog::getSaveFileName(this, tr("Save Document"),
145 directory, fileFilter());
146 directory = fileName;
147 if(fileName == "")
148 return;
150 directory.chop(fileName.length() - fileName.lastIndexOf('/') - 1);
151 settings.setValue("defaultDirectory", directory);
152 settings.endGroup();
154 QFile fout(fileName);
155 fout.open(QFile::WriteOnly);
156 fout.write(editor->document()->toPlainText().toAscii());
157 fout.close();
159 saved = editor->document()->toPlainText();
160 QStringList decompose = fileName.split('/');
161 title = decompose[decompose.count() - 1];
162 emit titleChanged(title);