BibleTime
cbooktreechooser.cpp
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#include "cbooktreechooser.h"
14
15#include <memory>
16#include <QApplication>
17#include <QCursor>
18#include <QHBoxLayout>
19#include <QStringList>
20#include <Qt>
21#include <QTreeWidgetItemIterator>
22#include <QTreeWidget>
23#include <QTreeWidgetItem>
24#include "../../backend/config/btconfig.h"
25#include "../../backend/drivers/btmodulelist.h"
26#include "../../backend/drivers/cswordbookmoduleinfo.h"
27#include "../../backend/drivers/cswordmoduleinfo.h"
28#include "../../backend/keys/cswordkey.h"
29#include "../../backend/keys/cswordtreekey.h"
30#include "../../util/btconnect.h"
31
32
34 CSwordKey * key,
35 QWidget * parent)
36 : CKeyChooser(parent)
37 , m_key(dynamic_cast<CSwordTreeKey *>(key))
38{
39
40 setModules(modules, false);
41
42 //if there is no module there is no key either
43 if (!modules.count()) {
44 m_modules.clear();
45 m_key = nullptr;
46 }
47
48 //now setup the keychooser widgets
49 m_treeView = new QTreeWidget(this);
50
51 QHBoxLayout* layout = new QHBoxLayout(this);
52 layout->setSpacing(0);
53 layout->setContentsMargins(0, 0, 0, 0);
54 layout->addWidget(m_treeView);
55 m_treeView->setHeaderHidden(true);
56
57 //when user selects the item whe must react
58 BT_CONNECT(m_treeView, &QTreeWidget::currentItemChanged,
60
61 setKey(key);
62 adjustFont();
63}
64
65/** Sets a new key to this keychooser. Inherited from ckeychooser. */
67 if (m_key != newKey ) {
68 m_key = dynamic_cast<CSwordTreeKey*>(newKey);
69 }
70
71 const QString key = m_key->key(); //key as text, path
72
73 QTreeWidgetItem* matching_item = m_treeView->topLevelItem(0);
74
75 QTreeWidgetItemIterator it(m_treeView);
76 while (*it) {
77 if ((*it)->text(1) == key) {
78 matching_item = (*it);
79 break;
80 }
81 ++it;
82 }
83
84 m_treeView->setCurrentItem( matching_item );
85 m_treeView->scrollToItem(matching_item);
86}
87
89 bool refresh)
90{
91 using CSBMI = CSwordBookModuleInfo;
92
93 //Add given modules into private list
94 m_modules.clear();
95 for (auto const * const m : modules) {
96 const CSBMI *book = dynamic_cast<const CSBMI*>(m);
97 if (book != nullptr) {
98 m_modules.append(book);
99 }
100 }
101
102 //if there exists a module and a key, setup the visible tree
103 if (refresh && m_modules.count() && m_key) {
104 auto const offset = m_key->offset(); //actually unnecessary, taken care of in setupTree
105 setupTree();
106 m_key->setOffset( offset );
107
108 adjustFont(); //only when refresh is set.
109 }
110}
111
112/** From ckeychooser. */
114 //Make sure the entries are displayed correctly.
115 m_treeView->setFont(btConfig().getFontForLanguage(*m_modules.first()->language()).second);
116}
117
118
119/** Refreshes the content. Inherited from ckeychooser. */
121 if (m_key) {
122 updateKey(m_key); //refresh with current key
123 }
124}
125
126
127/// \todo itemActivated is called too many times. As tested in GDB, the function
128//is called twice with the pointer to the correct book and twice with a null
129//pointer.
130
131/** Slot for signal when item is selected by user. */
132void CBookTreeChooser::itemActivated( QTreeWidgetItem* item ) {
133 //Sometimes Qt calls this function with a null pointer.
134 if (item) {
135 m_key->setKey(item->text(1));
136 //tell possible listeners about the change
137 Q_EMIT keyChanged(m_key);
138 }
139}
140
141/** Inherited from ckeychooser */
145
146/** Reimplementation to handle tree creation on show. */
147void CBookTreeChooser::showEvent(QShowEvent * const showEvent) {
148 if (!m_treeView->topLevelItemCount()) {
149 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
150 setupTree(); //create the tree structure
151 m_treeView->resize(m_treeView->sizeHint());
152 QApplication::restoreOverrideCursor();
153 }
154 CKeyChooser::showEvent(showEvent);
155}
156
157/** Creates the tree structure in the view. */
159 m_treeView->clear();
160
161 auto const offset = m_key->offset();
162
164 addKeyChildren(m_key, m_treeView->invisibleRootItem());
165
166 m_key->setOffset( offset );
167 setKey(m_key); // the module may have changed
168}
169
170/** Populates tree widget with items. */
171void CBookTreeChooser::addKeyChildren(CSwordTreeKey* key, QTreeWidgetItem* item) {
172 if (key->hasChildren()) {
173 key->positionToFirstChild();
174 do {
175 QStringList columns;
176 columns << key->getLocalNameUnicode() << key->key();
177 QTreeWidgetItem *i = new QTreeWidgetItem(item, columns, QTreeWidgetItem::Type);
178 i->setData(0, Qt::ToolTipRole, key->getLocalNameUnicode());
179 auto const offset = key->offset();
181 key->setOffset(offset);
182 }
183 while (key->positionToNextSibling());
184 }
185}
BtConfig & btConfig()
This is a shortchand for BtConfig::getInstance().
Definition btconfig.h:305
#define BT_CONNECT(...)
Definition btconnect.h:20
QList< CSwordModuleInfo const * > BtConstModuleList
QList< CSwordBookModuleInfo const * > m_modules
CSwordTreeKey * m_key
void updateKey(CSwordKey *) final override
void setKey(CSwordKey *key) final override
void showEvent(QShowEvent *const showEvent) override
QTreeWidget * m_treeView
void addKeyChildren(CSwordTreeKey *key, QTreeWidgetItem *item)
void setupTree()
Creates the first level of the tree structure.
CSwordKey * key() final override
CBookTreeChooser(BtConstModuleList const &modules, CSwordKey *key=nullptr, QWidget *parent=nullptr)
void setModules(BtConstModuleList const &modules, bool refresh=true) final override
void itemActivated(QTreeWidgetItem *item)
void refreshContent() final override
void keyChanged(CSwordKey *newKey)
Class for generic book support.
virtual QString key() const =0
CSwordKey implementation for Sword's TreeKey.
void positionToRoot()
QString key() const final override
Offset offset() const
bool setKey(const QString &key) final override
void setOffset(Offset value)