aboutsummaryrefslogtreecommitdiffstats
path: root/sources/pyside6/doc/tutorials/modelviewprogramming/qlistview-dnd.py
blob: 3a37cc0f3b849987786dab2ec45048a750a653b6 (plain)
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
# Copyright (C) 2022 The Qt Company Ltd.
# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause

import sys

from PySide6.QtWidgets import (QAbstractItemView, QApplication, QMainWindow,
                               QListView)
from PySide6.QtCore import (QByteArray, QDataStream, QIODevice, QMimeData,
                            QModelIndex, QStringListModel, Qt)


class DragDropListModel(QStringListModel):
    """A simple model that uses a QStringList as its data source."""

    def __init__(self, strings, parent=None):
          super().__init__(strings, parent)

#! [0]

    def canDropMimeData(self, data, action, row, column, parent):
        if not data.hasFormat("application/vnd.text.list"):
            return False

        if column > 0:
            return False

        return True
#! [0]
#! [1]
    def dropMimeData(self, data, action, row, column, parent):
        if not self.canDropMimeData(data, action, row, column, parent):
            return False

        if action == Qt.IgnoreAction:
            return True
#! [1]

#! [2]
        begin_row = 0

        if row != -1:
            begin_row = row
#! [2] #! [3]
        elif parent.isValid():
            begin_row = parent.row()
#! [3] #! [4]
        else:
            begin_row = self.rowCount(QModelIndex())
#! [4]

#! [5]
        encoded_data = data.data("application/vnd.text.list")
        stream = QDataStream(encoded_data, QIODevice.ReadOnly)
        new_items = []
        while not stream.atEnd():
            new_items.append(stream.readQString())
#! [5]

#! [6]
        self.insertRows(begin_row, len(new_items), QModelIndex())
        for text in new_items:
            idx = self.index(begin_row, 0, QModelIndex())
            self.setData(idx, text)
            begin_row += 1

        return True
#! [6]

#! [7]
    def flags(self, index):
        default_flags = super().flags(index)
        if index.isValid():
            return Qt.ItemIsDragEnabled | Qt.ItemIsDropEnabled | default_flags
        return Qt.ItemIsDropEnabled | default_flags
#! [7]

#! [8]
    def mimeData(self, indexes):
        mime_data = QMimeData()
        encoded_data = QByteArray()
        stream = QDataStream(encoded_data, QIODevice.WriteOnly)
        for index in indexes:
            if index.isValid():
                text = self.data(index, Qt.DisplayRole)
                stream.writeQString(text)

        mime_data.setData("application/vnd.text.list", encoded_data)
        return mime_data
#! [8]

#! [9]
    def mimeTypes(self):
        return ["application/vnd.text.list"]
#! [9]

#! [10]
    def supportedDropActions(self):
        return Qt.CopyAction | Qt.MoveAction
#! [10]


class MainWindow(QMainWindow):

    def __init__(self, parent=None):
        super().__init__(parent)

        file_menu = self.menuBar().addMenu("&File")
        quit_action = file_menu.addAction("E&xit")
        quit_action.setShortcut("Ctrl+Q")

#! [mainwindow0]
        self._list_view = QListView(self)
        self._list_view.setSelectionMode(QAbstractItemView.ExtendedSelection)
        self._list_view.setDragEnabled(True)
        self._list_view.setAcceptDrops(True)
        self._list_view.setDropIndicatorShown(True)
#! [mainwindow0]

        quit_action.triggered.connect(self.close)

        self.setup_list_items()

        self.setCentralWidget(self._list_view)
        self.setWindowTitle("List View")

    def setup_list_items(self):
        items = ["Oak", "Fir", "Pine", "Birch", "Hazel", "Redwood", "Sycamore",
                 "Chestnut", "Mahogany"]
        model = DragDropListModel(items, self)
        self._list_view.setModel(model)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec())