sublime
aggregatemodel.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
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
00031
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
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
00090
00091 return "";
00092 }
00093
00094 int AggregateModel::columnCount(const QModelIndex &parent) const
00095 {
00096
00097 return 1;
00098 }
00099
00100 int AggregateModel::rowCount(const QModelIndex &parent) const
00101 {
00102 if (!parent.isValid())
00103 {
00104
00105 return d->modelList.count();
00106 }
00107 else
00108 {
00109
00110 if (parent.column() != 0)
00111 return 0;
00112
00113
00114 if (parent.internalPointer() == d->internal)
00115 {
00116
00117 return d->modelList[parent.row()]->rowCount(QModelIndex());
00118 }
00119 else
00120 {
00121
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
00136 return d->modelNames[d->modelList[index.row()]];
00137 }
00138 else
00139 {
00140
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
00154 return QModelIndex();
00155 }
00156
00157
00158 QStandardItem *item = static_cast<QStandardItem*>(index.internalPointer());
00159 QModelIndex parent;
00160 if (!item->parent())
00161 {
00162
00163
00164 QStandardItemModel *model = item->model();
00165
00166 int row = d->modelList.indexOf(model);
00167 parent = createIndex(row, 0, d->internal);
00168 }
00169 else
00170 {
00171
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
00187 return createIndex(row, column, d->internal);
00188 }
00189 else if (parent.internalPointer() == d->internal)
00190 {
00191
00192
00193 QStandardItemModel *model = d->modelList[parent.row()];
00194
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
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