TumblerStyle.qml 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2016 The Qt Company Ltd.
  4. ** Contact: https://www.qt.io/licensing/
  5. **
  6. ** This file is part of the Qt Quick Extras module of the Qt Toolkit.
  7. **
  8. ** $QT_BEGIN_LICENSE:LGPL$
  9. ** Commercial License Usage
  10. ** Licensees holding valid commercial Qt licenses may use this file in
  11. ** accordance with the commercial license agreement provided with the
  12. ** Software or, alternatively, in accordance with the terms contained in
  13. ** a written agreement between you and The Qt Company. For licensing terms
  14. ** and conditions see https://www.qt.io/terms-conditions. For further
  15. ** information use the contact form at https://www.qt.io/contact-us.
  16. **
  17. ** GNU Lesser General Public License Usage
  18. ** Alternatively, this file may be used under the terms of the GNU Lesser
  19. ** General Public License version 3 as published by the Free Software
  20. ** Foundation and appearing in the file LICENSE.LGPL3 included in the
  21. ** packaging of this file. Please review the following information to
  22. ** ensure the GNU Lesser General Public License version 3 requirements
  23. ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
  24. **
  25. ** GNU General Public License Usage
  26. ** Alternatively, this file may be used under the terms of the GNU
  27. ** General Public License version 2.0 or (at your option) the GNU General
  28. ** Public license version 3 or any later version approved by the KDE Free
  29. ** Qt Foundation. The licenses are as published by the Free Software
  30. ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
  31. ** included in the packaging of this file. Please review the following
  32. ** information to ensure the GNU General Public License requirements will
  33. ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
  34. ** https://www.gnu.org/licenses/gpl-3.0.html.
  35. **
  36. ** $QT_END_LICENSE$
  37. **
  38. ****************************************************************************/
  39. import QtQuick 2.0
  40. import QtGraphicalEffects 1.0
  41. import QtQuick.Controls 1.4
  42. import QtQuick.Controls.Styles 1.4
  43. import QtQuick.Controls.Private 1.0
  44. import QtQuick.Extras 1.4
  45. import QtQuick.Extras.Private 1.0
  46. /*!
  47. \qmltype TumblerStyle
  48. \inqmlmodule QtQuick.Controls.Styles
  49. \since 5.5
  50. \ingroup controlsstyling
  51. \brief Provides custom styling for Tumbler.
  52. You can create a custom tumbler by replacing the following delegates:
  53. \list
  54. \li \l background
  55. \li \l foreground
  56. \li \l separator
  57. \li \l delegate
  58. \li \l highlight
  59. \li \l frame
  60. \endlist
  61. */
  62. Style {
  63. id: tumblerStyle
  64. padding.left: __padding
  65. padding.right: __padding
  66. padding.top: __padding
  67. padding.bottom: __padding
  68. /*!
  69. The \l Tumbler that this style is attached to.
  70. */
  71. readonly property Tumbler control: __control
  72. /*!
  73. \obsolete
  74. This property holds the spacing between each delegate.
  75. This property has no effect.
  76. */
  77. property real spacing: 0
  78. /*!
  79. This property holds the amount of items visible in each column.
  80. This value should be an odd number.
  81. */
  82. property int visibleItemCount: 3
  83. /*!
  84. \internal
  85. TODO: how do we handle differing padding values?
  86. */
  87. readonly property real __padding: Math.max(6, Math.round(TextSingleton.implicitHeight * 0.4))
  88. /*! \internal */
  89. property real __delegateHeight: 0
  90. /*! \internal */
  91. property real __separatorWidth: 0
  92. /*!
  93. The background of the tumbler.
  94. */
  95. property Component background: Rectangle {
  96. gradient: Gradient {
  97. GradientStop { position: 0.00; color: "#acacac" }
  98. GradientStop { position: 0.12; color: "#d5d5d5" }
  99. GradientStop { position: 0.24; color: "#e8e8e8" }
  100. GradientStop { position: 0.39; color: "#ffffff" }
  101. GradientStop { position: 0.61; color: "#ffffff" }
  102. GradientStop { position: 0.76; color: "#e8e8e8" }
  103. GradientStop { position: 0.88; color: "#d5d5d5" }
  104. GradientStop { position: 1.00; color: "#acacac" }
  105. }
  106. }
  107. /*!
  108. The foreground of the tumbler.
  109. */
  110. property Component foreground: Item {
  111. clip: true
  112. Rectangle {
  113. id: rect
  114. anchors.fill: parent
  115. // Go one pixel larger than our parent so that we can hide our one pixel frame
  116. // that the shadow is created from.
  117. anchors.margins: -1
  118. color: "transparent"
  119. border.color: "black"
  120. visible: false
  121. }
  122. DropShadow {
  123. anchors.fill: rect
  124. source: rect
  125. samples: 15
  126. spread: 0.45
  127. cached: true
  128. }
  129. }
  130. /*!
  131. The separator between each column.
  132. The \l {Item::}{implicitWidth} property must be set, and should be the
  133. same value for each separator.
  134. */
  135. property Component separator: Canvas {
  136. implicitWidth: Math.max(10, Math.round(TextSingleton.implicitHeight * 0.4))
  137. onPaint: {
  138. var ctx = getContext("2d");
  139. ctx.reset();
  140. ctx.fillStyle = "#11000000";
  141. ctx.fillRect(0, 0, width, height);
  142. ctx.fillStyle = "#11000000";
  143. ctx.fillRect(width * 0.2, 0, width * 0.6, height);
  144. ctx.fillStyle = "#66000000";
  145. ctx.fillRect(width * 0.4, 0, width * 0.2, height);
  146. }
  147. }
  148. /*!
  149. The foreground of each column.
  150. In terms of stacking order, this component is displayed above the
  151. delegate and highlight components, but below the foreground component.
  152. \table
  153. \row \li \c {readonly property int} \b styleData.column
  154. \li The index of the column that contains this item.
  155. \row \li \c {readonly property bool} \b styleData.activeFocus
  156. \li \c true if the column that contains this item has active focus.
  157. \endtable
  158. Delegates for items in specific columns can be defined using
  159. TumblerColumn's \l {TumblerColumn::columnForeground}{columnForeground}
  160. property, which will be used instead of this component.
  161. */
  162. property Component columnForeground
  163. /*!
  164. The frame around the tumbler.
  165. The \l {Item::}{implicitWidth} property must be set, and should be the
  166. same value for each separator.
  167. */
  168. property Component frame: Canvas {
  169. onPaint: {
  170. // workaround for QTBUG-40792
  171. var ctx = getContext("2d");
  172. ctx.reset();
  173. var cornerRadius = Math.max(2, Math.round(TextSingleton.implicitHeight * 0.2));
  174. var outerLineWidth = Math.max(1, Math.round(TextSingleton.implicitHeight * 0.05));
  175. var innerLineWidth = __padding - outerLineWidth;
  176. ctx.save();
  177. ctx.lineWidth = outerLineWidth;
  178. ctx.beginPath();
  179. ctx.roundedRect(0, 0, width, height, cornerRadius, cornerRadius);
  180. ctx.roundedRect(outerLineWidth, outerLineWidth, width - outerLineWidth * 2, height - outerLineWidth * 2,
  181. cornerRadius - outerLineWidth, cornerRadius - outerLineWidth);
  182. ctx.clip();
  183. ctx.beginPath();
  184. ctx.rect(0, 0, width, height);
  185. var gradient = ctx.createLinearGradient(width / 2, 0, width / 2, height);
  186. gradient.addColorStop(0, "#33b3b3b3");
  187. gradient.addColorStop(1, "#4ce6e6e6");
  188. ctx.fillStyle = gradient;
  189. ctx.fill();
  190. ctx.restore();
  191. // The inner stroke must account for its corner radius.
  192. cornerRadius -= outerLineWidth;
  193. ctx.save();
  194. ctx.lineWidth = innerLineWidth;
  195. ctx.beginPath();
  196. ctx.roundedRect(outerLineWidth, outerLineWidth, width - outerLineWidth * 2, height - outerLineWidth * 2,
  197. cornerRadius, cornerRadius);
  198. ctx.roundedRect(outerLineWidth + innerLineWidth, outerLineWidth + innerLineWidth,
  199. width - outerLineWidth * 2 - innerLineWidth * 2, height - outerLineWidth * 2 - innerLineWidth * 2,
  200. cornerRadius - innerLineWidth, cornerRadius - innerLineWidth);
  201. ctx.clip();
  202. ctx.beginPath();
  203. ctx.rect(0, 0, width, height);
  204. gradient = ctx.createLinearGradient(width / 2, 0, width / 2, height);
  205. gradient.addColorStop(0, "#4c666666");
  206. gradient.addColorStop(1, "#40cccccc");
  207. ctx.fillStyle = gradient;
  208. ctx.fill();
  209. ctx.restore();
  210. }
  211. }
  212. /*!
  213. The delegate provides a template defining each item instantiated in the
  214. column. Each instance of this component has access to the following properties:
  215. \table
  216. \row \li \c {readonly property int} \b styleData.index
  217. \li The index of this delegate in the model.
  218. \row \li \c {readonly property int} \b styleData.column
  219. \li The index of the column that contains this item.
  220. \row \li \c {readonly property real} \b styleData.value
  221. \li The value for this delegate from the model.
  222. \row \li \c {readonly property bool} \b styleData.current
  223. \li \c true if this delegate is the current item.
  224. \row \li \c {readonly property real} \b styleData.displacement
  225. \li \c A value from \c {-visibleItemCount / 2} to
  226. \c {visibleItemCount / 2} which represents how far away
  227. this item is from being the current item, with \c 0 being
  228. completely current.
  229. For example, the item below will be 40% opaque when
  230. it is not the current item, and transition to 100%
  231. opacity when it becomes the current item:
  232. \code
  233. delegate: Text {
  234. text: styleData.value
  235. opacity: 0.4 + Math.max(0, 1 - Math.abs(styleData.displacement)) * 0.6
  236. }
  237. \endcode
  238. \row \li \c {readonly property bool} \b styleData.activeFocus
  239. \li \c true if the column that contains this item has active focus.
  240. \endtable
  241. Properties of the model are also available depending upon the type of
  242. \l {qml-data-models}{Data Model}.
  243. Delegates for items in specific columns can be defined using
  244. TumblerColumn's \l {TumblerColumn::delegate}{delegate} property, which
  245. will be used instead of this delegate.
  246. The \l {Item::}{implicitHeight} property must be set, and it must be
  247. the same for each delegate.
  248. */
  249. property Component delegate: Item {
  250. implicitHeight: (control.height - padding.top - padding.bottom) / tumblerStyle.visibleItemCount
  251. Text {
  252. id: label
  253. text: styleData.value
  254. color: "#666666"
  255. opacity: 0.4 + Math.max(0, 1 - Math.abs(styleData.displacement)) * 0.6
  256. font.pixelSize: Math.round(TextSingleton.font.pixelSize * 1.25)
  257. anchors.centerIn: parent
  258. }
  259. }
  260. /*!
  261. The delegate for the highlight of each column.
  262. Delegates for the highlight of specific columns can be defined using
  263. TumblerColumn's \l {TumblerColumn::highlight}{highlight} property,
  264. which will be used instead of this delegate.
  265. Each instance of this component has access to the following properties:
  266. \table
  267. \row \li \c {readonly property int} \b styleData.index
  268. \li The index of this column in the tumbler.
  269. \row \li \c {readonly property int} \b styleData.columnIndex
  270. \li The index of the column that contains this highlight.
  271. \row \li \c {readonly property bool} \b styleData.activeFocus
  272. \li \c true if the column that contains this highlight has active focus.
  273. \endtable
  274. */
  275. property Component highlight
  276. /*! \internal */
  277. property Component panel: Item {
  278. implicitWidth: {
  279. var w = (__separatorWidth * (control.columnCount - 1)) + tumblerStyle.padding.left + tumblerStyle.padding.right;
  280. for (var i = 0; i < control.columnCount; ++i)
  281. w += control.getColumn(i).width;
  282. return w;
  283. }
  284. implicitHeight: TextSingleton.implicitHeight * 10 + tumblerStyle.padding.top + tumblerStyle.padding.bottom
  285. }
  286. }