BibleTime
btindexdialog.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-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 #include "btindexdialog.h"
14 
15 #include <QCheckBox>
16 #include <QHBoxLayout>
17 #include <QFlags>
18 #include <QList>
19 #include <QPushButton>
20 #include <QSizePolicy>
21 #include <QSpacerItem>
22 #include <QStringList>
23 #include <QTreeWidget>
24 #include <QTreeWidgetItem>
25 #include <QVBoxLayout>
26 #include "../../backend/config/btconfig.h"
27 #include "../../backend/drivers/cswordmoduleinfo.h"
28 #include "../../backend/managers/cswordbackend.h"
29 #include "../../util/btconnect.h"
30 #include "../../util/cresmgr.h"
31 #include "../../util/tool.h"
32 #include "../btmoduleindexdialog.h"
33 
34 
35 BtIndexDialog::BtIndexDialog(QWidget * parent, Qt::WindowFlags f)
36  : QDialog(parent, f)
37 {
38  setWindowIcon(CResMgr::searchdialog::icon());
39 
40  auto vboxLayout = new QVBoxLayout(this);
41  auto hboxLayout = new QHBoxLayout();
42 
43  m_autoDeleteOrphanedIndicesBox = new QCheckBox(this);
44  vboxLayout->addWidget(m_autoDeleteOrphanedIndicesBox);
45 
46  m_moduleList = new QTreeWidget(this);
47  vboxLayout->addWidget(m_moduleList);
48 
49  auto spacerItem =
50  new QSpacerItem(1, 1, QSizePolicy::Expanding, QSizePolicy::Minimum);
51  hboxLayout->addItem(spacerItem);
52 
53  m_deleteButton = new QPushButton(this);
54  m_deleteButton->setIcon(CResMgr::bookshelfmgr::indexpage::icon_delete());
55  hboxLayout->addWidget(m_deleteButton);
56 
57  m_createButton = new QPushButton(this);
58  m_createButton->setIcon(CResMgr::bookshelfmgr::indexpage::icon_create());
59  hboxLayout->addWidget(m_createButton);
60 
61  m_closeButton = new QPushButton(this);
62  m_closeButton->setIcon(CResMgr::searchdialog::icon_close());
63  hboxLayout->addWidget(m_closeButton);
64 
65  vboxLayout->addLayout(hboxLayout);
66 
67  // configure the list view
68  m_moduleList->setRootIsDecorated(true);
69  m_moduleList->setColumnWidth(0, util::tool::mWidth(*m_moduleList, 20));
70  m_moduleList->setSortingEnabled(false);
71 
73  btConfig().value<bool>(
74  QStringLiteral(
75  "settings/behaviour/autoDeleteOrphanedIndices"),
76  true));
77 
78  // connect our signals/slots
79  BT_CONNECT(m_createButton, &QPushButton::clicked,
81  BT_CONNECT(m_deleteButton, &QPushButton::clicked,
83  BT_CONNECT(m_closeButton, &QPushButton::clicked,
84  this, &BtIndexDialog::close);
87  BT_CONNECT(m_autoDeleteOrphanedIndicesBox, &QCheckBox::stateChanged,
89 
90  retranslateUi(); // also calls populateModuleList();
91 }
92 
93 /** Populates the module list with installed modules and orphaned indices */
95  m_moduleList->clear();
96 
97  // populate installed modules
98  m_modsWithIndices = new QTreeWidgetItem(m_moduleList);
99  m_modsWithIndices->setText(0, tr("Indexed Works"));
100  m_modsWithIndices->setFlags(Qt::ItemIsUserCheckable
101  | Qt::ItemIsEnabled
102  | Qt::ItemIsAutoTristate);
103  m_modsWithIndices->setExpanded(true);
104 
105  m_modsWithoutIndices = new QTreeWidgetItem(m_moduleList);
106  m_modsWithoutIndices->setText(0, tr("Unindexed Works"));
107  m_modsWithoutIndices->setFlags(Qt::ItemIsUserCheckable
108  | Qt::ItemIsEnabled
109  | Qt::ItemIsAutoTristate);
110  m_modsWithoutIndices->setExpanded(true);
111 
112  for (auto const & modulePtr : CSwordBackend::instance().moduleList()) {
113  if (modulePtr->hasIndex()) {
114  auto item = new QTreeWidgetItem(m_modsWithIndices);
115  item->setText(0, modulePtr->name());
116  item->setText(1, tr("%1 KiB").arg(modulePtr->indexSize() / 1024));
117  item->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
118  item->setCheckState(0, Qt::Unchecked);
119  } else {
120  auto item = new QTreeWidgetItem(m_modsWithoutIndices);
121  item->setText(0, modulePtr->name());
122  item->setText(1, tr("0 KiB"));
123  item->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
124  item->setCheckState(0, Qt::Checked);
125  }
126  }
127 }
128 
130  setWindowTitle(tr("Manage Search Indexes"));
131 
132  m_autoDeleteOrphanedIndicesBox->setToolTip(
133  tr("If selected, those indexes which have no corresponding "
134  "work will be deleted when BibleTime starts"));
136  tr("Automatically delete orphaned indexes when BibleTime "
137  "starts"));
138 
139  m_deleteButton->setToolTip(tr("Delete the selected indexes"));
140  m_deleteButton->setText(tr("Delete"));
141 
142  m_closeButton->setText(tr("&Close"));
143 
144  m_createButton->setToolTip(tr("Create new indexes for the selected works"));
145  m_createButton->setText(tr("Create..."));
146 
147  m_moduleList->setHeaderLabels(QStringList(tr("Work")) << tr("Index size"));
148 
150 }
151 
153  btConfig().setValue(
154  QStringLiteral("settings/behaviour/autoDeleteOrphanedIndices"),
155  newState == Qt::Checked);
156 }
157 
158 /** Creates indices for selected modules if no index currently exists */
160  QList<CSwordModuleInfo *> moduleList;
161 
162  auto & backend = CSwordBackend::instance();
163  for (int i = 0; i < m_modsWithoutIndices->childCount(); ++i) {
164  if (m_modsWithoutIndices->child(i)->checkState(0) == Qt::Checked) {
165  if (auto * module = backend.findModuleByName(
166  m_modsWithoutIndices->child(i)->text(0).toUtf8()))
167  moduleList.append(module);
168  }
169  }
170 
171  if (!moduleList.isEmpty()) { // This also shows the progress dialog
174  }
175 }
176 
177 /** Deletes indices for selected modules */
179  bool indicesDeleted = false;
180 
181  auto & backend = CSwordBackend::instance();
182  for (int i = 0; i < m_modsWithIndices->childCount(); ++i) {
183  if (m_modsWithIndices->child(i)->checkState(0) == Qt::Checked) {
184  if (auto * module = backend.findModuleByName(
185  m_modsWithIndices->child(i)->text(0).toUtf8()))
186  {
187  module->deleteIndex();
188  indicesDeleted = true;
189  }
190  }
191  }
192 
193  // repopulate the list if an action was taken
194  if (indicesDeleted)
196 }
197 
BtConfig & btConfig()
This is a shortchand for BtConfig::getInstance().
Definition: btconfig.h:305
#define BT_CONNECT(...)
Definition: btconnect.h:20
void setValue(QString const &key, T const &value)
Sets a value for a key.
Definition: btconfigcore.h:73
QCheckBox * m_autoDeleteOrphanedIndicesBox
Definition: btindexdialog.h:56
QPushButton * m_closeButton
Definition: btindexdialog.h:60
QPushButton * m_createButton
Definition: btindexdialog.h:59
QTreeWidget * m_moduleList
Definition: btindexdialog.h:57
void slotSwordSetupChanged()
void autoDeleteOrphanedIndicesChanged(int newState)
void populateModuleList()
QPushButton * m_deleteButton
Definition: btindexdialog.h:58
QTreeWidgetItem * m_modsWithoutIndices
Definition: btindexdialog.h:63
QTreeWidgetItem * m_modsWithIndices
Definition: btindexdialog.h:62
BtIndexDialog(QWidget *parent=nullptr, Qt::WindowFlags f=Qt::WindowFlags())
static bool indexAllModules(QList< CSwordModuleInfo * > const &modules)
static CSwordBackend & instance() noexcept
Definition: cswordbackend.h:98
void sigSwordSetupChanged()
std::unique_ptr< CSwordBackend > backend(sword::InstallSource const &is)
int mWidth(QWidget const &widget, int const mCount)
Calculates a maximum rendered text width for a widget and a string with the a given length.
Definition: tool.cpp:155