LeechCraft 0.6.70-17609-g3dde4097dd
Modular cross-platform feature rich live environment.
Loading...
Searching...
No Matches
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
12namespace 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}
int GetRow(const T_ptr &item) const
ModelItem * EnsureChild(int row)
Ensures there is a child item at the given row.
Definition modelitem.cpp:21
QAbstractItemModel * GetModel() const
Returns the wrapped model.
Definition modelitem.cpp:45
ModelItem()=default
Constructs a default (invalid) ModelItem having no model set.
void RefreshIndex(int modelStartingRow)
Updates the wrapped index so that it points at the given row.
Definition modelitem.cpp:39
const QModelIndex & GetIndex() const
Returns the index this ModelItem instance wraps.
Definition modelitem.cpp:34
ModelItem_ptr FindChild(QModelIndex index) const
Finds a child item for the given index.
Definition modelitem.cpp:50
std::shared_ptr< ModelItem > ModelItem_ptr
Definition modelitem.h:19
std::weak_ptr< ModelItem > ModelItem_wtr
Definition modelitem.h:20