1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/treemodel.cpp Tue Aug 05 07:36:53 2008 +0000
1.3 @@ -0,0 +1,130 @@
1.4 +#include <QtGui>
1.5 +
1.6 +#include "treeitem.h"
1.7 +#include "treemodel.h"
1.8 +
1.9 +TreeModel::TreeModel(QObject *parent)
1.10 + : QAbstractItemModel(parent)
1.11 +{
1.12 + QList<QVariant> rootData;
1.13 + rootData << "Heading" << "Type" <<"Note";
1.14 + rootItem = new TreeItem(rootData);
1.15 + setupModelData(rootItem);
1.16 +}
1.17 +
1.18 +TreeModel::~TreeModel()
1.19 +{
1.20 + delete rootItem;
1.21 +}
1.22 +
1.23 +int TreeModel::columnCount(const QModelIndex &parent) const
1.24 +{
1.25 + if (parent.isValid())
1.26 + return static_cast<TreeItem*>(parent.internalPointer())->columnCount();
1.27 + else
1.28 + return rootItem->columnCount();
1.29 +}
1.30 +
1.31 +QVariant TreeModel::data(const QModelIndex &index, int role) const
1.32 +{
1.33 + if (!index.isValid())
1.34 + return QVariant();
1.35 +
1.36 + if (role != Qt::DisplayRole)
1.37 + return QVariant();
1.38 +
1.39 + TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
1.40 +
1.41 + return item->data(index.column());
1.42 +}
1.43 +
1.44 +Qt::ItemFlags TreeModel::flags(const QModelIndex &index) const
1.45 +{
1.46 + if (!index.isValid())
1.47 + return Qt::ItemIsEnabled;
1.48 +
1.49 + return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
1.50 +}
1.51 +
1.52 +QVariant TreeModel::headerData(int section, Qt::Orientation orientation,
1.53 + int role) const
1.54 +{
1.55 + if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
1.56 + return rootItem->data(section);
1.57 +
1.58 + return QVariant();
1.59 +}
1.60 +
1.61 +QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent)
1.62 + const
1.63 +{
1.64 + TreeItem *parentItem;
1.65 +
1.66 + if (!parent.isValid())
1.67 + parentItem = rootItem;
1.68 + else
1.69 + parentItem = static_cast<TreeItem*>(parent.internalPointer());
1.70 +
1.71 + TreeItem *childItem = parentItem->child(row);
1.72 + if (childItem)
1.73 + return createIndex(row, column, childItem);
1.74 + else
1.75 + return QModelIndex();
1.76 +}
1.77 +
1.78 +QModelIndex TreeModel::parent(const QModelIndex &index) const
1.79 +{
1.80 + if (!index.isValid())
1.81 + return QModelIndex();
1.82 +
1.83 + TreeItem *childItem = static_cast<TreeItem*>(index.internalPointer());
1.84 + TreeItem *parentItem = childItem->parent();
1.85 +
1.86 + if (parentItem == rootItem)
1.87 + return QModelIndex();
1.88 +
1.89 + return createIndex(parentItem->row(), 0, parentItem);
1.90 +}
1.91 +
1.92 +int TreeModel::rowCount(const QModelIndex &parent) const
1.93 +{
1.94 + TreeItem *parentItem;
1.95 +
1.96 + if (!parent.isValid())
1.97 + parentItem = rootItem;
1.98 + else
1.99 + parentItem = static_cast<TreeItem*>(parent.internalPointer());
1.100 +
1.101 + return parentItem->childCount();
1.102 +}
1.103 +
1.104 +void TreeModel::setupModelData(TreeItem *root)
1.105 +{
1.106 + QList<QVariant> cData;
1.107 +
1.108 + cData << "Center of map" << "MapCenter"<<"Data 1";
1.109 + TreeItem *mco=new TreeItem (cData,root);
1.110 + root->appendChild (mco);
1.111 +
1.112 + cData.clear();
1.113 + cData << "Main A" << "Branch"<<"Data 2";
1.114 + TreeItem *bo=new TreeItem (cData,mco);
1.115 + mco->appendChild (bo);
1.116 + TreeItem *mainA=bo;
1.117 +
1.118 + cData.clear();
1.119 + cData << "Sub a" << "Branch"<<"Data";
1.120 + bo=new TreeItem (cData,mainA);
1.121 + mainA->appendChild (bo);
1.122 +
1.123 + cData.clear();
1.124 + cData << "Sub b" << "Branch"<<"Data";
1.125 + bo=new TreeItem (cData,mainA);
1.126 + mainA->appendChild (bo);
1.127 +
1.128 + cData.clear();
1.129 + cData << "Main B"<<"Branch" <<"Data 3";
1.130 + mco->appendChild(new TreeItem(cData, mco));
1.131 +
1.132 + //QModelIndex ix=index (0,0,QModelIndex() );
1.133 +}