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

« back to all changes in this revision

Viewing changes to installer/ReviewsWidget/ReviewsWidget.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 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 "ReviewsWidget.h"
22
 
 
23
 
#include <QtCore/QStringBuilder>
24
 
#include <QtWidgets/QLabel>
25
 
#include <QtWidgets/QPushButton>
26
 
#include <QHBoxLayout>
27
 
#include <QtWidgets/QToolButton>
28
 
 
29
 
#include <KDialog>
30
 
#include <KLocalizedString>
31
 
 
32
 
#include <ReviewsBackend/Review.h>
33
 
 
34
 
#include "ReviewWidget.h"
35
 
 
36
 
bool reviewsGreaterThan(Review *lhs, Review *rhs)
37
 
{
38
 
    return *lhs > *rhs;
39
 
}
40
 
 
41
 
ReviewsWidget::ReviewsWidget(QWidget *parent)
42
 
        : KVBox(parent)
43
 
        , m_pagesFetched(0)
44
 
{
45
 
    QWidget *headerWidget = new QWidget(this);
46
 
    QHBoxLayout *headerLayout = new QHBoxLayout(headerWidget);
47
 
    headerLayout->setMargin(0);
48
 
    headerWidget->setLayout(headerLayout);
49
 
 
50
 
    m_expandButton = new QToolButton(headerWidget);
51
 
    m_expandButton->setAutoRaise(true);
52
 
    m_expandButton->setArrowType(Qt::DownArrow);
53
 
    connect(m_expandButton, SIGNAL(clicked()), this, SLOT(expandButtonClicked()));
54
 
 
55
 
    QLabel *titleLabel = new QLabel(headerWidget);
56
 
    titleLabel->setText(QLatin1Literal("<h3>") %
57
 
                        i18nc("@title", "Reviews") % QLatin1Literal("</h3>"));
58
 
 
59
 
    QWidget *headerSpacer = new QWidget(headerWidget);
60
 
    headerSpacer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Minimum);
61
 
 
62
 
    headerLayout->addWidget(m_expandButton);
63
 
    headerLayout->addWidget(titleLabel);
64
 
    headerLayout->addWidget(headerSpacer);
65
 
 
66
 
    m_reviewContainer = new QWidget(this);
67
 
    m_reviewLayout = new QVBoxLayout(m_reviewContainer);
68
 
    m_reviewLayout->setSpacing(2*KDialog::spacingHint());
69
 
    m_reviewContainer->setLayout(m_reviewLayout);
70
 
 
71
 
    m_statusLabel = new QLabel(m_reviewContainer);
72
 
    m_statusLabel->setAlignment(Qt::AlignHCenter);
73
 
    m_statusLabel->setText(i18nc("@info:status", "Loading reviews"));
74
 
 
75
 
    m_reviewLayout->addWidget(m_statusLabel);
76
 
 
77
 
    m_moreButton = new QPushButton(this);
78
 
    m_moreButton->hide();
79
 
    m_moreButton->setText(i18nc("@action", "Check for more reviews"));
80
 
    connect(m_moreButton, SIGNAL(clicked()), this, SLOT(emitFetchPage()));
81
 
}
82
 
 
83
 
ReviewsWidget::~ReviewsWidget()
84
 
{
85
 
}
86
 
 
87
 
void ReviewsWidget::expandButtonClicked()
88
 
{
89
 
    if (m_reviewContainer->isHidden()) {
90
 
        m_expandButton->setArrowType(Qt::DownArrow);
91
 
        m_reviewContainer->show();
92
 
    } else {
93
 
        m_reviewContainer->hide();
94
 
        m_expandButton->setArrowType(Qt::RightArrow);
95
 
    }
96
 
}
97
 
 
98
 
void ReviewsWidget::addReviews(QList<Review *> reviews)
99
 
{
100
 
    if (reviews.isEmpty()) {
101
 
        m_moreButton->hide();
102
 
        if (!m_pagesFetched) {
103
 
            m_statusLabel->setText(i18nc("@info:status", "No reviews available"));
104
 
        }
105
 
        return;
106
 
    }
107
 
 
108
 
    m_pagesFetched++;
109
 
    m_statusLabel->hide();
110
 
    m_moreButton->show();
111
 
    m_moreButton->setEnabled(true);
112
 
 
113
 
    qSort(reviews.begin(), reviews.end(), reviewsGreaterThan);
114
 
 
115
 
    foreach (Review *review, reviews) {
116
 
        if (!review->shouldShow()) {
117
 
            continue;
118
 
        }
119
 
        ReviewWidget *reviewWidget = new ReviewWidget(m_reviewContainer);
120
 
        reviewWidget->setReview(review);
121
 
 
122
 
        m_reviewLayout->addWidget(reviewWidget);
123
 
    }
124
 
}
125
 
 
126
 
void ReviewsWidget::emitFetchPage()
127
 
{
128
 
    emit fetchPage(m_pagesFetched + 1);
129
 
    m_moreButton->setEnabled(false);
130
 
}
131
 
 
132
 
#include "ReviewsWidget.moc"