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-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
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/language.h"
29#include "../../backend/managers/cswordbackend.h"
30#include "../../util/btconnect.h"
31#include "btbookshelfwizard.h"
33
34// Sword includes:
35#pragma GCC diagnostic push
36#pragma GCC diagnostic ignored "-Wzero-as-null-pointer-constant"
37#include <installmgr.h> // IWYU pragma: keep for BtInstallBackend::source()
38#pragma GCC diagnostic pop
39
40
41namespace {
42QString const LanguagesKey = "GUI/BookshelfWizard/languages";
43} // anonymous namespace
44
46 : BtBookshelfWizardPage(parent)
47{
48 // Setup UI:
49 m_verticalLayout = new QVBoxLayout(this);
50 m_languagesListView = new QListView(this);
52
53 // Create languages Model:
54 m_model = new QStandardItemModel(this);
56 BT_CONNECT(m_model, &QStandardItemModel::dataChanged,
58}
59
61 setTitle(QApplication::translate("BookshelfWizard", "Choose Languages"));
62 setSubTitle(QApplication::translate("BookshelfWizard",
63 "Choose one or more languages to "
64 "install works from."));
65}
66
69
71
72 { // Select languages:
73 QStringList languages;
74 if (m_firstTimeInit) {
75 languages = btConfig().value<QStringList>(LanguagesKey);
76 if (languages.isEmpty())
77 languages = QStringList{tr("English")};
78 m_firstTimeInit = false;
79 } else {
80 languages = selectedLanguages();
81 }
82
83 bool scrolledToFirstSelected = false;
84 for (int row = 0; row < m_model->rowCount(); ++row) {
85 QStandardItem * const item = m_model->item(row, 0);
86 if (languages.contains(item->text())) {
87 item->setCheckState(Qt::Checked);
88 // Scroll to first selected item:
89 if (!scrolledToFirstSelected) {
90 m_languagesListView->scrollTo(m_model->indexFromItem(item));
91 scrolledToFirstSelected = true;
92 }
93 }
94 }
95 }
96
98}
99
101 // Get languages from sources:
102 std::set<QString> languages;
103 for (auto const & sourceName : btWizard().selectedSources()) {
104 std::unique_ptr<CSwordBackend const> const backend =
106 for (auto const * module : backend->moduleList())
107 languages.insert(module->language()->translatedName());
108 }
109
110 // Update languages model:
111 m_model->clear();
112 for (auto const & language : languages) {
113 auto * const item = new QStandardItem(language);
114 item->setCheckable(true);
115 m_model->appendRow(item);
116 }
117 if (languages.size() == 1u)
118 m_model->item(0, 0)->setCheckState(Qt::Checked);
119}
120
122{ return m_model->rowCount() == 1; }
123
124void BtBookshelfLanguagesPage::slotDataChanged() { Q_EMIT completeChanged(); }
125
127{ return selectedLanguages().count() > 0; }
128
130 QStringList languages;
131 for (int row = 0; row < m_model->rowCount(); ++row) {
132 QStandardItem * const item = m_model->item(row,0);
133 if (item->checkState() == Qt::Checked)
134 languages << item->text();
135 }
136 return languages;
137}
@ 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)