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"
26 #include <QMessageBox>
27 #include <QFileDialog>
29 SkinDocument::SkinDocument(QWidget
*parent
) : QWidget(parent
)
38 SkinDocument::SkinDocument(QString file
, QWidget
*parent
):
39 QWidget(parent
), fileName(file
)
43 /* Loading the file */
44 if(QFile::exists(fileName
))
47 fin
.open(QFile::ReadOnly
);
48 editor
->document()->setPlainText(QString(fin
.readAll()));
49 saved
= editor
->document()->toPlainText();
53 /* Setting the title */
54 QStringList decomposed
= fileName
.split('/');
55 title
= decomposed
.last();
58 SkinDocument::~SkinDocument()
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();
80 case QMessageBox::Save
:
82 /* After calling save, make sure the user actually went through */
83 if(editor
->document()->toPlainText() != saved
)
88 case QMessageBox::Discard
:
91 case QMessageBox::Cancel
:
99 void SkinDocument::setupUI()
101 /* Setting up the text edit */
102 layout
= new QHBoxLayout
;
103 editor
= new QPlainTextEdit(this);
104 layout
->addWidget(editor
);
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),
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('*'));
128 emit
titleChanged(title
);
131 void SkinDocument::save()
133 QFile
fout(fileName
);
141 fout
.open(QFile::WriteOnly
);
142 fout
.write(editor
->document()->toPlainText().toAscii());
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
;
158 settings
.beginGroup("SkinDocument");
160 directory
= settings
.value("defaultDirectory", "").toString();
162 fileName
= QFileDialog::getSaveFileName(this, tr("Save Document"),
163 directory
, fileFilter());
164 directory
= fileName
;
168 directory
.chop(fileName
.length() - fileName
.lastIndexOf('/') - 1);
169 settings
.setValue("defaultDirectory", directory
);
172 QFile
fout(fileName
);
173 fout
.open(QFile::WriteOnly
);
174 fout
.write(editor
->document()->toPlainText().toAscii());
177 saved
= editor
->document()->toPlainText();
178 QStringList decompose
= fileName
.split('/');
179 title
= decompose
[decompose
.count() - 1];
180 emit
titleChanged(title
);