defuze.me  Client
treemodel.cpp
00001 /****************************************************************************
00002  **
00003  ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
00004  ** All rights reserved.
00005  ** Contact: Nokia Corporation (qt-info@nokia.com)
00006  **
00007  ** This file is part of the examples of the Qt Toolkit.
00008  **
00009  ** $QT_BEGIN_LICENSE:BSD$
00010  ** You may use this file under the terms of the BSD license as follows:
00011  **
00012  ** "Redistribution and use in source and binary forms, with or without
00013  ** modification, are permitted provided that the following conditions are
00014  ** met:
00015  **   * Redistributions of source code must retain the above copyright
00016  **     notice, this list of conditions and the following disclaimer.
00017  **   * Redistributions in binary form must reproduce the above copyright
00018  **     notice, this list of conditions and the following disclaimer in
00019  **     the documentation and/or other materials provided with the
00020  **     distribution.
00021  **   * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
00022  **     the names of its contributors may be used to endorse or promote
00023  **     products derived from this software without specific prior written
00024  **     permission.
00025  **
00026  ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00027  ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00028  ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00029  ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00030  ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00031  ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00032  ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00033  ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00034  ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00035  ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00036  ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
00037  ** $QT_END_LICENSE$
00038  **
00039  ****************************************************************************/
00040 
00041 #include <QtGui>
00042 #include "treeitem.hpp"
00043 #include "treemodel.hpp"
00044 
00045 TreeModel::TreeModel(QObject *parent) : QAbstractItemModel(parent)
00046 {
00047     QList<QVariant> rootData;
00048     rootData << "1";
00049     rootItem = new TreeItem(rootData);
00050 }
00051 
00052 TreeModel::~TreeModel()
00053 {
00054     delete rootItem;
00055 }
00056 
00057 int TreeModel::columnCount(const QModelIndex &parent) const
00058 {
00059     if (parent.isValid())
00060         return static_cast<TreeItem*>(parent.internalPointer())->columnCount();
00061     else
00062         return rootItem->columnCount();
00063 }
00064 
00065 QVariant TreeModel::data(const QModelIndex &index, int role) const
00066 {
00067     if (!index.isValid())
00068         return QVariant();
00069 
00070     if (role != Qt::DisplayRole)
00071         return QVariant();
00072 
00073     TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
00074 
00075     return item->data(index.column());
00076 }
00077 
00078 Qt::ItemFlags TreeModel::flags(const QModelIndex &index) const
00079 {
00080     if (!index.isValid())
00081         return 0;
00082 
00083     return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
00084 }
00085 
00086 QVariant TreeModel::headerData(int section, Qt::Orientation orientation,
00087                                int role) const
00088 {
00089     if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
00090         return rootItem->data(section);
00091 
00092     return QVariant();
00093 }
00094 
00095 QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent)
00096 const
00097 {
00098     if (!hasIndex(row, column, parent))
00099         return QModelIndex();
00100 
00101     TreeItem *parentItem;
00102 
00103     if (!parent.isValid())
00104         parentItem = rootItem;
00105     else
00106         parentItem = static_cast<TreeItem*>(parent.internalPointer());
00107 
00108     TreeItem *childItem = parentItem->child(row);
00109     if (childItem)
00110         return createIndex(row, column, childItem);
00111     else
00112         return QModelIndex();
00113 }
00114 
00115 QModelIndex TreeModel::parent(const QModelIndex &index) const
00116 {
00117     if (!index.isValid())
00118         return QModelIndex();
00119 
00120     TreeItem *childItem = static_cast<TreeItem*>(index.internalPointer());
00121     TreeItem *parentItem = childItem->parent();
00122 
00123     if (parentItem == rootItem)
00124         return QModelIndex();
00125 
00126     return createIndex(parentItem->row(), 0, parentItem);
00127 }
00128 
00129 int TreeModel::rowCount(const QModelIndex &parent) const
00130 {
00131     TreeItem *parentItem;
00132     if (parent.column() > 0)
00133         return 0;
00134 
00135     if (!parent.isValid())
00136         parentItem = rootItem;
00137     else
00138         parentItem = static_cast<TreeItem*>(parent.internalPointer());
00139 
00140     return parentItem->childCount();
00141 }