42 : QAbstractItemModel (parent)
43 , DefaultAcceptsRowImpl_ (false)
54 return mapped.model ()->columnCount (mapped);
57 return Headers_.size ();
62 if (orient != Qt::Horizontal || role != Qt::DisplayRole)
65 return Headers_.at (column);
70 if (!index.isValid ())
77 catch (
const std::exception& e)
79 qWarning () << Q_FUNC_INFO
91 catch (
const std::exception& e)
93 qWarning () << Q_FUNC_INFO
101 if (!hasIndex (row, column, parent))
104 auto parentItem = parent.isValid () ?
105 static_cast<ModelItem*
> (parent.internalPointer ()) :
108 return createIndex (row, column, parentItem->EnsureChild (row));
113 if (!index.isValid () || index.internalPointer () == Root_.get ())
116 auto item =
static_cast<ModelItem*
> (index.internalPointer ());
117 auto parent = item->GetParent ();
121 return createIndex (
parent->GetRow (), 0,
parent.get ());
126 if (!parent.isValid ())
127 return Root_->GetRowCount ();
129 const auto item =
static_cast<ModelItem*
> (parent.internalPointer ());
130 return item->
GetModel ()->rowCount (item->GetIndex ());
137 for (
const auto& type : model->mimeTypes ())
138 if (!result.contains (type))
145 void Merge (QMimeData *out,
const QMimeData *sub)
147 for (
const auto& format : sub->formats ())
148 if (format !=
"text/uri-list" && !out->hasFormat (format))
149 out->setData (format, sub->data (format));
151 out->setUrls (out->urls () + sub->urls ());
157 QMimeData *result =
nullptr;
159 for (
const auto&
index : indexes)
163 const auto subresult = src.model ()->mimeData ({ src });
172 Merge (result, subresult);
182 if (!sourceIndex.isValid ())
186 auto parent = sourceIndex;
193 auto currentItem = Root_;
194 for (
const auto& idx : hier)
196 currentItem = currentItem->FindChild (idx);
199 qWarning () << Q_FUNC_INFO
200 <<
"no next item for"
207 return createIndex (currentItem->GetRow (), sourceIndex.column (), currentItem.get ());
212 const auto item = proxyIndex.isValid () ?
213 static_cast<ModelItem*
> (proxyIndex.internalPointer ()) :
216 const auto& srcIdx = item->GetIndex ();
217 return srcIdx.sibling (srcIdx.row (), proxyIndex.column ());
222 throw std::runtime_error (
"You should not set source model via setSourceModel()");
238 SIGNAL (columnsAboutToBeInserted (
const QModelIndex&,
int,
int)),
242 SIGNAL (columnsAboutToBeRemoved (
const QModelIndex&,
int,
int)),
246 SIGNAL (columnsInserted (
const QModelIndex&,
int,
int)),
250 SIGNAL (columnsRemoved (
const QModelIndex&,
int,
int)),
254 SIGNAL (dataChanged (
const QModelIndex&,
const QModelIndex&)),
258 SIGNAL (layoutAboutToBeChanged ()),
262 SIGNAL (layoutChanged ()),
266 SIGNAL (modelAboutToBeReset ()),
270 SIGNAL (modelReset ()),
274 SIGNAL (rowsAboutToBeInserted (
const QModelIndex&,
int,
int)),
278 SIGNAL (rowsAboutToBeRemoved (
const QModelIndex&,
int,
int)),
282 SIGNAL (rowsInserted (
const QModelIndex&,
int,
int)),
286 SIGNAL (rowsRemoved (
const QModelIndex&,
int,
int)),
290 if (
const auto rc = model->rowCount ())
294 for (
auto i = 0; i < rc; ++i)
295 Root_->AppendChild (model, model->index (i, 0), Root_);
317 qWarning () << Q_FUNC_INFO <<
"not found model" << model;
321 for (
auto r = Root_->begin (); r != Root_->end (); )
322 if ((*r)->GetModel () == model)
324 const auto idx = std::distance (Root_->begin (), r);
326 beginRemoveRows ({}, idx, idx);
327 r = Root_->EraseChild (r);
342 for (
auto i =
Models_.begin (); i != it; ++i)
343 result += (*i)->rowCount ({});
349 const auto child = Root_->GetChild (row);
350 const auto it =
FindModel (child->GetModel ());
360 const auto child = Root_->GetChild (row);
361 const auto it =
FindModel (child->GetModel ());
384 qWarning () <<
"model" << sender ()
385 <<
"called handleColumnsAboutToBeRemoved, ignoring it";
394 qWarning () <<
"model" << sender ()
395 <<
"called handleColumnsRemoved, ignoring it";
399 const QModelIndex& bottomRight)
407 const auto model =
static_cast<QAbstractItemModel*
> (sender ());
409 const auto startingRow = parent.isValid () ?
413 first + startingRow, last + startingRow);
419 auto model =
static_cast<QAbstractItemModel*
> (sender ());
421 const auto startingRow = parent.isValid () ?
425 first + startingRow, last + startingRow);
427 const auto rawItem = parent.isValid () ?
430 const auto& item = rawItem->shared_from_this ();
432 auto it = item->EraseChildren (item->begin () + startingRow + first,
433 item->begin () + startingRow + last + 1);
435 for ( ; it < item->end (); ++it)
437 if ((*it)->GetModel () != model)
440 (*it)->RefreshIndex (startingRow);
446 const auto model =
static_cast<QAbstractItemModel*
> (sender ());
448 const auto startingRow = parent.isValid () ?
452 const auto rawItem = parent.isValid () ?
455 const auto& item = rawItem->shared_from_this ();
457 for ( ; first <= last; ++first)
459 const auto& srcIdx = model->index (first, 0, parent);
460 item->InsertChild (startingRow + first, model, srcIdx, item);
466 for (
int rc = item->GetRowCount (); last < rc; ++last)
468 const auto child = item->GetChild (last);
469 if (child->GetModel () != model)
472 child->RefreshIndex (startingRow);
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)
virtual QModelIndex mapToSource(const QModelIndex &index) const
Returns the source model index corresponding to the given index from the sorting filter model...
const_iterator GetModelForRow(int row, int *starting=0) const
Returns the model for the given row.
QAbstractItemModel * GetModel() const
virtual void setSourceModel(QAbstractItemModel *)
virtual void handleModelAboutToBeReset()
virtual bool AcceptsRow(QAbstractItemModel *model, int row) const
Allows to filter rows from the resulting model.
virtual void handleColumnsRemoved(const QModelIndex &, int, int)
virtual QVariant headerData(int, Qt::Orientation, int=Qt::DisplayRole) const
virtual QVariant data(const QModelIndex &, int=Qt::DisplayRole) const
virtual void handleColumnsAboutToBeRemoved(const QModelIndex &, int, int)
virtual int columnCount(const QModelIndex &=QModelIndex()) const
virtual void handleColumnsAboutToBeInserted(const QModelIndex &, int, int)
size_t Size() const
Returns the number of child models in the merger.
virtual void handleRowsRemoved(const QModelIndex &, int, int)
virtual void handleRowsAboutToBeInserted(const QModelIndex &, int, int)
QList< QAbstractItemModel * > GetAllModels() const
Returns all models intalled into this one.
virtual Qt::ItemFlags flags(const QModelIndex &) const
models_t::const_iterator const_iterator
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 &)
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 handleRowsInserted(const QModelIndex &, int, int)
virtual QModelIndex parent(const QModelIndex &) const
int GetStartingRow(const_iterator it) const
Finds starting row for the model pointed by it.
virtual int rowCount(const QModelIndex &=QModelIndex()) const
virtual QModelIndex mapFromSource(const QModelIndex &index) const
Returns the model index in the MergeModel given the index from the source model.
void AddModel(QAbstractItemModel *model)
Adds a model to the list of source models.
QStringList mimeTypes() const
Returns the union of MIME types of the models.
void SetHeaders(const QStringList &headers)
Sets the new headers for this model.
virtual void handleColumnsInserted(const QModelIndex &, int, int)
virtual QModelIndex index(int, int, const QModelIndex &=QModelIndex()) const
QMimeData * mimeData(const QModelIndexList &indices) const
Returns the MIME data for the given indices.
virtual void handleModelReset()
void RemoveModel(QAbstractItemModel *model)
Removes a model from the list of source models.