generictreeitem.h
Go to the documentation of this file.
1 #ifndef DNAI_MODELS_GENERICTREEITEM_H
2 #define DNAI_MODELS_GENERICTREEITEM_H
3 
4 #include <exception>
5 
6 #include <QDebug>
7 #include <QModelIndex>
8 
9 namespace dnai
10 {
11  namespace models
12  {
13  template<class T>
14  class GenericTreeItem : public QObject
15  {
16  public:
17  explicit GenericTreeItem(T *parent = nullptr) : QObject(nullptr), m_parentItem(parent)
18  {
19  }
20 
21  virtual ~GenericTreeItem()
22  {
23  }
24  virtual void appendChild(T *child) {
25  child->m_parentItem = dynamic_cast<T *>(this);
26 
27  if (child->m_parentItem == nullptr)
28  {
29  qWarning() << "!!!!!! Your are trying to append a child with incompatible type in GenericTreeItem !!!!!!";
30  throw std::runtime_error("Your are trying to append a child with incompatible type in GenericTreeItem");
31  }
32 
33  m_childItems.append(child);
34  }
35  T *child(int row) const { return m_childItems.at(row); }
36  const QList<T *> &childrenItem() const { return m_childItems; }
37 
38  int childCount() const { return m_childItems.count(); }
39  virtual int columnCount() const = 0;
40  virtual int row() const
41  {
42  if (m_parentItem)
43  return m_parentItem->m_childItems.indexOf(const_cast<T*>(dynamic_cast<const T *>(this)));
44  return 0;
45  }
46  T *parentItem() const { return m_parentItem; }
47  QModelIndex idxmodel() const { return m_idx; }
48  void setIdx(const QModelIndex &ref)
49  {
50  if (ref == m_idx)
51  return;
52  m_idx = ref;
53  }
54  void removeOne(T *e)
55  {
56  if (m_childItems.contains(e))
57  m_childItems.removeOne(e);
58  }
59 
61  {
62  while (!m_childItems.isEmpty())
63  delete m_childItems.takeFirst();
64  }
65 
67  {
68  while (!m_childItems.isEmpty())
69  {
70  m_childItems.takeFirst()->m_parentItem = nullptr;
71  }
72  }
73 
74  private:
75  QList<T*> m_childItems;
77  QModelIndex m_idx;
78 
79  };
80  }
81 }
82 
83 #endif //DNAI_MODELS_GENERICTREEITEM_H
T * parentItem() const
Definition: generictreeitem.h:46
QModelIndex m_idx
Definition: generictreeitem.h:77
int childCount() const
Definition: generictreeitem.h:38
T * child(int row) const
Definition: generictreeitem.h:35
QModelIndex idxmodel() const
Definition: generictreeitem.h:47
void deleteChildren()
Definition: generictreeitem.h:60
Definition: generictreeitem.h:14
void setIdx(const QModelIndex &ref)
Definition: generictreeitem.h:48
virtual void appendChild(T *child)
Definition: generictreeitem.h:24
QList< T * > m_childItems
Definition: generictreeitem.h:75
const QList< T * > & childrenItem() const
Definition: generictreeitem.h:36
void removeOne(T *e)
Definition: generictreeitem.h:54
virtual int row() const
Definition: generictreeitem.h:40
Definition: api.h:13
virtual ~GenericTreeItem()
Definition: generictreeitem.h:21
void removeChildren()
Definition: generictreeitem.h:66
GenericTreeItem(T *parent=nullptr)
Definition: generictreeitem.h:17
virtual int columnCount() const =0
T * m_parentItem
Definition: generictreeitem.h:76