Border.qml 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2021 The Qt Company Ltd.
  4. ** Contact: https://www.qt.io/licensing/
  5. **
  6. ** This file is part of Qt Quick Studio Components.
  7. **
  8. ** $QT_BEGIN_LICENSE:GPL$
  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 General Public License Usage
  18. ** Alternatively, this file may be used under the terms of the GNU
  19. ** General Public License version 3 or (at your option) any later version
  20. ** approved by the KDE Free Qt Foundation. The licenses are as published by
  21. ** the Free Software Foundation and appearing in the file LICENSE.GPL3
  22. ** included in the packaging of this file. Please review the following
  23. ** information to ensure the GNU General Public License requirements will
  24. ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
  25. **
  26. ** $QT_END_LICENSE$
  27. **
  28. ****************************************************************************/
  29. import QtQuick 2.10
  30. import QtQuick.Shapes 1.0
  31. /*!
  32. \qmltype BorderItem
  33. \inqmlmodule QtQuick.Studio.Components
  34. \since QtQuick.Studio.Components 1.0
  35. \inherits Shape
  36. \brief A border drawn in four segments: left, top, right, and bottom.
  37. The Border type is used to create borders out of four segments: left,
  38. top, right, and bottom. The \l drawLeft, \l drawTop, \l drawRight, and
  39. \l drawBottom properties can be used to determine whether each of the
  40. segments is visible.
  41. The \l borderMode property determines whether the border is drawn along
  42. the inside or outside edge of the item, or on top of the edge.
  43. The \l radius property specifies whether the border corners are rounded.
  44. The radius can also be specified separately for each corner. Because this
  45. introduces curved edges to the corners, it may be appropriate to set the
  46. \c antialiasing property that is inherited from \l Item to improve the
  47. appearance of the border.
  48. The \l joinStyle property specifies how to connect two border line segments.
  49. The \l strokeColor, \l strokeWidth, and \l strokeStyle properties specify
  50. the appearance of the border line. The \l dashPattern and \l dashOffset
  51. properties specify the appearance of dashed lines.
  52. The \l capStyle property specifies whether line ends are square or
  53. rounded.
  54. \section2 Example Usage
  55. You can use the Border component in \QDS to create different kinds of
  56. borders.
  57. \image studio-border.png
  58. The QML code looks as follows:
  59. \code
  60. BorderItem {
  61. id: openLeft
  62. width: 99
  63. height: 99
  64. antialiasing: true
  65. drawLeft: false
  66. strokeColor: "gray"
  67. }
  68. BorderItem {
  69. id: openTop
  70. width: 99
  71. height: 99
  72. antialiasing: true
  73. strokeColor: "#808080"
  74. drawTop: false
  75. }
  76. BorderItem {
  77. id: asymmetricalCorners
  78. width: 99
  79. height: 99
  80. antialiasing: true
  81. bottomLeftRadius: 0
  82. topRightRadius: 0
  83. strokeColor: "#808080"
  84. }
  85. BorderItem {
  86. id: dashedBorder
  87. width: 99
  88. height: 99
  89. antialiasing: true
  90. strokeStyle: 4
  91. strokeColor: "#808080"
  92. }
  93. \endcode
  94. */
  95. Shape {
  96. id: root
  97. width: 200
  98. height: 150
  99. /*!
  100. The radius used to draw rounded corners.
  101. The default value is 10.
  102. If radius is non-zero, the corners will be rounded, otherwise they will
  103. be sharp. The radius can also be specified separately for each corner by
  104. using the \l bottomLeftRadius, \l bottomRightRadius, \l topLeftRadius, and
  105. \l topRightRadius properties.
  106. */
  107. property int radius: 10
  108. /*!
  109. The radius of the top left border corner.
  110. \sa radius
  111. */
  112. property int topLeftRadius: radius
  113. /*!
  114. The radius of the bottom left border corner.
  115. \sa radius
  116. */
  117. property int bottomLeftRadius: radius
  118. /*!
  119. The radius of the top right border corner.
  120. \sa radius
  121. */
  122. property int topRightRadius: radius
  123. /*!
  124. The radius of the bottom right border corner.
  125. \sa radius
  126. */
  127. property int bottomRightRadius: radius
  128. /*!
  129. Whether the border corner is beveled.
  130. */
  131. property bool bevel: false
  132. /*!
  133. The bevel of the top left border corner.
  134. \sa bevel
  135. */
  136. property bool topLeftBevel: bevel
  137. /*!
  138. The bevel of the top right border corner.
  139. \sa bevel
  140. */
  141. property bool topRightBevel: bevel
  142. /*!
  143. The bevel of the bottom right border corner.
  144. \sa bevel
  145. */
  146. property bool bottomRightBevel: bevel
  147. /*!
  148. The bevel of the bottom left border corner.
  149. \sa bevel
  150. */
  151. property bool bottomLeftBevel: bevel
  152. //property alias gradient: path.fillGradient
  153. /*!
  154. The style of the border line.
  155. \value ShapePath.SolidLine
  156. A solid line. This is the default value.
  157. \value ShapePath.DashLine
  158. Dashes separated by a few pixels.
  159. The \l dashPattern property specifies the dash pattern.
  160. \sa Qt::PenStyle
  161. */
  162. property alias strokeStyle: path.strokeStyle
  163. /*!
  164. The width of the border line.
  165. When set to a negative value, no line is drawn.
  166. The default value is 1.
  167. */
  168. property alias strokeWidth: path.strokeWidth
  169. /*!
  170. The color of the border line.
  171. When set to \c transparent, no line is drawn.
  172. The default value is \c red.
  173. \sa QColor
  174. */
  175. property alias strokeColor: path.strokeColor
  176. /*!
  177. The dash pattern of the border line specified as the dashes and the gaps
  178. between them.
  179. The dash pattern is specified in units of the pen's width. That is, a dash
  180. with the length 5 and width 10 is 50 pixels long.
  181. Each dash is also subject to cap styles, and therefore a dash of 1 with
  182. square cap set will extend 0.5 pixels out in each direction resulting in
  183. a total width of 2.
  184. The default \l capStyle is \c {ShapePath.SquareCap}, meaning that a square
  185. line end covers the end point and extends beyond it by half the line width.
  186. The default value is (4, 2), meaning a dash of 4 * \l strokeWidth pixels
  187. followed by a space of 2 * \l strokeWidth pixels.
  188. \sa QPen::setDashPattern()
  189. */
  190. property alias dashPattern: path.dashPattern
  191. /*!
  192. The join style used to connect two border line segments.
  193. \value ShapePath.MiterJoin
  194. The outer edges of the lines are extended to meet at an angle, and
  195. this area is filled.
  196. \value ShapePath.BevelJoin
  197. The triangular notch between the two lines is filled.
  198. This is the default value.
  199. \value ShapePath.RoundJoin
  200. A circular arc between the two lines is filled.
  201. \sa Qt::PenJoinStyle
  202. */
  203. property alias joinStyle: path.joinStyle
  204. /*!
  205. The starting point of the dash pattern for the border line.
  206. The offset is measured in terms of the units used to specify the dash
  207. pattern. For example, a pattern where each stroke is four units long,
  208. followed by a gap of two units, will begin with the stroke when drawn
  209. as a line. However, if the dash offset is set to 4.0, any line drawn
  210. will begin with the gap. Values of the offset up to 4.0 will cause part
  211. of the stroke to be drawn first, and values of the offset between 4.0 and
  212. 6.0 will cause the line to begin with part of the gap.
  213. The default value is 0.
  214. \sa QPen::setDashOffset()
  215. */
  216. property alias dashOffset: path.dashOffset
  217. /*!
  218. The cap style of the line.
  219. \value ShapePath.FlatCap
  220. A square line end that does not cover the end point of the line.
  221. \value ShapePath.SquareCap
  222. A square line end that covers the end point and extends beyond it
  223. by half the line width. This is the default value.
  224. \value ShapePath.RoundCap
  225. A rounded line end.
  226. \sa Qt::PenCapStyle
  227. */
  228. property alias capStyle: path.capStyle
  229. //property alias fillColor: path.fillColor
  230. /*!
  231. Whether the top border is visible.
  232. The border segment is drawn if this property is set to \c true.
  233. */
  234. property bool drawTop: true
  235. /*!
  236. Whether the bottom border is visible.
  237. The border segment is drawn if this property is set to \c true.
  238. */
  239. property bool drawBottom: true
  240. /*!
  241. Whether the right border is visible.
  242. The border segment is drawn if this property is set to \c true.
  243. */
  244. property bool drawRight: true
  245. /*!
  246. Whether the left border is visible.
  247. The border segment is drawn if this property is set to \c true.
  248. */
  249. property bool drawLeft: true
  250. layer.enabled: antialiasing
  251. layer.smooth: antialiasing
  252. layer.textureSize: Qt.size(width * 2, height * 2)
  253. /*!
  254. Where the border is drawn.
  255. \value Border.Inside
  256. The border is drawn along the inside edge of the item and does not
  257. affect the item width.
  258. This is the default value.
  259. \value Border.Middle
  260. The border is drawn over the edge of the item and does not
  261. affect the item width.
  262. \value Border.Outside
  263. The border is drawn along the outside edge of the item and increases
  264. the item width by the value of \l strokeWidth.
  265. \sa strokeWidth
  266. */
  267. property int borderMode: 0
  268. property real borderOffset: {
  269. if (root.borderMode === 0)
  270. return path.strokeWidth * 10.0 / 20.0
  271. if (root.borderMode === 1)
  272. return 0
  273. return -path.strokeWidth * 10.0 / 20.0
  274. }
  275. Item {
  276. anchors.fill: parent
  277. anchors.margins: {
  278. if (root.borderMode === 0)
  279. return 0
  280. if (root.borderMode === 1)
  281. return -root.strokeWidth / 2.0
  282. return -root.strokeWidth
  283. }
  284. }
  285. ShapePath {
  286. id: path
  287. joinStyle: ShapePath.MiterJoin
  288. strokeWidth: 1
  289. strokeColor: "red"
  290. fillColor: "transparent"
  291. startX: root.topLeftRadius + root.borderOffset
  292. startY: root.borderOffset
  293. }
  294. Item {
  295. id: shapes
  296. PathLine {
  297. x: root.width - root.topRightRadius - root.borderOffset
  298. y: root.borderOffset
  299. property bool add: root.drawTop
  300. }
  301. PathArc {
  302. x: root.width - root.borderOffset
  303. y: root.topRightRadius + root.borderOffset
  304. radiusX: topRightBevel ? 50000 : root.topRightRadius
  305. radiusY: topRightBevel ? 50000 : root.topRightRadius
  306. property bool add: root.drawTop && root.drawRight
  307. }
  308. PathMove {
  309. x: root.width - root.borderOffset
  310. y: root.topRightRadius + root.borderOffset
  311. property bool add: !root.drawTop
  312. }
  313. PathLine {
  314. x: root.width - root.borderOffset
  315. y: root.height - root.bottomRightRadius - root.borderOffset
  316. property bool add: root.drawRight
  317. }
  318. PathArc {
  319. x: root.width - root.bottomRightRadius - root.borderOffset
  320. y: root.height - root.borderOffset
  321. radiusX: bottomRightBevel ? 50000 : root.bottomRightRadius
  322. radiusY: bottomRightBevel ? 50000 : root.bottomRightRadius
  323. property bool add: root.drawRight && root.drawBottom
  324. }
  325. PathMove {
  326. x: root.width - root.bottomRightRadius - root.borderOffset
  327. y: root.height - root.borderOffset
  328. property bool add: !root.drawRight
  329. }
  330. PathLine {
  331. x: root.bottomLeftRadius + root.borderOffset
  332. y: root.height - root.borderOffset
  333. property bool add: root.drawBottom
  334. }
  335. PathArc {
  336. x: root.borderOffset
  337. y: root.height - root.bottomLeftRadius - root.borderOffset
  338. radiusX: bottomLeftBevel ? 50000 : root.bottomLeftRadius
  339. radiusY: bottomLeftBevel ? 50000 : root.bottomLeftRadius
  340. property bool add: root.drawBottom && root.drawLeft
  341. }
  342. PathMove {
  343. x: root.borderOffset
  344. y: root.height - root.bottomLeftRadius - root.borderOffset
  345. property bool add: !root.drawBottom
  346. }
  347. PathLine {
  348. x: root.borderOffset
  349. y: root.topLeftRadius + root.borderOffset
  350. property bool add: root.drawLeft
  351. }
  352. PathArc {
  353. x: root.topLeftRadius + root.borderOffset
  354. y: root.borderOffset
  355. radiusX: topLeftBevel ? 50000 : root.topLeftRadius
  356. radiusY: topLeftBevel ? 50000 : root.topLeftRadius
  357. property bool add: root.drawTop && root.drawLeft
  358. }
  359. }
  360. function invalidatePaths() {
  361. if (!root.__completed)
  362. return
  363. for (var i = 0; i < shapes.resources.length; i++) {
  364. var s = shapes.resources[i];
  365. if (s.add)
  366. path.pathElements.push(s)
  367. }
  368. }
  369. property bool __completed: false
  370. Component.onCompleted: {
  371. root.__completed = true
  372. invalidatePaths()
  373. }
  374. }