Changeset View
Changeset View
Standalone View
Standalone View
applets/notifications/notificationapplet.cpp
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | * Copyright 2018 Kai Uwe Broulik <kde@privat.broulik.de> | ||||
3 | * | ||||
4 | * This program is free software; you can redistribute it and/or | ||||
5 | * modify it under the terms of the GNU General Public License as | ||||
6 | * published by the Free Software Foundation; either version 2 of | ||||
7 | * the License or (at your option) version 3 or any later version | ||||
8 | * accepted by the membership of KDE e.V. (or its successor approved | ||||
9 | * by the membership of KDE e.V.), which shall act as a proxy | ||||
10 | * defined in Section 14 of version 3 of the license. | ||||
11 | * | ||||
12 | * This program is distributed in the hope that it will be useful, | ||||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
15 | * GNU General Public License for more details. | ||||
16 | * | ||||
17 | * You should have received a copy of the GNU General Public License | ||||
18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||||
19 | * | ||||
20 | */ | ||||
21 | | ||||
22 | #include "notificationapplet.h" | ||||
23 | | ||||
24 | #include <QClipboard> | ||||
25 | #include <QDrag> | ||||
26 | #include <QMimeData> | ||||
27 | #include <QQuickItem> | ||||
28 | #include <QQuickWindow> | ||||
29 | #include <QScreen> | ||||
30 | #include <QStyleHints> | ||||
31 | | ||||
32 | #include "filemenu.h" | ||||
33 | #include "thumbnailer.h" | ||||
34 | | ||||
35 | NotificationApplet::NotificationApplet(QObject *parent, const QVariantList &data) | ||||
36 | : Plasma::Applet(parent, data) | ||||
37 | { | ||||
38 | static bool s_typesRegistered = false; | ||||
39 | if (!s_typesRegistered) { | ||||
40 | const char uri[] = "org.kde.plasma.private.notifications"; | ||||
41 | qmlRegisterType<FileMenu>(uri, 2, 0, "FileMenu"); | ||||
42 | qmlRegisterType<Thumbnailer>(uri, 2, 0, "Thumbnailer"); | ||||
43 | qmlProtectModule(uri, 2); | ||||
44 | s_typesRegistered = true; | ||||
45 | } | ||||
46 | } | ||||
47 | | ||||
48 | NotificationApplet::~NotificationApplet() = default; | ||||
49 | | ||||
50 | void NotificationApplet::init() | ||||
51 | { | ||||
52 | | ||||
53 | } | ||||
54 | | ||||
55 | void NotificationApplet::configChanged() | ||||
56 | { | ||||
57 | | ||||
58 | } | ||||
59 | | ||||
60 | bool NotificationApplet::dragActive() const | ||||
61 | { | ||||
62 | return m_dragActive; | ||||
63 | } | ||||
64 | | ||||
65 | bool NotificationApplet::isDrag(int oldX, int oldY, int newX, int newY) const | ||||
66 | { | ||||
67 | return ((QPoint(oldX, oldY) - QPoint(newX, newY)).manhattanLength() >= qApp->styleHints()->startDragDistance()); | ||||
68 | } | ||||
69 | | ||||
70 | void NotificationApplet::startDrag(QQuickItem *item, const QUrl &url, const QPixmap &pixmap) | ||||
71 | { | ||||
72 | // This allows the caller to return, making sure we don't crash if | ||||
73 | // the caller is destroyed mid-drag | ||||
74 | | ||||
75 | QMetaObject::invokeMethod(this, "doDrag", Qt::QueuedConnection, | ||||
76 | Q_ARG(QQuickItem*, item), Q_ARG(QUrl, url), Q_ARG(QPixmap, pixmap)); | ||||
77 | } | ||||
78 | | ||||
79 | void NotificationApplet::doDrag(QQuickItem *item, const QUrl &url, const QPixmap &pixmap) | ||||
80 | { | ||||
81 | if (item && item->window() && item->window()->mouseGrabberItem()) { | ||||
82 | item->window()->mouseGrabberItem()->ungrabMouse(); | ||||
83 | } | ||||
84 | | ||||
85 | QDrag *drag = new QDrag(item); | ||||
86 | | ||||
87 | QMimeData *mimeData = new QMimeData(); | ||||
88 | | ||||
89 | if (!url.isEmpty()) { | ||||
90 | mimeData->setUrls(QList<QUrl>() << url); | ||||
91 | } | ||||
92 | | ||||
93 | drag->setMimeData(mimeData); | ||||
94 | | ||||
95 | if (!pixmap.isNull()) { | ||||
96 | drag->setPixmap(pixmap); | ||||
97 | } | ||||
98 | | ||||
99 | m_dragActive = true; | ||||
100 | emit dragActiveChanged(); | ||||
101 | | ||||
102 | drag->exec(); | ||||
103 | | ||||
104 | m_dragActive = false; | ||||
105 | emit dragActiveChanged(); | ||||
106 | } | ||||
107 | | ||||
108 | void NotificationApplet::setSelectionClipboardText(const QString &text) | ||||
109 | { | ||||
110 | // FIXME KDeclarative Clipboard item uses QClipboard::Mode for "mode" | ||||
111 | // which is an enum inaccessible from QML | ||||
112 | QGuiApplication::clipboard()->setText(text, QClipboard::Selection); | ||||
113 | } | ||||
114 | | ||||
115 | bool NotificationApplet::isPrimaryScreen(const QRect &rect) const | ||||
116 | { | ||||
117 | QScreen *screen = QGuiApplication::primaryScreen(); | ||||
118 | if (!screen) { | ||||
119 | return false; | ||||
120 | } | ||||
121 | | ||||
122 | // HACK | ||||
123 | return rect == screen->geometry(); | ||||
124 | } | ||||
125 | | ||||
126 | K_EXPORT_PLASMA_APPLET_WITH_JSON(icon, NotificationApplet, "metadata.json") | ||||
127 | | ||||
128 | #include "notificationapplet.moc" |