Themeditor: Got the ParseTreeNode class in good shape, preparing to start on ParseTreeModel

git-svn-id: svn://svn.rockbox.org/rockbox/trunk@26400 a1c6a512-1295-4272-9138-f99709370657
This commit is contained in:
Robert Bieber 2010-05-30 01:20:05 +00:00
parent cc07d68866
commit 9843626b69
4 changed files with 170 additions and 62 deletions

View file

@ -23,85 +23,48 @@
#include "parsetreemodel.h"
#include <QObject>
ParseTreeModel::ParseTreeModel(char* wps, QObject* parent):
ParseTreeModel::ParseTreeModel(char* document, QObject* parent):
QAbstractItemModel(parent)
{
this->tree = skin_parse(wps);
this->root = new ParseTreeNode(tree, 0);
this->tree = skin_parse(document);
this->root = new ParseTreeNode(tree);
}
ParseTreeModel::~ParseTreeModel()
{
delete root;
if(root)
delete root;
if(tree)
skin_free_tree(tree);
}
QString genCode()
QString ParseTreeModel::genCode()
{
return QString();
return root->genCode();
}
/*
QModelIndex ParseTreeModel::index(int row, int column,
const QModelIndex& parent) const
{
if(!hasIndex(row, column, parent))
return QModelIndex();
ParseTreeNode* parentLookup;
if(!parent.isValid())
parentLookup = root;
else
parentLookup = static_cast<ParseTreeNode*>(parent.internalPointer());
ParseTreeNode* childLookup = parentLookup->child(row);
if(childLookup)
return createIndex(row, column, childLookup);
else
return QModelIndex();
return QModelIndex();
}
QModelIndex ParseTreeModel::parent(const QModelIndex &child) const
{
if(!child.isValid())
return QModelIndex();
ParseTreeNode* childLookup = static_cast<ParseTreeNode*>
(child.internalPointer());
ParseTreeNode* parentLookup = childLookup->parent();
if(parentLookup == root)
return QModelIndex();
return createIndex(parentLookup->row(), 0, parentLookup);
return QModelIndex();
}
int ParseTreeModel::rowCount(const QModelIndex &parent) const
{
ParseTreeNode* parentLookup;
if(parent.column() > 0)
return 0;
if(!parent.isValid())
parentLookup = root;
else
parentLookup = static_cast<ParseTreeNode*>(parent.internalPointer());
return parentLookup->childCount();
return 0;
}
int ParseTreeModel::columnCount(const QModelIndex &parent) const
{
return 2;
return 0;
}
QVariant ParseTreeModel::data(const QModelIndex &index, int role) const
{
if(!index.isValid() || role != Qt::DisplayRole)
return QVariant();
ParseTreeNode* item = static_cast<ParseTreeNode*>(index.internalPointer());
return item->data(index.column());
return QVariant();
}
*/

View file

@ -40,18 +40,16 @@ class ParseTreeModel : public QAbstractItemModel
public:
/* Initializes a tree with a WPS document in a string */
ParseTreeModel(char* wps, QObject* parent = 0);
ParseTreeModel(char* document, QObject* parent = 0);
virtual ~ParseTreeModel();
QString genCode();
/*
QModelIndex index(int row, int column, const QModelIndex& parent) const;
QModelIndex parent(const QModelIndex &child) const;
int rowCount(const QModelIndex &parent) const;
int columnCount(const QModelIndex &parent) const;
QVariant data(const QModelIndex &index, int role) const;
*/
private:
ParseTreeNode* root;

View file

@ -181,6 +181,10 @@ QString ParseTreeNode::genCode() const
buffer.append(DEFAULTSYM);
break;
case skin_tag_parameter::CODE:
buffer.append(QObject::tr("This doesn't belong here"));
break;
}
}
else
@ -191,10 +195,152 @@ QString ParseTreeNode::genCode() const
return buffer;
}
/*
ParseTreeNode* child(int row);
int numChildren() const;
QVariant data(int column) const;
int getRow() const;
ParseTreeNode* getParent();
*/
ParseTreeNode* ParseTreeNode::child(int row)
{
if(row < 0 || row >= children.count())
return 0;
return children[row];
}
int ParseTreeNode::numChildren() const
{
return children.count();
}
QVariant ParseTreeNode::data(int column) const
{
switch(column)
{
/* Column 0 is the element type */
case 0:
if(element)
{
switch(element->type)
{
case LINE:
return QObject::tr("Logical Line");
case SUBLINES:
return QObject::tr("Alternator");
case COMMENT:
return QObject::tr("Comment");
case CONDITIONAL:
return QObject::tr("Conditional Tag");
case TAG:
return QObject::tr("Tag");
case NEWLINE:
return QObject::tr("Newline");
case TEXT:
return QObject::tr("Plaintext");
}
}
else if(param)
{
switch(param->type)
{
case skin_tag_parameter::STRING:
return QObject::tr("String");
case skin_tag_parameter::NUMERIC:
return QObject::tr("Number");
case skin_tag_parameter::DEFAULT:
return QObject::tr("Default Argument");
case skin_tag_parameter::CODE:
return QObject::tr("This doesn't belong here");
}
}
else
{
return QObject::tr("Root");
}
break;
/* Column 1 is the value */
case 1:
if(element)
{
switch(element->type)
{
case LINE:
return QString();
case SUBLINES:
return QString();
case NEWLINE:
return QObject::tr("\\n");
case TEXT:
case COMMENT:
return QString(element->text);
case CONDITIONAL:
case TAG:
return QString(element->name);
}
}
else if(param)
{
switch(param->type)
{
case skin_tag_parameter::DEFAULT:
return QObject::tr("-");
case skin_tag_parameter::STRING:
return QString(param->data.text);
case skin_tag_parameter::NUMERIC:
return QString::number(param->data.numeric, 10);
case skin_tag_parameter::CODE:
return QObject::tr("Seriously, something's wrong here");
}
}
else
{
return QString();
}
break;
/* Column 2 is the line number */
case 2:
if(element)
return QString::number(element->line, 10);
else
return QString();
break;
}
return QVariant();
}
int ParseTreeNode::getRow() const
{
if(!parent)
return -1;
return parent->children.indexOf(const_cast<ParseTreeNode*>(this));
}
ParseTreeNode* ParseTreeNode::getParent() const
{
return parent;
}
ParseTreeNode::~ParseTreeNode()
{
for(int i = 0; i < children.count(); i++)
delete children[i];
}

View file

@ -37,6 +37,7 @@ public:
ParseTreeNode(struct skin_element* data);
ParseTreeNode(struct skin_element* data, ParseTreeNode* parent);
ParseTreeNode(struct skin_tag_parameter* data, ParseTreeNode* parent);
virtual ~ParseTreeNode();
QString genCode() const;
@ -44,7 +45,7 @@ public:
int numChildren() const;
QVariant data(int column) const;
int getRow() const;
ParseTreeNode* getParent();
ParseTreeNode* getParent() const;
private:
ParseTreeNode* parent;