Advertisement
Guest User

Untitled

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