• Skip to content
  • Skip to link menu
KDE API Documentation - GCComboBox.qml Source File (GCompris-qt)
  • KDE Home
  • Contact Us
 

GCompris-qt

  • src
  • core
GCComboBox.qml
1 /* GCompris - GCComboBox.qml
2  *
3  * Copyright (C) 2015 Johnny Jazeix <jazeix@gmail.com>
4  *
5  * Authors:
6  * Johnny Jazeix <jazeix@gmail.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, see <http://www.gnu.org/licenses/>.
20  */
21 import QtQuick 2.2
22 import QtQuick.Controls 1.0
23 import GCompris 1.0
24 
42 Item {
43  id: gccombobox
44  focus: true
45 
46  width: button.width
47  height: button.height
48 
54  property Item background
55 
60  property int currentIndex: -1
61 
66  property string currentText
67 
68 
73  property alias model: gridview.model
74 
79  property string label
80 
86  readonly property bool isModelArray: model.constructor === Array
87 
88  // start and stop trigs the animation
89  signal start
90  signal stop
91 
92  // emitted at stop animation end
93  signal close
94 
95  onCurrentIndexChanged: {
96  currentText = isModelArray ? model[currentIndex].text : (model && model.get(currentIndex) ? model.get(currentIndex).text : "")
97  }
98 
103  Flow {
104  width: button.width+labelText.width+10
105  spacing: 5 * ApplicationInfo.ratio
106  Rectangle {
107  id: button
108  visible: true
109  // Add radius to add some space between text and borders
110  implicitWidth: Math.max(200, currentTextBox.width+radius)
111  implicitHeight: 50 * ApplicationInfo.ratio
112  border.width: 2
113  border.color: "black"
114  radius: 10
115  gradient: Gradient {
116  GradientStop { position: 0 ; color: mouseArea.pressed ? "#87ff5c" : "#ffe85c" }
117  GradientStop { position: 1 ; color: mouseArea.pressed ? "#44ff00" : "#f8d600" }
118  }
119  // Current value of combobox
120  GCText {
121  id: currentTextBox
122  anchors.horizontalCenter: parent.horizontalCenter
123  anchors.verticalCenter: parent.verticalCenter
124  text: currentText
125  fontSize: mediumSize
126  }
127  MouseArea {
128  id: mouseArea
129  anchors.fill: parent
130  onReleased: {
131  popup.visible = true
132  }
133  }
134  }
135 
136  GCText {
137  id: labelText
138  text: label
139  fontSize: mediumSize
140  wrapMode: Text.WordWrap
141  }
142  }
143 
148  Item {
149  id: popup
150  visible: false
151  width: parent.width
152  height: parent.height
153 
154  parent: background
155 
156  focus: visible
157 
158  // Forward event to activity if key pressed is not one of the handled key
159  // (ctrl+F should still resize the window for example)
160  Keys.onPressed: background.currentActivity.Keys.onPressed(event)
161 
162  Keys.onRightPressed: gridview.moveCurrentIndexRight();
163  Keys.onLeftPressed: gridview.moveCurrentIndexLeft();
164  Keys.onDownPressed: gridview.moveCurrentIndexDown();
165  Keys.onUpPressed: gridview.moveCurrentIndexUp();
166 
167  Keys.onEscapePressed: {
168  // Keep the old value
169  discardChange();
170  hidePopUpAndRestoreFocus();
171  }
172  Keys.onEnterPressed: {
173  acceptChange();
174  hidePopUpAndRestoreFocus();
175  }
176  Keys.onReturnPressed: {
177  acceptChange();
178  hidePopUpAndRestoreFocus();
179  }
180 
181  Keys.onSpacePressed: {
182  acceptChange();
183  hidePopUpAndRestoreFocus();
184  }
185 
186  // Don't accept the list value, restore previous value
187  function discardChange() {
188  if(isModelArray) {
189  for(var i = 0 ; i < model.count ; ++ i) {
190  if(model[currentIndex].text === currentText) {
191  currentIndex = i;
192  break;
193  }
194  }
195  }
196  else {
197  for(var i = 0 ; i < model.length ; ++ i) {
198  if(model.get(currentIndex).text === currentText) {
199  currentIndex = i;
200  break;
201  }
202  }
203  }
204  gridview.currentIndex = currentIndex;
205  }
206 
207  // Accept the change. Updates the currentIndex and text of the button
208  function acceptChange() {
209  currentIndex = gridview.currentIndex;
210  currentText = isModelArray ? model[currentIndex].text : (model && model.get(currentIndex) ? model.get(currentIndex).text : "")
211  }
212 
213  function hidePopUpAndRestoreFocus() {
214  popup.visible = false;
215  // Restore focus on previous activity for keyboard input
216  background.currentActivity.forceActiveFocus();
217  }
218 
219  Rectangle {
220  id: listBackground
221  anchors.fill: parent
222  radius: 10
223  color: "grey"
224 
225  Rectangle {
226  id : headerDescription
227  z: 10
228  width: gridview.width
229  height: gridview.elementHeight
230  GCText {
231  text: label
232  fontSize: mediumSize
233  wrapMode: Text.WordWrap
234  anchors.horizontalCenter: parent.horizontalCenter
235  }
236  GCButtonCancel {
237  id: discardIcon
238  anchors.right: headerDescription.right
239  anchors.top: headerDescription.top
240 
241  MouseArea {
242  anchors.fill: parent
243  onClicked: {
244  popup.acceptChange();
245  popup.hidePopUpAndRestoreFocus();
246  }
247  }
248  }
249  }
250 
251  GridView {
252  id: gridview
253  z: 4
254  readonly property int elementHeight: 40 * ApplicationInfo.ratio
255 
256  // each element has a 300 width size minimum. If the screen is larger than it, we do a grid with cases with 300px for width at minimum.
257  // If you have a better idea/formula to have a different column number, don't hesitate, change it :).
258  readonly property int numberOfColumns: Math.max(1, Math.floor(width / 300))
259  contentHeight: isModelArray ? elementHeight*model.count/numberOfColumns : elementHeight*model.length/numberOfColumns
260  width: listBackground.width
261  height: listBackground.height-headerDescription.height
262  currentIndex: gccombobox.currentIndex
263  flickableDirection: Flickable.VerticalFlick
264  clip: true
265  anchors.top: headerDescription.bottom
266  anchors.topMargin: headerDescription.height / 2
267  cellWidth: width / numberOfColumns
268  cellHeight: elementHeight
269 
270  delegate: Component {
271  Rectangle {
272  width: gridview.cellWidth
273  height: gridview.elementHeight
274  color: GridView.isCurrentItem ? "darkcyan" : "beige"
275  border.width: GridView.isCurrentItem ? 3 : 2
276  radius: 5
277  Image {
278  id: isSelectedIcon
279  visible: parent.GridView.isCurrentItem
280  source: "qrc:/gcompris/src/core/resource/apply.svg"
281  anchors.right: textValue.left
282  anchors.top: parent.top
283  anchors.rightMargin: 10
284  sourceSize.width: (gridview.elementHeight*0.8) * ApplicationInfo.ratio
285  }
286  GCText {
287  id: textValue
288  text: isModelArray ? modelData.text : model.text
289  anchors.centerIn: parent
290  }
291  MouseArea {
292  anchors.fill: parent
293  onClicked: {
294  currentIndex = index
295  popup.acceptChange();
296  popup.hidePopUpAndRestoreFocus();
297  }
298  }
299  }
300  }
301  }
302  }
303  }
304 }
GCButtonCancel
A QML component representing GCompris' cancel button.
Definition: GCButtonCancel.qml:30
GCText
A QML component unifying text presentation in GCompris.
Definition: GCText.qml:47
QtQuick
GCompris
ApplicationInfo
A general purpose singleton that exposes miscellaneous native functions to the QML layer...
Definition: ApplicationInfo.h:43
ApplicationInfo::ratio
qreal ratio
Ratio factor used for scaling of sizes on high-dpi devices.
Definition: ApplicationInfo.h:91
This file is part of the KDE documentation.
Documentation copyright © 1996-2015 The KDE developers.
Generated on Tue Jun 2 2015 21:47:47 by doxygen 1.8.9.1 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

GCompris-qt

Skip menu "GCompris-qt"
  • Main Page
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • File List
  • Modules

Class Picker

Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal