BibleTime
btbookshelflanguagespage.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-2026 by the BibleTime developers.
8* The BibleTime source code is licensed under the GNU General Public License
9* version 2.0.
10*
11**********/
12
14
15#include <memory>
16#include <QApplication>
17#include <QList>
18#include <QListView>
19#include <QStandardItem>
20#include <QStandardItemModel>
21#include <QStringLiteral>
22#include <Qt>
23#include <QVBoxLayout>
24#include <set>
25#include "../../backend/btinstallbackend.h"
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 "btbookshelfwizard.h"
32
33// Sword includes:
34#ifdef __GNUC__
35#pragma GCC diagnostic push
36#pragma GCC diagnostic ignored "-Wzero-as-null-pointer-constant"
37#endif
38#include <installmgr.h> // IWYU pragma: keep for BtInstallBackend::source()
39#ifdef __GNUC__
40#pragma GCC diagnostic pop
41#endif
42
43
44namespace {
45QString const LanguagesKey = "GUI/BookshelfWizard/languages";
46} // anonymous namespace
47
49 : BtBookshelfWizardPage(parent)
50{
51 // Setup UI:
52 m_verticalLayout = new QVBoxLayout(this);
53 m_languagesListView = new QListView(this);
55
56 // Create languages Model:
57 m_model = new QStandardItemModel(this);
59 BT_CONNECT(m_model, &QStandardItemModel::dataChanged,
61}
62
64 setTitle(QApplication::translate("BookshelfWizard", "Choose Languages"));
65 setSubTitle(QApplication::translate("BookshelfWizard",
66 "Choose one or more languages to "
67 "install works from."));
68}
69
72
74
75 { // Select languages:
76 QStringList languages;
77 if (m_firstTimeInit) {
78 languages = btConfig().value<QStringList>(LanguagesKey);
79 if (languages.isEmpty())
80 languages = QStringList{tr("English")};
81 m_firstTimeInit = false;
82 } else {
83 languages = selectedLanguages();
84 }
85
86 bool scrolledToFirstSelected = false;
87 for (int row = 0; row < m_model->rowCount(); ++row) {
88 QStandardItem * const item = m_model->item(row, 0);
89 if (languages.contains(item->text())) {
90 item->setCheckState(Qt::Checked);
91 // Scroll to first selected item:
92 if (!scrolledToFirstSelected) {
93 m_languagesListView->scrollTo(m_model->indexFromItem(item));
94 scrolledToFirstSelected = true;
95 }
96 }
97 }
98 }
99
101}
102
104 // Get languages from sources:
105 std::set<QString> languages;
106 for (auto const & sourceName : btWizard().selectedSources()) {
107 std::unique_ptr<CSwordBackend const> const backend =
109 for (auto const * module : backend->moduleList())
110 languages.insert(module->language()->translatedName());
111 }
112
113 // Update languages model:
114 m_model->clear();
115 for (auto const & language : languages) {
116 auto * const item = new QStandardItem(language);
117 item->setCheckable(true);
118 m_model->appendRow(item);
119 }
120 if (languages.size() == 1u)
121 m_model->item(0, 0)->setCheckState(Qt::Checked);
122}
123
125{ return m_model->rowCount() == 1; }
126
127void BtBookshelfLanguagesPage::slotDataChanged() { Q_EMIT completeChanged(); }
128
130{ return selectedLanguages().count() > 0; }
131
133 QStringList languages;
134 for (int row = 0; row < m_model->rowCount(); ++row) {
135 QStandardItem * const item = m_model->item(row,0);
136 if (item->checkState() == Qt::Checked)
137 languages << item->text();
138 }
139 return languages;
140}
@ installWorksPage
BtConfig & btConfig()
This is a shortchand for BtConfig::getInstance().
Definition btconfig.h:305
#define BT_CONNECT(...)
Definition btconnect.h:20
int nextId() const final override
bool isComplete() const final override
BtBookshelfLanguagesPage(QWidget *parent=nullptr)
BtBookshelfWizard & btWizard() const noexcept
QStringList selectedSources() const
T value(QString const &key, T const &defaultValue=T()) const
Returns the settings value for the given global key.
sword::InstallSource source(const QString &name)
std::unique_ptr< CSwordBackend > backend(sword::InstallSource const &is)