• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • KDevelop Platform Libraries
  • Sitemap
  • Contact Us
 

sublime

aggregatemodel.cpp

00001 /***************************************************************************
00002  *   Copyright 2007 Alexander Dymo  <adymo@kdevelop.org>            *
00003  *                                                                         *
00004  *   This program is free software; you can redistribute it and/or modify  *
00005  *   it under the terms of the GNU Library General Public License as       *
00006  *   published by the Free Software Foundation; either version 2 of the    *
00007  *   License, or (at your option) any later version.                       *
00008  *                                                                         *
00009  *   This program is distributed in the hope that it will be useful,       *
00010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00012  *   GNU General Public License for more details.                          *
00013  *                                                                         *
00014  *   You should have received a copy of the GNU Library General Public     *
00015  *   License along with this program; if not, write to the                 *
00016  *   Free Software Foundation, Inc.,                                       *
00017  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
00018  ***************************************************************************/
00019 #include "aggregatemodel.h"
00020 
00021 #include <QStandardItem>
00022 #include <QStandardItemModel>
00023 
00024 #include <kdebug.h>
00025 
00026 namespace Sublime {
00027 
00028 struct AggregateModelPrivate {
00029 
00030     /*Instance of this class is used as an internal pointer to the aggregator items
00031     in the model to differentiate between aggregators and non-aggregators.*/
00032     class AggregateInternalData {
00033     };
00034 
00035     AggregateModelPrivate()
00036     {
00037         internal = new AggregateInternalData();
00038     }
00039     ~AggregateModelPrivate()
00040     {
00041         delete internal;
00042     }
00043 
00044     QList<QStandardItemModel*> modelList;
00045     QMap<QStandardItemModel*, QString> modelNames;
00046     AggregateInternalData *internal;
00047 };
00048 
00049 
00050 
00051 AggregateModel::AggregateModel(QObject *parent)
00052     :QAbstractItemModel(parent)
00053 {
00054     d = new AggregateModelPrivate();
00055 }
00056 
00057 AggregateModel::~AggregateModel()
00058 {
00059     delete d;
00060 }
00061 
00062 void AggregateModel::addModel(const QString &name, QStandardItemModel *model)
00063 {
00064     d->modelList << model;
00065     d->modelNames[model] = name;
00066     reset();
00067 }
00068 
00069 void AggregateModel::removeModel(QStandardItemModel *model)
00070 {
00071     d->modelList.removeAll(model);
00072     d->modelNames.remove(model);
00073     reset();
00074 }
00075 
00076 
00077 
00078 // reimplemented methods from QAbstractItemModel
00079 
00080 Qt::ItemFlags AggregateModel::flags(const QModelIndex &index) const
00081 {
00082     if (!index.isValid())
00083         return 0;
00084     return Qt::ItemIsEnabled & Qt::ItemIsSelectable;
00085 }
00086 
00087 QVariant AggregateModel::headerData(int section, Qt::Orientation orientation, int role) const
00088 {
00089     //there's nothing to return here because aggregated models will have different headers
00090     //so we just use empty headers for aggregate model.
00091     return "";
00092 }
00093 
00094 int AggregateModel::columnCount(const QModelIndex &parent) const
00095 {
00096     //only 1 column is supported atm
00097     return 1;
00098 }
00099 
00100 int AggregateModel::rowCount(const QModelIndex &parent) const
00101 {
00102     if (!parent.isValid())
00103     {
00104         //toplevel items represent aggregated models
00105         return d->modelList.count();
00106     }
00107     else
00108     {
00109         //Qt model guideline - only 1st column has children
00110         if (parent.column() != 0)
00111             return 0;
00112 
00113         //find out if the parent is an aggregator
00114         if (parent.internalPointer() == d->internal)
00115         {
00116             //return the number of toplevel rows in the source model
00117             return d->modelList[parent.row()]->rowCount(QModelIndex());
00118         }
00119         else
00120         {
00121             //we have a standard item in the source model - just map it into our model
00122             QStandardItem *item = static_cast<QStandardItem*>(parent.internalPointer());
00123             return item->rowCount();
00124         }
00125     }
00126 }
00127 
00128 QVariant AggregateModel::data(const QModelIndex &index, int role) const
00129 {
00130     if (!index.isValid() || (role != Qt::DisplayRole))
00131         return QVariant();
00132 
00133     if (!index.parent().isValid())
00134     {
00135         //aggregator item
00136         return d->modelNames[d->modelList[index.row()]];
00137     }
00138     else
00139     {
00140         //we have a standard item in the source model - just map it into our model
00141         QStandardItem *item = static_cast<QStandardItem*>(index.internalPointer());
00142         return item->data(role);
00143     }
00144 }
00145 
00146 QModelIndex AggregateModel::parent(const QModelIndex &index) const
00147 {
00148     if (!index.isValid())
00149         return QModelIndex();
00150 
00151     if (index.internalPointer() == d->internal)
00152     {
00153         //this is aggregator item, it has no parents
00154         return QModelIndex();
00155     }
00156 
00157     //this is just an item from the model
00158     QStandardItem *item = static_cast<QStandardItem*>(index.internalPointer());
00159     QModelIndex parent;
00160     if (!item->parent())
00161     {
00162         //we need to find the aggregator item that owns this index
00163         //first find the model for this index
00164         QStandardItemModel *model = item->model();
00165         //next find the row number of the aggregator item
00166         int row = d->modelList.indexOf(model);
00167         parent = createIndex(row, 0, d->internal);
00168     }
00169     else
00170     {
00171         //we have a standard item in the source model - just map it into our model
00172         parent = createIndex(item->parent()->row(), 0, item->parent());
00173     }
00174     return parent;
00175 }
00176 
00177 QModelIndex AggregateModel::index(int row, int column, const QModelIndex &parent) const
00178 {
00179     if (row < 0 || column < 0)
00180         return QModelIndex();
00181 
00182     if (!parent.isValid())
00183     {
00184         if (column > 1 || row >= d->modelList.count())
00185             return QModelIndex();
00186         //this is an aggregator item
00187         return createIndex(row, column, d->internal);
00188     }
00189     else if (parent.internalPointer() == d->internal)
00190     {
00191         //the parent is an aggregator
00192         //find the model that holds the items
00193         QStandardItemModel *model = d->modelList[parent.row()];
00194         //this is the first level of items
00195         QStandardItem *item = model->item(row, column);
00196         if (item)
00197             return createIndex(row, column, item);
00198         else
00199             return QModelIndex();
00200     }
00201     else
00202     {
00203         //we have a standard item in the source model - just map it into our model
00204         QStandardItem *parentItem = static_cast<QStandardItem*>(parent.internalPointer());
00205         return createIndex(row, column, parentItem->child(row, column));
00206     }
00207 }
00208 
00209 }
00210 
00211 #include "aggregatemodel.moc"
00212 

sublime

Skip menu "sublime"
  • Main Page
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members
  • Related Pages

KDevelop Platform Libraries

Skip menu "KDevelop Platform Libraries"
  • interfaces
  • language
  •   codegen
  •   duchain
  •   editor
  • outputview
  •     interfaces
  • project
  • shell
  • sublime
  • util
  • vcs
Generated for KDevelop Platform Libraries by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal