2010-05-26 21:13:14 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright (C) 2010 Robert Bieber
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
#include "parsetreemodel.h"
|
2010-06-01 19:55:20 +00:00
|
|
|
#include "symbols.h"
|
2010-06-17 20:44:11 +00:00
|
|
|
#include "rbscreen.h"
|
2010-06-18 21:10:01 +00:00
|
|
|
#include "rbrenderinfo.h"
|
2010-06-01 19:55:20 +00:00
|
|
|
|
|
|
|
#include <cstdlib>
|
|
|
|
|
2010-05-26 21:13:14 +00:00
|
|
|
#include <QObject>
|
2010-06-17 20:44:11 +00:00
|
|
|
#include <QPixmap>
|
2010-06-21 20:11:58 +00:00
|
|
|
#include <QMap>
|
|
|
|
#include <QDir>
|
2010-05-26 21:13:14 +00:00
|
|
|
|
2010-06-22 07:55:50 +00:00
|
|
|
#include <iostream>
|
|
|
|
|
2010-06-01 21:25:02 +00:00
|
|
|
ParseTreeModel::ParseTreeModel(const char* document, QObject* parent):
|
2010-05-26 21:13:14 +00:00
|
|
|
QAbstractItemModel(parent)
|
|
|
|
{
|
2010-05-30 01:20:05 +00:00
|
|
|
this->tree = skin_parse(document);
|
2010-06-02 20:36:30 +00:00
|
|
|
|
|
|
|
if(tree)
|
|
|
|
this->root = new ParseTreeNode(tree);
|
|
|
|
else
|
|
|
|
this->root = 0;
|
2010-06-17 06:59:46 +00:00
|
|
|
|
|
|
|
scene = new QGraphicsScene();
|
2010-05-26 21:13:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ParseTreeModel::~ParseTreeModel()
|
|
|
|
{
|
2010-05-30 01:20:05 +00:00
|
|
|
if(root)
|
|
|
|
delete root;
|
|
|
|
if(tree)
|
|
|
|
skin_free_tree(tree);
|
2010-05-26 21:13:14 +00:00
|
|
|
}
|
|
|
|
|
2010-05-30 01:20:05 +00:00
|
|
|
QString ParseTreeModel::genCode()
|
2010-05-29 00:04:04 +00:00
|
|
|
{
|
2010-06-06 03:35:40 +00:00
|
|
|
if(root)
|
|
|
|
return root->genCode();
|
|
|
|
else
|
|
|
|
return "";
|
2010-05-29 00:04:04 +00:00
|
|
|
}
|
|
|
|
|
2010-06-07 20:29:46 +00:00
|
|
|
QString ParseTreeModel::changeTree(const char *document)
|
2010-06-02 20:36:30 +00:00
|
|
|
{
|
|
|
|
struct skin_element* test = skin_parse(document);
|
|
|
|
|
|
|
|
if(!test)
|
2010-06-07 20:29:46 +00:00
|
|
|
{
|
|
|
|
QString error = tr("Error on line ") +
|
|
|
|
QString::number(skin_error_line())
|
2010-07-18 00:59:02 +00:00
|
|
|
+ tr(", column ") + QString::number(skin_error_col())
|
2010-06-07 20:29:46 +00:00
|
|
|
+ tr(": ") + QString(skin_error_message());
|
|
|
|
return error;
|
|
|
|
}
|
2010-06-02 20:36:30 +00:00
|
|
|
|
|
|
|
ParseTreeNode* temp = new ParseTreeNode(test);
|
|
|
|
|
|
|
|
if(root)
|
|
|
|
{
|
|
|
|
emit beginRemoveRows(QModelIndex(), 0, root->numChildren() - 1);
|
|
|
|
delete root;
|
|
|
|
emit endRemoveRows();
|
|
|
|
}
|
|
|
|
|
|
|
|
root = temp;
|
|
|
|
|
|
|
|
emit beginInsertRows(QModelIndex(), 0, temp->numChildren() - 1);
|
|
|
|
emit endInsertRows();
|
|
|
|
|
2010-06-07 20:29:46 +00:00
|
|
|
return tr("Document Parses Successfully");
|
2010-06-02 20:36:30 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2010-05-26 21:13:14 +00:00
|
|
|
QModelIndex ParseTreeModel::index(int row, int column,
|
|
|
|
const QModelIndex& parent) const
|
|
|
|
{
|
2010-05-30 01:47:35 +00:00
|
|
|
if(!hasIndex(row, column, parent))
|
|
|
|
return QModelIndex();
|
|
|
|
|
|
|
|
ParseTreeNode* foundParent;
|
|
|
|
|
|
|
|
if(parent.isValid())
|
|
|
|
foundParent = static_cast<ParseTreeNode*>(parent.internalPointer());
|
|
|
|
else
|
|
|
|
foundParent = root;
|
|
|
|
|
|
|
|
if(row < foundParent->numChildren() && row >= 0)
|
|
|
|
return createIndex(row, column, foundParent->child(row));
|
|
|
|
else
|
|
|
|
return QModelIndex();
|
2010-05-26 21:13:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QModelIndex ParseTreeModel::parent(const QModelIndex &child) const
|
|
|
|
{
|
2010-05-30 01:47:35 +00:00
|
|
|
if(!child.isValid())
|
|
|
|
return QModelIndex();
|
|
|
|
|
|
|
|
ParseTreeNode* foundParent = static_cast<ParseTreeNode*>
|
|
|
|
(child.internalPointer())->getParent();
|
|
|
|
|
|
|
|
if(foundParent == root)
|
|
|
|
return QModelIndex();
|
|
|
|
|
|
|
|
return createIndex(foundParent->getRow(), 0, foundParent);
|
2010-05-26 21:13:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int ParseTreeModel::rowCount(const QModelIndex &parent) const
|
|
|
|
{
|
2010-06-02 20:36:30 +00:00
|
|
|
if(!root)
|
|
|
|
return 0;
|
|
|
|
|
2010-05-30 01:47:35 +00:00
|
|
|
if(!parent.isValid())
|
|
|
|
return root->numChildren();
|
|
|
|
|
2010-06-01 19:55:20 +00:00
|
|
|
if(parent.column() != typeColumn)
|
2010-05-30 01:47:35 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
return static_cast<ParseTreeNode*>(parent.internalPointer())->numChildren();
|
2010-05-26 21:13:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int ParseTreeModel::columnCount(const QModelIndex &parent) const
|
|
|
|
{
|
2010-06-03 20:05:55 +00:00
|
|
|
if(parent.isValid())
|
|
|
|
return numColumns;
|
|
|
|
else
|
|
|
|
return numColumns;
|
2010-05-26 21:13:14 +00:00
|
|
|
}
|
2010-06-01 19:55:20 +00:00
|
|
|
|
2010-05-26 21:13:14 +00:00
|
|
|
QVariant ParseTreeModel::data(const QModelIndex &index, int role) const
|
|
|
|
{
|
2010-05-30 01:47:35 +00:00
|
|
|
if(!index.isValid())
|
|
|
|
return QVariant();
|
|
|
|
|
|
|
|
if(role != Qt::DisplayRole)
|
|
|
|
return QVariant();
|
|
|
|
|
|
|
|
return static_cast<ParseTreeNode*>(index.internalPointer())->
|
|
|
|
data(index.column());
|
2010-05-26 21:13:14 +00:00
|
|
|
}
|
2010-06-01 19:55:20 +00:00
|
|
|
|
|
|
|
QVariant ParseTreeModel::headerData(int col, Qt::Orientation orientation,
|
|
|
|
int role) const
|
|
|
|
{
|
|
|
|
if(orientation != Qt::Horizontal)
|
|
|
|
return QVariant();
|
|
|
|
|
|
|
|
if(col >= numColumns)
|
|
|
|
return QVariant();
|
|
|
|
|
|
|
|
if(role != Qt::DisplayRole)
|
|
|
|
return QVariant();
|
|
|
|
|
|
|
|
switch(col)
|
|
|
|
{
|
|
|
|
case typeColumn:
|
|
|
|
return QObject::tr("Type");
|
|
|
|
|
|
|
|
case lineColumn:
|
|
|
|
return QObject::tr("Line");
|
|
|
|
|
|
|
|
case valueColumn:
|
|
|
|
return QObject::tr("Value");
|
|
|
|
}
|
|
|
|
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
|
|
|
Qt::ItemFlags ParseTreeModel::flags(const QModelIndex &index) const
|
|
|
|
{
|
|
|
|
Qt::ItemFlags retval = Qt::ItemIsEnabled | Qt::ItemIsSelectable;
|
|
|
|
|
|
|
|
ParseTreeNode* element = static_cast<ParseTreeNode*>
|
|
|
|
(index.internalPointer());
|
2010-06-01 20:19:51 +00:00
|
|
|
|
|
|
|
if((element->isParam()
|
|
|
|
|| element->getElement()->type == TEXT
|
|
|
|
|| element->getElement()->type == COMMENT)
|
|
|
|
&& index.column() == valueColumn)
|
|
|
|
{
|
2010-06-01 19:55:20 +00:00
|
|
|
retval |= Qt::ItemIsEditable;
|
2010-06-01 20:19:51 +00:00
|
|
|
}
|
2010-06-01 19:55:20 +00:00
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ParseTreeModel::setData(const QModelIndex &index, const QVariant &value,
|
|
|
|
int role)
|
|
|
|
{
|
|
|
|
if(role != Qt::EditRole)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if(index.column() != valueColumn)
|
|
|
|
return false;
|
|
|
|
|
2010-06-01 20:19:51 +00:00
|
|
|
ParseTreeNode* node = static_cast<ParseTreeNode*>
|
2010-06-01 19:55:20 +00:00
|
|
|
(index.internalPointer());
|
|
|
|
|
2010-06-01 20:19:51 +00:00
|
|
|
if(node->isParam())
|
2010-06-01 19:55:20 +00:00
|
|
|
{
|
2010-06-01 20:19:51 +00:00
|
|
|
struct skin_tag_parameter* param = node->getParam();
|
|
|
|
|
|
|
|
/* Now that we've established that we do, in fact, have a parameter,
|
|
|
|
* set it to its new value if an acceptable one has been entered
|
|
|
|
*/
|
|
|
|
if(value.toString().trimmed() == QString(QChar(DEFAULTSYM)))
|
|
|
|
{
|
|
|
|
if(islower(param->type_code))
|
|
|
|
param->type = skin_tag_parameter::DEFAULT;
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else if(tolower(param->type_code) == 's'
|
|
|
|
|| tolower(param->type_code) == 'f')
|
|
|
|
{
|
|
|
|
if(param->type == skin_tag_parameter::STRING)
|
|
|
|
free(param->data.text);
|
|
|
|
|
|
|
|
param->type = skin_tag_parameter::STRING;
|
|
|
|
param->data.text = strdup(value.toString().trimmed().toAscii());
|
|
|
|
}
|
|
|
|
else if(tolower(param->type_code) == 'i')
|
|
|
|
{
|
|
|
|
if(!value.canConvert(QVariant::Int))
|
|
|
|
return false;
|
|
|
|
|
2010-07-15 06:24:11 +00:00
|
|
|
param->type = skin_tag_parameter::INTEGER;
|
|
|
|
param->data.number = value.toInt();
|
2010-06-01 20:19:51 +00:00
|
|
|
}
|
2010-06-01 19:55:20 +00:00
|
|
|
else
|
2010-06-01 20:19:51 +00:00
|
|
|
{
|
2010-06-01 19:55:20 +00:00
|
|
|
return false;
|
2010-06-01 20:19:51 +00:00
|
|
|
}
|
2010-06-01 19:55:20 +00:00
|
|
|
}
|
2010-06-01 20:19:51 +00:00
|
|
|
else
|
2010-06-01 19:55:20 +00:00
|
|
|
{
|
2010-06-01 20:19:51 +00:00
|
|
|
struct skin_element* element = node->getElement();
|
2010-06-01 19:55:20 +00:00
|
|
|
|
2010-06-01 20:19:51 +00:00
|
|
|
if(element->type != COMMENT && element->type != TEXT)
|
2010-06-01 19:55:20 +00:00
|
|
|
return false;
|
|
|
|
|
2010-06-10 21:02:44 +00:00
|
|
|
free(element->data);
|
|
|
|
element->data = strdup(value.toString().trimmed().toAscii());
|
2010-06-01 19:55:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
emit dataChanged(index, index);
|
|
|
|
return true;
|
|
|
|
}
|
2010-06-17 06:59:46 +00:00
|
|
|
|
2010-06-21 20:11:58 +00:00
|
|
|
QGraphicsScene* ParseTreeModel::render(ProjectModel* project,
|
2010-06-26 05:18:21 +00:00
|
|
|
DeviceState* device, const QString* file)
|
2010-06-17 06:59:46 +00:00
|
|
|
{
|
|
|
|
scene->clear();
|
|
|
|
|
2010-06-17 20:44:11 +00:00
|
|
|
/* Setting the background */
|
|
|
|
scene->setBackgroundBrush(QBrush(QPixmap(":/render/scenebg.png")));
|
|
|
|
|
2010-06-21 20:11:58 +00:00
|
|
|
/* Preparing settings */
|
|
|
|
QMap<QString, QString> settings;
|
|
|
|
if(project)
|
|
|
|
settings = project->getSettings();
|
|
|
|
|
|
|
|
/* Setting themebase if it can't be derived from the project */
|
|
|
|
if(settings.value("themebase", "") == "" && file && QFile::exists(*file))
|
|
|
|
{
|
|
|
|
QDir base(*file);
|
|
|
|
base.cdUp();
|
|
|
|
settings.insert("themebase", base.canonicalPath());
|
|
|
|
}
|
|
|
|
|
2010-07-03 22:57:42 +00:00
|
|
|
/* Finding imagebase and determining remote/wps status */
|
2010-06-26 05:51:07 +00:00
|
|
|
bool remote = false;
|
2010-07-03 22:57:42 +00:00
|
|
|
bool wps = false;
|
2010-06-22 07:55:50 +00:00
|
|
|
if(file)
|
|
|
|
{
|
|
|
|
QString skinFile = *file;
|
|
|
|
QStringList decomp = skinFile.split("/");
|
|
|
|
skinFile = decomp[decomp.count() - 1];
|
|
|
|
skinFile.chop(skinFile.length() - skinFile.lastIndexOf("."));
|
|
|
|
settings.insert("imagepath", settings.value("themebase","") + "/wps/" +
|
|
|
|
skinFile);
|
2010-06-26 05:51:07 +00:00
|
|
|
|
|
|
|
decomp = file->split(".");
|
|
|
|
QString extension = decomp.last();
|
|
|
|
if(extension[0] == 'r')
|
|
|
|
remote = true;
|
2010-07-03 22:57:42 +00:00
|
|
|
if(extension.right(3) == "wps")
|
|
|
|
wps = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Rendering SBS, if necessary */
|
|
|
|
RBScreen* sbsScreen = 0;
|
|
|
|
if(wps && device->data("rendersbs").toBool())
|
|
|
|
{
|
|
|
|
QString sbsFile = settings.value(remote ? "rsbs" : "sbs", "");
|
|
|
|
sbsFile.replace("/.rockbox" , settings.value("themebase",""));
|
|
|
|
|
|
|
|
if(QFile::exists(sbsFile))
|
|
|
|
{
|
|
|
|
QFile sbs(sbsFile);
|
|
|
|
sbs.open(QFile::ReadOnly | QFile::Text);
|
|
|
|
ParseTreeModel sbsModel(QString(sbs.readAll()).toAscii());
|
|
|
|
|
|
|
|
if(sbsModel.root != 0)
|
|
|
|
{
|
|
|
|
RBRenderInfo sbsInfo(&sbsModel, project, &settings, device,
|
|
|
|
sbsScreen);
|
|
|
|
|
|
|
|
sbsScreen = new RBScreen(sbsInfo, remote);
|
|
|
|
scene->addItem(sbsScreen);
|
|
|
|
|
|
|
|
sbsInfo = RBRenderInfo(&sbsModel, project, &settings, device,
|
|
|
|
sbsScreen);
|
|
|
|
sbsModel.root->render(sbsInfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2010-06-22 07:55:50 +00:00
|
|
|
}
|
|
|
|
|
2010-06-21 20:11:58 +00:00
|
|
|
RBScreen* screen = 0;
|
2010-07-04 01:47:04 +00:00
|
|
|
RBRenderInfo info(this, project, &settings, device, screen, sbsScreen);
|
2010-06-21 20:11:58 +00:00
|
|
|
|
2010-06-17 20:44:11 +00:00
|
|
|
/* Adding the screen */
|
2010-07-03 22:57:42 +00:00
|
|
|
if(sbsScreen)
|
|
|
|
screen = new RBScreen(info, remote, sbsScreen->getCustomUI());
|
|
|
|
else
|
|
|
|
screen = new RBScreen(info, remote);
|
|
|
|
|
|
|
|
if(!sbsScreen)
|
|
|
|
scene->addItem(screen);
|
2010-06-17 06:59:46 +00:00
|
|
|
|
2010-07-04 01:47:04 +00:00
|
|
|
info = RBRenderInfo(this, project, &settings, device, screen, sbsScreen);
|
2010-06-21 20:11:58 +00:00
|
|
|
|
2010-06-18 21:10:01 +00:00
|
|
|
|
|
|
|
/* Rendering the tree */
|
|
|
|
if(root)
|
|
|
|
root->render(info);
|
|
|
|
|
2010-06-17 06:59:46 +00:00
|
|
|
return scene;
|
|
|
|
}
|