1
/***************************************************************************
2
* Copyright Ā© 2011,2012 Jonathan Thomas <echidnaman@kubuntu.org> *
4
* This program is free software; you can redistribute it and/or *
5
* modify it under the terms of the GNU General Public License as *
6
* published by the Free Software Foundation; either version 2 of *
7
* the License or (at your option) version 3 or any later version *
8
* accepted by the membership of KDE e.V. (or its successor approved *
9
* by the membership of KDE e.V.), which shall act as a proxy *
10
* defined in Section 14 of version 3 of the license. *
12
* This program is distributed in the hope that it will be useful, *
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15
* GNU General Public License for more details. *
17
* You should have received a copy of the GNU General Public License *
18
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
19
***************************************************************************/
21
#include "AddonsWidget.h"
24
#include <QtCore/QStringBuilder>
25
#include <QHBoxLayout>
26
#include <QtWidgets/QLabel>
28
#include <QtWidgets/QPushButton>
29
#include <QStandardItemModel>
30
#include <QtWidgets/QToolButton>
33
#include <KLocalizedString>
36
#include <resources/AbstractResourcesBackend.h>
37
#include <resources/AbstractResource.h>
38
#include <resources/ResourcesModel.h>
40
//FIXME: Port to the ApplicationAddonsModel, use QAbstractItemView::setIndexWidget
41
// so we can still use our widget as a "delegate"
43
AddonsWidget::AddonsWidget(QWidget *parent)
47
QWidget *headerWidget = new QWidget(this);
48
QHBoxLayout *headerLayout = new QHBoxLayout(headerWidget);
49
headerLayout->setMargin(0);
50
headerWidget->setLayout(headerLayout);
52
m_expandButton = new QToolButton(headerWidget);
53
m_expandButton->setAutoRaise(true);
54
m_expandButton->setArrowType(Qt::DownArrow);
55
connect(m_expandButton, SIGNAL(clicked()), this, SLOT(expandButtonClicked()));
57
QLabel *titleLabel = new QLabel(headerWidget);
58
titleLabel->setText(QLatin1Literal("<h3>") %
59
i18nc("@title", "Addons") % QLatin1Literal("</h3>"));
61
QWidget *headerSpacer = new QWidget(headerWidget);
62
headerSpacer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Minimum);
64
headerLayout->addWidget(m_expandButton);
65
headerLayout->addWidget(titleLabel);
66
headerLayout->addWidget(headerSpacer);
68
m_addonsWidget = new QWidget(this);
69
QVBoxLayout *addonsLayout = new QVBoxLayout(m_addonsWidget);
70
addonsLayout->setMargin(0);
71
m_addonsWidget->setLayout(addonsLayout);
73
m_addonsView = new QListView(m_addonsWidget);
74
m_addonsView->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum);
75
m_addonsModel = new QStandardItemModel(this);
76
m_addonsView->setModel(m_addonsModel);
78
QWidget *addonsButtonBox = new QWidget(m_addonsWidget);
79
QHBoxLayout *addonButtonsLayout = new QHBoxLayout(addonsButtonBox);
81
QWidget *addonsButtonSpacer = new QWidget(m_addonsWidget);
82
addonsButtonSpacer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Minimum);
84
m_addonsRevertButton = new QPushButton(addonsButtonBox);
85
m_addonsRevertButton->setIcon(QIcon::fromTheme("edit-undo"));
86
m_addonsRevertButton->setText(i18nc("@action:button", "Revert"));
87
connect(m_addonsRevertButton, SIGNAL(clicked()),
88
this, SLOT(populateModel()));
90
m_addonsApplyButton = new QPushButton(addonsButtonBox);
91
m_addonsApplyButton->setIcon(QIcon::fromTheme("dialog-ok-apply"));
92
m_addonsApplyButton->setText(i18nc("@action:button", "Apply"));
93
m_addonsApplyButton->setToolTip(i18nc("@info:tooltip", "Apply changes to addons"));
94
connect(m_addonsApplyButton, SIGNAL(clicked()),
95
this, SLOT(emitApplyButtonClicked()));
97
addonButtonsLayout->addWidget(addonsButtonSpacer);
98
addonButtonsLayout->addWidget(m_addonsRevertButton);
99
addonButtonsLayout->addWidget(m_addonsApplyButton);
101
addonsLayout->addWidget(m_addonsView);
102
addonsLayout->addWidget(addonsButtonBox);
104
connect(m_addonsModel, SIGNAL(dataChanged(QModelIndex,QModelIndex)),
105
this, SLOT(addonStateChanged(QModelIndex,QModelIndex)));
108
void AddonsWidget::fetchingChanged()
110
if(m_resource && m_resource->backend()->isFetching()) {
111
m_resource = nullptr;
112
m_changedAddons.clear();
113
m_availableAddons.clear();
114
m_addonsModel->clear();
120
void AddonsWidget::setResource(AbstractResource *resource)
122
m_resource = resource;
124
// Clear addons when a reload starts
125
connect(m_resource->backend(), SIGNAL(fetchingChanged()), this, SLOT(fetchingChanged()));
130
void AddonsWidget::repaintViewport()
132
m_addonsView->viewport()->update();
133
m_addonsView->viewport()->repaint();
136
void AddonsWidget::populateModel()
138
m_addonsModel->clear();
144
m_availableAddons = m_resource->addonsInformation();
146
for (const PackageState &addon : m_availableAddons) {
147
// Check if we have an application for the addon
148
AbstractResource *addonResource = 0;
150
for (AbstractResource *resource : m_resource->backend()->allResources()) {
154
if (resource->packageName() == addon.name()) {
155
addonResource = resource;
160
QStandardItem *addonItem = new QStandardItem;
161
addonItem->setData(addon.name());
162
QString resourceName = QLatin1String(" (") % addon.name() % ')';
164
addonItem->setText(addonResource->name() % resourceName);
165
addonItem->setIcon(QIcon::fromTheme(addonResource->icon()));
167
addonItem->setText(addon.description() % resourceName);
168
addonItem->setIcon(QIcon::fromTheme("applications-other"));
171
addonItem->setEditable(false);
172
addonItem->setCheckable(true);
174
if (addon.isInstalled()) {
175
addonItem->setCheckState(Qt::Checked);
177
addonItem->setCheckState(Qt::Unchecked);
180
m_addonsModel->appendRow(addonItem);
183
m_addonsRevertButton->setEnabled(false);
184
m_addonsApplyButton->setEnabled(false);
187
void AddonsWidget::expandButtonClicked()
189
if (m_addonsWidget->isHidden()) {
190
m_expandButton->setArrowType(Qt::DownArrow);
191
m_addonsWidget->show();
193
m_addonsWidget->hide();
194
m_expandButton->setArrowType(Qt::RightArrow);
198
void AddonsWidget::addonStateChanged(const QModelIndex &left, const QModelIndex &right)
201
QStandardItem *item = m_addonsModel->itemFromIndex(left);
202
PackageState *addon = nullptr;
204
QString addonName = item->data().toString();
205
for (PackageState state : m_availableAddons) {
206
if (state.name() == addonName) {
214
if (addon->isInstalled()) {
215
switch (item->checkState()) {
217
m_changedAddons.remove(addonName);
220
m_changedAddons[addonName] = false;
226
switch (item->checkState()) {
228
m_changedAddons[addonName] = true;
231
m_changedAddons.remove(addonName);
238
m_addonsRevertButton->setEnabled(!m_changedAddons.isEmpty());
239
m_addonsApplyButton->setEnabled(!m_changedAddons.isEmpty());
242
void AddonsWidget::emitApplyButtonClicked()
244
// ResourcesModel *resourcesModel = ResourcesModel::global();
245
//resourcesModel->installApplication(m_resource, m_changedAddons);
247
emit applyButtonClicked();