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