Changeset View
Changeset View
Standalone View
Standalone View
applets/pager/plugin/windowmodel.cpp
- This file was added.
1 | /******************************************************************** | ||||
---|---|---|---|---|---|
2 | Copyright 2016 Eike Hein <hein.org> | ||||
3 | | ||||
4 | This program is free software; you can redistribute it and/or modify | ||||
5 | it under the terms of the GNU General Public License as published by | ||||
6 | the Free Software Foundation; either version 2 of the License, or | ||||
7 | (at your option) any later version. | ||||
8 | | ||||
9 | This program is distributed in the hope that it will be useful, | ||||
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
12 | GNU General Public License for more details. | ||||
13 | | ||||
14 | You should have received a copy of the GNU General Public License | ||||
15 | along with this program; if not, write to the | ||||
16 | Free Software Foundation, Inc., | ||||
17 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA . | ||||
18 | *********************************************************************/ | ||||
19 | | ||||
20 | #include "windowmodel.h" | ||||
21 | #include "pagermodel.h" | ||||
22 | | ||||
23 | #include <abstracttasksmodel.h> | ||||
24 | | ||||
25 | #include <QApplication> | ||||
26 | #include <QDesktopWidget> | ||||
27 | #include <QMetaEnum> | ||||
28 | | ||||
29 | #include <KWindowSystem> | ||||
30 | | ||||
31 | using namespace TaskManager; | ||||
32 | | ||||
33 | class WindowModel::Private | ||||
34 | { | ||||
35 | public: | ||||
36 | Private(WindowModel *q); | ||||
37 | | ||||
38 | PagerModel *pagerModel = nullptr; | ||||
39 | | ||||
40 | QDesktopWidget *desktopWidget = QApplication::desktop(); | ||||
41 | | ||||
42 | private: | ||||
43 | WindowModel *q; | ||||
44 | }; | ||||
45 | | ||||
46 | WindowModel::Private::Private(WindowModel *q) | ||||
47 | : q(q) | ||||
48 | { | ||||
49 | } | ||||
50 | | ||||
51 | WindowModel::WindowModel(PagerModel *parent) | ||||
52 | : TaskFilterProxyModel(parent) | ||||
53 | , d(new Private(this)) | ||||
54 | { | ||||
55 | d->pagerModel = parent; | ||||
56 | } | ||||
57 | | ||||
58 | WindowModel::~WindowModel() | ||||
59 | { | ||||
60 | } | ||||
61 | | ||||
62 | QHash<int, QByteArray> WindowModel::roleNames() const | ||||
63 | { | ||||
64 | QHash<int, QByteArray> roles = TaskFilterProxyModel::roleNames(); | ||||
65 | | ||||
66 | QMetaEnum e = metaObject()->enumerator(metaObject()->indexOfEnumerator("WindowModelRoles")); | ||||
67 | | ||||
68 | for (int i = 0; i < e.keyCount(); ++i) { | ||||
69 | roles.insert(e.value(i), e.key(i)); | ||||
70 | } | ||||
71 | | ||||
72 | return roles; | ||||
73 | } | ||||
74 | | ||||
75 | QVariant WindowModel::data(const QModelIndex &index, int role) const | ||||
76 | { | ||||
77 | if (role == AbstractTasksModel::Geometry && KWindowSystem::mapViewport()) { | ||||
78 | | ||||
79 | const QRect &windowGeo = TaskFilterProxyModel::data(index, role).toRect(); | ||||
80 | const QRect &desktopGeo = d->desktopWidget->geometry(); | ||||
81 | | ||||
82 | int x = windowGeo.center().x() % desktopGeo.width(); | ||||
83 | int y = windowGeo.center().y() % desktopGeo.height(); | ||||
84 | | ||||
85 | if (x < 0) { | ||||
86 | x = x + desktopGeo.width(); | ||||
87 | } | ||||
88 | | ||||
89 | if (y < 0) { | ||||
90 | y = y + desktopGeo.height(); | ||||
91 | } | ||||
92 | | ||||
93 | return QRect(x - windowGeo.width() / 2, y - windowGeo.height() / 2, | ||||
94 | windowGeo.width(), windowGeo.height()); | ||||
95 | } else if (role == StackingOrder) { | ||||
96 | #if HAVE_X11 | ||||
97 | const QVariantList &winIds = TaskFilterProxyModel::data(index, AbstractTasksModel::LegacyWinIdList).toList(); | ||||
98 | | ||||
99 | if (winIds.count()) { | ||||
100 | const WId winId = winIds.at(0).toLongLong(); | ||||
101 | const int z = d->pagerModel->stackingOrder().indexOf(winId); | ||||
102 | | ||||
103 | if (z != -1) { | ||||
104 | return z; | ||||
105 | } | ||||
106 | } | ||||
107 | #endif | ||||
108 | | ||||
109 | return 0; | ||||
110 | } | ||||
111 | | ||||
112 | return TaskFilterProxyModel::data(index, role); | ||||
113 | } | ||||
114 | | ||||
115 | void WindowModel::refreshStackingOrder() | ||||
116 | { | ||||
117 | if (rowCount()) { | ||||
118 | emit dataChanged(index(0, 0), index(rowCount() - 1, 0), QVector<int>{StackingOrder}); | ||||
119 | } | ||||
120 | } |