42 : QAbstractItemModel (parent)
53 return mapped.model ()->columnCount (mapped);
56 return Headers_.size ();
61 if (orient != Qt::Horizontal || role != Qt::DisplayRole)
64 return Headers_.at (column);
69 if (!
index.isValid ())
76 catch (
const std::exception& e)
78 qWarning () << Q_FUNC_INFO
90 catch (
const std::exception& e)
92 qWarning () << Q_FUNC_INFO
100 if (!hasIndex (row, column,
parent))
103 auto parentItem =
parent.isValid () ?
107 return createIndex (row, column, parentItem->EnsureChild (row));
112 if (!
index.isValid () ||
index.internalPointer () == Root_.get ())
116 auto parent = item->GetParent ();
120 return createIndex (
parent->GetRow (), 0,
parent.get ());
126 return Root_->GetRowCount ();
129 return item->
GetModel ()->rowCount (item->GetIndex ());
136 for (
const auto& type : model->mimeTypes ())
137 if (!result.contains (type))
144 void Merge (QMimeData *out,
const QMimeData *sub)
146 for (
const auto& format : sub->formats ())
147 if (format !=
"text/uri-list" && !out->hasFormat (format))
148 out->setData (format, sub->data (format));
150 out->setUrls (out->urls () + sub->urls ());
156 QMimeData *result =
nullptr;
158 for (
const auto&
index : indexes)
162 const auto subresult = src.model ()->mimeData ({ src });
171 Merge (result, subresult);
181 if (!sourceIndex.isValid ())
185 auto parent = sourceIndex;
192 auto currentItem = Root_;
193 for (
const auto& idx : hier)
195 currentItem = currentItem->FindChild (idx);
198 qWarning () << Q_FUNC_INFO
199 <<
"no next item for" 206 return createIndex (currentItem->GetRow (), sourceIndex.column (), currentItem.get ());
211 const auto item = proxyIndex.isValid () ?
212 static_cast<ModelItem*
> (proxyIndex.internalPointer ()) :
215 const auto& srcIdx = item->GetIndex ();
216 return srcIdx.sibling (srcIdx.row (), proxyIndex.column ());
221 throw std::runtime_error (
"You should not set source model via setSourceModel()");
237 SIGNAL (columnsAboutToBeInserted (
const QModelIndex&,
int,
int)),
241 SIGNAL (columnsAboutToBeRemoved (
const QModelIndex&,
int,
int)),
245 SIGNAL (columnsInserted (
const QModelIndex&,
int,
int)),
249 SIGNAL (columnsRemoved (
const QModelIndex&,
int,
int)),
253 SIGNAL (dataChanged (
const QModelIndex&,
const QModelIndex&)),
257 SIGNAL (layoutAboutToBeChanged ()),
261 SIGNAL (layoutChanged ()),
265 SIGNAL (modelAboutToBeReset ()),
269 SIGNAL (modelReset ()),
273 SIGNAL (rowsAboutToBeInserted (
const QModelIndex&,
int,
int)),
277 SIGNAL (rowsAboutToBeRemoved (
const QModelIndex&,
int,
int)),
281 SIGNAL (rowsInserted (
const QModelIndex&,
int,
int)),
285 SIGNAL (rowsRemoved (
const QModelIndex&,
int,
int)),
289 if (
const auto rc = model->rowCount ())
293 for (
auto i = 0; i < rc; ++i)
294 Root_->AppendChild (model, model->index (i, 0), Root_);
316 qWarning () << Q_FUNC_INFO <<
"not found model" << model;
320 for (
auto r = Root_->begin (); r != Root_->end (); )
321 if ((*r)->GetModel () == model)
323 const auto idx = std::distance (Root_->begin (), r);
325 beginRemoveRows ({}, idx, idx);
326 r = Root_->EraseChild (r);
343 for (
auto i =
Models_.begin (); i != it; ++i)
344 result += (*i)->rowCount ({});
350 const auto child = Root_->GetChild (row);
351 const auto it =
FindModel (child->GetModel ());
361 const auto child = Root_->GetChild (row);
362 const auto it =
FindModel (child->GetModel ());
385 qWarning () <<
"model" << sender ()
386 <<
"called handleColumnsAboutToBeRemoved, ignoring it";
395 qWarning () <<
"model" << sender ()
396 <<
"called handleColumnsRemoved, ignoring it";
400 const QModelIndex& bottomRight)
408 const auto model =
static_cast<QAbstractItemModel*
> (sender ());
410 const auto startingRow =
parent.isValid () ?
414 first + startingRow, last + startingRow);
420 auto model =
static_cast<QAbstractItemModel*
> (sender ());
422 const auto startingRow =
parent.isValid () ?
426 first + startingRow, last + startingRow);
428 const auto rawItem =
parent.isValid () ?
431 const auto& item = rawItem->shared_from_this ();
433 auto it = item->EraseChildren (item->begin () + startingRow + first,
434 item->begin () + startingRow + last + 1);
436 RemovalRefreshers_.push ([=] ()
mutable 438 for ( ; it != item->end () && (*it)->GetModel () == model; ++it)
439 (*it)->RefreshIndex (startingRow);
445 const auto model =
static_cast<QAbstractItemModel*
> (sender ());
447 const auto startingRow =
parent.isValid () ?
451 const auto rawItem =
parent.isValid () ?
454 const auto& item = rawItem->shared_from_this ();
456 for ( ; first <= last; ++first)
458 const auto& srcIdx = model->index (first, 0,
parent);
459 item->InsertChild (startingRow + first, model, srcIdx, item);
465 for (
int rc = item->GetRowCount (); last < rc; ++last)
467 const auto child = item->GetChild (last);
468 if (child->GetModel () != model)
471 child->RefreshIndex (startingRow);
479 RemovalRefreshers_.pop () ();
485 const auto model =
static_cast<QAbstractItemModel*
> (sender ());
486 if (
const auto rc = model->rowCount ())
489 beginRemoveRows ({}, startingRow, rc + startingRow - 1);
490 Root_->EraseChildren (Root_->begin () + startingRow, Root_->begin () + startingRow + rc);
497 const auto model =
static_cast<QAbstractItemModel*
> (sender ());
498 if (
const auto rc = model->rowCount ())
502 beginInsertRows ({}, startingRow, rc + startingRow - 1);
504 for (
int i = 0; i < rc; ++i)
505 Root_->InsertChild (startingRow + i, model, model->index (i, 0, {}), Root_);
513 DefaultAcceptsRowImpl_ =
true;
517 int MergeModel::RowCount (QAbstractItemModel *model)
const 522 int orig = model->rowCount ();
523 if (DefaultAcceptsRowImpl_)
527 for (
int i = 0; i < orig; ++i)
QMimeData * mimeData(const QModelIndexList &indices) const override
Returns the MIME data for the given indices.
const_iterator FindModel(const QAbstractItemModel *model) const
Returns a const_iterator corresponding to the passed model, or one-past-end if no such model is found...
virtual void setSourceModel(QAbstractItemModel *)
virtual void handleModelAboutToBeReset()
QModelIndex index(int, int, const QModelIndex &=QModelIndex()) const override
virtual void handleColumnsRemoved(const QModelIndex &, int, int)
virtual void handleColumnsAboutToBeRemoved(const QModelIndex &, int, int)
virtual void handleColumnsAboutToBeInserted(const QModelIndex &, int, int)
QAbstractItemModel * GetModel() const
Returns the wrapped model.
virtual void handleRowsRemoved(const QModelIndex &, int, int)
virtual void handleRowsAboutToBeInserted(const QModelIndex &, int, int)
QVariant headerData(int, Qt::Orientation, int=Qt::DisplayRole) const override
QVariant data(const QModelIndex &, int=Qt::DisplayRole) const override
virtual QModelIndex mapFromSource(const QModelIndex &index) const
Returns the model index in the MergeModel given the index from the source model.
virtual bool AcceptsRow(QAbstractItemModel *model, int row) const
Allows to filter rows from the resulting model.
QStringList mimeTypes() const override
Returns the union of MIME types of the models.
models_t::const_iterator const_iterator
QList< QAbstractItemModel * > GetAllModels() const
Returns all models intalled into this one.
models_t::iterator iterator
MergeModel(const QStringList &headers, QObject *parent=0)
Constructs the merge model.
virtual void handleRowsAboutToBeRemoved(const QModelIndex &, int, int)
virtual void handleDataChanged(const QModelIndex &, const QModelIndex &)
virtual void handleRowsInserted(const QModelIndex &, int, int)
Qt::ItemFlags flags(const QModelIndex &) const override
QModelIndex parent(const QModelIndex &) const override
int GetStartingRow(const_iterator it) const
Finds starting row for the model pointed by it.
void AddModel(QAbstractItemModel *model)
Adds a model to the list of source models.
void SetHeaders(const QStringList &headers)
Sets the new headers for this model.
Provides a proxying API on top of an QAbstractItemModel.
const_iterator GetModelForRow(int row, int *starting=0) const
Returns the model for the given row.
virtual void handleColumnsInserted(const QModelIndex &, int, int)
virtual QModelIndex mapToSource(const QModelIndex &index) const
Returns the source model index corresponding to the given index from the sorting filter model...
virtual void handleModelReset()
size_t Size() const
Returns the number of child models in the merger.
int columnCount(const QModelIndex &=QModelIndex()) const override
void RemoveModel(QAbstractItemModel *model)
Removes a model from the list of source models.
int rowCount(const QModelIndex &=QModelIndex()) const override