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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/***************************************************************************
 *   Copyright © 2011 Jonathan Thomas <echidnaman@kubuntu.org>             *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or         *
 *   modify it under the terms of the GNU General Public License as        *
 *   published by the Free Software Foundation; either version 2 of        *
 *   the License or (at your option) version 3 or any later version        *
 *   accepted by the membership of KDE e.V. (or its successor approved     *
 *   by the membership of KDE e.V.), which shall act as a proxy            *
 *   defined in Section 14 of version 3 of the license.                    *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
 ***************************************************************************/

#include "ReviewsWidget.h"

#include <QtCore/QStringBuilder>
#include <QtGui/QLabel>
#include <QtGui/QPushButton>
#include <QtGui/QHBoxLayout>
#include <QtGui/QToolButton>

#include <KDialog>
#include <KLocale>

#include <ReviewsBackend/Review.h>

#include "ReviewWidget.h"

bool reviewsGreaterThan(Review *lhs, Review *rhs)
{
    return *lhs > *rhs;
}

ReviewsWidget::ReviewsWidget(QWidget *parent)
        : KVBox(parent)
	, m_pagesFetched(0)
{
    QWidget *headerWidget = new QWidget(this);
    QHBoxLayout *headerLayout = new QHBoxLayout(headerWidget);
    headerLayout->setMargin(0);
    headerWidget->setLayout(headerLayout);

    m_expandButton = new QToolButton(headerWidget);
    m_expandButton->setAutoRaise(true);
    m_expandButton->setArrowType(Qt::DownArrow);
    connect(m_expandButton, SIGNAL(clicked()), this, SLOT(expandButtonClicked()));

    QLabel *titleLabel = new QLabel(headerWidget);
    titleLabel->setText(QLatin1Literal("<h3>") %
                        i18nc("@title", "Reviews") % QLatin1Literal("</h3>"));

    QWidget *headerSpacer = new QWidget(headerWidget);
    headerSpacer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Minimum);

    headerLayout->addWidget(m_expandButton);
    headerLayout->addWidget(titleLabel);
    headerLayout->addWidget(headerSpacer);

    m_reviewContainer = new QWidget(this);
    m_reviewLayout = new QVBoxLayout(m_reviewContainer);
    m_reviewLayout->setSpacing(2*KDialog::spacingHint());
    m_reviewContainer->setLayout(m_reviewLayout);

    m_statusLabel = new QLabel(m_reviewContainer);
    m_statusLabel->setAlignment(Qt::AlignHCenter);
    m_statusLabel->setText(i18nc("@info:status", "Loading reviews"));

    m_reviewLayout->addWidget(m_statusLabel);

    m_moreButton = new QPushButton(this);
    m_moreButton->hide();
    m_moreButton->setText(i18nc("@action", "Check for more reviews"));
    connect(m_moreButton, SIGNAL(clicked()), this, SLOT(emitFetchPage()));
}

ReviewsWidget::~ReviewsWidget()
{
}

void ReviewsWidget::expandButtonClicked()
{
    if (m_reviewContainer->isHidden()) {
        m_expandButton->setArrowType(Qt::DownArrow);
        m_reviewContainer->show();
    } else {
        m_reviewContainer->hide();
        m_expandButton->setArrowType(Qt::RightArrow);
    }
}

void ReviewsWidget::addReviews(QList<Review *> reviews)
{
    if (reviews.isEmpty()) {
	m_moreButton->hide();
	if (!m_pagesFetched) {
	    m_statusLabel->setText(i18nc("@info:status", "No reviews available"));
	}
        return;
    }

    m_pagesFetched++;
    m_statusLabel->hide();
    m_moreButton->show();
    m_moreButton->setEnabled(true);

    qSort(reviews.begin(), reviews.end(), reviewsGreaterThan);

    foreach (Review *review, reviews) {
        if (!review->shouldShow()) {
            continue;
        }
        ReviewWidget *reviewWidget = new ReviewWidget(m_reviewContainer);
        reviewWidget->setReview(review);

        m_reviewLayout->addWidget(reviewWidget);
    }
}

void ReviewsWidget::emitFetchPage()
{
    emit fetchPage(m_pagesFetched + 1);
    m_moreButton->setEnabled(false);
}

#include "ReviewsWidget.moc"