TableView.qml 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. Row
  25. {
  26. id: headerBar
  27. Repeater
  28. {
  29. id: headerRepeater
  30. model: columnHeaders
  31. Rectangle
  32. {
  33. width: 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. parent.parent.width = Math.max(10, parent.parent.width + mouseX); //Don't go smaller than 10 pixels, to make sure you can still scale it back.
  72. let sum_widths = 0;
  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. parent.parent.width -= sum_widths - tableBase.width; //Limit the total width to not exceed the view.
  80. }
  81. }
  82. tableView.forceLayout();
  83. }
  84. }
  85. }
  86. }
  87. }
  88. }
  89. TableView
  90. {
  91. id: tableView
  92. anchors
  93. {
  94. top: headerBar.bottom
  95. left: parent.left
  96. right: parent.right
  97. bottom: parent.bottom
  98. }
  99. clip: true
  100. ScrollBar.vertical: UM.ScrollBar {}
  101. columnWidthProvider: function(column)
  102. {
  103. return headerBar.children[column].width; //Cells get the same width as their column header.
  104. }
  105. delegate: Rectangle
  106. {
  107. implicitHeight: Math.max(1, cellContent.height)
  108. color: UM.Theme.getColor((tableBase.currentRow == row) ? "primary" : ((row % 2 == 0) ? "main_background" : "viewport_background"))
  109. Label
  110. {
  111. id: cellContent
  112. width: parent.width
  113. text: display
  114. verticalAlignment: Text.AlignVCenter
  115. elide: Text.ElideRight
  116. font: UM.Theme.getFont("default")
  117. color: UM.Theme.getColor("text")
  118. }
  119. TextMetrics
  120. {
  121. id: cellTextMetrics
  122. text: cellContent.text
  123. font: cellContent.font
  124. elide: cellContent.elide
  125. elideWidth: cellContent.width
  126. }
  127. UM.TooltipArea
  128. {
  129. anchors.fill: parent
  130. text: (cellTextMetrics.elidedText == cellContent.text) ? "" : cellContent.text //Show full text in tooltip if it was elided.
  131. onClicked:
  132. {
  133. if(tableBase.allowSelection)
  134. {
  135. tableBase.currentRow = row; //Select this row.
  136. }
  137. }
  138. onDoubleClicked:
  139. {
  140. tableBase.onDoubleClicked(row);
  141. }
  142. }
  143. }
  144. Connections
  145. {
  146. target: model
  147. function onRowCountChanged()
  148. {
  149. tableView.contentY = 0; //When the number of rows is reduced, make sure to scroll back to the start.
  150. }
  151. }
  152. }
  153. }