TableView.qml 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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.5 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. property string sectionRole: ""
  25. Row
  26. {
  27. id: headerBar
  28. Repeater
  29. {
  30. id: headerRepeater
  31. model: columnHeaders
  32. Rectangle
  33. {
  34. width: Math.max(1, Math.round(tableBase.width / headerRepeater.count))
  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; //Don't go smaller than 10 pixels, to make sure you can still scale it back.
  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 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. clip: true
  106. ScrollBar.vertical: UM.ScrollBar {}
  107. columnWidthProvider: function(column)
  108. {
  109. return headerBar.children[column].width; //Cells get the same width as their column header.
  110. }
  111. delegate: Rectangle
  112. {
  113. implicitHeight: Math.max(1, cellContent.height)
  114. color: UM.Theme.getColor((tableBase.currentRow == row) ? "primary" : ((row % 2 == 0) ? "main_background" : "viewport_background"))
  115. Label
  116. {
  117. id: cellContent
  118. width: parent.width
  119. text: display
  120. verticalAlignment: Text.AlignVCenter
  121. elide: Text.ElideRight
  122. font: UM.Theme.getFont("default")
  123. color: UM.Theme.getColor("text")
  124. }
  125. TextMetrics
  126. {
  127. id: cellTextMetrics
  128. text: cellContent.text
  129. font: cellContent.font
  130. elide: cellContent.elide
  131. elideWidth: cellContent.width
  132. }
  133. UM.TooltipArea
  134. {
  135. anchors.fill: parent
  136. text: (cellTextMetrics.elidedText == cellContent.text) ? "" : cellContent.text //Show full text in tooltip if it was elided.
  137. onClicked:
  138. {
  139. if(tableBase.allowSelection)
  140. {
  141. tableBase.currentRow = row; //Select this row.
  142. }
  143. }
  144. onDoubleClicked:
  145. {
  146. tableBase.onDoubleClicked(row);
  147. }
  148. }
  149. }
  150. Connections
  151. {
  152. target: model
  153. function onRowCountChanged()
  154. {
  155. tableView.contentY = 0; //When the number of rows is reduced, make sure to scroll back to the start.
  156. }
  157. }
  158. }
  159. Connections
  160. {
  161. target: model
  162. function onRowsChanged()
  163. {
  164. let first_column = model.columns[0].display;
  165. if(model.rows.length > 0 && model.rows[0][first_column].startsWith("<b>")) //First item is already a section header.
  166. {
  167. return; //Assume we already added section headers. Prevent infinite recursion.
  168. }
  169. if(sectionRole === "" || model.rows.length == 0) //No section headers, or no items at all.
  170. {
  171. tableView.model.rows = model.rows;
  172. return;
  173. }
  174. //Insert section headers in the rows.
  175. let last_section = "";
  176. let new_rows = [];
  177. for(let i = 0; i < model.rows.length; ++i)
  178. {
  179. let item_section = model.rows[i][sectionRole];
  180. if(item_section !== last_section) //Starting a new section.
  181. {
  182. let section_header = {};
  183. for(let key in model.rows[i])
  184. {
  185. section_header[key] = (key === first_column) ? "<b>" + item_section + "</b>" : ""; //Put the section header in the first column.
  186. }
  187. new_rows.push(section_header); //Add a row representing a section header.
  188. last_section = item_section;
  189. }
  190. new_rows.push(model.rows[i]);
  191. }
  192. tableView.model.rows = new_rows;
  193. }
  194. }
  195. }