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