LeechCraft  0.6.70-15082-g543737046d
Modular cross-platform feature rich live environment.
modelitem.cpp
Go to the documentation of this file.
1 /**********************************************************************
2  * LeechCraft - modular cross-platform feature rich internet client.
3  * Copyright (C) 2006-2014 Georg Rudoy
4  *
5  * Distributed under the Boost Software License, Version 1.0.
6  * (See accompanying file LICENSE or copy at https://www.boost.org/LICENSE_1_0.txt)
7  **********************************************************************/
8 
9 #include "modelitem.h"
10 #include <algorithm>
11 
12 namespace LC::Util
13 {
14  ModelItem::ModelItem (QAbstractItemModel *model, const QModelIndex& idx, const ModelItem_wtr& parent)
15  : ModelItemBase { parent }
16  , Model_ { model }
17  , SrcIdx_ { idx }
18  {
19  }
20 
22  {
23  if (Children_.value (row))
24  return Children_.at (row).get ();
25 
26  if (Children_.size () <= row)
27  Children_.resize (row + 1);
28 
29  const auto& childIdx = Model_->index (row, 0, SrcIdx_);
30  Children_ [row] = std::make_shared<ModelItem> (Model_, childIdx, shared_from_this ());
31  return Children_.at (row).get ();
32  }
33 
34  const QModelIndex& ModelItem::GetIndex () const
35  {
36  return SrcIdx_;
37  }
38 
39  void ModelItem::RefreshIndex (int modelStartingRow)
40  {
41  if (SrcIdx_.isValid ())
42  SrcIdx_ = Model_->index (GetRow () - modelStartingRow, 0, Parent_.lock ()->GetIndex ());
43  }
44 
45  QAbstractItemModel* ModelItem::GetModel () const
46  {
47  return Model_;
48  }
49 
50  ModelItem_ptr ModelItem::FindChild (QModelIndex index) const
51  {
52  index = index.sibling (index.row (), 0);
53 
54  const auto pos = std::find_if (Children_.begin (), Children_.end (),
55  [&index] (const ModelItem_ptr& item) { return item->GetIndex () == index; });
56  return pos == Children_.end () ? ModelItem_ptr {} : *pos;
57  }
58 }
LC::Util
Definition: icoreproxy.h:33
LC::Util::MergeModel::parent
QModelIndex parent(const QModelIndex &) const override
Definition: mergemodel.cpp:91
LC::Util::ModelItem::FindChild
ModelItem_ptr FindChild(QModelIndex index) const
Finds a child item for the given index.
Definition: modelitem.cpp:56
LC::Util::ModelItem
Provides a proxying API on top of an QAbstractItemModel.
Definition: modelitem.h:37
LC::Util::ModelItemBase< ModelItem >::Parent_
T_wptr Parent_
Definition: modelitembase.h:66
LC::Util::ModelItem_wtr
std::weak_ptr< ModelItem > ModelItem_wtr
Definition: modelitem.h:26
LC::Util::ModelItem::RefreshIndex
void RefreshIndex(int modelStartingRow)
Updates the wrapped index so that it points at the given row.
Definition: modelitem.cpp:45
LC::Util::ModelItemBase< ModelItem >::GetRow
int GetRow() const
Returns the index of this item in the parent's children list.
Definition: modelitembase.h:334
LC::Util::ModelItem_ptr
std::shared_ptr< ModelItem > ModelItem_ptr
Definition: modelitem.h:23
LC::Util::ModelItem::EnsureChild
ModelItem * EnsureChild(int row)
Ensures there is a child item at the given row.
Definition: modelitem.cpp:27
LC::Util::ModelItem::GetIndex
const QModelIndex & GetIndex() const
Returns the index this ModelItem instance wraps.
Definition: modelitem.cpp:40
LC::Util::ModelItem::ModelItem
ModelItem()=default
Constructs a default (invalid) ModelItem having no model set.
LC::Util::ModelItem::GetModel
QAbstractItemModel * GetModel() const
Returns the wrapped model.
Definition: modelitem.cpp:51
modelitem.h
LC::Util::ModelItemBase< ModelItem >::Children_
TList_t Children_
Definition: modelitembase.h:67