~ubuntu-branches/ubuntu/vivid/muon/vivid-proposed

« back to all changes in this revision

Viewing changes to installer/ResourceDetailsView/AddonsWidget.cpp

  • Committer: Package Import Robot
  • Author(s): Scarlett Clark
  • Date: 2015-03-24 07:36:31 UTC
  • mto: This revision was merged to the branch mainline in revision 86.
  • Revision ID: package-import@ubuntu.com-20150324073631-7nmay5episnfkdlt
Tags: upstream-5.2.2
ImportĀ upstreamĀ versionĀ 5.2.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/***************************************************************************
2
 
 *   Copyright Ā© 2011,2012 Jonathan Thomas <echidnaman@kubuntu.org>        *
3
 
 *                                                                         *
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.                    *
11
 
 *                                                                         *
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.                          *
16
 
 *                                                                         *
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
 
 ***************************************************************************/
20
 
 
21
 
#include "AddonsWidget.h"
22
 
 
23
 
// Qt includes
24
 
#include <QtCore/QStringBuilder>
25
 
#include <QHBoxLayout>
26
 
#include <QtWidgets/QLabel>
27
 
#include <QListView>
28
 
#include <QtWidgets/QPushButton>
29
 
#include <QStandardItemModel>
30
 
#include <QtWidgets/QToolButton>
31
 
 
32
 
// KDE includes
33
 
#include <KLocalizedString>
34
 
 
35
 
// Libmuon includes
36
 
#include <resources/AbstractResourcesBackend.h>
37
 
#include <resources/AbstractResource.h>
38
 
#include <resources/ResourcesModel.h>
39
 
 
40
 
//FIXME: Port to the ApplicationAddonsModel, use QAbstractItemView::setIndexWidget
41
 
//       so we can still use our widget as a "delegate"
42
 
 
43
 
AddonsWidget::AddonsWidget(QWidget *parent)
44
 
        : KVBox(parent)
45
 
        , m_resource(nullptr)
46
 
{
47
 
    QWidget *headerWidget = new QWidget(this);
48
 
    QHBoxLayout *headerLayout = new QHBoxLayout(headerWidget);
49
 
    headerLayout->setMargin(0);
50
 
    headerWidget->setLayout(headerLayout);
51
 
 
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()));
56
 
 
57
 
    QLabel *titleLabel = new QLabel(headerWidget);
58
 
    titleLabel->setText(QLatin1Literal("<h3>") %
59
 
                        i18nc("@title", "Addons") % QLatin1Literal("</h3>"));
60
 
 
61
 
    QWidget *headerSpacer = new QWidget(headerWidget);
62
 
    headerSpacer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Minimum);
63
 
 
64
 
    headerLayout->addWidget(m_expandButton);
65
 
    headerLayout->addWidget(titleLabel);
66
 
    headerLayout->addWidget(headerSpacer);
67
 
 
68
 
    m_addonsWidget = new QWidget(this);
69
 
    QVBoxLayout *addonsLayout = new QVBoxLayout(m_addonsWidget);
70
 
    addonsLayout->setMargin(0);
71
 
    m_addonsWidget->setLayout(addonsLayout);
72
 
 
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);
77
 
 
78
 
    QWidget *addonsButtonBox = new QWidget(m_addonsWidget);
79
 
    QHBoxLayout *addonButtonsLayout = new QHBoxLayout(addonsButtonBox);
80
 
 
81
 
    QWidget *addonsButtonSpacer = new QWidget(m_addonsWidget);
82
 
    addonsButtonSpacer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Minimum);
83
 
 
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()));
89
 
 
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()));
96
 
 
97
 
    addonButtonsLayout->addWidget(addonsButtonSpacer);
98
 
    addonButtonsLayout->addWidget(m_addonsRevertButton);
99
 
    addonButtonsLayout->addWidget(m_addonsApplyButton);
100
 
 
101
 
    addonsLayout->addWidget(m_addonsView);
102
 
    addonsLayout->addWidget(addonsButtonBox);
103
 
 
104
 
    connect(m_addonsModel, SIGNAL(dataChanged(QModelIndex,QModelIndex)),
105
 
            this, SLOT(addonStateChanged(QModelIndex,QModelIndex)));
106
 
}
107
 
 
108
 
void AddonsWidget::fetchingChanged()
109
 
{
110
 
    if(m_resource && m_resource->backend()->isFetching()) {
111
 
        m_resource = nullptr;
112
 
        m_changedAddons.clear();
113
 
        m_availableAddons.clear();
114
 
        m_addonsModel->clear();
115
 
    } else {
116
 
        populateModel();
117
 
    }
118
 
}
119
 
 
120
 
void AddonsWidget::setResource(AbstractResource *resource)
121
 
{
122
 
    m_resource = resource;
123
 
 
124
 
    // Clear addons when a reload starts
125
 
    connect(m_resource->backend(), SIGNAL(fetchingChanged()), this, SLOT(fetchingChanged()));
126
 
 
127
 
    populateModel();
128
 
}
129
 
 
130
 
void AddonsWidget::repaintViewport()
131
 
{
132
 
    m_addonsView->viewport()->update();
133
 
    m_addonsView->viewport()->repaint();
134
 
}
135
 
 
136
 
void AddonsWidget::populateModel()
137
 
{
138
 
    m_addonsModel->clear();
139
 
 
140
 
    if (!m_resource) {
141
 
        return;
142
 
    }
143
 
 
144
 
    m_availableAddons = m_resource->addonsInformation();
145
 
 
146
 
    for (const PackageState &addon : m_availableAddons) {
147
 
        // Check if we have an application for the addon
148
 
        AbstractResource *addonResource = 0;
149
 
 
150
 
        for (AbstractResource *resource : m_resource->backend()->allResources()) {
151
 
            if (!resource)
152
 
                continue;
153
 
 
154
 
            if (resource->packageName() == addon.name()) {
155
 
                addonResource = resource;
156
 
                break;
157
 
            }
158
 
        }
159
 
 
160
 
        QStandardItem *addonItem = new QStandardItem;
161
 
        addonItem->setData(addon.name());
162
 
        QString resourceName = QLatin1String(" (") % addon.name() % ')';
163
 
        if (addonResource) {
164
 
            addonItem->setText(addonResource->name() % resourceName);
165
 
            addonItem->setIcon(QIcon::fromTheme(addonResource->icon()));
166
 
        } else {
167
 
            addonItem->setText(addon.description() % resourceName);
168
 
            addonItem->setIcon(QIcon::fromTheme("applications-other"));
169
 
        }
170
 
 
171
 
        addonItem->setEditable(false);
172
 
        addonItem->setCheckable(true);
173
 
 
174
 
        if (addon.isInstalled()) {
175
 
            addonItem->setCheckState(Qt::Checked);
176
 
        } else {
177
 
            addonItem->setCheckState(Qt::Unchecked);
178
 
        }
179
 
 
180
 
        m_addonsModel->appendRow(addonItem);
181
 
    }
182
 
 
183
 
    m_addonsRevertButton->setEnabled(false);
184
 
    m_addonsApplyButton->setEnabled(false);
185
 
}
186
 
 
187
 
void AddonsWidget::expandButtonClicked()
188
 
{
189
 
    if (m_addonsWidget->isHidden()) {
190
 
        m_expandButton->setArrowType(Qt::DownArrow);
191
 
        m_addonsWidget->show();
192
 
    } else {
193
 
        m_addonsWidget->hide();
194
 
        m_expandButton->setArrowType(Qt::RightArrow);
195
 
    }
196
 
}
197
 
 
198
 
void AddonsWidget::addonStateChanged(const QModelIndex &left, const QModelIndex &right)
199
 
{
200
 
    Q_UNUSED(right);
201
 
    QStandardItem *item = m_addonsModel->itemFromIndex(left);
202
 
    PackageState *addon = nullptr;
203
 
 
204
 
    QString addonName = item->data().toString();
205
 
    for (PackageState state : m_availableAddons) {
206
 
        if (state.name() == addonName) {
207
 
            addon = &state;
208
 
        }
209
 
    }
210
 
 
211
 
    if (!addon)
212
 
        return;
213
 
 
214
 
    if (addon->isInstalled()) {
215
 
        switch (item->checkState()) {
216
 
        case Qt::Checked:
217
 
            m_changedAddons.remove(addonName);
218
 
            break;
219
 
        case Qt::Unchecked:
220
 
            m_changedAddons[addonName] = false;
221
 
            break;
222
 
        default:
223
 
            break;
224
 
        }
225
 
    } else {
226
 
        switch (item->checkState()) {
227
 
        case Qt::Checked:
228
 
            m_changedAddons[addonName] = true;
229
 
            break;
230
 
        case Qt::Unchecked:
231
 
            m_changedAddons.remove(addonName);
232
 
            break;
233
 
        default:
234
 
            break;
235
 
        }
236
 
    }
237
 
 
238
 
    m_addonsRevertButton->setEnabled(!m_changedAddons.isEmpty());
239
 
    m_addonsApplyButton->setEnabled(!m_changedAddons.isEmpty());
240
 
}
241
 
 
242
 
void AddonsWidget::emitApplyButtonClicked()
243
 
{
244
 
//     ResourcesModel *resourcesModel = ResourcesModel::global();
245
 
    //resourcesModel->installApplication(m_resource, m_changedAddons);
246
 
 
247
 
    emit applyButtonClicked();
248
 
}