KTextAddons

bergamotenginelanguagewidget.cpp
1/*
2 SPDX-FileCopyrightText: 2023-2024 Laurent Montel <montel.org>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#include "bergamotenginelanguagewidget.h"
8#include "libbergamot_debug.h"
9#include "translatormodel.h"
10#include "translatorproxymodel.h"
11#include <KLocalizedString>
12#include <KMessageBox>
13#include <QLabel>
14#include <QLineEdit>
15#include <QProgressBar>
16#include <QPushButton>
17#include <QTreeView>
18#include <QVBoxLayout>
19
20#include <KLineEditEventHandler>
21
22BergamotEngineLanguageWidget::BergamotEngineLanguageWidget(QWidget *parent)
23 : QWidget{parent}
24 , mTreeView(new QTreeView(this))
25 , mSearchLineEdit(new QLineEdit(this))
26 , mTranslatorModel(new TranslatorModel(this))
27 , mTranslatorProxyModel(new TranslatorProxyModel(this))
28 , mProgressBar(new QProgressBar(this))
29 , mProgressBarLabel(new QLabel(this))
30 , mProgressBarWidget(new QWidget(this))
31{
32 auto mainLayout = new QVBoxLayout(this);
33 mainLayout->setObjectName(QStringLiteral("mainLayout"));
34 mainLayout->setContentsMargins({});
35
36 auto hboxLayout = new QHBoxLayout;
37 hboxLayout->setObjectName(QStringLiteral("hboxLayout"));
38 hboxLayout->setContentsMargins({});
39 mainLayout->addLayout(hboxLayout);
40
41 auto vboxLayout = new QVBoxLayout;
42 vboxLayout->setObjectName(QStringLiteral("vboxLayout"));
43 vboxLayout->setContentsMargins({});
44
45 mSearchLineEdit->setObjectName(QStringLiteral("mSearchLineEdit"));
46 vboxLayout->addWidget(mSearchLineEdit);
47 mSearchLineEdit->setPlaceholderText(i18nc("@info:placeholder", "Search…"));
48 mSearchLineEdit->setClearButtonEnabled(true);
49
51
52 mTreeView->setObjectName(QStringLiteral("mTreeView"));
53 connect(ManagerModelTranslator::self(), &ManagerModelTranslator::errorText, this, &BergamotEngineLanguageWidget::slotError);
54 connect(ManagerModelTranslator::self(), &ManagerModelTranslator::progress, this, &BergamotEngineLanguageWidget::slotProgressInfo);
55 connect(ManagerModelTranslator::self(), &ManagerModelTranslator::extractDone, mTranslatorModel, &TranslatorModel::updateInstalledLanguage);
56 connect(ManagerModelTranslator::self(), &ManagerModelTranslator::downLoadModelListDone, this, &BergamotEngineLanguageWidget::updateListModel);
57
58 // TODO store list on local.
59 if (ManagerModelTranslator::self()->needDownloadModelList()) {
60 ManagerModelTranslator::self()->downloadListModels();
61 } else {
62 mTranslatorModel->insertTranslators(ManagerModelTranslator::self()->translators());
63 }
64
65 mTranslatorProxyModel->setSortCaseSensitivity(Qt::CaseInsensitive);
66 mTranslatorProxyModel->setSourceModel(mTranslatorModel);
67 mTreeView->setModel(mTranslatorProxyModel);
68 mTreeView->setRootIsDecorated(false);
69 mTreeView->setSortingEnabled(true);
70 vboxLayout->addWidget(mTreeView);
71
72 hboxLayout->addLayout(vboxLayout);
73
74 auto buttonLayout = new QVBoxLayout;
75 buttonLayout->setObjectName(QStringLiteral("buttonLayout"));
76 hboxLayout->addLayout(buttonLayout);
77
78 auto downLoadLanguage = new QPushButton(QIcon::fromTheme(QStringLiteral("download-symbolic")), i18n("Download"), this);
79 downLoadLanguage->setObjectName(QStringLiteral("downLoadLanguage"));
80 buttonLayout->addWidget(downLoadLanguage);
81 connect(downLoadLanguage, &QPushButton::clicked, this, [this]() {
82 const auto currentlySelectedIndex = mTranslatorProxyModel->mapToSource(mTreeView->selectionModel()->currentIndex());
83 const QModelIndex modelIndex = mTranslatorModel->index(currentlySelectedIndex.row(), TranslatorModel::Url);
84 const QModelIndex modelIndexCheckSum = mTranslatorModel->index(currentlySelectedIndex.row(), TranslatorModel::CheckSum);
85
86 const QString url = modelIndex.data().toString();
87 const QString checkSum = modelIndexCheckSum.data().toString();
88 qCDebug(TRANSLATOR_LIBBERGAMOT_LOG) << " url " << url << " checksum " << checkSum;
89 slotDownLoad(url, checkSum);
90 });
91
92 auto deleteLanguage = new QPushButton(QIcon::fromTheme(QStringLiteral("edit-delete")), i18n("Delete"), this);
93 deleteLanguage->setObjectName(QStringLiteral("downLoadLanguage"));
94 buttonLayout->addWidget(deleteLanguage);
95
96 auto updateListLanguage = new QPushButton(i18nc("@action:button", "Update List"), this);
97 updateListLanguage->setToolTip(i18nc("@info:tooltip", "Update list of languages from network"));
98 updateListLanguage->setObjectName(QStringLiteral("updateListLanguage"));
99 buttonLayout->addWidget(updateListLanguage);
100 buttonLayout->addStretch(1); // spacer
101 connect(updateListLanguage, &QPushButton::clicked, this, &BergamotEngineLanguageWidget::slotUpdateListLanguage);
102
103 connect(mSearchLineEdit, &QLineEdit::textChanged, this, &BergamotEngineLanguageWidget::slotTextChanged);
104
105 auto progressBarLayout = new QHBoxLayout;
106 progressBarLayout->setObjectName(QStringLiteral("progressBarLayout"));
107
108 mProgressBarLabel->setObjectName(QStringLiteral("mProgressBarLabel"));
109 progressBarLayout->addWidget(mProgressBarLabel);
110
111 mProgressBar->setObjectName(QStringLiteral("mProgressBar"));
112 progressBarLayout->addWidget(mProgressBar);
113
114 mProgressBarWidget->setObjectName(QStringLiteral("mProgressBarWidget"));
115 mProgressBarWidget->setLayout(progressBarLayout);
116
117 mainLayout->addWidget(mProgressBarWidget);
118 mProgressBarWidget->setVisible(false);
119
120 mTreeView->setColumnHidden(TranslatorModel::Url, true);
121 mTreeView->setColumnHidden(TranslatorModel::CheckSum, true);
122 mTreeView->setColumnHidden(TranslatorModel::Identifier, true);
123 mTreeView->setColumnHidden(TranslatorModel::NeedToUpdateLanguage, true);
124 auto updateButton = [this, downLoadLanguage, deleteLanguage]() {
125 const bool hasSelectedItem = mTreeView->currentIndex().isValid();
126 const auto currentlySelectedIndex = mTranslatorProxyModel->mapToSource(mTreeView->selectionModel()->currentIndex());
127 const QModelIndex modelIndex = mTranslatorModel->index(currentlySelectedIndex.row(), TranslatorModel::Installed);
128 const QModelIndex modelIndexNeedToUpdate = mTranslatorModel->index(currentlySelectedIndex.row(), TranslatorModel::NeedToUpdateLanguage);
129 const bool isInstalled = modelIndex.data().toBool();
130 const bool needToUpdate = modelIndexNeedToUpdate.data().toBool();
131
132 if (needToUpdate) {
133 downLoadLanguage->setEnabled(hasSelectedItem && needToUpdate);
134 } else {
135 downLoadLanguage->setEnabled(hasSelectedItem && !isInstalled);
136 }
137 deleteLanguage->setEnabled(hasSelectedItem && isInstalled);
138 };
139
140 connect(deleteLanguage, &QPushButton::clicked, this, [this, updateButton]() {
141 const auto currentlySelectedIndex = mTranslatorProxyModel->mapToSource(mTreeView->selectionModel()->currentIndex());
142 const QModelIndex modelIndex = mTranslatorModel->index(currentlySelectedIndex.row(), TranslatorModel::Identifier);
143 const QString identifier = modelIndex.data().toString();
144 slotDelete(identifier);
145 updateButton();
146 });
147
148 connect(mTreeView->selectionModel(), &QItemSelectionModel::selectionChanged, this, [updateButton]() {
149 updateButton();
150 });
151 downLoadLanguage->setEnabled(false);
152 deleteLanguage->setEnabled(false);
153}
154
155BergamotEngineLanguageWidget::~BergamotEngineLanguageWidget() = default;
156
157void BergamotEngineLanguageWidget::slotProgressInfo(const ManagerModelTranslator::ProgressInfo &info)
158{
159 if (info.bytesReceived != info.bytesTotal) {
160 mProgressBarWidget->setVisible(true);
161 } else {
162 mProgressBarWidget->setVisible(false);
163 }
164 mProgressBarLabel->setText(info.languageName);
165 mProgressBar->setRange(0, 100);
166 mProgressBar->setValue((info.bytesReceived * 100) / info.bytesTotal);
167}
168
169void BergamotEngineLanguageWidget::slotError(const QString &str)
170{
171 KMessageBox::error(this, i18n("Error: %1", str), i18n("Bergamot"));
172}
173
174void BergamotEngineLanguageWidget::slotTextChanged(const QString &str)
175{
176 mTranslatorProxyModel->setSearchString(str);
177}
178
179void BergamotEngineLanguageWidget::slotDownLoad(const QString &url, const QString &checkSum)
180{
181 ManagerModelTranslator::self()->downloadLanguage(url, checkSum);
182}
183
184void BergamotEngineLanguageWidget::slotDelete(const QString &identifier)
185{
186 mTranslatorModel->removeLanguage(identifier);
187}
188
189void BergamotEngineLanguageWidget::slotUpdateListLanguage()
190{
191 ManagerModelTranslator::self()->downloadListModels();
192}
193
194void BergamotEngineLanguageWidget::updateListModel()
195{
196 mTranslatorModel->insertTranslators(ManagerModelTranslator::self()->translators());
197}
198
199#include "moc_bergamotenginelanguagewidget.cpp"
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
void catchReturnKey(QObject *lineEdit)
void error(QWidget *parent, const QString &text, const QString &title, const KGuiItem &buttonOk, Options options=Notify)
void clicked(bool checked)
QIcon fromTheme(const QString &name)
void selectionChanged(const QItemSelection &selected, const QItemSelection &deselected)
void setText(const QString &)
void textChanged(const QString &text)
QVariant data(int role) const const
void setObjectName(QAnyStringView name)
void setRange(int minimum, int maximum)
void setValue(int value)
CaseInsensitive
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
bool toBool() const const
QString toString() const const
virtual void setVisible(bool visible)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 16:56:02 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.