demos/interview/model.cpp

Go to the documentation of this file.
00001 /****************************************************************************
00002 **
00003 ** Copyright (C) 2004-2006 Trolltech ASA. All rights reserved.
00004 **
00005 ** This file is part of the demonstration applications of the Qt Toolkit.
00006 **
00007 ** This file may be used under the terms of the GNU General Public
00008 ** License version 2.0 as published by the Free Software Foundation
00009 ** and appearing in the file LICENSE.GPL included in the packaging of
00010 ** this file.  Please review the following information to ensure GNU
00011 ** General Public Licensing requirements will be met:
00012 ** http://www.trolltech.com/products/qt/opensource.html
00013 **
00014 ** If you are unsure which license is appropriate for your use, please
00015 ** review the following information:
00016 ** http://www.trolltech.com/products/qt/licensing.html or contact the
00017 ** sales department at sales@trolltech.com.
00018 **
00019 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00020 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
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 }

Generated on Thu Mar 15 11:52:33 2007 for Qt 4.2 User's Guide by  doxygen 1.5.1