BibleTime
item.h
Go to the documentation of this file.
1 /*********
2 *
3 * In the name of the Father, and of the Son, and of the Holy Spirit.
4 *
5 * This file is part of BibleTime's source code, https://bibletime.info/
6 *
7 * Copyright 1999-2021 by the BibleTime developers.
8 * The BibleTime source code is licensed under the GNU General Public License
9 * version 2.0.
10 *
11 **********/
12 
13 #pragma once
14 
15 #include <Qt>
16 #include <QList>
17 #include <QVariant>
18 #include "../../util/btassert.h"
19 
20 
21 class CSwordModuleInfo;
22 
23 namespace BookshelfModel {
24 
25 class Item {
26 
27 public: // types:
28 
29  enum Type {
30  ITEM_ROOT = 0,
34  ITEM_INDEXING = 4
35  };
36 
37 public: // methods:
38 
40  : m_type(type)
41  , m_parent(nullptr)
42  , m_checkState(Qt::Unchecked) {}
43 
44  virtual ~Item();
45 
46  /**
47  \brief Returns the type of this item.
48  */
49  Type type() const { return m_type; }
50 
51  /**
52  \brief Returns a pointer to the parent item of this item.
53  \retval 0 if this item has no parent.
54  */
55  Item * parent() const { return m_parent; }
56 
57  /**
58  \brief Returns the list of child items of this node.
59  */
61 
62  /**
63  \brief Returns the index of this item under its parent.
64  \retval -1 if this item has no parent.
65  */
66  int childIndex() const {
67  return m_parent == nullptr
68  ? -1
69  : m_parent->m_children.indexOf(const_cast<Item *>(this));
70  }
71 
72  /**
73  \brief Returns the position for where the given child item would be
74  inserted.
75  \param[in] newItem Pointer to the item that would be inserted.
76  */
77  int indexFor(Item const & newItem);
78 
79  /**
80  \brief Inserts the given item as a child at the given index.
81  \pre The given index is a valid position for the item.
82  \param[in] index The child index to insert the item at.
83  \param[in] newItem The item to insert.
84  */
85  void insertChild(int index, Item * newItem) {
86  BT_ASSERT(newItem);
87  BT_ASSERT(index >= 0 && index <= m_children.size());
88  m_children.insert(index, newItem);
89  newItem->setParent(this);
90  }
91 
92  template <class T>
93  T * getGroupItem(CSwordModuleInfo & module, int & outIndex) {
94  for (int i = 0; i < m_children.size(); i++) {
95  BT_ASSERT(m_children.at(i)->type() == T::staticItemType());
96  T * item = static_cast<T *>(m_children.at(i));
97  if (item->fitFor(module)) {
98  outIndex = i;
99  return item;
100  }
101  }
102  return nullptr;
103  }
104 
105  /**
106  \brief Returns data for this item.
107  */
108  virtual QVariant data(int role = Qt::DisplayRole) const;
109 
110  /**
111  \brief Returns the check state of this item.
112  */
113  Qt::CheckState checkState() const { return m_checkState; }
114 
115  /**
116  \brief Sets the check state of this item.
117  \param[in] state new check state.
118  */
119  void setCheckState(const Qt::CheckState state) { m_checkState = state; }
120 
121  /**
122  \brief Returns whether this item is fit to contain the given module.
123  \param[in] module The module to check with.
124  \retval true If this item is a group and can contain the given module.
125  \retval false This item is not a group or is a wrong group.
126  */
127  virtual bool fitFor(const CSwordModuleInfo & module) const = 0;
128 
129  /**
130  \brief Comparsion operator used sorting child items.
131  */
132  virtual bool operator<(const Item & other) const;
133 
134 private: // methods:
135 
136  void setParent(Item * parent) noexcept
137  { m_parent = (static_cast<void>(BT_ASSERT(parent)), parent); }
138 
139 private: // fields:
140 
144  Qt::CheckState m_checkState;
145 
146 };
147 
148 class RootItem: public Item {
149 
150 public: // methods:
151 
153 
154  bool fitFor(const CSwordModuleInfo &) const override;
155 
156 };
157 
158 template <Item::Type TYPE>
159 class GroupItem: public Item {
160 
161 public: // methods:
162 
163  GroupItem() : Item(TYPE) {}
164 
165  static Item::Type staticItemType() { return TYPE; }
166 
167 };
168 
169 } // Namespace BookshelfModel
#define BT_ASSERT(...)
Definition: btassert.h:17
static Item::Type staticItemType()
Definition: item.h:165
Qt::CheckState m_checkState
Definition: item.h:144
void setCheckState(const Qt::CheckState state)
Sets the check state of this item.
Definition: item.h:119
virtual QVariant data(int role=Qt::DisplayRole) const
Returns data for this item.
Definition: item.cpp:44
T * getGroupItem(CSwordModuleInfo &module, int &outIndex)
Definition: item.h:93
int indexFor(Item const &newItem)
Returns the position for where the given child item would be inserted.
Definition: item.cpp:27
Type type() const
Returns the type of this item.
Definition: item.h:49
Item(Type type)
Definition: item.h:39
QList< Item * > & children()
Returns the list of child items of this node.
Definition: item.h:60
int childIndex() const
Returns the index of this item under its parent.
Definition: item.h:66
Item * parent() const
Returns a pointer to the parent item of this item.
Definition: item.h:55
void insertChild(int index, Item *newItem)
Inserts the given item as a child at the given index.
Definition: item.h:85
virtual bool operator<(const Item &other) const
Comparsion operator used sorting child items.
Definition: item.cpp:65
Item * m_parent
Definition: item.h:142
virtual bool fitFor(const CSwordModuleInfo &module) const =0
Returns whether this item is fit to contain the given module.
Qt::CheckState checkState() const
Returns the check state of this item.
Definition: item.h:113
virtual ~Item()
Definition: item.cpp:23
void setParent(Item *parent) noexcept
Definition: item.h:136
QList< Item * > m_children
Definition: item.h:143
bool fitFor(const CSwordModuleInfo &) const override
Returns whether this item is fit to contain the given module.
Definition: item.cpp:74
#define T(f)