Table.qml 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. //Copyright (C) 2022 Ultimaker B.V.
  2. //Cura is released under the terms of the LGPLv3 or higher.
  3. import Qt.labs.qmlmodels 1.0
  4. import QtQuick 2.15
  5. import QtQuick.Controls 2.15
  6. import UM 1.2 as UM
  7. /*
  8. * A re-sizeable table of data.
  9. *
  10. * This table combines a list of headers with a TableView to show certain roles in a table.
  11. * The columns of the table can be resized.
  12. * When the table becomes too big, you can scroll through the table. When a column becomes too small, the contents of
  13. * the table are elided.
  14. * The table gets Cura's themeing.
  15. */
  16. Item
  17. {
  18. id: tableBase
  19. required property var columnHeaders //The text to show in the headers of each column.
  20. property alias model: tableView.model //A TableModel to display in this table. To use a ListModel for the rows, use "rows: listModel.items"
  21. property int currentRow: -1 //The selected row index.
  22. property var onDoubleClicked: function(row) {} //Something to execute when double clicked. Accepts one argument: The index of the row that was clicked on.
  23. property bool allowSelection: true //Whether to allow the user to select items.
  24. Row
  25. {
  26. id: headerBar
  27. Repeater
  28. {
  29. id: headerRepeater
  30. model: columnHeaders
  31. Rectangle
  32. {
  33. //minimumWidth: Math.max(1, Math.round(tableBase.width / headerRepeater.count))
  34. width: 300
  35. height: UM.Theme.getSize("section").height
  36. color: UM.Theme.getColor("secondary")
  37. Label
  38. {
  39. id: contentText
  40. anchors.left: parent.left
  41. anchors.leftMargin: UM.Theme.getSize("narrow_margin").width
  42. anchors.right: parent.right
  43. anchors.rightMargin: UM.Theme.getSize("narrow_margin").width
  44. text: modelData
  45. font: UM.Theme.getFont("medium_bold")
  46. color: UM.Theme.getColor("text")
  47. elide: Text.ElideRight
  48. }
  49. Rectangle //Resize handle.
  50. {
  51. anchors
  52. {
  53. right: parent.right
  54. top: parent.top
  55. bottom: parent.bottom
  56. }
  57. width: UM.Theme.getSize("thick_lining").width
  58. color: UM.Theme.getColor("thick_lining")
  59. MouseArea
  60. {
  61. anchors.fill: parent
  62. cursorShape: Qt.SizeHorCursor
  63. drag
  64. {
  65. target: parent
  66. axis: Drag.XAxis
  67. }
  68. onMouseXChanged:
  69. {
  70. if(drag.active)
  71. {
  72. let new_width = parent.parent.width + mouseX;
  73. let sum_widths = mouseX;
  74. for(let i = 0; i < headerBar.children.length; ++i)
  75. {
  76. sum_widths += headerBar.children[i].width;
  77. }
  78. if(sum_widths > tableBase.width)
  79. {
  80. new_width -= sum_widths - tableBase.width; //Limit the total width to not exceed the view.
  81. }
  82. let width_fraction = new_width / tableBase.width; //Scale with the same fraction along with the total width, if the table is resized.
  83. parent.parent.width = Qt.binding(function() { return Math.max(10, Math.round(tableBase.width * width_fraction)) });
  84. }
  85. }
  86. }
  87. }
  88. onWidthChanged:
  89. {
  90. tableView.forceLayout(); //Rescale table cells underneath as well.
  91. }
  92. }
  93. }
  94. }
  95. TableView
  96. {
  97. id: tableView
  98. anchors
  99. {
  100. top: headerBar.bottom
  101. left: parent.left
  102. right: parent.right
  103. bottom: parent.bottom
  104. }
  105. flickableDirection: Flickable.AutoFlickIfNeeded
  106. clip: true
  107. ScrollBar.vertical: ScrollBar
  108. {
  109. // Vertical ScrollBar, styled similarly to the scrollBar in the settings panel
  110. id: verticalScrollBar
  111. visible: tableView.contentHeight > tableView.height
  112. background: Rectangle
  113. {
  114. implicitWidth: UM.Theme.getSize("scrollbar").width
  115. radius: Math.round(implicitWidth / 2)
  116. color: UM.Theme.getColor("scrollbar_background")
  117. }
  118. contentItem: Rectangle
  119. {
  120. id: scrollViewHandle
  121. implicitWidth: UM.Theme.getSize("scrollbar").width
  122. radius: Math.round(implicitWidth / 2)
  123. color: verticalScrollBar.pressed ? UM.Theme.getColor("scrollbar_handle_down") : verticalScrollBar.hovered ? UM.Theme.getColor("scrollbar_handle_hover") : UM.Theme.getColor("scrollbar_handle")
  124. Behavior on color { ColorAnimation { duration: 50; } }
  125. }
  126. }
  127. columnWidthProvider: function(column)
  128. {
  129. return headerBar.children[column].width; //Cells get the same width as their column header.
  130. }
  131. delegate: Rectangle
  132. {
  133. implicitHeight: Math.max(1, cellContent.height)
  134. color: UM.Theme.getColor((tableBase.currentRow == row) ? "primary" : ((row % 2 == 0) ? "main_background" : "viewport_background"))
  135. Label
  136. {
  137. id: cellContent
  138. width: parent.width
  139. text: display
  140. verticalAlignment: Text.AlignVCenter
  141. elide: Text.ElideRight
  142. font: UM.Theme.getFont("default")
  143. color: UM.Theme.getColor("text")
  144. }
  145. TextMetrics
  146. {
  147. id: cellTextMetrics
  148. text: cellContent.text
  149. font: cellContent.font
  150. elide: cellContent.elide
  151. elideWidth: cellContent.width
  152. }
  153. UM.TooltipArea
  154. {
  155. anchors.fill: parent
  156. acceptedButtons: Qt.LeftButton
  157. text: (cellTextMetrics.elidedText == cellContent.text) ? "" : cellContent.text //Show full text in tooltip if it was elided.
  158. onClicked:
  159. {
  160. if(tableBase.allowSelection)
  161. {
  162. tableBase.currentRow = row; //Select this row.
  163. }
  164. }
  165. onDoubleClicked:
  166. {
  167. tableBase.onDoubleClicked(row);
  168. }
  169. }
  170. }
  171. Connections
  172. {
  173. target: model
  174. function onRowCountChanged()
  175. {
  176. tableView.contentY = 0; //When the number of rows is reduced, make sure to scroll back to the start.
  177. }
  178. }
  179. }
  180. }