File: TableViewPage.qml

package info (click to toggle)
kirigami-addons 1.11.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 9,324 kB
  • sloc: ansic: 31,525; cpp: 3,607; xml: 82; java: 76; makefile: 4; sh: 1
file content (147 lines) | stat: -rw-r--r-- 4,736 bytes parent folder | download | duplicates (2)
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
142
143
144
145
146
147
/*
 * 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.BookTableModel

import Qt.labs.qmlmodels

Kirigami.Page {
    id: root

    title: i18nc("@title:group", "Table View for QAbstractTableModel")

    topPadding: 0
    leftPadding: 0
    bottomPadding: 0
    rightPadding: 0

    Kirigami.Theme.colorSet: Kirigami.Theme.View
    Kirigami.Theme.inherit: false

    contentItem: QQC2.ScrollView {
        contentItem: Tables.KTableView {
            id: view
            model: bookTableModel

            interactive: false
            clip: true
            alternatingRows: false

            sortOrder: Qt.AscendingOrder
            sortRole: BookRoles.YearRole

            onColumnClicked: function (index, headerComponent) {
                if (view.sortRole !== headerComponent.role) {
                    view.sortRole = index;
                    view.sortOrder = Qt.AscendingOrder;
                } else {
                    view.sortOrder = view.sortOrder === Qt.AscendingOrder ? Qt.DescendingOrder : Qt.AscendingOrder
                }

                view.model.sort(view.sortRole, 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 currentRow = view.selectionModel.currentIndex.row;
                let currentColumn = view.selectionModel.currentIndex.column;

                view.selectionModel.clear();
                for (let i in selectedIndexes) {
                    view.selectionModel.select(selectedIndexes[i], ItemSelectionModel.Select);
                }

                view.selectionModel.setCurrentIndex(view.model.index(currentRow, currentColumn), 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

                    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
                },

                Tables.HeaderComponent {
                    width: 100
                    title: i18nc("@title:column", "Rating")
                    textRole: "rating"
                    role: BookRoles.RatingRole

                    leading: Kirigami.Icon {
                        source: "star-shape"
                        implicitWidth: view.compact ? Kirigami.Units.iconSizes.small : Kirigami.Units.iconSizes.medium
                        implicitHeight: implicitWidth
                    }
                }
            ]
        }
    }

    TableModel {
        id: __exampleModel
        TableModelColumn { display: "title" }
        TableModelColumn { display: "author" }
        TableModelColumn { display: "year" }
        TableModelColumn { display: "rating" }

        rows: [
            {
                title: "Harry Potter and the Philosopher's Stone",
                author: "J.K. Rowling",
                year: 1997,
                rating: 4.5
            },
            {
                title: "Harry Potter and the Philosopher's Stone",
                author: "J.K. Rowling",
                year: 1997,
                rating: 4.5
            },
            {
                title: "Harry Potter and the Philosopher's Stone",
                author: "J.K. Rowling",
                year: 1997,
                rating: 4.5
            },
            {
                title: "Harry Potter and the Philosopher's Stone",
                author: "J.K. Rowling",
                year: 1997,
                rating: 4.5
            },
        ]
    }
}