FluTreeView.qml 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. import QtQuick
  2. import QtQuick.Window
  3. import QtQuick.Layouts
  4. import QtQuick.Controls
  5. import FluentUI
  6. Item {
  7. enum TreeViewSelectionMode {
  8. None,
  9. Single,
  10. Multiple
  11. }
  12. property int selectionMode: FluTreeView.None
  13. property var currentElement
  14. property var currentParentElement
  15. property var rootModel: tree_model.get(0).items
  16. signal itemClicked(var item)
  17. id:root
  18. ListModel{
  19. id:tree_model
  20. ListElement{
  21. text: "根节点"
  22. expanded:true
  23. items:[]
  24. key:"123456"
  25. multipSelected:false
  26. multipIndex:0
  27. multipParentKey:""
  28. }
  29. }
  30. Component{
  31. id: delegate_root
  32. Column{
  33. width: calculateWidth()
  34. property var itemModel: model
  35. Repeater{
  36. id: repeater_first_level
  37. model: items
  38. delegate: delegate_items
  39. }
  40. function calculateWidth(){
  41. var w = 0;
  42. for(var i = 0; i < repeater_first_level.count; i++) {
  43. var child = repeater_first_level.itemAt(i)
  44. if(w < child.width_hint){
  45. w = child.width_hint;
  46. }
  47. }
  48. return w;
  49. }
  50. }
  51. }
  52. Component{
  53. id:delegate_items
  54. Column{
  55. id:item_layout
  56. property real level: (mapToItem(list_root,0,0).x+list_root.contentX)/0.001
  57. property var text: model.text??"Item"
  58. property bool hasChild : (model.items !== undefined) && (model.items.count !== 0)
  59. property var items: model.items??[]
  60. property var expanded: model.expanded??true
  61. property int width_hint: calculateWidth()
  62. property bool singleSelected: currentElement === model
  63. property var itemModel: model
  64. function calculateWidth(){
  65. var w = Math.max(list_root.width, item_layout_row.implicitWidth + 10);
  66. if(expanded){
  67. for(var i = 0; i < repeater_items.count; i++) {
  68. var child = repeater_items.itemAt(i)
  69. if(w < child.width_hint){
  70. w = child.width_hint;
  71. }
  72. }
  73. }
  74. return w;
  75. }
  76. Item{
  77. id:item_layout_rect
  78. width: list_root.contentWidth
  79. height: item_layout_row.implicitHeight
  80. Rectangle{
  81. anchors.fill: parent
  82. anchors.margins: 2
  83. color:{
  84. if(FluTheme.dark){
  85. if(item_layout.singleSelected && selectionMode === FluTreeView.Single){
  86. return Qt.rgba(62/255,62/255,62/255,1)
  87. }
  88. return (item_layout_mouse.containsMouse || item_layout_expanded.hovered || item_layout_checkbox.hovered)?Qt.rgba(62/255,62/255,62/255,1):Qt.rgba(0,0,0,0)
  89. }else{
  90. if(item_layout.singleSelected && selectionMode === FluTreeView.Single){
  91. return Qt.rgba(0,0,0,0.06)
  92. }
  93. return (item_layout_mouse.containsMouse || item_layout_expanded.hovered || item_layout_checkbox.hovered)?Qt.rgba(0,0,0,0.03):Qt.rgba(0,0,0,0)
  94. }
  95. }
  96. Rectangle{
  97. width: 3
  98. color:FluTheme.primaryColor.dark
  99. visible: item_layout.singleSelected && (selectionMode === FluTreeView.Single)
  100. radius: 3
  101. height: 20
  102. anchors{
  103. left: parent.left
  104. verticalCenter: parent.verticalCenter
  105. }
  106. }
  107. MouseArea{
  108. id:item_layout_mouse
  109. anchors.fill: parent
  110. hoverEnabled: true
  111. onClicked: {
  112. item_layout_rect.onClickItem()
  113. }
  114. }
  115. }
  116. function onClickItem(){
  117. if(selectionMode === FluTreeView.None){
  118. itemClicked(model)
  119. }
  120. if(selectionMode === FluTreeView.Single){
  121. currentElement = model
  122. if(item_layout.parent.parent.parent.itemModel){
  123. currentParentElement = item_layout.parent.parent.parent.itemModel
  124. }else{
  125. if(item_layout.parent.itemModel){
  126. currentParentElement = item_layout.parent.itemModel
  127. }
  128. }
  129. }
  130. if(selectionMode === FluTreeView.Multiple){
  131. }
  132. }
  133. RowLayout{
  134. id:item_layout_row
  135. anchors.verticalCenter: item_layout_rect.verticalCenter
  136. Item{
  137. width: 15*level
  138. Layout.alignment: Qt.AlignVCenter
  139. }
  140. FluCheckBox{
  141. id:item_layout_checkbox
  142. text:""
  143. selected: itemModel.multipSelected
  144. visible: selectionMode === FluTreeView.Multiple
  145. Layout.leftMargin: 5
  146. function refreshCheckBox(){
  147. const stack = [tree_model.get(0)];
  148. const result = [];
  149. while (stack.length > 0) {
  150. const curr = stack.pop();
  151. result.unshift(curr);
  152. if (curr.items) {
  153. for(var i=0 ; i<curr.items.count ; i++){
  154. curr.items.setProperty(i,"multipIndex",i)
  155. curr.items.setProperty(i,"multipParentKey",curr.key)
  156. stack.push(curr.items.get(i));
  157. }
  158. }
  159. }
  160. for(var j=0 ; j<result.length-1 ; j++){
  161. var item = result[j]
  162. let obj = result.find(function(o) {
  163. return o.key === item.multipParentKey;
  164. });
  165. if((item.items !== undefined) && (item.items.count !== 0)){
  166. var items = item.items
  167. for(var k=0 ; k<items.count ; k++){
  168. if(items.get(k).multipSelected === false){
  169. obj.items.setProperty(item.multipIndex,"multipSelected",false)
  170. break
  171. }
  172. obj.items.setProperty(item.multipIndex,"multipSelected",true)
  173. }
  174. }
  175. }
  176. }
  177. clickFunc:function(){
  178. if(hasChild){
  179. const stack = [itemModel];
  180. while (stack.length > 0) {
  181. const curr = stack.pop();
  182. if (curr.items) {
  183. for(var i=0 ; i<curr.items.count ; i++){
  184. curr.items.setProperty(i,"multipSelected",!itemModel.multipSelected)
  185. stack.push(curr.items.get(i));
  186. }
  187. }
  188. }
  189. refreshCheckBox()
  190. }else{
  191. itemModel.multipSelected = !itemModel.multipSelected
  192. refreshCheckBox()
  193. }
  194. }
  195. }
  196. FluIconButton{
  197. id:item_layout_expanded
  198. color:"#00000000"
  199. iconSource:item_layout.expanded?FluentIcons.ChevronDown:FluentIcons.ChevronRight
  200. opacity: item_layout.hasChild
  201. iconSize: 15
  202. onClicked: {
  203. if(!item_layout.hasChild){
  204. item_layout_rect.onClickItem()
  205. return
  206. }
  207. model.expanded = !model.expanded
  208. }
  209. }
  210. FluText {
  211. text: item_layout.text
  212. Layout.alignment: Qt.AlignVCenter
  213. topPadding: 7
  214. bottomPadding: 7
  215. }
  216. }
  217. }
  218. Item{
  219. id:item_sub
  220. visible: {
  221. if(!hasChild){
  222. return false
  223. }
  224. return item_layout.expanded??false
  225. }
  226. width: item_sub_layout.implicitWidth
  227. height: item_sub_layout.implicitHeight
  228. x:0.001
  229. Column{
  230. id: item_sub_layout
  231. Repeater{
  232. id:repeater_items
  233. model: item_layout.items
  234. delegate: delegate_items
  235. }
  236. }
  237. }
  238. }
  239. }
  240. ListView {
  241. id: list_root
  242. anchors.fill: parent
  243. delegate: delegate_root
  244. contentWidth: contentItem.childrenRect.width
  245. model: tree_model
  246. flickableDirection: Flickable.HorizontalAndVerticalFlick
  247. clip: true
  248. ScrollBar.vertical: FluScrollBar {}
  249. ScrollBar.horizontal: FluScrollBar { }
  250. }
  251. function updateData(items){
  252. rootModel.clear()
  253. rootModel.append(items)
  254. }
  255. function signleData(){
  256. return currentElement
  257. }
  258. function multipData(){
  259. const stack = [tree_model.get(0)];
  260. const result = [];
  261. while (stack.length > 0) {
  262. const curr = stack.pop();
  263. if(curr.multipSelected){
  264. result.push(curr)
  265. }
  266. for(var i=0 ; i<curr.items.count ; i++){
  267. stack.push(curr.items.get(i));
  268. }
  269. }
  270. return result
  271. }
  272. function createItem(text="Title",expanded=true,items=[]){
  273. return {text:text,expanded:expanded,items:items,key:uniqueRandom(),multipSelected:false,multipIndex:0,multipParentKey:""};
  274. }
  275. function uniqueRandom() {
  276. var timestamp = Date.now();
  277. var random = Math.floor(Math.random() * 1000000);
  278. return timestamp.toString() + random.toString();
  279. }
  280. }