LeechCraft  %{LEECHCRAFT_VERSION}
Modular cross-platform feature rich live environment.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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  * Boost Software License - Version 1.0 - August 17th, 2003
6  *
7  * Permission is hereby granted, free of charge, to any person or organization
8  * obtaining a copy of the software and accompanying documentation covered by
9  * this license (the "Software") to use, reproduce, display, distribute,
10  * execute, and transmit the Software, and to prepare derivative works of the
11  * Software, and to permit third-parties to whom the Software is furnished to
12  * do so, all subject to the following:
13  *
14  * The copyright notices in the Software and this entire statement, including
15  * the above license grant, this restriction and the following disclaimer,
16  * must be included in all copies of the Software, in whole or in part, and
17  * all derivative works of the Software, unless such copies or derivative
18  * works are solely in the form of machine-executable object code generated by
19  * a source language processor.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
24  * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
25  * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
26  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27  * DEALINGS IN THE SOFTWARE.
28  **********************************************************************/
29 
30 #include "modelitem.h"
31 #include <algorithm>
32 
33 namespace LeechCraft
34 {
35 namespace Util
36 {
38  : Model_ { nullptr }
39  {
40  }
41 
42  ModelItem::ModelItem (QAbstractItemModel *model, const QModelIndex& idx, const ModelItem_wtr& parent)
43  : Parent_ { parent }
44  , Model_ { model }
45  , SrcIdx_ { idx }
46  {
47  }
48 
50  {
51  return Children_.begin ();
52  }
53 
55  {
56  return Children_.end ();
57  }
58 
60  {
61  return Children_.begin ();
62  }
63 
65  {
66  return Children_.end ();
67  }
68 
70  {
71  return Children_.value (row);
72  }
73 
75  {
76  return Children_;
77  }
78 
80  {
81  return Children_.size ();
82  }
83 
85  {
86  if (Children_.value (row))
87  return Children_.at (row).get ();
88 
89  if (Children_.size () <= row)
90  Children_.resize (row + 1);
91 
92  const auto& childIdx = Model_->index (row, 0, SrcIdx_);
93  Children_ [row].reset (new ModelItem { Model_, childIdx, shared_from_this () });
94  return Children_.at (row).get ();
95  }
96 
98  {
99  return Children_.erase (it);
100  }
101 
103  {
104  return Children_.erase (begin, end);
105  }
106 
107  const QModelIndex& ModelItem::GetIndex () const
108  {
109  return SrcIdx_;
110  }
111 
112  void ModelItem::RefreshIndex (int modelStartingRow)
113  {
114  if (SrcIdx_.isValid ())
115  SrcIdx_ = Model_->index (GetRow () - modelStartingRow, 0, Parent_.lock ()->GetIndex ());
116  }
117 
118  QAbstractItemModel* ModelItem::GetModel () const
119  {
120  return Model_;
121  }
122 
124  {
125  return Parent_.lock ();
126  }
127 
128  int ModelItem::GetRow (const ModelItem_ptr& item) const
129  {
130  return Children_.indexOf (item);
131  }
132 
133  int ModelItem::GetRow (const ModelItem_cptr& item) const
134  {
135  const auto pos = std::find (Children_.begin (), Children_.end (), item);
136  return pos == Children_.end () ?
137  -1 :
138  std::distance (Children_.begin (), pos);
139  }
140 
141  int ModelItem::GetRow () const
142  {
143  return Parent_.lock ()->GetRow (shared_from_this ());
144  }
145 
146  ModelItem_ptr ModelItem::FindChild (QModelIndex index) const
147  {
148  index = index.sibling (index.row (), 0);
149 
150  const auto pos = std::find_if (Children_.begin (), Children_.end (),
151  [&index] (const ModelItem_ptr& item) { return item->GetIndex () == index; });
152  return pos == Children_.end () ? ModelItem_ptr {} : *pos;
153  }
154 }
155 }
ModelItem_ptr GetParent() const
Definition: modelitem.cpp:123
std::shared_ptr< ModelItem > ModelItem_ptr
Definition: modelitem.h:41
QVector< ModelItem_ptr > ModelItemsList_t
Definition: modelitem.h:45
QAbstractItemModel * GetModel() const
Definition: modelitem.cpp:118
ModelItemsList_t::const_iterator const_iterator
Definition: modelitem.h:57
const ModelItemsList_t & GetChildren() const
Definition: modelitem.cpp:74
iterator EraseChild(iterator it)
Definition: modelitem.cpp:97
std::shared_ptr< const ModelItem > ModelItem_cptr
Definition: modelitem.h:46
std::weak_ptr< ModelItem > ModelItem_wtr
Definition: modelitem.h:44
const QModelIndex & GetIndex() const
Definition: modelitem.cpp:107
iterator EraseChildren(iterator begin, iterator end)
Definition: modelitem.cpp:102
ModelItem_ptr FindChild(QModelIndex index) const
Definition: modelitem.cpp:146
ModelItem_ptr GetChild(int row) const
Definition: modelitem.cpp:69
void RefreshIndex(int modelStartingRow)
Definition: modelitem.cpp:112
ModelItem * EnsureChild(int row)
Definition: modelitem.cpp:84
ModelItemsList_t::iterator iterator
Definition: modelitem.h:56