#ifndef H5MODEL_H
#define H5MODEL_H
#include <QAbstractItemModel>
#include <QModelIndex>
#include <QVariant>
class H5Node;
{
Q_OBJECT
public:
~H5Model();
// readonly
QVariant headerData
(int section, Qt
::Orientation orientation,
int role
) const;
// resizeable
public slots:
private:
// variables
H5Node* m_rootNode;
};
#endif // H5MODEL_H
#ifndef H5MODEL_H
#define H5MODEL_H
#include <QAbstractItemModel>
#include <QModelIndex>
#include <QVariant>
class H5Node;
class H5Model : public QAbstractItemModel
{
Q_OBJECT
public:
H5Model(QObject* parent=0);
~H5Model();
// readonly
QVariant data(const QModelIndex & index, int role) const;
Qt::ItemFlags flags(const QModelIndex & index) const;
QVariant headerData(int section, Qt::Orientation orientation, int role) const;
QModelIndex index(int row, int column, const QModelIndex & parent) const;
QModelIndex parent(const QModelIndex & index) const;
int rowCount(const QModelIndex & parent=QModelIndex()) const;
int columnCount(const QModelIndex & parent=QModelIndex()) const;
// resizeable
bool insertRows(int row, int count, const QModelIndex & parent=QModelIndex());
public slots:
void h5TreeClicked(const QModelIndex & index);
private:
// variables
H5Node* m_rootNode;
};
#endif // H5MODEL_H
To copy to clipboard, switch view to plain text mode
h5model.cpp
#include <QtGui>
#include <QDebug>
#include "h5node.h"
#include "h5model.h"
{
m_rootNode = new H5Node(H5Node::Root, "/data/data", 0);
m_rootNode->loadChildren();
}
H5Model::~H5Model()
{
delete m_rootNode;
}
int H5Model
::columnCount(const QModelIndex & parent
) const {
if (parent.isValid())
return static_cast<H5Node*>(parent.internalPointer())->columnCount();
else
return m_rootNode->columnCount();
}
{
if (!index.isValid() || role != Qt::DisplayRole)
H5Node* node = static_cast<H5Node*>(index.internalPointer());
return node->data(index.column());
}
Qt
::ItemFlags H5Model
::flags(const QModelIndex & index
) const {
if (!index.isValid())
return 0;
return Qt::ItemIsEditable | Qt::ItemIsEnabled | Qt::ItemIsSelectable;
}
QVariant H5Model
::headerData(int section, Qt
::Orientation orientation,
int role
) const {
if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
if (section == 0)
return "Name";
else if (section == 1)
return "Type";
else
}
}
{
if (!hasIndex(row, column, parent))
H5Node* parentItem;
if (!parent.isValid())
parentItem = m_rootNode;
else
parentItem = static_cast<H5Node*>(parent.internalPointer());
H5Node* childItem = parentItem->child(row);
if (childItem)
return createIndex(row, column, childItem);
else
}
{
if (!index.isValid())
H5Node* childItem = static_cast<H5Node*>(index.internalPointer());
H5Node* parentItem = childItem->parent();
if (parentItem == m_rootNode)
return createIndex(parentItem->row(), 0, parentItem);
}
int H5Model
::rowCount(const QModelIndex & parent
) const {
H5Node* parentItem;
if (parent.column() > 0)
return 0;
if (!parent.isValid())
parentItem = m_rootNode;
else
parentItem = static_cast<H5Node*>(parent.internalPointer());
return parentItem->childCount();
}
bool insertRows
(int row,
int count,
const QModelIndex & parent
) {
Q_UNUSED(row)
Q_UNUSED(count)
H5Node* node = static_cast<H5Node*>(parent.internalPointer());
int numChildren = node->loadChildren();
if (numChildren == 0) {
return false;
}
beginInsertRows(parent, 0, numChildren - 1);
node->loadChildren();
endInsertRows();
return true;
}
// slot
{
insertRows(0, 0, index);
}
#include <QtGui>
#include <QDebug>
#include "h5node.h"
#include "h5model.h"
H5Model::H5Model(QObject* parent) : QAbstractItemModel(parent)
{
m_rootNode = new H5Node(H5Node::Root, "/data/data", 0);
m_rootNode->loadChildren();
}
H5Model::~H5Model()
{
delete m_rootNode;
}
int H5Model::columnCount(const QModelIndex & parent) const
{
if (parent.isValid())
return static_cast<H5Node*>(parent.internalPointer())->columnCount();
else
return m_rootNode->columnCount();
}
QVariant H5Model::data(const QModelIndex & index, int role) const
{
if (!index.isValid() || role != Qt::DisplayRole)
return QVariant();
H5Node* node = static_cast<H5Node*>(index.internalPointer());
return node->data(index.column());
}
Qt::ItemFlags H5Model::flags(const QModelIndex & index) const
{
if (!index.isValid())
return 0;
return Qt::ItemIsEditable | Qt::ItemIsEnabled | Qt::ItemIsSelectable;
}
QVariant H5Model::headerData(int section, Qt::Orientation orientation, int role) const
{
if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
if (section == 0)
return "Name";
else if (section == 1)
return "Type";
else
return QVariant();
}
return QVariant();
}
QModelIndex H5Model::index(int row, int column, const QModelIndex & parent) const
{
if (!hasIndex(row, column, parent))
return QModelIndex();
H5Node* parentItem;
if (!parent.isValid())
parentItem = m_rootNode;
else
parentItem = static_cast<H5Node*>(parent.internalPointer());
H5Node* childItem = parentItem->child(row);
if (childItem)
return createIndex(row, column, childItem);
else
return QModelIndex();
}
QModelIndex H5Model::parent(const QModelIndex & index) const
{
if (!index.isValid())
return QModelIndex();
H5Node* childItem = static_cast<H5Node*>(index.internalPointer());
H5Node* parentItem = childItem->parent();
if (parentItem == m_rootNode)
return QModelIndex();
return createIndex(parentItem->row(), 0, parentItem);
}
int H5Model::rowCount(const QModelIndex & parent) const
{
H5Node* parentItem;
if (parent.column() > 0)
return 0;
if (!parent.isValid())
parentItem = m_rootNode;
else
parentItem = static_cast<H5Node*>(parent.internalPointer());
return parentItem->childCount();
}
bool insertRows(int row, int count, const QModelIndex & parent)
{
Q_UNUSED(row)
Q_UNUSED(count)
H5Node* node = static_cast<H5Node*>(parent.internalPointer());
int numChildren = node->loadChildren();
if (numChildren == 0) {
return false;
}
beginInsertRows(parent, 0, numChildren - 1);
node->loadChildren();
endInsertRows();
return true;
}
// slot
void H5Model::h5TreeClicked(const QModelIndex & index)
{
insertRows(0, 0, index);
}
To copy to clipboard, switch view to plain text mode
Bookmarks