Advertisement
Guest User

Untitled

a guest
Feb 2nd, 2021
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
QML 5.19 KB | None | 0 0
  1. import QtQuick 2.12
  2. import QtQuick.Window 2.12
  3. import QtQuick.Controls 2.12
  4. import QtQml.Models 2.12
  5.  
  6. ApplicationWindow {
  7.     id: root
  8.     width: 400
  9.     height: 500
  10.     visible: true
  11.     title: qsTr("Hello World")
  12.  
  13.     property bool isGridStyle: enableGrid.checked
  14.     property int selectedIndex: 0
  15.  
  16.     header: ToolBar {
  17.         ToolButton {
  18.             id: enableGrid
  19.             text: checked ? "To List" : "To Grid"
  20.             checkable: true
  21.         }
  22.     }
  23.  
  24.     ListModel {
  25.         id: sourceModel
  26.         ListElement {
  27.             name: "test1"
  28.             color: "red"
  29.         }
  30.         ListElement {
  31.             name: "test2"
  32.             color: "green"
  33.         }
  34.         ListElement {
  35.             name: "test3"
  36.             color: "blue"
  37.         }
  38.         ListElement {
  39.             name: "test1"
  40.             color: "red"
  41.         }
  42.         ListElement {
  43.             name: "test2"
  44.             color: "green"
  45.         }
  46.         ListElement {
  47.             name: "test3"
  48.             color: "blue"
  49.         }
  50.         ListElement {
  51.             name: "test1"
  52.             color: "red"
  53.         }
  54.         ListElement {
  55.             name: "test2"
  56.             color: "green"
  57.         }
  58.         ListElement {
  59.             name: "test3"
  60.             color: "blue"
  61.         }
  62.     }
  63.  
  64.     Component {
  65.         id: cellComponent
  66.         Rectangle {
  67.             id: cell
  68.             width: root.isGridStyle ? 50 : 100
  69.             height: root.isGridStyle ? 50 : 34
  70.  
  71.             Behavior on width {
  72.                 NumberAnimation { duration: 1000 }
  73.             }
  74.             Behavior on height {
  75.                 NumberAnimation { duration: 1000 }
  76.             }
  77.             Behavior on x {
  78.                 NumberAnimation { duration: 1000 }
  79.             }
  80.             Behavior on y {
  81.                 NumberAnimation { duration: 1000 }
  82.             }
  83.             color: "#00000000"
  84.             border.width: 2
  85.             border.color: model.index % 2 ? "lightgray" : "gray"
  86.             function updateAnchors() {
  87.                 if (root.isGridStyle) {
  88.                     colorRect.anchors.verticalCenter = undefined
  89.                     colorRect.anchors.left = undefined
  90.                     nameLabel.anchors.verticalCenter = undefined
  91.                     nameLabel.anchors.left = undefined
  92.  
  93.                     colorRect.anchors.horizontalCenter = cell.horizontalCenter
  94.                     colorRect.anchors.top = cell.top
  95.                     nameLabel.anchors.horizontalCenter = cell.horizontalCenter
  96.                     nameLabel.anchors.top = colorRect.bottom
  97.                 } else {
  98.                     colorRect.anchors.horizontalCenter = undefined
  99.                     colorRect.anchors.top.horizontalCenter = undefined
  100.                     nameLabel.anchors.horizontalCenter = undefined
  101.                     nameLabel.anchors.top = undefined
  102.  
  103.                     colorRect.anchors.verticalCenter = cell.verticalCenter
  104.                     colorRect.anchors.left = cell.left
  105.                     nameLabel.anchors.verticalCenter = cell.verticalCenter
  106.                     nameLabel.anchors.left = colorRect.right
  107.                 }
  108.             }
  109.             Component.onCompleted: updateAnchors()
  110.             Connections {
  111.                 target: root
  112.                 onIsGridStyleChanged: {
  113.                     updateAnchors()
  114.                 }
  115.             }
  116.             Rectangle {
  117.                 id: colorRect
  118.                 color: model.color
  119.                 width: 30
  120.                 height: 30
  121.                 anchors.margins: 2
  122.             }
  123.             Label {
  124.                 id: nameLabel
  125.                 anchors.margins: 2
  126.                 text: model.name
  127.             }
  128.             MouseArea {
  129.                 anchors.fill: parent
  130.                 onClicked: {
  131.                     root.selectedIndex = model.index
  132.                 }
  133.             }
  134.         }
  135.     }
  136.  
  137.     Rectangle {
  138.         id: highlightBar
  139.         width: viewLoader.item.currentItem.width
  140.         height: viewLoader.item.currentItem.height
  141.         color: "lightblue"
  142.         y: viewLoader.item.currentItem.y
  143.         x: viewLoader.item.currentItem.x
  144.         Behavior on y { NumberAnimation { duration: 300 } }
  145.         Behavior on x { NumberAnimation { duration: 300 } }
  146.         Behavior on width { NumberAnimation { duration: 300 } }
  147.         Behavior on height { NumberAnimation { duration: 300 } }
  148.     }
  149.  
  150.     Component {
  151.         id: listViewComponent
  152.         ListView {
  153.             anchors.fill: parent
  154.             model: sourceModel
  155.             delegate: cellComponent
  156.             highlight: highlightBar
  157.             highlightFollowsCurrentItem: false
  158.             currentIndex: root.selectedIndex
  159.         }
  160.     }
  161.  
  162.     Component {
  163.         id: gridViewComponent
  164.         GridView {
  165.             anchors.fill: parent
  166.             model: sourceModel
  167.             delegate: cellComponent
  168.             highlight: highlightBar
  169.             highlightFollowsCurrentItem: false
  170.             currentIndex: root.selectedIndex
  171.         }
  172.     }
  173.  
  174.     Loader {
  175.         id: viewLoader
  176.         anchors.fill: parent
  177.         sourceComponent: isGridStyle ? gridViewComponent : listViewComponent
  178.  
  179.     }
  180.  
  181. }
  182.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement