Main Page | Class Hierarchy | Class List | Directories | File List | Class Members | Related Pages

RootModel.cpp

00001 /***************************************************************************
00002  *   Copyright (C) 2006 by Graeme Foster                                   *
00003  *   email    foster.graeme@gmail.com                                      *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   along with this program; if not, write to the                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00019  ***************************************************************************/
00020 #include "RootModel.h"
00021 
00022 RootModel::RootModel(QObject *parent)
00023         : QAbstractItemModel(parent)
00024 {
00025         rootItem = 0;
00026 }
00027 
00028 /******************************************************************************
00029  * Inherited from QAbstractItemModel
00030  ******************************************************************************/
00031 int RootModel::columnCount(const QModelIndex &index) const
00032 {
00033         RootItem * item = getRootItem(index);
00034         if (item == 0)
00035                 return 0;
00036         else
00037                 return item->columnCount();
00038 }
00039 
00040 QVariant RootModel::data(const QModelIndex &index, int role) const
00041 {
00042         if (!index.isValid())
00043                 return QVariant();
00044 
00045         RootItem * item = getRootItem(index);
00046         if (role == Qt::TextColorRole)
00047                 return item->textColour(index.column());
00048         if (role == Qt::BackgroundColorRole)
00049                 return item->bgColour(index.column());
00050         if (role != Qt::DisplayRole)
00051                 return QVariant();
00052         return item->data(index.column());
00053 }
00054 
00055 bool RootModel::setData ( const QModelIndex & index, const QVariant & value, int role)
00056 {
00057         if (!index.isValid())
00058                 return false;
00059 
00060         if (role != Qt::EditRole)
00061                 return false;
00062 
00063         RootItem *item = getRootItem(index);
00064         return item->setData(index.column(),value);
00065         
00066 }
00067 
00068 Qt::ItemFlags RootModel::flags(const QModelIndex &index) const
00069 {
00070         if (!index.isValid())
00071                 return Qt::ItemIsEnabled;
00072 
00073         return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
00074 }
00075 
00076 QModelIndex RootModel::index(int row, int column, const QModelIndex &parent) const
00077 {
00078         RootItem *parentItem;
00079 
00080         if (!parent.isValid())
00081                 parentItem = rootItem;
00082         else
00083                 parentItem = getRootItem(parent);
00084 
00085         RootItem *childItem = parentItem->child(row);
00086         if (childItem)
00087                 return createIndex(row, column, childItem);
00088         else
00089                 return QModelIndex();
00090 }
00091 
00092 QModelIndex RootModel::parent(const QModelIndex &index) const
00093 {
00094         if (!index.isValid())
00095                 return QModelIndex();
00096 
00097         RootItem * childItem = getRootItem(index);
00098         RootItem * parentItem = childItem->parent();
00099 
00100         if (parentItem == rootItem)
00101                 return QModelIndex();
00102         if (parentItem == 0)
00103                 return QModelIndex();
00104         return createIndex(parentItem->row(), 0, parentItem);
00105 }
00106 
00107 int RootModel::rowCount(const QModelIndex &parent) const
00108 {
00109         RootItem *parentItem;
00110 
00111         if (!parent.isValid())
00112                 parentItem = rootItem;
00113         else
00114                 parentItem = getRootItem(parent);
00115 
00116         return parentItem->childCount();
00117 }
00118 
00119 /******************************************************************************
00120  * Methods used to add or delete objects from the treeView.
00121  ******************************************************************************/
00122 bool RootModel::insertItem (RootItem * item, QModelIndex & index, int posn)
00123 {
00124         // The index should be the index of item's parent.
00125         // However the index passed could be its own index
00126         // (which will be the case when the Add button is pressed)
00127         // If that is the case then change the index to the parent
00128         RootItem * parent = static_cast<RootItem *>(index.internalPointer());
00129         if (parent != 0)
00130                 if (parent->rtti() == item->rtti())
00131                         index = index.parent();
00132         beginInsertRows(index, posn, posn);
00133         // Insert the item into the model
00134         if (item->parent() != 0)
00135                 item->parent()->insertChild(item);
00136         else
00137                 rootItem->insertChild(item);
00138         endInsertRows();
00139         return true;
00140 }
00141 
00142 bool RootModel::deleteItem (DataClass * dcObj)
00143 {
00144         RootItem * item = rootItem->findItem(dcObj);
00145         if (item == 0)
00146                 return false;
00147         
00148         beginRemoveRows(QModelIndex(), rowCount(), rowCount());
00149         if (item->parent() != 0)
00150                 item->parent()->deleteChild(item);
00151         else
00152                 rootItem->deleteChild(item);
00153         endRemoveRows();
00154         return true;
00155 }
00156 
00157 void RootModel::sort(CompareRI compare, DataClass * dc)
00158 {
00159         RootItem * item = rootItem->findItem(dc);
00160         if (item != 0)
00161                 item->parent()->sort(compare);
00162 }
00163 
00164 /******************************************************************************
00165  * Private Methods
00166  ******************************************************************************/
00167 void RootModel::clearData(RootItem * item)
00168 {
00169         if(item == 0)
00170                 rootItem->clearAllChildren();
00171         else
00172                 item->clearAllChildren();
00173 }

Generated on Thu Apr 6 16:27:17 2006 for time-table by  doxygen 1.4.4