1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
|
/*
* Copyright 2023 Evgeny Chesnokov <echesnokov@astralinux.ru>
* SPDX-License-Identifier: LGPL-2.0-or-later
*/
import QtQuick
import QtQuick.Controls as QQC2
import org.kde.kirigami as Kirigami
import org.kde.kirigamiaddons.tableview as Tables
import org.kde.kirigamiaddons.BookListModel
Kirigami.Page {
id: root
title: i18nc("@title:group", "Table View for QAbstractListModel")
topPadding: 0
leftPadding: 0
bottomPadding: 0
rightPadding: 0
Kirigami.Theme.colorSet: Kirigami.Theme.View
Kirigami.Theme.inherit: false
contentItem: QQC2.ScrollView {
Tables.ListTableView {
id: view
model: bookListModel
clip: true
sortOrder: Qt.AscendingOrder
sortRole: bookListModel.sortRole
onColumnClicked: function (index, headerComponent) {
if (view.model.sortRole !== headerComponent.role) {
view.model.sortRole = headerComponent.role;
view.sortOrder = Qt.AscendingOrder;
} else {
view.sortOrder = view.sortOrder === Qt.AscendingOrder ? Qt.DescendingOrder : Qt.AscendingOrder
}
view.model.sort(0, view.sortOrder);
// After sorting we need update selection
__resetSelection();
}
function __resetSelection() {
// NOTE: Making a forced copy of the list
let selectedIndexes = Array(...view.selectionModel.selectedIndexes)
let currentIndex = view.selectionModel.currentIndex.row;
view.selectionModel.clear();
for (let i in selectedIndexes) {
view.selectionModel.select(selectedIndexes[i], ItemSelectionModel.Select);
}
view.selectionModel.setCurrentIndex(view.model.index(currentIndex, 0), ItemSelectionModel.Select);
}
headerComponents: [
Tables.HeaderComponent {
width: 200
title: i18nc("@title:column", "Book")
textRole: "title"
role: BookRoles.TitleRole
},
Tables.HeaderComponent {
width: 200
title: i18nc("@title:column", "Author")
textRole: "author"
role: BookRoles.AuthorRole
draggable: true
leading: Kirigami.Icon {
source: "social"
implicitWidth: view.compact ? Kirigami.Units.iconSizes.small : Kirigami.Units.iconSizes.medium
implicitHeight: implicitWidth
}
},
Tables.HeaderComponent {
width: 100
title: i18nc("@title:column", "Year")
textRole: "year"
role: BookRoles.YearRole
draggable: true
},
Tables.HeaderComponent {
width: 100
title: i18nc("@title:column", "Rating")
textRole: "rating"
role: BookRoles.RatingRole
draggable: true
leading: Kirigami.Icon {
source: "star-shape"
implicitWidth: view.compact ? Kirigami.Units.iconSizes.small : Kirigami.Units.iconSizes.medium
implicitHeight: implicitWidth
}
}
]
}
}
ListModel {
id: __exampleModel
ListElement {
title: "Harry Potter and the Philosopher's Stone"
author: "J.K. Rowling"
year: 1997
rating: 4.5
}
ListElement {
title: "Harry Potter and the Philosopher's Stone"
author: "J.K. Rowling"
year: 1997
rating: 4.5
}
ListElement {
title: "Harry Potter and the Philosopher's Stone"
author: "J.K. Rowling"
year: 1997
rating: 4.5
}
ListElement {
title: "Harry Potter and the Philosopher's Stone"
author: "J.K. Rowling"
year: 1997
rating: 4.5
}
}
}
|