ClipboardPage.qml 6.49 KB
Newer Older
1
/*
2
3
    SPDX-FileCopyrightText: 2014 Martin Gräßlin <mgraesslin@kde.org>
    SPDX-FileCopyrightText: 2014 Kai Uwe Broulik <kde@privat.broulik.de>
4

5
    SPDX-License-Identifier: GPL-2.0-or-later
6
*/
7

8
import QtQuick 2.4
9
import QtQuick.Layouts 1.1
10

11
12
import org.kde.plasma.plasmoid 2.0
import org.kde.plasma.core 2.0 as PlasmaCore
13
import org.kde.plasma.components 3.0 as PlasmaComponents3
14
import org.kde.plasma.extras 2.0 as PlasmaExtras
15

16
17
Menu {
    id: clipboardMenu
18
    Keys.onPressed: {
19
        function forwardToFilter() {
20
            if (filter.enabled && event.text !== "" && !filter.activeFocus) {
21
22
23
24
25
26
27
28
29
30
31
                clipboardMenu.view.currentIndex = -1
                if (event.matches(StandardKey.Paste)) {
                    filter.paste();
                } else {
                    filter.text = "";
                    filter.text += event.text;
                }
                filter.forceActiveFocus();
                event.accepted = true;
            }
        }
32
        if (stack.currentItem !== clipboardMenu) {
33
34
35
            event.accepted = false;
            return;
        }
36
37
38
39
40
41
42
        switch(event.key) {
            case Qt.Key_Enter:
            case Qt.Key_Return: {
                if (clipboardMenu.view.currentIndex >= 0) {
                    var uuid = clipboardMenu.model.get(clipboardMenu.view.currentIndex).UuidRole
                    if (uuid) {
                        clipboardSource.service(uuid, "select")
43
44
45
                        if (plasmoid.hideOnWindowDeactivate) {
                            plasmoid.expanded = false;
                        }
46
47
48
49
50
                    }
                }
                break;
            }
            case Qt.Key_Escape: {
51
                if (filter.text != "") {
52
                    filter.text = "";
53
                    event.accepted = true;
54
55
56
                }
                break;
            }
57
58
            case Qt.Key_F: {
                if (event.modifiers & Qt.ControlModifier) {
59
                    filter.forceActiveFocus();
60
                    filter.selectAll();
61
                    event.accepted = true;
62
63
                } else {
                    forwardToFilter();
64
                }
65
66
67
68
69
70
71
                break;
            }
            case Qt.Key_Tab:
            case Qt.Key_Backtab: {
                // prevent search filter from getting Tab key events
                break;
            }
72
73
74
75
76
77
78
            case Qt.Key_Backspace: {
                // filter.text += event.text wil break if the key is backspace
                filter.forceActiveFocus();
                filter.text = filter.text.slice(0, -1);
                event.accepted = true;
                break;
            }
79
80
            default: {
                forwardToFilter();
81
82
83
84
            }
        }
    }

85
    Keys.forwardTo: [stack.currentItem]
86

87
88
89
    property var header: PlasmaExtras.PlasmoidHeading {
        RowLayout {
            anchors.fill: parent
90
            enabled: clipboardMenu.model.count > 0 || filter.text.length > 0
91

92
            PlasmaComponents3.TextField {
93
                id: filter
94
                placeholderText: i18n("Search…")
95
96
                clearButtonShown: true
                Layout.fillWidth: true
97

98
99
                inputMethodHints: Qt.ImhNoPredictiveText

100
101
                Keys.onUpPressed: clipboardMenu.arrowKeyPressed(event)
                Keys.onDownPressed: clipboardMenu.arrowKeyPressed(event)
102

103
104
105
106
107
108
                Connections {
                    target: main
                    function onClearSearchField() {
                        filter.clear()
                    }
                }
109
            }
110
            PlasmaComponents3.ToolButton {
111
                visible: !(plasmoid.containmentDisplayHints & PlasmaCore.Types.ContainmentDrawsPlasmoidHeading) && plasmoid.action("clearHistory").visible
112

113
                icon.name: "edit-clear-history"
114
115
116
117
                onClicked: {
                    clipboardSource.service("", "clearHistory")
                    filter.clear()
                }
118
119
120
121

                PlasmaComponents3.ToolTip {
                    text: i18n("Clear history")
                }
122
            }
123
124
        }
    }
125

126
127
    model: PlasmaCore.SortFilterModel {
        sourceModel: clipboardSource.models.clipboard
128
        filterRole: "DisplayRole"
129
130
131
132
133
134
135
136
137
        filterRegExp: filter.text
    }
    supportsBarcodes: {
        try {
            let prisonTest = Qt.createQmlObject("import QtQml 2.0; import org.kde.prison 1.0; QtObject {}", this);
            prisonTest.destroy();
        } catch (e) {
            console.log("Barcodes not supported:", e);
            return false;
138
        }
139
140
141
142
143
144
145
146
147
        return true;
    }
    Layout.fillWidth: true
    Layout.fillHeight: true
    Layout.topMargin: PlasmaCore.Units.smallSpacing
    onItemSelected: clipboardSource.service(uuid, "select")
    onRemove: clipboardSource.service(uuid, "remove")
    onEdit: {
        stack.push(Qt.resolvedUrl("EditPage.qml"), {
148
            text: clipboardMenu.model.get(clipboardMenu.view.currentIndex).DisplayRole,
149
150
151
152
153
154
155
156
157
158
159
            uuid: uuid
        });
    }
    onBarcode: {
        stack.push(Qt.resolvedUrl("BarcodePage.qml"), {
            text: text
        });
    }
    onAction: {
        clipboardSource.service(uuid, "action")
        clipboardMenu.view.currentIndex = 0
160
    }
161
162
163
164
165

    Component.onCompleted: {
        // Intercept up/down key to prevent ListView from accepting the key event.
        clipboardMenu.view.Keys.upPressed.connect(clipboardMenu.arrowKeyPressed);
        clipboardMenu.view.Keys.downPressed.connect(clipboardMenu.arrowKeyPressed);
166
167
168

        // Focus on the search field when the applet is opened for the first time
        filter.forceActiveFocus();
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
    }

    function goToCurrent() {
        clipboardMenu.view.positionViewAtIndex(clipboardMenu.view.currentIndex, ListView.Contain);
        if (clipboardMenu.view.currentIndex !== -1) {
            clipboardMenu.view.currentItem.forceActiveFocus();
        }
    }

    function arrowKeyPressed(event) {
        switch (event.key) {
        case Qt.Key_Up: {
            if (clipboardMenu.view.currentIndex === 0) {
                clipboardMenu.view.currentIndex = -1;
                filter.forceActiveFocus();
                filter.selectAll();
            } else {
                clipboardMenu.view.decrementCurrentIndex();
                goToCurrent();
            }
            event.accepted = true;
            break;
        }
        case Qt.Key_Down: {
            clipboardMenu.view.incrementCurrentIndex();
            goToCurrent();
            event.accepted = true;
            break;
        }
        default:
            break;
        }
    }
202
}