Unity 8
 All Classes Functions Properties
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 
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  // TODO cimi: Yet another fix requiring Palette update.
80  color: "grey" //Theme.palette.selected.backgroundText
81  opacity: .8
82  text: widgetData["rating-label"] || i18n.tr("Rate this")
83  }
84 
85  Rating {
86  id: rating
87  objectName: "rating"
88  anchors {
89  verticalCenter: parent.verticalCenter
90  right: parent.right
91  }
92  size: 5
93  onValueChanged: {
94  if (widgetData["visible"] === "rating") root.submit();
95  }
96 
97  property var urlIconEmpty: widgetData["rating-icon-empty"]
98  property var urlIconFull: widgetData["rating-icon-full"]
99  }
100  }
101 
102  Item {
103  id: reviewContainer
104  implicitHeight: reviewLabel.implicitHeight + reviewSubmitContainer.implicitHeight + anchors.topMargin
105 
106  readonly property real innerMargin: units.gu(1)
107 
108  anchors {
109  left: parent.left
110  right: parent.right
111  top: ratingLabelAndWidgetContainer.visible ? ratingLabelAndWidgetContainer.bottom : parent.top
112  bottom: parent.bottom
113  topMargin: ratingLabelAndWidgetContainer.visible ? reviewContainer.innerMargin : 0
114  }
115  visible: widgetData["visible"] !== "rating"
116 
117  Label {
118  objectName: "reviewLabel"
119  id: reviewLabel
120  anchors {
121  top: parent.top
122  left: parent.left
123  right: parent.right
124  }
125  // TODO cimi: Yet another fix requiring Palette update.
126  color: "grey" //Theme.palette.selected.backgroundText
127  opacity: .8
128  text: widgetData["review-label"] || i18n.tr("Add a review")
129  }
130 
131  Item {
132  id: reviewSubmitContainer
133  anchors {
134  top: reviewLabel.bottom
135  left: parent.left
136  right: parent.right
137  bottom: parent.bottom
138  topMargin: reviewContainer.innerMargin
139  }
140  implicitHeight: reviewTextArea.implicitHeight + anchors.topMargin
141 
142  TextArea {
143  id: reviewTextArea
144  objectName: "reviewTextArea"
145  anchors {
146  top: parent.top
147  left: parent.left
148  right: submitButton.left
149  rightMargin: reviewContainer.innerMargin
150  }
151  }
152 
153  Button {
154  id: submitButton
155  objectName: "submitButton"
156 
157  readonly property bool readyToSubmit: {
158  if ((widgetData["required"] !== "review" && rating.value < 0) ||
159  (widgetData["required"] !== "rating" && reviewTextArea.text === "")) return false;
160  else return true;
161  }
162 
163  anchors {
164  top: parent.top
165  right: parent.right
166  }
167  color: readyToSubmit ? Theme.palette.selected.base : Theme.palette.normal.base
168  text: widgetData["submit-label"] || i18n.tr("Send")
169  onClicked: {
170  if (readyToSubmit) root.submit()
171  }
172  }
173  }
174  }
175 }