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

KDE's Doxygen guidelines are available online.