2 * Copyright (C) 2014,2015 Canonical, Ltd.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 3.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 import Ubuntu.Components 1.3
19 import "../../Components"
20 import "PreviewSingleton"
22 /*! \brief Preview widget for rating.
24 The widget can show a rating widget and a field to enter a comment.
25 The visibility of the two widgets is specified by widgetData["visible"],
26 accepting "both", "rating" or "review".
27 The requirement of the review is specified by widgetData["visible"],
28 accepting "both", "rating" or "review".
29 It is possible to customise labels, widgetData["rating-label"] for the rating,
30 widgetData["rewiew-label"] for the comment field and widgetData["submit-label"]
31 for the submit button.
32 The icons used in the rating widget can be customised with
33 widgetData["rating-icon-empty"] and widgetData["rating-icon-full"].
34 The successeful submit emits triggered(widgetId, widgetData["required"], data),
35 with data being {"rating": rating value, "review": review comment, "author": null (for now)}.
41 switch(widgetData["visible"]) {
44 return ratingLabelAndWidgetContainer.implicitHeight + (reviewContainer.visible ? reviewContainer.implicitHeight : 0);
46 return ratingLabelAndWidgetContainer.implicitHeight;
48 return reviewContainer.implicitHeight;
52 clip: reviewContainer.visible
54 property alias ratingValue: rating.value
55 property alias reviewText: reviewTextArea.text
57 onRatingValueChanged: storeRatingState()
58 onReviewTextChanged: storeTextState()
59 onWidgetIdChanged: restoreReviewState()
61 function initializeWidgetExtraData() {
62 if (typeof(PreviewSingleton.widgetExtraData[widgetId]) == "undefined") PreviewSingleton.widgetExtraData[widgetId] = [];
65 function storeRatingState() {
66 initializeWidgetExtraData();
67 PreviewSingleton.widgetExtraData[widgetId][0] = ratingValue;
70 function storeTextState() {
71 initializeWidgetExtraData();
72 PreviewSingleton.widgetExtraData[widgetId][1] = reviewText;
75 function restoreReviewState() {
76 if (!PreviewSingleton.widgetExtraData[widgetId]) return;
77 if (PreviewSingleton.widgetExtraData[widgetId][0] > 0) ratingValue = PreviewSingleton.widgetExtraData[widgetId][0];
78 if (typeof(PreviewSingleton.widgetExtraData[widgetId][1]) != "undefined" &&
79 PreviewSingleton.widgetExtraData[widgetId][1] != "") reviewText = PreviewSingleton.widgetExtraData[widgetId][1];
83 // checks rating-input requirements
84 if (((widgetData["required"] === "both" ||
85 widgetData["required"] === "rating") &&
87 ((widgetData["required"] === "both" ||
88 widgetData["required"] === "review") &&
89 reviewTextArea.text === "")) return;
91 var data = {"rating": rating.value, "review": reviewTextArea.text, "author": null};
92 triggered(root.widgetId, "rated", data);
96 id: ratingLabelAndWidgetContainer
97 anchors { left: parent.left; right: parent.right; }
98 spacing: units.gu(0.5)
99 visible: widgetData["visible"] !== "review"
103 objectName: "ratingLabel"
104 anchors { left: parent.left; right: parent.right; }
106 color: root.scopeStyle ? root.scopeStyle.foreground : theme.palette.normal.baseText
108 text: widgetData["rating-label"] || i18n.tr("Rate this")
114 anchors.left: parent.left
118 if (widgetData && widgetData["visible"] === "rating") root.submit();
121 property var urlIconEmpty: widgetData["rating-icon-empty"] || "image://theme/non-starred"
122 property var urlIconFull: widgetData["rating-icon-full"] || "image://theme/starred"
128 objectName: "reviewContainer"
129 implicitHeight: visible ? reviewSubmitContainer.implicitHeight + anchors.topMargin : 0
131 readonly property real innerMargin: units.gu(1)
136 top: ratingLabelAndWidgetContainer.visible ? ratingLabelAndWidgetContainer.bottom : parent.top
137 bottom: parent.bottom
138 topMargin: ratingLabelAndWidgetContainer.visible ? innerMargin : 0
141 switch(widgetData["visible"]) {
144 return widgetData["required"] === "review" || rating.value > 0;
152 Behavior on implicitHeight {
153 UbuntuNumberAnimation {
154 duration: UbuntuAnimation.FastDuration
155 easing.type: Easing.OutCubic
160 id: reviewSubmitContainer
161 objectName: "reviewSubmitContainer"
163 implicitHeight: reviewTextArea.implicitHeight + anchors.topMargin
167 objectName: "reviewTextArea"
168 property bool inputMethodVisible: Qt.inputMethod.visible
169 onInputMethodVisibleChanged: {
170 if(inputMethodVisible && activeFocus)
171 root.makeSureVisible(reviewTextArea);
174 if (visible && widgetData["visible"] !== "review")
180 right: submitButton.left
181 rightMargin: reviewContainer.innerMargin
183 placeholderText: widgetData["review-label"] || i18n.tr("Add a review")
188 objectName: "submitButton"
190 readonly property bool readyToSubmit: {
191 if ((widgetData["required"] !== "review" && rating.value < 0) ||
192 (widgetData["required"] !== "rating" && reviewTextArea.text === "")) return false;
200 enabled: readyToSubmit
201 text: widgetData["submit-label"] || i18n.tr("Send")
202 onClicked: root.submit()