KWayland

plasmawindowmodel.cpp
1 /*
2  SPDX-FileCopyrightText: 2015 Eike Hein <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
5 */
6 #include "plasmawindowmodel.h"
7 #include "plasmawindowmanagement.h"
8 
9 #include <QMetaEnum>
10 
11 namespace KWayland
12 {
13 namespace Client
14 {
15 
16 class Q_DECL_HIDDEN PlasmaWindowModel::Private
17 {
18 public:
19  Private(PlasmaWindowModel *q);
20  QList<PlasmaWindow*> windows;
21  PlasmaWindow *window = nullptr;
22 
23  void addWindow(PlasmaWindow *window);
24  void dataChanged(PlasmaWindow *window, int role);
25 
26 private:
27  PlasmaWindowModel *q;
28 };
29 
30 PlasmaWindowModel::Private::Private(PlasmaWindowModel *q)
31  : q(q)
32 {
33 }
34 
35 void PlasmaWindowModel::Private::addWindow(PlasmaWindow *window)
36 {
37  if (windows.indexOf(window) != -1) {
38  return;
39  }
40 
41  const int count = windows.count();
42  q->beginInsertRows(QModelIndex(), count, count);
43  windows.append(window);
44  q->endInsertRows();
45 
46  auto removeWindow = [window, this] {
47  const int row = windows.indexOf(window);
48  if (row != -1) {
49  q->beginRemoveRows(QModelIndex(), row, row);
50  windows.removeAt(row);
51  q->endRemoveRows();
52  }
53  };
54 
55  QObject::connect(window, &PlasmaWindow::unmapped, q, removeWindow);
56  QObject::connect(window, &QObject::destroyed, q, removeWindow);
57 
58  QObject::connect(window, &PlasmaWindow::titleChanged, q,
59  [window, this] { this->dataChanged(window, Qt::DisplayRole); }
60  );
61 
62  QObject::connect(window, &PlasmaWindow::iconChanged, q,
63  [window, this] { this->dataChanged(window, Qt::DecorationRole); }
64  );
65 
66  QObject::connect(window, &PlasmaWindow::appIdChanged, q,
67  [window, this] { this->dataChanged(window, PlasmaWindowModel::AppId); }
68  );
69 
70  QObject::connect(window, &PlasmaWindow::activeChanged, q,
71  [window, this] { this->dataChanged(window, IsActive); }
72  );
73 
74  QObject::connect(window, &PlasmaWindow::fullscreenableChanged, q,
75  [window, this] { this->dataChanged(window, IsFullscreenable); }
76  );
77 
78  QObject::connect(window, &PlasmaWindow::fullscreenChanged, q,
79  [window, this] { this->dataChanged(window, IsFullscreen); }
80  );
81 
82  QObject::connect(window, &PlasmaWindow::maximizeableChanged, q,
83  [window, this] { this->dataChanged(window, IsMaximizable); }
84  );
85 
86  QObject::connect(window, &PlasmaWindow::maximizedChanged, q,
87  [window, this] { this->dataChanged(window, IsMaximized); }
88  );
89 
90  QObject::connect(window, &PlasmaWindow::minimizeableChanged, q,
91  [window, this] { this->dataChanged(window, IsMinimizable); }
92  );
93 
94  QObject::connect(window, &PlasmaWindow::minimizedChanged, q,
95  [window, this] { this->dataChanged(window, IsMinimized); }
96  );
97 
98  QObject::connect(window, &PlasmaWindow::keepAboveChanged, q,
99  [window, this] { this->dataChanged(window, IsKeepAbove); }
100  );
101 
102  QObject::connect(window, &PlasmaWindow::keepBelowChanged, q,
103  [window, this] { this->dataChanged(window, IsKeepBelow); }
104  );
105 
106  QObject::connect(window, &PlasmaWindow::virtualDesktopChanged, q,
107  [window, this] { this->dataChanged(window, VirtualDesktop); }
108  );
109 
110  QObject::connect(window, &PlasmaWindow::onAllDesktopsChanged, q,
111  [window, this] { this->dataChanged(window, IsOnAllDesktops); }
112  );
113 
114  QObject::connect(window, &PlasmaWindow::demandsAttentionChanged, q,
115  [window, this] { this->dataChanged(window, IsDemandingAttention); }
116  );
117 
118  QObject::connect(window, &PlasmaWindow::skipTaskbarChanged, q,
119  [window, this] { this->dataChanged(window, SkipTaskbar); }
120  );
121 
122  QObject::connect(window, &PlasmaWindow::skipSwitcherChanged, q,
123  [window, this] { this->dataChanged(window, SkipSwitcher); }
124  );
125 
126  QObject::connect(window, &PlasmaWindow::shadeableChanged, q,
127  [window, this] { this->dataChanged(window, IsShadeable); }
128  );
129 
130  QObject::connect(window, &PlasmaWindow::shadedChanged, q,
131  [window, this] { this->dataChanged(window, IsShaded); }
132  );
133 
134  QObject::connect(window, &PlasmaWindow::movableChanged, q,
135  [window, this] { this->dataChanged(window, IsMovable); }
136  );
137 
138  QObject::connect(window, &PlasmaWindow::resizableChanged, q,
139  [window, this] { this->dataChanged(window, IsResizable); }
140  );
141 
142  QObject::connect(window, &PlasmaWindow::virtualDesktopChangeableChanged, q,
143  [window, this] { this->dataChanged(window, IsVirtualDesktopChangeable); }
144  );
145 
146  QObject::connect(window, &PlasmaWindow::closeableChanged, q,
147  [window, this] { this->dataChanged(window, IsCloseable); }
148  );
149 
150  QObject::connect(window, &PlasmaWindow::geometryChanged, q,
151  [window, this] { this->dataChanged(window, Geometry); }
152  );
153 
154  QObject::connect(window, &PlasmaWindow::plasmaVirtualDesktopEntered, q,
155  [window, this] { this->dataChanged(window, VirtualDesktops); }
156  );
157 
158  QObject::connect(window, &PlasmaWindow::plasmaVirtualDesktopLeft, q,
159  [window, this] { this->dataChanged(window, VirtualDesktops); }
160  );
161 }
162 
163 void PlasmaWindowModel::Private::dataChanged(PlasmaWindow *window, int role)
164 {
165  QModelIndex idx = q->index(windows.indexOf(window));
166  emit q->dataChanged(idx, idx, QVector<int>() << role);
167 }
168 
169 PlasmaWindowModel::PlasmaWindowModel(PlasmaWindowManagement *parent)
170  : QAbstractListModel(parent)
171  , d(new Private(this))
172 {
173  connect(parent, &PlasmaWindowManagement::interfaceAboutToBeReleased, this,
174  [this] {
175  beginResetModel();
176  d->windows.clear();
177  endResetModel();
178  }
179  );
180 
181  connect(parent, &PlasmaWindowManagement::windowCreated, this,
182  [this](PlasmaWindow *window) {
183  d->addWindow(window);
184  }
185  );
186 
187  for (auto it = parent->windows().constBegin(); it != parent->windows().constEnd(); ++it) {
188  d->addWindow(*it);
189  }
190 }
191 
192 PlasmaWindowModel::~PlasmaWindowModel()
193 {
194 }
195 
196 QHash<int, QByteArray> PlasmaWindowModel::roleNames() const
197 {
199 
200  roles.insert(Qt::DisplayRole, "DisplayRole");
201  roles.insert(Qt::DecorationRole, "DecorationRole");
202 
203  QMetaEnum e = metaObject()->enumerator(metaObject()->indexOfEnumerator("AdditionalRoles"));
204 
205  for (int i = 0; i < e.keyCount(); ++i) {
206  roles.insert(e.value(i), e.key(i));
207  }
208 
209  return roles;
210 }
211 
212 QVariant PlasmaWindowModel::data(const QModelIndex &index, int role) const
213 {
214  if (!index.isValid() || index.row() >= d->windows.count()) {
215  return QVariant();
216  }
217 
218  const PlasmaWindow *window = d->windows.at(index.row());
219 
220  if (role == Qt::DisplayRole) {
221  return window->title();
222  } else if (role == Qt::DecorationRole) {
223  return window->icon();
224  } else if (role == AppId) {
225  return window->appId();
226  } else if (role == Pid) {
227  return window->pid();
228  } else if (role == IsActive) {
229  return window->isActive();
230  } else if (role == IsFullscreenable) {
231  return window->isFullscreenable();
232  } else if (role == IsFullscreen) {
233  return window->isFullscreen();
234  } else if (role == IsMaximizable) {
235  return window->isMaximizeable();
236  } else if (role == IsMaximized) {
237  return window->isMaximized();
238  } else if (role == IsMinimizable) {
239  return window->isMinimizeable();
240  } else if (role == IsMinimized) {
241  return window->isMinimized();
242  } else if (role == IsKeepAbove) {
243  return window->isKeepAbove();
244  } else if (role == IsKeepBelow) {
245  return window->isKeepBelow();
246  } else if (role == VirtualDesktop) {
247  return window->virtualDesktop();
248  } else if (role == IsOnAllDesktops) {
249  return window->isOnAllDesktops();
250  } else if (role == IsDemandingAttention) {
251  return window->isDemandingAttention();
252  } else if (role == SkipTaskbar) {
253  return window->skipTaskbar();
254  } else if (role == SkipSwitcher) {
255  return window->skipSwitcher();
256  } else if (role == IsShadeable) {
257  return window->isShadeable();
258  } else if (role == IsShaded) {
259  return window->isShaded();
260  } else if (role == IsMovable) {
261  return window->isMovable();
262  } else if (role == IsResizable) {
263  return window->isResizable();
264  } else if (role == IsVirtualDesktopChangeable) {
265  return window->isVirtualDesktopChangeable();
266  } else if (role == IsCloseable) {
267  return window->isCloseable();
268  } else if (role == Geometry) {
269  return window->geometry();
270  } else if (role == VirtualDesktops) {
271  return window->plasmaVirtualDesktops();
272  } else if (role == Uuid) {
273  return window->uuid();
274  }
275 
276  return QVariant();
277 }
278 
279 int PlasmaWindowModel::rowCount(const QModelIndex &parent) const
280 {
281  return parent.isValid() ? 0 : d->windows.count();
282 }
283 
284 QModelIndex PlasmaWindowModel::index(int row, int column, const QModelIndex &parent) const
285 {
286  return hasIndex(row, column, parent) ? createIndex(row, column, d->windows.at(row)) : QModelIndex();
287 }
288 
289 Q_INVOKABLE void PlasmaWindowModel::requestActivate(int row)
290 {
291  if (row >= 0 && row < d->windows.count()) {
292  d->windows.at(row)->requestActivate();
293  }
294 }
295 
296 Q_INVOKABLE void PlasmaWindowModel::requestClose(int row)
297 {
298  if (row >= 0 && row < d->windows.count()) {
299  d->windows.at(row)->requestClose();
300  }
301 }
302 
303 Q_INVOKABLE void PlasmaWindowModel::requestMove(int row)
304 {
305  if (row >= 0 && row < d->windows.count()) {
306  d->windows.at(row)->requestMove();
307  }
308 }
309 
310 Q_INVOKABLE void PlasmaWindowModel::requestResize(int row)
311 {
312  if (row >= 0 && row < d->windows.count()) {
313  d->windows.at(row)->requestResize();
314  }
315 }
316 
317 Q_INVOKABLE void PlasmaWindowModel::requestVirtualDesktop(int row, quint32 desktop)
318 {
319  if (row >= 0 && row < d->windows.count()) {
320  d->windows.at(row)->requestVirtualDesktop(desktop);
321  }
322 }
323 
324 Q_INVOKABLE void PlasmaWindowModel::requestToggleKeepAbove(int row)
325 {
326  if (row >= 0 && row < d->windows.count()) {
327  d->windows.at(row)->requestToggleKeepAbove();
328  }
329 }
330 
331 Q_INVOKABLE void PlasmaWindowModel::requestToggleKeepBelow(int row)
332 {
333  if (row >= 0 && row < d->windows.count()) {
334  d->windows.at(row)->requestToggleKeepBelow();
335  }
336 }
337 
338 Q_INVOKABLE void PlasmaWindowModel::requestToggleMinimized(int row)
339 {
340  if (row >= 0 && row < d->windows.count()) {
341  d->windows.at(row)->requestToggleMinimized();
342  }
343 }
344 
345 Q_INVOKABLE void PlasmaWindowModel::requestToggleMaximized(int row)
346 {
347  if (row >= 0 && row < d->windows.count()) {
348  d->windows.at(row)->requestToggleMaximized();
349  }
350 }
351 
352 Q_INVOKABLE void PlasmaWindowModel::setMinimizedGeometry(int row, Surface *panel, const QRect &geom)
353 {
354  if (row >= 0 && row < d->windows.count()) {
355  d->windows.at(row)->setMinimizedGeometry(panel, geom);
356  }
357 }
358 
359 Q_INVOKABLE void PlasmaWindowModel::requestToggleShaded(int row)
360 {
361  if (row >= 0 && row < d->windows.count()) {
362  d->windows.at(row)->requestToggleShaded();
363  }
364 }
365 
366 }
367 }
QHash::iterator insert(const Key &key, const T &value)
KJOBWIDGETS_EXPORT QWidget * window(KJob *job)
int keyCount() const const
virtual const QMetaObject * metaObject() const const
bool isValid() const const
Wrapper for the wl_surface interface.
Definition: surface.h:44
DisplayRole
int value(int index) const const
int row() const const
QMetaEnum enumerator(int index) const const
Q_INVOKABLEQ_INVOKABLE
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QObject * parent() const const
void destroyed(QObject *obj)
const char * key(int index) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Wed Aug 12 2020 22:50:23 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.