Trying to implement QAbstractItemModel for parse trees, haven't got it working yet (current state will spawn an empty treeview window)

git-svn-id: svn://svn.rockbox.org/rockbox/trunk@26318 a1c6a512-1295-4272-9138-f99709370657
This commit is contained in:
Robert Bieber 2010-05-26 21:13:14 +00:00
parent 28a7c5d369
commit 565cd00963
6 changed files with 267 additions and 3 deletions

View file

@ -32,9 +32,10 @@ namespace wps
#include <cstdio>
#include <QtGui/QApplication>
#include <QFileSystemModel>
#include <QTreeView>
#include "parsetreemodel.h"
int main(int argc, char* argv[])
{
@ -46,6 +47,14 @@ int main(int argc, char* argv[])
wps::skin_free_tree(test);
QApplication app(argc, argv);
QTreeView tree;
ParseTreeModel model(doc);
tree.setModel(&model);
tree.show();
return app.exec();
return 0;
}

View file

@ -0,0 +1,101 @@
/***************************************************************************
* __________ __ ___.
* 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"
#include <QObject>
ParseTreeModel::ParseTreeModel(char* wps, QObject* parent):
QAbstractItemModel(parent)
{
this->wps = skin_parse(wps);
skin_debug_tree(this->wps);
this->root = new ParseTreeNode(this->wps, 0, true);
}
ParseTreeModel::~ParseTreeModel()
{
delete root;
}
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();
}
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);
}
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();
}
int ParseTreeModel::columnCount(const QModelIndex &parent) const
{
return 2;
}
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());
}

View file

@ -0,0 +1,59 @@
/***************************************************************************
* __________ __ ___.
* 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.
*
****************************************************************************/
extern "C"
{
#include "skin_parser.h"
#include "skin_debug.h"
}
#ifndef PARSETREEMODEL_H
#define PARSETREEMODEL_H
#include <QAbstractItemModel>
#include <QList>
#include "parsetreenode.h"
class ParseTreeModel : public QAbstractItemModel
{
Q_OBJECT
public:
/* Initializes a tree with a WPS document in a string */
ParseTreeModel(char* wps, QObject* parent = 0);
virtual ~ParseTreeModel();
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;
struct skin_element* wps;
};
#endif // PARSETREEMODEL_H

View file

@ -0,0 +1,56 @@
#include "parsetreenode.h"
ParseTreeNode::ParseTreeNode(struct skin_element* data, ParseTreeNode* parent,
bool stop):
parentLink(parent), element(data)
{
if(stop)
return;
for(int i = 0; i < 5; i++)
appendChild(new ParseTreeNode(data, this, true));
}
ParseTreeNode::~ParseTreeNode()
{
qDeleteAll(children);
}
void ParseTreeNode::appendChild(ParseTreeNode* child)
{
children.append(child);
}
ParseTreeNode* ParseTreeNode::child(int row)
{
return children[row];
}
int ParseTreeNode::childCount() const
{
return children.count();
}
int ParseTreeNode::columnCount() const
{
return 2;
}
QVariant ParseTreeNode::data(int column) const
{
if(column == 0)
return element->type;
else
return element->line;
}
int ParseTreeNode::row() const
{
if(parentLink)
return parentLink->children.indexOf(const_cast<ParseTreeNode*>(this));
return 0;
}
ParseTreeNode* ParseTreeNode::parent()
{
return parentLink;
}

View file

@ -0,0 +1,35 @@
#ifndef PARSETREENODE_H
#define PARSETREENODE_H
extern "C"
{
#include "skin_parser.h"
}
#include <QString>
#include <QVariant>
#include <QList>
class ParseTreeNode
{
public:
ParseTreeNode(struct skin_element* data, ParseTreeNode* parent, bool stop = false);
virtual ~ParseTreeNode();
void appendChild(ParseTreeNode* child);
ParseTreeNode* child(int row);
int childCount() const;
int columnCount() const;
QVariant data(int column) const;
int row() const;
ParseTreeNode* parent();
private:
ParseTreeNode* parentLink;
QList<ParseTreeNode*> children;
struct skin_element* element;
};
#endif // PARSETREENODE_H

View file

@ -2,10 +2,14 @@ HEADERS += tag_table.h \
symbols.h \
skin_parser.h \
skin_scan.h \
skin_debug.h
skin_debug.h \
parsetreemodel.h \
parsetreenode.h
SOURCES += tag_table.c \
skin_parser.c \
skin_scan.c \
skin_debug.c \
main.cpp
main.cpp \
parsetreemodel.cpp \
parsetreenode.cpp
OTHER_FILES += README