Table.qml 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. width: Math.max(1, Math.round(tableBase.width / headerRepeater.count))
  34. height: UM.Theme.getSize("section").height
  35. color: UM.Theme.getColor("secondary")
  36. Label
  37. {
  38. id: contentText
  39. anchors.left: parent.left
  40. anchors.leftMargin: UM.Theme.getSize("narrow_margin").width
  41. anchors.right: parent.right
  42. anchors.rightMargin: UM.Theme.getSize("narrow_margin").width
  43. text: modelData
  44. font: UM.Theme.getFont("medium_bold")
  45. color: UM.Theme.getColor("text")
  46. elide: Text.ElideRight
  47. }
  48. Rectangle //Resize handle.
  49. {
  50. anchors
  51. {
  52. right: parent.right
  53. top: parent.top
  54. bottom: parent.bottom
  55. }
  56. width: UM.Theme.getSize("thick_lining").width
  57. color: UM.Theme.getColor("thick_lining")
  58. MouseArea
  59. {
  60. anchors.fill: parent
  61. cursorShape: Qt.SizeHorCursor
  62. drag
  63. {
  64. target: parent
  65. axis: Drag.XAxis
  66. }
  67. onMouseXChanged:
  68. {
  69. if(drag.active)
  70. {
  71. let new_width = parent.parent.width + mouseX;
  72. let sum_widths = mouseX;
  73. for(let i = 0; i < headerBar.children.length; ++i)
  74. {
  75. sum_widths += headerBar.children[i].width;
  76. }
  77. if(sum_widths > tableBase.width)
  78. {
  79. new_width -= sum_widths - tableBase.width; //Limit the total width to not exceed the view.
  80. }
  81. let width_fraction = new_width / tableBase.width; //Scale with the same fraction along with the total width, if the table is resized.
  82. parent.parent.width = Qt.binding(function() { return tableBase.width * width_fraction });
  83. }
  84. }
  85. }
  86. }
  87. onWidthChanged:
  88. {
  89. tableView.forceLayout(); //Rescale table cells underneath as well.
  90. }
  91. }
  92. }
  93. }
  94. TableView
  95. {
  96. id: tableView
  97. anchors
  98. {
  99. top: headerBar.bottom
  100. left: parent.left
  101. right: parent.right
  102. bottom: parent.bottom
  103. }
  104. clip: true
  105. ScrollBar.vertical: ScrollBar
  106. {
  107. // Vertical ScrollBar, styled similarly to the scrollBar in the settings panel
  108. id: verticalScrollBar
  109. visible: tableView.contentHeight > tableView.height
  110. background: Rectangle
  111. {
  112. implicitWidth: UM.Theme.getSize("scrollbar").width
  113. radius: Math.round(implicitWidth / 2)
  114. color: UM.Theme.getColor("scrollbar_background")
  115. }
  116. contentItem: Rectangle
  117. {
  118. id: scrollViewHandle
  119. implicitWidth: UM.Theme.getSize("scrollbar").width
  120. radius: Math.round(implicitWidth / 2)
  121. color: verticalScrollBar.pressed ? UM.Theme.getColor("scrollbar_handle_down") : verticalScrollBar.hovered ? UM.Theme.getColor("scrollbar_handle_hover") : UM.Theme.getColor("scrollbar_handle")
  122. Behavior on color { ColorAnimation { duration: 50; } }
  123. }
  124. }
  125. columnWidthProvider: function(column)
  126. {
  127. return headerBar.children[column].width; //Cells get the same width as their column header.
  128. }
  129. delegate: Rectangle
  130. {
  131. implicitHeight: Math.max(1, cellContent.height)
  132. color: UM.Theme.getColor((tableBase.currentRow == row) ? "primary" : ((row % 2 == 0) ? "main_background" : "viewport_background"))
  133. Label
  134. {
  135. id: cellContent
  136. width: parent.width
  137. text: display
  138. verticalAlignment: Text.AlignVCenter
  139. elide: Text.ElideRight
  140. font: UM.Theme.getFont("default")
  141. color: UM.Theme.getColor("text")
  142. }
  143. TextMetrics
  144. {
  145. id: cellTextMetrics
  146. text: cellContent.text
  147. font: cellContent.font
  148. elide: cellContent.elide
  149. elideWidth: cellContent.width
  150. }
  151. UM.TooltipArea
  152. {
  153. anchors.fill: parent
  154. text: (cellTextMetrics.elidedText == cellContent.text) ? "" : cellContent.text //Show full text in tooltip if it was elided.
  155. onClicked:
  156. {
  157. if(tableBase.allowSelection)
  158. {
  159. tableBase.currentRow = row; //Select this row.
  160. }
  161. }
  162. onDoubleClicked:
  163. {
  164. tableBase.onDoubleClicked(row);
  165. }
  166. }
  167. }
  168. Connections
  169. {
  170. target: model
  171. function onRowCountChanged()
  172. {
  173. tableView.contentY = 0; //When the number of rows is reduced, make sure to scroll back to the start.
  174. }
  175. }
  176. }
  177. }