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-2025 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
22
23namespace BookshelfModel {
24
25class Item {
26
27public: // types:
28
36
37public: // methods:
38
39 Item(Type const type)
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 */
60 QList<Item *> & children() { return m_children; }
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 const index, Item * const 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 const & 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 * const 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(Qt::CheckState const 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(CSwordModuleInfo const & module) const = 0;
128
129 /**
130 \brief Comparsion operator used sorting child items.
131 */
132 virtual bool operator<(Item const & other) const;
133
134private: // methods:
135
136 void setParent(Item * const parent) noexcept
137 { m_parent = (static_cast<void>(BT_ASSERT(parent)), parent); }
138
139private: // fields:
140
143 QList<Item *> m_children;
144 Qt::CheckState m_checkState;
145
146};
147
148class RootItem: public Item {
149
150public: // methods:
151
153
154 bool fitFor(CSwordModuleInfo const &) const override;
155
156};
157
158template <Item::Type TYPE>
159class GroupItem: public Item {
160
161public: // 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
virtual QVariant data(int role=Qt::DisplayRole) const
Returns data for this item.
Definition item.cpp:44
QList< Item * > & children()
Returns the list of child items of this node.
Definition item.h:60
void insertChild(int const index, Item *const newItem)
Inserts the given item as a child at the given index.
Definition item.h:85
Item(Type const type)
Definition item.h:39
int indexFor(Item const &newItem)
Returns the position for where the given child item would be inserted.
Definition item.cpp:27
virtual bool operator<(Item const &other) const
Comparsion operator used sorting child items.
Definition item.cpp:65
Type type() const
Returns the type of this item.
Definition item.h:49
void setCheckState(Qt::CheckState const state)
Sets the check state of this item.
Definition item.h:119
int childIndex() const
Returns the index of this item under its parent.
Definition item.h:66
T * getGroupItem(CSwordModuleInfo const &module, int &outIndex)
Definition item.h:93
void setParent(Item *const parent) noexcept
Definition item.h:136
Qt::CheckState checkState() const
Returns the check state of this item.
Definition item.h:113
virtual bool fitFor(CSwordModuleInfo const &module) const =0
Returns whether this item is fit to contain the given module.
virtual ~Item()
Definition item.cpp:23
Item * parent() const
Returns a pointer to the parent item of this item.
Definition item.h:55
QList< Item * > m_children
Definition item.h:143
bool fitFor(CSwordModuleInfo const &) const override
Returns whether this item is fit to contain the given module.
Definition item.cpp:74
#define T(f)