Unity 8
 All Classes Functions
PreviewRatingInput.qml
1 /*
2  * Copyright (C) 2014 Canonical, Ltd.
3  *
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.
7  *
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.
12  *
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/>.
15  */
16 
17 import QtQuick 2.0
18 import Ubuntu.Components 0.1
19 import "../../Components"
20 
21 /*! \brief Preview widget for rating.
22 
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)}.
35 */
36 
37 PreviewWidget {
38  id: root
39  implicitHeight: {
40  switch(widgetData["visible"]) {
41  default:
42  case "both":
43  return ratingLabelAndWidgetContainer.implicitHeight + reviewContainer.implicitHeight;
44  case "rating":
45  return ratingLabelAndWidgetContainer.implicitHeight;
46  case "review":
47  return reviewContainer.implicitHeight;
48  }
49  }
50 
51  function submit() {
52  // checks rating-input requirements
53  if (((widgetData["required"] === "both" ||
54  widgetData["required"] === "rating") &&
55  rating.value < 0) ||
56  ((widgetData["required"] === "both" ||
57  widgetData["required"] === "review") &&
58  reviewTextArea.text === "")) return;
59 
60  var data = {"rating": rating.value, "review": reviewTextArea.text, "author": null};
61  triggered(root.widgetId, "rated", data);
62  }
63 
64  Item {
65  id: ratingLabelAndWidgetContainer
66  anchors {
67  left: parent.left
68  right: parent.right
69  }
70  implicitHeight: rating.height
71  visible: widgetData["visible"] !== "review"
72 
73  Label {
74  objectName: "ratingLabel"
75  anchors {
76  verticalCenter: parent.verticalCenter
77  left: parent.left
78  }
79  color: root.scopeStyle ? root.scopeStyle.foreground : Theme.palette.normal.baseText
80  opacity: .8
81  text: widgetData["rating-label"] || i18n.tr("Rate this")
82  }
83 
84  Rating {
85  id: rating
86  objectName: "rating"
87  anchors {
88  verticalCenter: parent.verticalCenter
89  right: parent.right
90  }
91  size: 5
92  onValueChanged: {
93  if (widgetData["visible"] === "rating") root.submit();
94  }
95 
96  property var urlIconEmpty: widgetData["rating-icon-empty"]
97  property var urlIconFull: widgetData["rating-icon-full"]
98  }
99  }
100 
101  Item {
102  id: reviewContainer
103  implicitHeight: reviewLabel.implicitHeight + reviewSubmitContainer.implicitHeight + anchors.topMargin
104 
105  readonly property real innerMargin: units.gu(1)
106 
107  anchors {
108  left: parent.left
109  right: parent.right
110  top: ratingLabelAndWidgetContainer.visible ? ratingLabelAndWidgetContainer.bottom : parent.top
111  bottom: parent.bottom
112  topMargin: ratingLabelAndWidgetContainer.visible ? reviewContainer.innerMargin : 0
113  }
114  visible: widgetData["visible"] !== "rating"
115 
116  Label {
117  objectName: "reviewLabel"
118  id: reviewLabel
119  anchors {
120  top: parent.top
121  left: parent.left
122  right: parent.right
123  }
124  color: root.scopeStyle ? root.scopeStyle.foreground : Theme.palette.normal.baseText
125  opacity: .8
126  text: widgetData["review-label"] || i18n.tr("Add a review")
127  }
128 
129  Item {
130  id: reviewSubmitContainer
131  anchors {
132  top: reviewLabel.bottom
133  left: parent.left
134  right: parent.right
135  bottom: parent.bottom
136  topMargin: reviewContainer.innerMargin
137  }
138  implicitHeight: reviewTextArea.implicitHeight + anchors.topMargin
139 
140  TextArea {
141  id: reviewTextArea
142  objectName: "reviewTextArea"
143  anchors {
144  top: parent.top
145  left: parent.left
146  right: submitButton.left
147  rightMargin: reviewContainer.innerMargin
148  }
149  }
150 
151  Button {
152  id: submitButton
153  objectName: "submitButton"
154 
155  readonly property bool readyToSubmit: {
156  if ((widgetData["required"] !== "review" && rating.value < 0) ||
157  (widgetData["required"] !== "rating" && reviewTextArea.text === "")) return false;
158  else return true;
159  }
160 
161  anchors {
162  top: parent.top
163  right: parent.right
164  }
165  color: readyToSubmit ? Theme.palette.selected.base : Theme.palette.normal.base
166  text: widgetData["submit-label"] || i18n.tr("Send")
167  onClicked: {
168  if (readyToSubmit) root.submit()
169  }
170  }
171  }
172  }
173 }