2010-06-04 07:57:19 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright (C) 2010 Robert Bieber
|
|
|
|
*
|
2010-06-15 06:54:58 +00:00
|
|
|
* This program is free software; can redistribute it and/or
|
2010-06-04 07:57:19 +00:00
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
2010-06-15 06:54:58 +00:00
|
|
|
* of the License, or (at your optiyouon) any later version.
|
2010-06-04 07:57:19 +00:00
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "skindocument.h"
|
|
|
|
|
2010-06-05 08:22:30 +00:00
|
|
|
#include <QFile>
|
|
|
|
#include <QSettings>
|
2010-06-07 03:25:40 +00:00
|
|
|
#include <QColor>
|
2010-06-05 08:40:27 +00:00
|
|
|
#include <QMessageBox>
|
|
|
|
#include <QFileDialog>
|
2010-06-05 08:22:30 +00:00
|
|
|
|
2010-06-07 21:59:16 +00:00
|
|
|
#include <iostream>
|
|
|
|
|
2010-07-03 06:08:59 +00:00
|
|
|
#include <QDebug>
|
|
|
|
|
2010-07-07 09:33:47 +00:00
|
|
|
const int SkinDocument::updateInterval = 500;
|
|
|
|
|
2010-06-17 06:59:46 +00:00
|
|
|
SkinDocument::SkinDocument(QLabel* statusLabel, ProjectModel* project,
|
2010-06-26 05:18:21 +00:00
|
|
|
DeviceState* device, QWidget *parent)
|
2010-06-17 06:59:46 +00:00
|
|
|
:TabContent(parent), statusLabel(statusLabel),
|
2010-06-26 05:18:21 +00:00
|
|
|
project(project), device(device)
|
2010-06-04 07:57:19 +00:00
|
|
|
{
|
|
|
|
setupUI();
|
|
|
|
|
2010-06-15 06:54:58 +00:00
|
|
|
titleText = "Untitled";
|
2010-06-05 08:22:30 +00:00
|
|
|
fileName = "";
|
2010-06-05 19:47:49 +00:00
|
|
|
saved = "";
|
2010-06-11 20:51:49 +00:00
|
|
|
parseStatus = tr("Empty document");
|
|
|
|
blockUpdate = false;
|
2010-06-29 06:14:36 +00:00
|
|
|
currentLine = -1;
|
2010-06-04 07:57:19 +00:00
|
|
|
}
|
|
|
|
|
2010-06-17 06:59:46 +00:00
|
|
|
SkinDocument::SkinDocument(QLabel* statusLabel, QString file,
|
2010-06-26 05:18:21 +00:00
|
|
|
ProjectModel* project, DeviceState* device,
|
|
|
|
QWidget *parent)
|
2010-06-17 06:59:46 +00:00
|
|
|
:TabContent(parent), fileName(file),
|
2010-06-26 05:18:21 +00:00
|
|
|
statusLabel(statusLabel), project(project),
|
|
|
|
device(device)
|
2010-06-07 01:08:25 +00:00
|
|
|
{
|
|
|
|
setupUI();
|
2010-06-11 20:51:49 +00:00
|
|
|
blockUpdate = false;
|
2010-06-07 01:08:25 +00:00
|
|
|
|
|
|
|
/* Loading the file */
|
|
|
|
if(QFile::exists(fileName))
|
|
|
|
{
|
|
|
|
QFile fin(fileName);
|
|
|
|
fin.open(QFile::ReadOnly);
|
|
|
|
editor->document()->setPlainText(QString(fin.readAll()));
|
|
|
|
saved = editor->document()->toPlainText();
|
2010-06-08 07:57:43 +00:00
|
|
|
editor->setTextCursor(QTextCursor(editor->document()->begin()));
|
2010-06-07 01:08:25 +00:00
|
|
|
fin.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Setting the title */
|
|
|
|
QStringList decomposed = fileName.split('/');
|
2010-06-15 06:54:58 +00:00
|
|
|
titleText = decomposed.last();
|
2010-07-07 09:33:47 +00:00
|
|
|
|
|
|
|
lastUpdate = QTime::currentTime();
|
2010-06-07 01:08:25 +00:00
|
|
|
}
|
|
|
|
|
2010-06-04 07:57:19 +00:00
|
|
|
SkinDocument::~SkinDocument()
|
|
|
|
{
|
2010-06-17 06:59:46 +00:00
|
|
|
highlighter->deleteLater();
|
|
|
|
model->deleteLater();
|
2010-06-04 07:57:19 +00:00
|
|
|
}
|
|
|
|
|
2010-06-07 03:25:40 +00:00
|
|
|
void SkinDocument::connectPrefs(PreferencesDialog* prefs)
|
|
|
|
{
|
|
|
|
QObject::connect(prefs, SIGNAL(accepted()),
|
2010-06-07 07:57:56 +00:00
|
|
|
this, SLOT(settingsChanged()));
|
2010-06-07 03:25:40 +00:00
|
|
|
QObject::connect(prefs, SIGNAL(accepted()),
|
|
|
|
highlighter, SLOT(loadSettings()));
|
|
|
|
}
|
|
|
|
|
2010-06-05 07:38:29 +00:00
|
|
|
bool SkinDocument::requestClose()
|
|
|
|
{
|
2010-06-11 20:51:49 +00:00
|
|
|
/* Storing the response in blockUpdate will also block updates to the
|
|
|
|
status bar if the tab is being closed */
|
2010-06-05 19:47:49 +00:00
|
|
|
if(editor->document()->toPlainText() != saved)
|
|
|
|
{
|
|
|
|
/* Spawning the "Are you sure?" dialog */
|
|
|
|
QMessageBox confirm(this);
|
2010-06-06 01:51:23 +00:00
|
|
|
confirm.setWindowTitle(tr("Confirm Close"));
|
2010-06-15 06:54:58 +00:00
|
|
|
confirm.setText(titleText + tr(" has been modified."));
|
2010-06-05 19:47:49 +00:00
|
|
|
confirm.setInformativeText(tr("Do you want to save your changes?"));
|
|
|
|
confirm.setStandardButtons(QMessageBox::Save | QMessageBox::Discard
|
|
|
|
| QMessageBox::Cancel);
|
|
|
|
confirm.setDefaultButton(QMessageBox::Save);
|
|
|
|
int confirmation = confirm.exec();
|
|
|
|
|
|
|
|
switch(confirmation)
|
|
|
|
{
|
|
|
|
case QMessageBox::Save:
|
|
|
|
save();
|
|
|
|
/* After calling save, make sure the user actually went through */
|
|
|
|
if(editor->document()->toPlainText() != saved)
|
2010-06-11 20:51:49 +00:00
|
|
|
blockUpdate = false;
|
2010-06-05 19:47:49 +00:00
|
|
|
else
|
2010-06-11 20:51:49 +00:00
|
|
|
blockUpdate = true;
|
|
|
|
break;
|
2010-06-05 19:47:49 +00:00
|
|
|
|
|
|
|
case QMessageBox::Discard:
|
2010-06-11 20:51:49 +00:00
|
|
|
blockUpdate = true;
|
|
|
|
break;
|
2010-06-05 19:47:49 +00:00
|
|
|
|
|
|
|
case QMessageBox::Cancel:
|
2010-06-11 20:51:49 +00:00
|
|
|
blockUpdate = false;
|
|
|
|
break;
|
2010-06-05 19:47:49 +00:00
|
|
|
}
|
|
|
|
}
|
2010-06-11 20:51:49 +00:00
|
|
|
else
|
|
|
|
blockUpdate = true;
|
2010-06-05 19:47:49 +00:00
|
|
|
|
2010-06-11 20:51:49 +00:00
|
|
|
return blockUpdate;
|
2010-06-05 07:38:29 +00:00
|
|
|
}
|
|
|
|
|
2010-06-04 07:57:19 +00:00
|
|
|
void SkinDocument::setupUI()
|
|
|
|
{
|
|
|
|
/* Setting up the text edit */
|
|
|
|
layout = new QHBoxLayout;
|
2010-06-08 07:57:43 +00:00
|
|
|
editor = new CodeEditor(this);
|
2010-06-07 08:05:52 +00:00
|
|
|
editor->setLineWrapMode(QPlainTextEdit::NoWrap);
|
2010-06-04 07:57:19 +00:00
|
|
|
layout->addWidget(editor);
|
|
|
|
|
|
|
|
setLayout(layout);
|
|
|
|
|
|
|
|
/* Attaching the syntax highlighter */
|
2010-06-07 03:25:40 +00:00
|
|
|
highlighter = new SkinHighlighter(editor->document());
|
2010-06-04 07:57:19 +00:00
|
|
|
|
|
|
|
/* Setting up the model */
|
|
|
|
model = new ParseTreeModel("");
|
|
|
|
|
|
|
|
/* Connecting the editor's signal */
|
|
|
|
QObject::connect(editor, SIGNAL(textChanged()),
|
|
|
|
this, SLOT(codeChanged()));
|
2010-06-11 21:24:38 +00:00
|
|
|
QObject::connect(editor, SIGNAL(cursorPositionChanged()),
|
|
|
|
this, SLOT(cursorChanged()));
|
2010-06-07 03:25:40 +00:00
|
|
|
|
2010-06-26 05:18:21 +00:00
|
|
|
/* Connecting to device setting changes */
|
|
|
|
QObject::connect(device, SIGNAL(settingsChanged()),
|
|
|
|
this, SLOT(deviceChanged()));
|
|
|
|
|
2010-06-26 07:59:23 +00:00
|
|
|
/* Attaching the find/replace dialog */
|
|
|
|
findReplace = new FindReplaceDialog(this);
|
|
|
|
findReplace->setModal(false);
|
|
|
|
findReplace->setTextEdit(editor);
|
|
|
|
findReplace->hide();
|
|
|
|
|
2010-06-07 07:57:56 +00:00
|
|
|
settingsChanged();
|
2010-07-07 09:33:47 +00:00
|
|
|
|
|
|
|
/* Setting up a timer to check for updates */
|
|
|
|
checkUpdate.setInterval(500);
|
|
|
|
QObject::connect(&checkUpdate, SIGNAL(timeout()),
|
|
|
|
this, SLOT(codeChanged()));
|
2010-06-07 03:25:40 +00:00
|
|
|
}
|
|
|
|
|
2010-06-07 07:57:56 +00:00
|
|
|
void SkinDocument::settingsChanged()
|
2010-06-07 03:25:40 +00:00
|
|
|
{
|
|
|
|
/* Setting the editor colors */
|
|
|
|
QSettings settings;
|
|
|
|
settings.beginGroup("SkinDocument");
|
|
|
|
|
|
|
|
QColor fg = settings.value("fgColor", Qt::black).value<QColor>();
|
|
|
|
QColor bg = settings.value("bgColor", Qt::white).value<QColor>();
|
|
|
|
QPalette palette;
|
|
|
|
palette.setColor(QPalette::All, QPalette::Base, bg);
|
|
|
|
palette.setColor(QPalette::All, QPalette::Text, fg);
|
|
|
|
editor->setPalette(palette);
|
2010-06-07 07:57:56 +00:00
|
|
|
|
2010-06-07 21:59:16 +00:00
|
|
|
QColor highlight = settings.value("errorColor", Qt::red).value<QColor>();
|
2010-06-11 19:51:34 +00:00
|
|
|
editor->setErrorColor(highlight);
|
2010-06-07 21:59:16 +00:00
|
|
|
|
2010-06-07 07:57:56 +00:00
|
|
|
/* Setting the font */
|
2010-06-07 22:35:32 +00:00
|
|
|
QFont def("Monospace");
|
|
|
|
def.setStyleHint(QFont::TypeWriter);
|
|
|
|
QFont family = settings.value("fontFamily", def).value<QFont>();
|
2010-06-07 07:57:56 +00:00
|
|
|
family.setPointSize(settings.value("fontSize", 12).toInt());
|
|
|
|
editor->setFont(family);
|
|
|
|
|
2010-06-07 03:25:40 +00:00
|
|
|
editor->repaint();
|
|
|
|
|
|
|
|
settings.endGroup();
|
|
|
|
|
2010-06-04 07:57:19 +00:00
|
|
|
}
|
|
|
|
|
2010-06-11 21:24:38 +00:00
|
|
|
void SkinDocument::cursorChanged()
|
|
|
|
{
|
|
|
|
if(editor->isError(editor->textCursor().blockNumber() + 1))
|
|
|
|
{
|
|
|
|
QTextCursor line = editor->textCursor();
|
|
|
|
line.movePosition(QTextCursor::StartOfLine);
|
|
|
|
line.movePosition(QTextCursor::EndOfLine, QTextCursor::KeepAnchor);
|
|
|
|
skin_parse(line.selectedText().toAscii());
|
|
|
|
if(skin_error_line() > 0)
|
|
|
|
parseStatus = tr("Error on line ") +
|
|
|
|
QString::number(line.blockNumber() + 1) + tr(": ") +
|
|
|
|
skin_error_message();
|
|
|
|
statusLabel->setText(parseStatus);
|
|
|
|
}
|
|
|
|
else if(editor->hasErrors())
|
|
|
|
{
|
|
|
|
parseStatus = tr("Errors in document");
|
|
|
|
statusLabel->setText(parseStatus);
|
|
|
|
}
|
2010-06-29 06:14:36 +00:00
|
|
|
else if(editor->textCursor().blockNumber() != currentLine)
|
2010-06-15 06:54:58 +00:00
|
|
|
{
|
2010-06-29 06:14:36 +00:00
|
|
|
currentLine = editor->textCursor().blockNumber();
|
2010-06-15 06:54:58 +00:00
|
|
|
emit lineChanged(editor->textCursor().blockNumber() + 1);
|
|
|
|
}
|
2010-06-11 21:24:38 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2010-06-04 07:57:19 +00:00
|
|
|
void SkinDocument::codeChanged()
|
|
|
|
{
|
2010-06-11 20:51:49 +00:00
|
|
|
if(blockUpdate)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if(editor->document()->isEmpty())
|
|
|
|
{
|
|
|
|
parseStatus = tr("Empty document");
|
|
|
|
statusLabel->setText(parseStatus);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-06-11 19:03:49 +00:00
|
|
|
editor->clearErrors();
|
2010-06-07 20:29:46 +00:00
|
|
|
parseStatus = model->changeTree(editor->document()->
|
|
|
|
toPlainText().toAscii());
|
2010-06-11 20:51:49 +00:00
|
|
|
if(skin_error_line() > 0)
|
|
|
|
parseStatus = tr("Errors in document");
|
2010-06-07 20:29:46 +00:00
|
|
|
statusLabel->setText(parseStatus);
|
2010-06-05 19:47:49 +00:00
|
|
|
|
2010-06-07 21:59:16 +00:00
|
|
|
/* Highlighting if an error was found */
|
|
|
|
if(skin_error_line() > 0)
|
|
|
|
{
|
2010-06-11 19:03:49 +00:00
|
|
|
editor->addError(skin_error_line());
|
2010-06-11 19:51:34 +00:00
|
|
|
|
|
|
|
/* Now we're going to attempt parsing again at each line, until we find
|
2010-06-11 20:11:45 +00:00
|
|
|
one that won't error out*/
|
2010-06-11 19:51:34 +00:00
|
|
|
QTextDocument doc(editor->document()->toPlainText());
|
2010-06-11 20:11:45 +00:00
|
|
|
int base = 0;
|
|
|
|
while(skin_error_line() > 0 && !doc.isEmpty())
|
2010-06-11 19:51:34 +00:00
|
|
|
{
|
|
|
|
QTextCursor rest(&doc);
|
|
|
|
|
|
|
|
for(int i = 0; i < skin_error_line(); i++)
|
|
|
|
rest.movePosition(QTextCursor::NextBlock,
|
|
|
|
QTextCursor::KeepAnchor);
|
2010-06-11 20:11:45 +00:00
|
|
|
if(skin_error_line() == doc.blockCount())
|
|
|
|
rest.movePosition(QTextCursor::End, QTextCursor::KeepAnchor);
|
|
|
|
|
|
|
|
rest.removeSelectedText();
|
|
|
|
base += skin_error_line();
|
|
|
|
|
|
|
|
skin_parse(doc.toPlainText().toAscii());
|
|
|
|
|
|
|
|
if(skin_error_line() > 0)
|
|
|
|
editor->addError(base + skin_error_line());
|
|
|
|
|
|
|
|
}
|
2010-06-07 21:59:16 +00:00
|
|
|
}
|
|
|
|
|
2010-06-05 19:47:49 +00:00
|
|
|
if(editor->document()->toPlainText() != saved)
|
2010-06-15 06:54:58 +00:00
|
|
|
emit titleChanged(titleText + QChar('*'));
|
2010-06-05 19:47:49 +00:00
|
|
|
else
|
2010-06-15 06:54:58 +00:00
|
|
|
emit titleChanged(titleText);
|
2010-06-11 21:24:38 +00:00
|
|
|
|
2010-07-07 09:33:47 +00:00
|
|
|
if(lastUpdate.msecsTo(QTime::currentTime()) >= updateInterval)
|
|
|
|
{
|
|
|
|
model->render(project, device, &fileName);
|
|
|
|
checkUpdate.stop();
|
|
|
|
lastUpdate = QTime::currentTime();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
checkUpdate.start();
|
|
|
|
}
|
2010-06-11 21:24:38 +00:00
|
|
|
cursorChanged();
|
|
|
|
|
2010-06-05 08:22:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SkinDocument::save()
|
|
|
|
{
|
|
|
|
QFile fout(fileName);
|
|
|
|
|
|
|
|
if(!fout.exists())
|
|
|
|
{
|
2010-06-05 19:47:49 +00:00
|
|
|
saveAs();
|
2010-06-05 08:22:30 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
fout.open(QFile::WriteOnly);
|
|
|
|
fout.write(editor->document()->toPlainText().toAscii());
|
|
|
|
fout.close();
|
|
|
|
|
2010-06-05 19:47:49 +00:00
|
|
|
saved = editor->document()->toPlainText();
|
|
|
|
QStringList decompose = fileName.split('/');
|
2010-06-15 06:54:58 +00:00
|
|
|
titleText = decompose.last();
|
|
|
|
emit titleChanged(titleText);
|
2010-06-05 19:47:49 +00:00
|
|
|
|
2010-06-26 05:51:07 +00:00
|
|
|
scene();
|
|
|
|
|
2010-06-05 08:22:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SkinDocument::saveAs()
|
|
|
|
{
|
|
|
|
/* Determining the directory to open */
|
2010-06-05 19:47:49 +00:00
|
|
|
QString directory = fileName;
|
2010-06-05 08:22:30 +00:00
|
|
|
|
2010-06-05 19:47:49 +00:00
|
|
|
QSettings settings;
|
2010-06-05 08:40:27 +00:00
|
|
|
settings.beginGroup("SkinDocument");
|
2010-06-05 19:47:49 +00:00
|
|
|
if(directory == "")
|
|
|
|
directory = settings.value("defaultDirectory", "").toString();
|
|
|
|
|
|
|
|
fileName = QFileDialog::getSaveFileName(this, tr("Save Document"),
|
2010-06-06 03:35:40 +00:00
|
|
|
directory, fileFilter());
|
2010-06-05 19:47:49 +00:00
|
|
|
directory = fileName;
|
|
|
|
if(fileName == "")
|
|
|
|
return;
|
2010-06-05 08:40:27 +00:00
|
|
|
|
|
|
|
directory.chop(fileName.length() - fileName.lastIndexOf('/') - 1);
|
|
|
|
settings.setValue("defaultDirectory", directory);
|
|
|
|
settings.endGroup();
|
|
|
|
|
|
|
|
QFile fout(fileName);
|
|
|
|
fout.open(QFile::WriteOnly);
|
|
|
|
fout.write(editor->document()->toPlainText().toAscii());
|
|
|
|
fout.close();
|
|
|
|
|
2010-06-05 19:47:49 +00:00
|
|
|
saved = editor->document()->toPlainText();
|
|
|
|
QStringList decompose = fileName.split('/');
|
2010-06-15 06:54:58 +00:00
|
|
|
titleText = decompose[decompose.count() - 1];
|
|
|
|
emit titleChanged(titleText);
|
2010-06-05 19:47:49 +00:00
|
|
|
|
2010-06-26 05:51:07 +00:00
|
|
|
scene();
|
|
|
|
|
2010-06-04 07:57:19 +00:00
|
|
|
}
|
2010-06-17 06:59:46 +00:00
|
|
|
|
|
|
|
QString SkinDocument::findSetting(QString key, QString fallback)
|
|
|
|
{
|
|
|
|
if(!project)
|
|
|
|
return fallback;
|
|
|
|
else
|
|
|
|
return project->getSetting(key, fallback);
|
|
|
|
}
|