Unity 8
TopLevelWindowModel.h
1 /*
2  * Copyright (C) 2016-2017 Canonical, Ltd.
3  * Copyright 2019 UBports Foundation
4  *
5  * This program is free software: you can redistribute it and/or modify it under
6  * the terms of the GNU Lesser General Public License version 3, as published by
7  * the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranties of MERCHANTABILITY,
11  * SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef TOPLEVELWINDOWMODEL_H
19 #define TOPLEVELWINDOWMODEL_H
20 
21 #include <QAbstractListModel>
22 #include <QAtomicInteger>
23 #include <QLoggingCategory>
24 
25 #include "WindowManagerGlobal.h"
26 
27 Q_DECLARE_LOGGING_CATEGORY(TOPLEVELWINDOWMODEL)
28 
29 class Window;
30 
31 namespace unity {
32  namespace shell {
33  namespace application {
34  class ApplicationInfoInterface;
35  class ApplicationManagerInterface;
36  class MirSurfaceInterface;
37  class SurfaceManagerInterface;
38  }
39  }
40 }
41 
54 class WINDOWMANAGERQML_EXPORT TopLevelWindowModel : public QAbstractListModel
55 {
56  Q_OBJECT
57 
63  Q_PROPERTY(int count READ rowCount NOTIFY countChanged)
64 
65 
70  Q_PROPERTY(unity::shell::application::MirSurfaceInterface* inputMethodSurface READ inputMethodSurface NOTIFY inputMethodSurfaceChanged)
71 
75  Q_PROPERTY(Window* focusedWindow READ focusedWindow NOTIFY focusedWindowChanged)
76 
77  Q_PROPERTY(unity::shell::application::SurfaceManagerInterface* surfaceManager
78  READ surfaceManager
79  WRITE setSurfaceManager
80  NOTIFY surfaceManagerChanged)
81 
82  Q_PROPERTY(unity::shell::application::ApplicationManagerInterface* applicationManager
83  READ applicationManager
84  WRITE setApplicationManager
85  NOTIFY applicationManagerChanged)
86 
91  Q_PROPERTY(int nextId READ nextId)
92 
93 
112  Q_PROPERTY(bool rootFocus READ rootFocus WRITE setRootFocus NOTIFY rootFocusChanged)
113 
114 public:
121  enum Roles {
122  WindowRole = Qt::UserRole,
123  ApplicationRole = Qt::UserRole + 1,
124  };
125 
127 
128  // From QAbstractItemModel
129  int rowCount(const QModelIndex &parent = QModelIndex()) const override;
130  QVariant data(const QModelIndex& index, int role) const override;
131  QHash<int, QByteArray> roleNames() const override {
132  QHash<int, QByteArray> roleNames { {WindowRole, "window"},
133  {ApplicationRole, "application"} };
134  return roleNames;
135  }
136 
137  // Own API
138 
139  unity::shell::application::MirSurfaceInterface* inputMethodSurface() const;
140  Window* focusedWindow() const;
141 
142  unity::shell::application::ApplicationManagerInterface *applicationManager() const { return m_applicationManager; }
143  void setApplicationManager(unity::shell::application::ApplicationManagerInterface*);
144 
145  unity::shell::application::SurfaceManagerInterface *surfaceManager() const { return m_surfaceManager; }
146  void setSurfaceManager(unity::shell::application::SurfaceManagerInterface*);
147 
148  int nextId() const { return m_nextId.load(); }
149 
150 public:
159  Q_INVOKABLE unity::shell::application::MirSurfaceInterface *surfaceAt(int index) const;
160 
166  Q_INVOKABLE Window *windowAt(int index) const;
167 
171  Q_INVOKABLE unity::shell::application::ApplicationInfoInterface *applicationAt(int index) const;
172 
176  Q_INVOKABLE int idAt(int index) const;
177 
183  Q_INVOKABLE int indexForId(int id) const;
184 
188  Q_INVOKABLE void raiseId(int id);
189 
193  Q_INVOKABLE void closeAllWindows();
194 
198  Q_INVOKABLE void pendingActivation();
199 
200  void setRootFocus(bool focus);
201  bool rootFocus();
202 
203 Q_SIGNALS:
204  void countChanged();
205  void inputMethodSurfaceChanged(unity::shell::application::MirSurfaceInterface* inputMethodSurface);
206  void focusedWindowChanged(Window *focusedWindow);
207  void applicationManagerChanged(unity::shell::application::ApplicationManagerInterface*);
208  void surfaceManagerChanged(unity::shell::application::SurfaceManagerInterface*);
209 
215  void listChanged();
216 
217  void closedAllWindows();
218 
219  void rootFocusChanged();
220 
221 private Q_SLOTS:
222  void onSurfaceCreated(unity::shell::application::MirSurfaceInterface *surface);
223  void onSurfacesRaised(const QVector<unity::shell::application::MirSurfaceInterface*> &surfaces);
224 
225  void onModificationsStarted();
226  void onModificationsEnded();
227 
228 private:
229  void doRaiseId(int id);
230  int generateId();
231  int nextFreeId(int candidateId, const int latestId);
232  int nextId(int id) const;
233  QString toString();
234  int indexOf(unity::shell::application::MirSurfaceInterface *surface);
235 
236  void setInputMethodWindow(Window *window);
237  void setFocusedWindow(Window *window);
238  void removeInputMethodWindow();
239  int findIndexOf(const unity::shell::application::MirSurfaceInterface *surface) const;
240  void deleteAt(int index);
241  void removeAt(int index);
242 
243  void addApplication(unity::shell::application::ApplicationInfoInterface *application);
244  void removeApplication(unity::shell::application::ApplicationInfoInterface *application);
245 
246  void prependPlaceholder(unity::shell::application::ApplicationInfoInterface *application);
247  void prependSurface(unity::shell::application::MirSurfaceInterface *surface,
248  unity::shell::application::ApplicationInfoInterface *application);
249  void prependSurfaceHelper(unity::shell::application::MirSurfaceInterface *surface,
250  unity::shell::application::ApplicationInfoInterface *application);
251  void prependWindow(Window *window, unity::shell::application::ApplicationInfoInterface *application);
252 
253  void connectWindow(Window *window);
254  void connectSurface(unity::shell::application::MirSurfaceInterface *surface);
255 
256  void onSurfaceDied(unity::shell::application::MirSurfaceInterface *surface);
257  void onSurfaceDestroyed(unity::shell::application::MirSurfaceInterface *surface);
258 
259  void move(int from, int to);
260 
261  void activateEmptyWindow(Window *window);
262 
263  void activateTopMostWindowWithoutId(int forbiddenId);
264 
265  Window *createWindow(unity::shell::application::MirSurfaceInterface *surface);
266  Window *createWindowWithId(unity::shell::application::MirSurfaceInterface *surface, int id);
267  Window *createNullWindow();
268 
269  struct ModelEntry {
270  ModelEntry() {}
271  ModelEntry(Window *window,
272  unity::shell::application::ApplicationInfoInterface *application)
273  : window(window), application(application) {}
274  Window *window{nullptr};
275  unity::shell::application::ApplicationInfoInterface *application{nullptr};
276  bool removeOnceSurfaceDestroyed{false};
277  };
278 
279  QVector<ModelEntry> m_windowModel;
280  Window* m_inputMethodWindow{nullptr};
281  Window* m_focusedWindow{nullptr};
282  Window* m_nullWindow;
283  Window* m_previousWindow{nullptr};
284  bool m_pendingActivation;
285 
286  QAtomicInteger<int> m_nextId{1};
287 
288  unity::shell::application::ApplicationManagerInterface* m_applicationManager{nullptr};
289  unity::shell::application::SurfaceManagerInterface *m_surfaceManager{nullptr};
290  bool m_surfaceManagerBusy;
291 
292  enum ModelState {
293  IdleState,
294  InsertingState,
295  RemovingState,
296  MovingState,
297  ResettingState
298  };
299  ModelState m_modelState{IdleState};
300 
301  // Valid between modificationsStarted and modificationsEnded
302  bool m_focusedWindowChanged{false};
303  Window *m_newlyFocusedWindow{nullptr};
304 
305  bool m_closingAllApps{false};
306 };
307 
308 #endif // TOPLEVELWINDOWMODEL_H
A slightly higher concept than MirSurface.
Definition: Window.h:47
Roles
The Roles supported by the model.
A model of top-level surfaces.