BibleTime
btbookshelfsourcespage.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 <QApplication>
16#include <QDialog>
17#include <QFontMetrics>
18#include <QHeaderView>
19#include <QMessageBox>
20#include <QModelIndex>
21#include <QPushButton>
22#include <QStandardItem>
23#include <QStandardItemModel>
24#include <QStringLiteral>
25#include <QTableView>
26#include <Qt>
27#include <QtGlobal>
28#include <QVariant>
29#include <QVBoxLayout>
30#include <QWizardPage>
31#include "../../backend/config/btconfig.h"
32#include "../../backend/btinstallbackend.h"
33#include "../../util/btconnect.h"
36#include "btbookshelfwizard.h"
38
39// Sword includes:
40#ifdef __GNUC__
41#pragma GCC diagnostic push
42#pragma GCC diagnostic ignored "-Wextra-semi"
43#pragma GCC diagnostic ignored "-Wzero-as-null-pointer-constant"
44#endif
45#include <installmgr.h>
46#include <swbuf.h>
47#ifdef __GNUC__
48#pragma GCC diagnostic pop
49#endif
50
51
52namespace {
53char const buttonPropertyName[] = "BtBookshelfSourcesPageButtonProperty";
54constexpr bool const ButtonTagAdd = true;
55constexpr bool const ButtonTagRemove = false;
56QString const SourcesKey = "GUI/BookshelfWizard/sources";
57QStringList const initialSelection{"CrossWire", "Bible.org", "Xiphos"};
58} // anonymous namespace
59
60
62 : BtBookshelfWizardPage(parent)
63{
64 // Setup UI:
65 m_verticalLayout = new QVBoxLayout(this);
66 m_sourcesTableView = new QTableView(this);
67 m_sourcesTableView->horizontalHeader()->setVisible(false);
68 m_sourcesTableView->verticalHeader()->setVisible(false);
70
71 // Create sources model:
72 m_model = new QStandardItemModel(this);
73 m_sourcesTableView->setModel(m_model);
74 BT_CONNECT(m_model, &QStandardItemModel::dataChanged,
75 this, &QWizardPage::completeChanged);
76}
77
79 setTitle(QApplication::translate("BookshelfWizard",
80 "Choose Remote Libraries"));
81 setSubTitle(QApplication::translate(
82 "BookshelfWizard",
83 "Choose one or more remote libraries to install works from."));
84}
85
93
95 QStringList saveSources;
96 if (m_firstTimeInit) {
97 saveSources << btConfig().value<QStringList>(SourcesKey, QStringList{});
98 if (saveSources.empty())
99 saveSources << initialSelection;
100 m_firstTimeInit = false;
101 } else {
102 saveSources << selectedSources();
103 }
104
105 // Do before updating models to get correct
106 // column width for buttons
108
110 selectSourcesInModel(saveSources);
111}
112
113void BtBookshelfSourcesPage::selectSourcesInModel(const QStringList& sources) {
114 for (int row = 0; row < m_model->rowCount(); ++row) {
115 QStandardItem * const item = m_model->item(row,0);
116 if (sources.contains(item->text()))
117 item->setCheckState(Qt::Checked);
118 }
119}
120
122 QStringList sourceList = BtInstallBackend::sourceNameList();
123 m_model->clear();
124 m_model->setColumnCount(2);
125
126 auto const addButton = [this](int row, int column,
127 QString const & text, bool const tag) {
128 QPushButton * const button = new QPushButton(text, this);
129 button->setProperty(buttonPropertyName, tag);
130 m_sourcesTableView->setIndexWidget(m_model->index(row, column), button);
131 return button;
132 };
133
134 QString const removeText = tr("Remove");
135 for (auto const & source : sourceList) {
136 auto * const item = new QStandardItem(source);
137 item->setCheckable(true);
138 m_model->appendRow(item);
139 int const row = m_model->rowCount() - 1;
140 QPushButton * button = addButton(row, 1, removeText, ButtonTagRemove);
141 BT_CONNECT(button, &QPushButton::clicked,
142 [this, row]{ slotButtonClicked(row); });
143 }
144
145 m_model->appendRow(new QStandardItem(tr("< Add new remote library >")));
146 int const row = m_model->rowCount() - 1;
147 QString const addText = tr("Add");
148 QPushButton * const button = addButton(row, 1, addText, ButtonTagAdd);
149 BT_CONNECT(button, &QPushButton::clicked,
150 [this, row]{ slotButtonClicked(row); });
151
152 m_sourcesTableView->horizontalHeader()->setSectionResizeMode(1, QHeaderView::Fixed);
153 m_sourcesTableView->horizontalHeader()->setSectionResizeMode(0, QHeaderView::Stretch);
154 m_sourcesTableView->setShowGrid(false);
155
156 // Calculate button column width:
157 QFontMetrics const fontMetrics = m_sourcesTableView->fontMetrics();
158 m_sourcesTableView->setColumnWidth(
159 1,
160 qMax(fontMetrics.horizontalAdvance(removeText),
161 fontMetrics.horizontalAdvance(addText)) + 60);
162}
163
165 for (int row = 0; row < m_model->rowCount(); ++row)
166 if (m_model->item(row, 0)->checkState() == Qt::Checked)
167 return true;
168 return false;
169}
170
172 QStringList sources;
173 for (int row = 0; row < m_model->rowCount(); ++row) {
174 QStandardItem * const item = m_model->item(row, 0);
175 if (item->checkState() == Qt::Checked)
176 sources << item->text();
177 }
178 return sources;
179}
180
182 QModelIndex const index = m_model->index(row, 1);
183 if (static_cast<QPushButton *>(
184 m_sourcesTableView->indexWidget(index))->property(
185 buttonPropertyName).toBool() == ButtonTagRemove)
186 {
187 if (QMessageBox::warning(
188 this,
189 tr("Delete Source"),
190 tr("Do you really want to delete this source?"),
191 QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes)
193 m_model->item(index.row(), 0)->text());
194 QStringList const saveSources = selectedSources();
196 selectSourcesInModel(saveSources);
197 return;
198 }
199
201 QStringList const saveSources = selectedSources();
202 if (dlg.exec() != QDialog::Accepted)
203 return;
204 if (dlg.wasRemoteListAdded()) {
206 selectSourcesInModel(saveSources);
207 return;
208 }
209 sword::InstallSource newSource = dlg.getSource();
210 if (*(newSource.type.c_str()) != '\0') // we have a valid source to add
213 selectSourcesInModel(saveSources);
214}
@ installWorksPage
BtConfig & btConfig()
This is a shortchand for BtConfig::getInstance().
Definition btconfig.h:305
#define BT_CONNECT(...)
Definition btconnect.h:20
void selectSourcesInModel(QStringList const &sources)
QStandardItemModel * m_model
void initializePage() final override
BtBookshelfSourcesPage(QWidget *parent=nullptr)
bool isComplete() const final override
QStringList selectedSources() const
int nextId() const final override
BtBookshelfWizard & btWizard() const noexcept
BtBookshelfLanguagesPage & languagesPage() const noexcept
T value(QString const &key, T const &defaultValue=T()) const
Returns the settings value for the given global key.
bool deleteSource(const QString &name)
bool addSource(sword::InstallSource &source)
QStringList sourceNameList()