dashboardwindow.cpp 7.1 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/***************************************************************************
 *   Copyright (C) 2015 by Eike Hein <hein@kde.org>                        *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA .        *
 ***************************************************************************/

Eike Hein's avatar
Eike Hein committed
20
#include "dashboardwindow.h"
21

Eike Hein's avatar
Eike Hein committed
22
#include <QCoreApplication>
23
24
25
26
27
28
#include <QIcon>
#include <QScreen>

#include <KWindowEffects>
#include <KWindowSystem>

29
30
31
32
DashboardWindow::DashboardWindow(QQuickItem *parent) : QQuickWindow(parent ? parent->window() : nullptr)
, m_mainItem(nullptr)
, m_visualParentItem(nullptr)
, m_visualParentWindow(nullptr)
33
34
35
36
{
    setClearBeforeRendering(true);
    setFlags(Qt::FramelessWindowHint);

37
    setIcon(QIcon::fromTheme(QStringLiteral("plasma")));
38

Eike Hein's avatar
Eike Hein committed
39
    connect(&m_theme, &Plasma::Theme::themeChanged, this, &DashboardWindow::updateTheme);
40
41
}

Eike Hein's avatar
Eike Hein committed
42
DashboardWindow::~DashboardWindow()
43
44
45
{
}

Eike Hein's avatar
Eike Hein committed
46
QQuickItem *DashboardWindow::mainItem() const
47
48
49
50
{
    return m_mainItem;
}

Eike Hein's avatar
Eike Hein committed
51
void DashboardWindow::setMainItem(QQuickItem *item)
52
{
53
    if (m_mainItem != item) {
54
55
56
57
        if (m_mainItem) {
            m_mainItem->setVisible(false);
        }

58
        m_mainItem = item;
59

60
        if (m_mainItem) {
61
62
63
64
65
66
67
68
            m_mainItem->setVisible(isVisible());
            m_mainItem->setParentItem(contentItem());
        }

        emit mainItemChanged();
    }
}

Eike Hein's avatar
Eike Hein committed
69
QQuickItem *DashboardWindow::visualParent() const
70
71
72
73
{
    return m_visualParentItem;
}

Eike Hein's avatar
Eike Hein committed
74
void DashboardWindow::setVisualParent(QQuickItem *item)
75
76
77
{
    if (m_visualParentItem != item) {
        if (m_visualParentItem) {
Eike Hein's avatar
Eike Hein committed
78
            disconnect(m_visualParentItem.data(), &QQuickItem::windowChanged, this, &DashboardWindow::visualParentWindowChanged);
79
80
81
82
83
        }

        m_visualParentItem = item;

        if (m_visualParentItem) {
Eike Hein's avatar
Eike Hein committed
84
85
86
87
            if (m_visualParentItem->window()) {
                visualParentWindowChanged(m_visualParentItem->window());
            }

Eike Hein's avatar
Eike Hein committed
88
            connect(m_visualParentItem.data(), &QQuickItem::windowChanged, this, &DashboardWindow::visualParentWindowChanged);
89
90
91
92
93
94
        }

        emit visualParentChanged();
    }
}

95
96
97
98
99
100
101
102
103
104
105
106
107
108
QColor DashboardWindow::backgroundColor() const
{
    return color();
}

void DashboardWindow::setBackgroundColor(const QColor &c)
{
    if (color() != c) {
        setColor(c);

        emit backgroundColorChanged();
    }
}

Eike Hein's avatar
Eike Hein committed
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
QQuickItem *DashboardWindow::keyEventProxy() const
{
    return m_keyEventProxy;
}

void DashboardWindow::setKeyEventProxy(QQuickItem *item)
{
    if (m_keyEventProxy != item) {
        m_keyEventProxy = item;

        emit keyEventProxyChanged();
    }
}

void DashboardWindow::toggle() {
124
125
126
127
128
    if (isVisible()) {
        close();
    } else {
        resize(screen()->size());
        showFullScreen();
129
        KWindowSystem::forceActiveWindow(winId());
130
131
132
    }
}

Eike Hein's avatar
Eike Hein committed
133
bool DashboardWindow::event(QEvent *event)
134
{
Eike Hein's avatar
Eike Hein committed
135
    if (event->type() == QEvent::Expose) {
136
137
        // FIXME TODO: We can remove this once we depend on Qt 5.6.1+.
        // See: https://bugreports.qt.io/browse/QTBUG-26978
Eike Hein's avatar
Eike Hein committed
138
        KWindowSystem::setState(winId(), NET::SkipTaskbar | NET::SkipPager);
139
140
141
142
143
144
    } else if (event->type() == QEvent::PlatformSurface) {
        const QPlatformSurfaceEvent *pSEvent = static_cast<QPlatformSurfaceEvent *>(event);

        if (pSEvent->surfaceEventType() == QPlatformSurfaceEvent::SurfaceCreated) {
            KWindowSystem::setState(winId(), NET::SkipTaskbar | NET::SkipPager);
        }
Eike Hein's avatar
Eike Hein committed
145
146
    } else if (event->type() == QEvent::Show) {
        updateTheme();
147

Eike Hein's avatar
Eike Hein committed
148
149
150
151
152
153
154
155
156
157
158
159
        if (m_mainItem) {
            m_mainItem->setVisible(true);
        }
    } else if (event->type() == QEvent::Hide) {
        if (m_mainItem) {
            m_mainItem->setVisible(false);
        }
    } else if (event->type() == QEvent::FocusOut) {
        if (isVisible()) {
            KWindowSystem::raiseWindow(winId());
            KWindowSystem::forceActiveWindow(winId());
        }
160
161
    }

Eike Hein's avatar
Eike Hein committed
162
    return QQuickWindow::event(event);
163
164
}

Eike Hein's avatar
Eike Hein committed
165
166
167
void DashboardWindow::keyPressEvent(QKeyEvent *e)
{
    if (e->key() == Qt::Key_Escape) {
168
        emit keyEscapePressed();
Eike Hein's avatar
Eike Hein committed
169
170
171
172
173
174
175
176
177
178
179
180
181

        return;
    } else if (m_keyEventProxy && !m_keyEventProxy->hasActiveFocus()
        && !(e->key() == Qt::Key_Home)
        && !(e->key() == Qt::Key_End)
        && !(e->key() == Qt::Key_Left)
        && !(e->key() == Qt::Key_Up)
        && !(e->key() == Qt::Key_Right)
        && !(e->key() == Qt::Key_Down)
        && !(e->key() == Qt::Key_PageUp)
        && !(e->key() == Qt::Key_PageDown)
        && !(e->key() == Qt::Key_Enter)
        && !(e->key() == Qt::Key_Return)
Eike Hein's avatar
Eike Hein committed
182
183
184
        && !(e->key() == Qt::Key_Menu)
        && !(e->key() == Qt::Key_Tab)
        && !(e->key() == Qt::Key_Backtab)) {
185
186
187

        QPointer<QQuickItem> previousFocusItem = activeFocusItem();

Eike Hein's avatar
Eike Hein committed
188
189
190
191
192
        m_keyEventProxy->forceActiveFocus();
        QEvent* eventCopy = new QKeyEvent(e->type(), e->key(), e->modifiers(),
            e->nativeScanCode(), e->nativeVirtualKey(), e->nativeModifiers(),
            e->text(), e->isAutoRepeat(), e->count());
        QCoreApplication::postEvent(this, eventCopy);
193
194
195
196

        // We _need_ to do it twice to make sure the event ping-pong needed
        // for delivery happens before we sap focus again.
        QCoreApplication::processEvents();
197
198
199
200
201
        QCoreApplication::processEvents();

        if (previousFocusItem) {
            previousFocusItem->forceActiveFocus();
        }
Eike Hein's avatar
Eike Hein committed
202
203
204
205
206
207
208
209

        return;
    }

    QQuickWindow::keyPressEvent(e);
}

void DashboardWindow::updateTheme()
210
211
212
213
{
    KWindowEffects::enableBlurBehind(winId(), true);
}

Eike Hein's avatar
Eike Hein committed
214
void DashboardWindow::visualParentWindowChanged(QQuickWindow *window)
215
216
{
    if (m_visualParentWindow) {
Eike Hein's avatar
Eike Hein committed
217
        disconnect(m_visualParentWindow.data(), &QQuickWindow::screenChanged, this, &DashboardWindow::visualParentScreenChanged);
218
219
220
221
222
    }

    m_visualParentWindow = window;

    if (m_visualParentWindow) {
Eike Hein's avatar
Eike Hein committed
223
224
        visualParentScreenChanged(m_visualParentWindow->screen());

Eike Hein's avatar
Eike Hein committed
225
        connect(m_visualParentWindow.data(), &QQuickWindow::screenChanged, this, &DashboardWindow::visualParentScreenChanged);
226
227
228
    }
}

Eike Hein's avatar
Eike Hein committed
229
void DashboardWindow::visualParentScreenChanged(QScreen *screen)
230
231
{
    if (screen) {
Eike Hein's avatar
Eike Hein committed
232
        setScreen(screen);
Eike Hein's avatar
Eike Hein committed
233
        setGeometry(screen->geometry());
234
235
    }
}