LeechCraft  0.6.70-11552-gf61ee51c3d
Modular cross-platform feature rich live environment.
modelitembase.h
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 #pragma once
31 
32 #include <memory>
33 #include <QVector>
34 
35 namespace LeechCraft
36 {
37 namespace Util
38 {
68  template<typename T>
69  class ModelItemBase : public std::enable_shared_from_this<T>
70  {
71  protected:
72  using T_wptr = std::weak_ptr<T>;
73  using T_ptr = std::shared_ptr<T>;
74  using T_cptr = std::shared_ptr<const T>;
75  using TList_t = QVector<T_ptr>;
76 
79 
82  ModelItemBase () = default;
83 
88  ModelItemBase (const T_wptr& parent)
89  : Parent_ { parent }
90  {
91  }
92  public:
95  using iterator = typename TList_t::iterator;
96 
99  using const_iterator = typename TList_t::const_iterator;
100 
108  {
109  return Children_.begin ();
110  }
111 
118  {
119  return Children_.end ();
120  }
121 
129  {
130  return Children_.begin ();
131  }
132 
139  {
140  return Children_.end ();
141  }
142 
152  T_ptr GetChild (int row) const
153  {
154  return Children_.value (row);
155  }
156 
161  const TList_t& GetChildren () const
162  {
163  return Children_;
164  }
165 
171  {
172  return Children_;
173  }
174 
179  int GetRowCount () const
180  {
181  return Children_.size ();
182  }
183 
189  bool IsEmpty () const
190  {
191  return Children_.isEmpty ();
192  }
193 
207  {
208  return Children_.erase (it);
209  }
210 
231  {
232  return Children_.erase (begin, end);
233  }
234 
244  void AppendExisting (const T_ptr& t)
245  {
246  Children_ << t;
247  }
248 
258  void AppendExisting (const TList_t& items)
259  {
260  Children_ += items;
261  }
262 
274  template<typename... Args>
275  T_ptr& AppendChild (Args&&... args)
276  {
277  Children_.append (std::make_shared<T> (std::forward<Args> (args)...));
278  return Children_.last ();
279  }
280 
295  template<typename... Args>
296  T_ptr& InsertChild (int pos, Args&&... args)
297  {
298  Children_.insert (pos, std::make_shared<T> (std::forward<Args> (args)...));
299  return Children_ [pos];
300  }
301 
308  T_ptr GetParent () const
309  {
310  return Parent_.lock ();
311  }
312 
318  int GetRow (const T_ptr& item) const
319  {
320  return Children_.indexOf (item);
321  }
322 
328  int GetRow (const T_cptr& item) const
329  {
330  const auto pos = std::find (Children_.begin (), Children_.end (), item);
331  return pos == Children_.end () ?
332  -1 :
333  std::distance (Children_.begin (), pos);
334  }
335 
345  int GetRow () const
346  {
347  const auto parent = GetParent ();
348  if (!parent)
349  return -1;
350  return parent->GetRow (this->shared_from_this ());
351  }
352  };
353 }
354 }
ModelItemBase(const T_wptr &parent)
Constructs a ModelItemBase with a given parent item.
Definition: modelitembase.h:88
detail::ExprTree< detail::ExprType::LeafStaticPlaceholder, boost::mpl::int_< Idx > > pos
Definition: oral.h:883
TList_t & GetChildren()
Returns a non-constant reference to the list of children.
int GetRow() const
Returns the index of this item in the parent's children list.
iterator EraseChildren(iterator begin, iterator end)
Erases all child items in the given range.
int GetRow(const T_ptr &item) const
Returns the index of the item in the children list.
std::shared_ptr< const ModelItem > T_cptr
Definition: modelitembase.h:74
iterator end()
Returns a non-const iterator pointing past the last child item.
typename TList_t::iterator iterator
A non-const iterator for the list of children.
Definition: modelitembase.h:95
std::shared_ptr< ModelItem > T_ptr
Definition: modelitembase.h:73
int GetRowCount() const
Returns the children count.
int GetRow(const T_cptr &item) const
Returns the index of the item in the children list.
Base class for model items for tree-like models.
Definition: modelitembase.h:69
bool IsEmpty() const
Returns whether there are any children at all.
T_ptr & AppendChild(Args &&... args)
Creates a new child item, appends it and returns it.
ModelItemBase()=default
Constructs a default ModelItemBase with no parent.
iterator begin()
Returns a non-const iterator pointing to the beginning of the child items list.
const_iterator begin() const
Returns a const iterator pointing to the beginning of the child items list.
T_ptr GetParent() const
Returns the pointer to the parent item.
iterator EraseChild(iterator it)
Erases a child item at the position defined by it.
void AppendExisting(const T_ptr &t)
Appends a child item t to the list of child items.
T_ptr GetChild(int row) const
Returns a child at the given row.
const TList_t & GetChildren() const
Returns a constant reference to the list of children.
void AppendExisting(const TList_t &items)
Appends a list of items to the list of child items.
const_iterator end() const
Returns a const iterator pointing past the last child item.
T_ptr & InsertChild(int pos, Args &&... args)
Creates a new child item, inserts it at the given position and returns it.
typename TList_t::const_iterator const_iterator
A const iterator for the list of children.
Definition: modelitembase.h:99