00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "model.h"
00025 #include <QIcon>
00026 #include <QPixmap>
00027
00028 Model::Model(int rows, int columns, QObject *parent)
00029 : QAbstractItemModel(parent),
00030 rc(rows), cc(columns),
00031 tree(new QVector<Node>(rows, Node(0)))
00032 {
00033
00034 }
00035
00036 Model::~Model()
00037 {
00038 delete tree;
00039 }
00040
00041 QModelIndex Model::index(int row, int column, const QModelIndex &parent) const
00042 {
00043 if (row < rc && row >= 0 && column < cc && column >= 0) {
00044 Node *p = static_cast<Node*>(parent.internalPointer());
00045 Node *n = node(row, p);
00046 if (n)
00047 return createIndex(row, column, n);
00048 }
00049 return QModelIndex();
00050 }
00051
00052 QModelIndex Model::parent(const QModelIndex &child) const
00053 {
00054 if (child.isValid()) {
00055 Node *n = static_cast<Node*>(child.internalPointer());
00056 Node *p = parent(n);
00057 if (p)
00058 return createIndex(row(p), 0, p);
00059 }
00060 return QModelIndex();
00061 }
00062
00063 int Model::rowCount(const QModelIndex &parent) const
00064 {
00065 Q_UNUSED(parent);
00066 return rc;
00067 }
00068
00069 int Model::columnCount(const QModelIndex &parent) const
00070 {
00071 Q_UNUSED(parent);
00072 return cc;
00073 }
00074
00075 QVariant Model::data(const QModelIndex &index, int role) const
00076 {
00077 static QIcon folder(QPixmap(":/images/folder.png"));
00078
00079 if (role == Qt::DisplayRole)
00080 return "Item " + QString::number(index.row()) + ":" + QString::number(index.column());
00081 if (role == Qt::DecorationRole)
00082 return qVariantFromValue(folder);
00083 return QVariant();
00084 }
00085
00086 QVariant Model::headerData(int section, Qt::Orientation orientation, int role) const
00087 {
00088 static QIcon services(QPixmap(":/images/services.png"));
00089 if (role == Qt::DisplayRole)
00090 return QString::number(section);
00091 if (role == Qt::DecorationRole)
00092 return qVariantFromValue(services);
00093 return QAbstractItemModel::headerData(section, orientation, role);
00094 }
00095
00096 bool Model::hasChildren(const QModelIndex &parent) const
00097 {
00098 Q_UNUSED(parent);
00099 return rc > 0 && cc > 0;
00100 }
00101
00102 Qt::ItemFlags Model::flags(const QModelIndex &) const
00103 {
00104 return (Qt::ItemIsDragEnabled|Qt::ItemIsSelectable|Qt::ItemIsEnabled);
00105 }
00106
00107 Model::Node *Model::node(int row, Node *parent) const
00108 {
00109 if (parent && !parent->children)
00110 parent->children = new QVector<Node>(rc, Node(parent));
00111 QVector<Node> *v = parent ? parent->children : tree;
00112 return const_cast<Node*>(&(v->at(row)));
00113 }
00114
00115 Model::Node *Model::parent(Node *child) const
00116 {
00117 return child ? child->parent : 0;
00118 }
00119
00120 int Model::row(Node *node) const
00121 {
00122 const Node *first = node->parent ? &(node->parent->children->at(0)) : &(tree->at(0));
00123 return (node - first);
00124 }