1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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"
27 #include <QMessageBox>
28 #include <QFileDialog>
30 SkinDocument::SkinDocument(QWidget
*parent
) :
40 SkinDocument::~SkinDocument()
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();
62 case QMessageBox::Save
:
64 /* After calling save, make sure the user actually went through */
65 if(editor
->document()->toPlainText() != saved
)
70 case QMessageBox::Discard
:
73 case QMessageBox::Cancel
:
81 void SkinDocument::setupUI()
83 /* Setting up the text edit */
84 layout
= new QHBoxLayout
;
85 editor
= new QPlainTextEdit(this);
86 layout
->addWidget(editor
);
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),
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('*'));
110 emit
titleChanged(title
);
113 void SkinDocument::save()
115 QFile
fout(fileName
);
123 fout
.open(QFile::WriteOnly
);
124 fout
.write(editor
->document()->toPlainText().toAscii());
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
;
140 settings
.beginGroup("SkinDocument");
142 directory
= settings
.value("defaultDirectory", "").toString();
144 fileName
= QFileDialog::getSaveFileName(this, tr("Save Document"),
145 directory
, fileFilter());
146 directory
= fileName
;
150 directory
.chop(fileName
.length() - fileName
.lastIndexOf('/') - 1);
151 settings
.setValue("defaultDirectory", directory
);
154 QFile
fout(fileName
);
155 fout
.open(QFile::WriteOnly
);
156 fout
.write(editor
->document()->toPlainText().toAscii());
159 saved
= editor
->document()->toPlainText();
160 QStringList decompose
= fileName
.split('/');
161 title
= decompose
[decompose
.count() - 1];
162 emit
titleChanged(title
);