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"
21 /*! \brief Preview widget for rating.
23 The widget can show a rating widget and a field to enter a comment.
24 The visibility of the two widgets is specified by widgetData["visible"],
25 accepting "both", "rating" or "review".
26 The requirement of the review is specified by widgetData["visible"],
27 accepting "both", "rating" or "review".
28 It is possible to customise labels, widgetData["rating-label"] for the rating,
29 widgetData["rewiew-label"] for the comment field and widgetData["submit-label"]
30 for the submit button.
31 The icons used in the rating widget can be customised with
32 widgetData["rating-icon-empty"] and widgetData["rating-icon-full"].
33 The successeful submit emits triggered(widgetId, widgetData["required"], data),
34 with data being {"rating": rating value, "review": review comment, "author": null (for now)}.
40 switch(widgetData["visible"]) {
43 return ratingLabelAndWidgetContainer.implicitHeight + reviewContainer.implicitHeight;
45 return ratingLabelAndWidgetContainer.implicitHeight;
47 return reviewContainer.implicitHeight;
51 property alias ratingValue: rating.value
52 property alias reviewText: reviewTextArea.text
55 // checks rating-input requirements
56 if (((widgetData["required"] === "both" ||
57 widgetData["required"] === "rating") &&
59 ((widgetData["required"] === "both" ||
60 widgetData["required"] === "review") &&
61 reviewTextArea.text === "")) return;
63 var data = {"rating": rating.value, "review": reviewTextArea.text, "author": null};
64 triggered(root.widgetId, "rated", data);
68 id: ratingLabelAndWidgetContainer
73 implicitHeight: rating.height
74 visible: widgetData["visible"] !== "review"
77 objectName: "ratingLabel"
79 verticalCenter: parent.verticalCenter
82 color: root.scopeStyle ? root.scopeStyle.foreground : theme.palette.normal.baseText
84 text: widgetData["rating-label"] || i18n.tr("Rate this")
91 verticalCenter: parent.verticalCenter
96 if (widgetData["visible"] === "rating") root.submit();
99 property var urlIconEmpty: widgetData["rating-icon-empty"]
100 property var urlIconFull: widgetData["rating-icon-full"]
106 implicitHeight: reviewLabel.implicitHeight + reviewSubmitContainer.implicitHeight + anchors.topMargin
108 readonly property real innerMargin: units.gu(1)
113 top: ratingLabelAndWidgetContainer.visible ? ratingLabelAndWidgetContainer.bottom : parent.top
114 bottom: parent.bottom
115 topMargin: ratingLabelAndWidgetContainer.visible ? reviewContainer.innerMargin : 0
117 visible: widgetData["visible"] !== "rating"
120 objectName: "reviewLabel"
127 color: root.scopeStyle ? root.scopeStyle.foreground : theme.palette.normal.baseText
129 text: widgetData["review-label"] || i18n.tr("Add a review")
133 id: reviewSubmitContainer
135 top: reviewLabel.bottom
138 bottom: parent.bottom
139 topMargin: reviewContainer.innerMargin
141 implicitHeight: reviewTextArea.implicitHeight + anchors.topMargin
145 objectName: "reviewTextArea"
146 property bool inputMethodVisible: Qt.inputMethod.visible
147 onInputMethodVisibleChanged: {
148 if(inputMethodVisible && activeFocus)
149 root.makeSureVisible(reviewTextArea);
154 right: submitButton.left
155 rightMargin: reviewContainer.innerMargin
161 objectName: "submitButton"
163 readonly property bool readyToSubmit: {
164 if ((widgetData["required"] !== "review" && rating.value < 0) ||
165 (widgetData["required"] !== "rating" && reviewTextArea.text === "")) return false;
173 color: readyToSubmit ? theme.palette.selected.base : theme.palette.normal.base
174 text: widgetData["submit-label"] || i18n.tr("Send")
176 if (readyToSubmit) root.submit()