CLListbox.qml 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /**
  2. * Copyright © 2010 Digia Plc
  3. * Copyright © 2010 Nokia Corporation
  4. *
  5. * All rights reserved.
  6. *
  7. * Nokia and Nokia Connecting People are registered trademarks of
  8. * Nokia Corporation.
  9. * Java and all Java-based marks are trademarks or registered
  10. * trademarks of
  11. * Sun Microsystems, Inc. Other product and company names
  12. * mentioned herein may be
  13. * trademarks or trade names of their respective owners.
  14. *
  15. *
  16. * Subject to the conditions below, you may, without charge:
  17. *
  18. * · Use, copy, modify and/or merge copies of this software and
  19. * associated documentation files (the "Software")
  20. *
  21. * · Publish, distribute, sub-licence and/or sell new software
  22. * derived from or incorporating the Software.
  23. *
  24. *
  25. * This file, unmodified, shall be included with all copies or
  26. * substantial portions
  27. * of the Software that are distributed in source code form.
  28. *
  29. * The Software cannot constitute the primary value of any new
  30. * software derived
  31. * from or incorporating the Software.
  32. *
  33. * Any person dealing with the Software shall not misrepresent
  34. * the source of the Software.
  35. *
  36. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
  37. * KIND, EXPRESS OR IMPLIED,
  38. * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  39. * MERCHANTABILITY, FITNESS FOR A
  40. * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  41. * AUTHORS OR COPYRIGHT
  42. * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  43. * WHETHER IN AN ACTION
  44. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  45. * CONNECTION WITH THE
  46. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  47. */
  48. import Qt 4.7
  49. import "includes"
  50. import "gradients"
  51. Rectangle {
  52. id: structure
  53. property real itemHeight: 30
  54. property int itemsVisible: 5
  55. property ListModel items: TestItemList {}
  56. property bool selectMany: false
  57. property bool enableCLSlider: true
  58. property bool allowDeSelect: true
  59. property CLStyle style: CLStyle {}
  60. property color colorWhenDefault: style.colorWhenDefault
  61. property color colorWhenHovered: style.colorWhenHovered
  62. property color colorWhenSelected: style.colorWhenSelected
  63. property color textColor: style.textColor
  64. property color borderColor: style.borderColor
  65. property int borderWidth: style.borderWidth
  66. property real fontSize: style.fontSize
  67. property string fontFamily: style.fontFamily
  68. property string fontWeight: style.fontWeight
  69. property color bgColor: style.bgColor
  70. property color sliderColor: colorWhenHovered
  71. property color sliderBgColor: borderColor
  72. property bool gradientDefaultOn: style.gradientDefaultOn
  73. property bool gradientHoveredOn: style.gradientHoveredOn
  74. property bool gradientSelectedOn: style.gradientSelectedOn
  75. property bool gradientBgOn: style.gradientBgOn
  76. property Gradient gradientWhenDefault: style.gradientWhenDefault
  77. property Gradient gradientWhenHovered: style.gradientWhenHovered
  78. property Gradient gradientBg: style.gradientBg
  79. property Gradient gradientWhenSelected: style.gradientWhenSelected
  80. property Gradient nullGradient: Gradient{}
  81. property bool hoveredStateOn: style.hoveredStateOn
  82. signal itemClicked(int index)
  83. signal change()
  84. /**
  85. * Gets the values of selected items
  86. *
  87. * @return array
  88. */
  89. function getSelected() {
  90. var theArray = Array();
  91. var arrayCount = 0;
  92. for (var i=0; i<theList.count; ++i) {
  93. if (theList.model.get(i).selected) {
  94. theArray[arrayCount] = theList.model.get(i).value;
  95. ++arrayCount;
  96. }
  97. }
  98. return theArray;
  99. }
  100. /**
  101. * Get names of items
  102. *
  103. * @return array
  104. */
  105. function getNames() {
  106. var theArray = Array();
  107. for (var i=0; i<theList.count; ++i) {
  108. theArray[i] = theList.model.get(i).label;
  109. }
  110. return theArray;
  111. }
  112. /**
  113. * Removes all selections
  114. *
  115. * @return Nothing
  116. */
  117. function clearSelections() {
  118. for (var i=0; i<theList.count; ++i) {
  119. theList.model.setProperty(i, "selected", false);
  120. }
  121. }
  122. /**
  123. * Counts items
  124. *
  125. * @return Nothing
  126. */
  127. function countChildren() {
  128. return theList.count;
  129. }
  130. /**
  131. * Selects a specific item
  132. *
  133. * @param index Index of the item to be selected
  134. * @return bool
  135. */
  136. function select(index) {
  137. if (index < 0) return false;
  138. if (index >= theList.count) return false;
  139. theList.model.setProperty(index, "selected", true);
  140. structure.change();
  141. return true;
  142. }
  143. /**
  144. * Removes selection from a specific item
  145. *
  146. * @param index Index of the item to be unselected
  147. * @return bool
  148. */
  149. function deSelect(index) {
  150. if (index < 0) return false;
  151. if (index >= theList.count) return false;
  152. theList.model.setProperty(index, "selected", false);
  153. structure.change();
  154. return true;
  155. }
  156. /**
  157. * Checks whether an item is selected. Returns -1 on error, 1 on selected and 0 on not selected.
  158. *
  159. * @param idnex Index of the item to be checked
  160. * @return int
  161. */
  162. function isSelected(index) {
  163. if (index < 0) return -1;
  164. if (index >= theList.count) return -1;
  165. return (theList.model.get(index).selected)?1:0;
  166. }
  167. onItemClicked: {
  168. if (selectMany) {
  169. if (theList.model.get(index).selected) theList.model.setProperty(index, "selected", false);
  170. else theList.model.setProperty(index, "selected", true);
  171. } else {
  172. if (theList.model.get(index).selected) {
  173. if (structure.allowDeSelect) theList.model.setProperty(index, "selected", false);
  174. } else {
  175. for (var i=0; i<theList.count; ++i) theList.model.setProperty(i, "selected", false);
  176. theList.model.setProperty(index, "selected", true);
  177. }
  178. }
  179. structure.change();
  180. }
  181. Component.onCompleted: {
  182. var alreadyFoundOne = false;
  183. if (!selectMany) for (var i=0; i<theList.count; ++i) {
  184. if (alreadyFoundOne) {
  185. theList.model.setProperty(i, "selected", false);
  186. } else {
  187. if (theList.model.get(i).selected) alreadyFoundOne = true;
  188. }
  189. }
  190. var available = (theList.count<itemsVisible)? theList.count : itemsVisible;
  191. structure.height = (available * structure.itemHeight) + 2*borderWidth;
  192. smallerRect.height = (available * structure.itemHeight);
  193. }
  194. onHeightChanged: {
  195. var available = (theList.count<itemsVisible)? theList.count : itemsVisible;
  196. structure.height = (available * structure.itemHeight) + 2*borderWidth;
  197. smallerRect.height = (available * structure.itemHeight);
  198. }
  199. onItemsVisibleChanged: {
  200. if (itemsVisible < 1) itemsVisible = 1;
  201. var available = (theList.count<itemsVisible)? theList.count : itemsVisible;
  202. structure.height = (available * structure.itemHeight) + 2*borderWidth;
  203. smallerRect.height = (available * structure.itemHeight);
  204. }
  205. onItemHeightChanged: {
  206. var available = (theList.count<itemsVisible)? theList.count : itemsVisible;
  207. structure.height = (available * structure.itemHeight) + 2*borderWidth;
  208. smallerRect.height = (available * structure.itemHeight);
  209. }
  210. onSelectManyChanged: {
  211. var alreadyFoundOne = false;
  212. if (!selectMany) for (var i=0; i<theList.count; ++i) {
  213. if (alreadyFoundOne) {
  214. theList.model.setProperty(i, "selected", false);
  215. }
  216. else {
  217. if (theList.model.get(i).selected) alreadyFoundOne = true;
  218. }
  219. }
  220. structure.change();
  221. }
  222. width: 160
  223. height: 400
  224. clip: true
  225. color: bgColor
  226. gradient: (gradientBgOn)?gradientBg:nullGradient
  227. border.width: borderWidth
  228. border.color: borderColor
  229. Component {
  230. id: listItem
  231. Rectangle {
  232. id: rectis
  233. property string valuex: value
  234. property string labelx: label
  235. property int indexx: index
  236. property bool selectedx: selected
  237. width: smallerRect.width
  238. height: structure.itemHeight
  239. color: colorWhenDefault
  240. gradient: (gradientDefaultOn)?gradientWhenDefault:nullGradient
  241. MouseArea {
  242. id: mousepad
  243. anchors.fill: parent
  244. hoverEnabled: true
  245. onClicked: structure.itemClicked(indexx)
  246. }
  247. Text {
  248. anchors.verticalCenter: parent.verticalCenter
  249. x: 8
  250. font.pointSize: fontSize
  251. font.family: fontFamily
  252. font.weight: fontWeight
  253. color: textColor
  254. text: labelx
  255. }
  256. states: [
  257. State {
  258. id: stateSelected
  259. name: "selectedx"; when: rectis.selectedx
  260. PropertyChanges { target: rectis; color: colorWhenSelected; gradient: (gradientSelectedOn)?gradientWhenSelected:nullGradient }
  261. },
  262. State {
  263. id: stateHovered
  264. name: "hovered"; when: (mousepad.containsMouse && hoveredStateOn)
  265. PropertyChanges { target: rectis; color: colorWhenHovered; gradient: (gradientHoveredOn)?gradientWhenHovered:nullGradient }
  266. }
  267. ]
  268. }
  269. }
  270. Rectangle {
  271. id: smallerRect
  272. x: structure.borderWidth
  273. y: structure.borderWidth
  274. width: structure.width - 2*structure.borderWidth
  275. height: structure.width - 2*structure.borderWidth
  276. clip: true
  277. color: "transparent"
  278. ListView {
  279. id: theList
  280. interactive: (theList.count<=structure.itemsVisible)?false:true;
  281. model: items
  282. delegate: listItem
  283. anchors.fill: parent
  284. snapMode: ListView.SnapOneItem
  285. }
  286. }
  287. Rectangle {
  288. id: scroller
  289. visible: (theList.count>structure.itemsVisible && enableCLSlider)?true:false;
  290. color: structure.sliderBgColor
  291. width: 5
  292. height: smallerRect.height
  293. x: smallerRect.x + smallerRect.width - scroller.width
  294. y: smallerRect.y
  295. opacity: 0.7
  296. clip: true
  297. Rectangle {
  298. id: sled
  299. color: structure.sliderColor
  300. width: scroller.width
  301. height: smallerRect.height * ( structure.itemsVisible / theList.count )
  302. y: Math.round( ((theList.contentY / itemHeight) / (theList.count - itemsVisible)) * (scroller.height-sled.height) )
  303. radius: 2
  304. smooth: true
  305. }
  306. }
  307. }