ModelAggregator.cpp
Go to the documentation of this file.
00001 #include "ModelAggregator.h"
00002 
00003 #include <ModelAggregator.moc>
00004 
00005 namespace Aseba
00006 {
00007         ModelAggregator::ModelAggregator(QObject* parent) :
00008                 QAbstractItemModel(parent)
00009         {
00010 
00011         }
00012 
00013         void ModelAggregator::addModel(QAbstractItemModel* model, unsigned int column)
00014         {
00015                 ModelDescription description;
00016                 description.model = model;
00017                 description.column = column;
00018                 models.append(description);
00019         }
00020 
00021         int ModelAggregator::columnCount(const QModelIndex &parent) const
00022         {
00023                 return 1;
00024         }
00025 
00026         int ModelAggregator::rowCount(const QModelIndex & /*parent*/) const
00027         {
00028                 int count = 0;
00029                 for (ModelList::ConstIterator it = models.begin(); it != models.end(); it++)
00030                         count += (*it).model->rowCount();
00031                 return count;
00032         }
00033 
00034         QVariant ModelAggregator::data(const QModelIndex &index, int role) const
00035         {
00036                 if (!index.isValid())
00037                         return QVariant();
00038 
00039                 int count = 0;
00040                 int previousCount = 0;
00041                 int modelID = 0;
00042                 for (ModelList::ConstIterator it = models.begin(); it != models.end(); it++, modelID++)
00043                 {
00044                         previousCount = count;
00045                         count += (*it).model->rowCount();
00046                         if (index.row() < count)
00047                         {
00048                                 QAbstractItemModel* thisModel = (*it).model;
00049                                 unsigned int thisColumn = (*it).column;
00050                                 QModelIndex newIndex = thisModel->index(index.row()-previousCount, thisColumn);
00051                                 return thisModel->data(newIndex, role);
00052                         }
00053                 }
00054 
00055                 return QVariant();
00056         }
00057 
00058         bool ModelAggregator::hasIndex(int row, int column, const QModelIndex &parent) const
00059         {
00060                 if (parent.isValid())
00061                         return false;
00062                 if (column >= columnCount())
00063                         return false;
00064                 if (row > rowCount())
00065                         return false;
00066                 return true;
00067         }
00068 
00069         QModelIndex ModelAggregator::index(int row, int column, const QModelIndex &parent) const
00070         {
00071                 return hasIndex(row, column, parent) ? createIndex(row, column, 0) : QModelIndex();
00072         }
00073 
00074         QModelIndex ModelAggregator::parent(const QModelIndex &child) const
00075         {
00076                 return QModelIndex();
00077         }
00078 
00079 
00080         // *** TreeChainsawFilter ***
00081         void TreeChainsawFilter::setSourceModel(QAbstractItemModel *sourceModel)
00082         {
00083                 QAbstractProxyModel::setSourceModel(sourceModel);
00084                 sort(0, Qt::AscendingOrder);
00085         }
00086 
00087         int TreeChainsawFilter::rowCount(const QModelIndex &parent) const
00088         {
00089                 return indexList.count();
00090         }
00091 
00092         int TreeChainsawFilter::columnCount(const QModelIndex &parent) const
00093         {
00094                 return 1;
00095         }
00096 
00097         bool TreeChainsawFilter::hasIndex(int row, int column, const QModelIndex &parent) const
00098         {
00099                 if (parent.isValid())
00100                         return false;
00101                 if (column >= columnCount())
00102                         return false;
00103                 if (row > rowCount())
00104                         return false;
00105                 return true;
00106         }
00107 
00108         QModelIndex TreeChainsawFilter::index(int row, int column, const QModelIndex &parent) const
00109         {
00110                 return hasIndex(row, column, parent) ? createIndex(row, column, 0) : QModelIndex();
00111         }
00112 
00113         QModelIndex TreeChainsawFilter::parent(const QModelIndex &child) const
00114         {
00115                 return QModelIndex();
00116         }
00117 
00118         // sourceIndex -> proxyIndex
00119         QModelIndex TreeChainsawFilter::mapFromSource(const QModelIndex &sourceIndex) const
00120         {
00121                 if (!sourceIndex.isValid())
00122                         return QModelIndex();
00123 
00124                 IndexLinkList::ConstIterator it;
00125                 for (it = indexList.constBegin(); it != indexList.constEnd(); it++)
00126                         if (sourceIndex == it->source)
00127                                 return it->proxy;
00128 
00129                 // not found
00130                 return QModelIndex();
00131         }
00132 
00133         // proxyIndex -> sourceIndex
00134         QModelIndex TreeChainsawFilter::mapToSource(const QModelIndex &proxyIndex) const
00135         {
00136                 if (!proxyIndex.isValid())
00137                         return QModelIndex();
00138 
00139                 IndexLinkList::ConstIterator it;
00140                 for (it = indexList.constBegin(); it != indexList.constEnd(); it++)
00141                         if (proxyIndex == it->proxy)
00142                                 return it->source;
00143 
00144                 // not found
00145                 return QModelIndex();
00146         }
00147 
00148         // rebuild the internal data structure
00149         void TreeChainsawFilter::sort(int /* column */, Qt::SortOrder /* order */)
00150         {
00151                 if (!sourceModel())
00152                         return;
00153 
00154                 // get the root and walk the tree
00155                 sortWalkTree(QModelIndex());
00156         }
00157 
00158         // rebuild the internal data structure, private function
00159         void TreeChainsawFilter::sortWalkTree(const QModelIndex &parent)
00160         {
00161                 static int rowCounter = 0;
00162 
00163                 if (!parent.isValid())
00164                         // root node
00165                         rowCounter = 0;
00166 
00167                 QAbstractItemModel* model = sourceModel();
00168                 int childCount = model->rowCount(parent);
00169                 if (childCount == 0)
00170                 {
00171                         // leaf!
00172                         ModelIndexLink indexLink;
00173                         indexLink.source = parent;
00174                         indexLink.proxy = createIndex(rowCounter++, 0, 0);
00175                         indexList.append(indexLink);
00176                         return;
00177                 }
00178 
00179                 // iterate on children
00180                 for (int row = 0; row < childCount; row++)
00181                 {
00182                         //QModelIndex child = parent.child(row, 0);
00183                         QModelIndex child = model->index(row, 0, parent);
00184                         sortWalkTree(child);
00185                 }
00186         }
00187 };


aseba
Author(s): Stéphane Magnenat
autogenerated on Thu Jan 2 2014 11:17:16