1/*
2 *   SPDX-FileCopyrightText: 2012 Aleix Pol Gonzalez <aleixpol@blue-systems.com>
3 *
4 *   SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6
7import QtQuick 2.1
8import QtQuick.Layouts 1.1
9import QtQuick.Controls 2.1
10import org.kde.discover 2.0
11import org.kde.kirigami 2.14 as Kirigami
12
13Kirigami.AbstractCard {
14    id: item
15    visible: model.shouldShow
16    property bool compact: false
17    property bool separator: true
18    signal markUseful(bool useful)
19
20    // Spacers to indent nested comments/replies
21    Layout.leftMargin: depth * Kirigami.Units.largeSpacing
22
23    contentItem: Item {
24        implicitHeight: mainContent.childrenRect.height
25        implicitWidth: mainContent.childrenRect.width
26        ColumnLayout {
27            id: mainContent
28            anchors {
29                left: parent.left
30                top: parent.top
31                right: parent.right
32            }
33            // Header with stars and date of review
34            RowLayout {
35                Layout.fillWidth: true
36                Layout.leftMargin: Kirigami.Units.largeSpacing
37                Layout.rightMargin: Kirigami.Units.largeSpacing
38                Rating {
39                    id: rating
40                    Layout.fillWidth: true
41                    Layout.bottomMargin: Kirigami.Units.largeSpacing
42                    rating: model.rating
43                    starSize: Kirigami.Units.gridUnit
44                }
45                Label {
46                    Layout.fillWidth: true
47                    horizontalAlignment: Text.AlignRight
48                    text: date.toLocaleDateString(Qt.locale(), "MMMM yyyy")
49                    opacity: 0.6
50                }
51            }
52
53            // Review title and author
54            Label {
55                id: content
56                Layout.fillWidth: true
57                Layout.leftMargin: Kirigami.Units.largeSpacing
58                Layout.rightMargin: Kirigami.Units.largeSpacing
59
60                elide: Text.ElideRight
61                readonly property string author: reviewer ? reviewer : i18n("unknown reviewer")
62                text: summary ? i18n("<b>%1</b> by %2", summary, author) : i18n("Comment by %1", author)
63            }
64
65            // Review text
66            Label {
67                Layout.fillWidth: true
68                Layout.leftMargin: Kirigami.Units.largeSpacing
69                Layout.rightMargin: Kirigami.Units.largeSpacing
70                Layout.bottomMargin: Kirigami.Units.smallSpacing
71
72                text: display
73                wrapMode: Text.Wrap
74            }
75
76            Label {
77                Layout.fillWidth: true
78                Layout.leftMargin: Kirigami.Units.largeSpacing
79                Layout.rightMargin: Kirigami.Units.largeSpacing
80                text: packageVersion ? i18n("Version: %1", packageVersion) : i18n("Version: unknown")
81                elide: Text.ElideRight
82                opacity: 0.6
83            }
84        }
85    }
86
87    footer: Loader {
88        active: !item.compact
89        sourceComponent: RowLayout {
90            id: rateTheReviewLayout
91            visible: !item.compact
92            Label {
93                Layout.leftMargin: Kirigami.Units.largeSpacing
94                visible: usefulnessTotal !== 0
95                text: i18n("Votes: %1 out of %2", usefulnessFavorable, usefulnessTotal)
96            }
97
98            Label {
99                Layout.fillWidth: true
100                Layout.leftMargin: usefulnessTotal === 0 ? Kirigami.Units.largeSpacing : 0
101                horizontalAlignment: Text.AlignRight
102                text: i18n("Was this review useful?")
103                elide: Text.ElideLeft
104            }
105
106            // Useful/Not Useful buttons
107            Button {
108                id: yesButton
109                Layout.maximumWidth: Kirigami.Units.gridUnit * 3
110                Layout.topMargin: Kirigami.Units.smallSpacing
111                Layout.bottomMargin: Kirigami.Units.smallSpacing
112                Layout.alignment: Qt.AlignVCenter
113
114                text: i18nc("Keep this string as short as humanly possible", "Yes")
115
116                checkable: true
117                checked: usefulChoice === ReviewsModel.Yes
118                onClicked: {
119                    noButton.checked = false
120                    item.markUseful(true)
121                }
122            }
123            Button {
124                id: noButton
125                Layout.maximumWidth: Kirigami.Units.gridUnit * 3
126                Layout.topMargin: Kirigami.Units.smallSpacing
127                Layout.bottomMargin: Kirigami.Units.smallSpacing
128                Layout.rightMargin: Kirigami.Units.largeSpacing
129                Layout.alignment: Qt.AlignVCenter
130
131                text: i18nc("Keep this string as short as humanly possible", "No")
132
133                checkable: true
134                checked: usefulChoice === ReviewsModel.No
135                onClicked: {
136                    yesButton.checked = false
137                    item.markUseful(false)
138                }
139            }
140        }
141    }
142}
143