42 : QAbstractItemModel (parent)
44 , Root_ (std::make_shared<ModelItem> ())
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
98 QModelIndex
MergeModel::index (
int row,
int column,
const QModelIndex& parent)
const
103 auto parentItem =
parent.isValid () ?
104 static_cast<ModelItem*> (
parent.internalPointer ()) :
107 return createIndex (row, column, parentItem->EnsureChild (row));
112 if (!
index.isValid () ||
index.internalPointer () == Root_.get ())
115 auto item = static_cast<ModelItem*> (
index.internalPointer ());
116 auto parent = item->GetParent ();
120 return createIndex (
parent->GetRow (), 0,
parent.get ());
126 return Root_->GetRowCount ();
128 const auto item = static_cast<ModelItem*> (
parent.internalPointer ());
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);
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 () ?
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)