Unity 8
TopLevelWindowModel.h
1 /*
2  * Copyright (C) 2016-2017 Canonical, Ltd.
3  *
4  * This program is free software: you can redistribute it and/or modify it under
5  * the terms of the GNU Lesser General Public License version 3, as published by
6  * the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful, but WITHOUT
9  * ANY WARRANTY; without even the implied warranties of MERCHANTABILITY,
10  * SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  * Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  */
16 
17 #ifndef TOPLEVELWINDOWMODEL_H
18 #define TOPLEVELWINDOWMODEL_H
19 
20 #include <QAbstractListModel>
21 #include <QLoggingCategory>
22 
23 #include "WindowManagerGlobal.h"
24 
25 Q_DECLARE_LOGGING_CATEGORY(TOPLEVELWINDOWMODEL)
26 
27 class Window;
28 
29 namespace unity {
30  namespace shell {
31  namespace application {
32  class ApplicationInfoInterface;
33  class ApplicationManagerInterface;
34  class MirSurfaceInterface;
35  class SurfaceManagerInterface;
36  }
37  }
38 }
39 
52 class WINDOWMANAGERQML_EXPORT TopLevelWindowModel : public QAbstractListModel
53 {
54  Q_OBJECT
55 
61  Q_PROPERTY(int count READ rowCount NOTIFY countChanged)
62 
63 
68  Q_PROPERTY(unity::shell::application::MirSurfaceInterface* inputMethodSurface READ inputMethodSurface NOTIFY inputMethodSurfaceChanged)
69 
73  Q_PROPERTY(Window* focusedWindow READ focusedWindow NOTIFY focusedWindowChanged)
74 
75  Q_PROPERTY(unity::shell::application::SurfaceManagerInterface* surfaceManager
76  READ surfaceManager
77  WRITE setSurfaceManager
78  NOTIFY surfaceManagerChanged)
79 
80  Q_PROPERTY(unity::shell::application::ApplicationManagerInterface* applicationManager
81  READ applicationManager
82  WRITE setApplicationManager
83  NOTIFY applicationManagerChanged)
84 
89  Q_PROPERTY(int nextId READ nextId NOTIFY nextIdChanged)
90 
91 public:
98  enum Roles {
99  WindowRole = Qt::UserRole,
100  ApplicationRole = Qt::UserRole + 1,
101  };
102 
104 
105  // From QAbstractItemModel
106  int rowCount(const QModelIndex &parent = QModelIndex()) const override;
107  QVariant data(const QModelIndex& index, int role) const override;
108  QHash<int, QByteArray> roleNames() const override {
109  QHash<int, QByteArray> roleNames { {WindowRole, "window"},
110  {ApplicationRole, "application"} };
111  return roleNames;
112  }
113 
114  // Own API
115 
116  unity::shell::application::MirSurfaceInterface* inputMethodSurface() const;
117  Window* focusedWindow() const;
118 
119  unity::shell::application::ApplicationManagerInterface *applicationManager() const { return m_applicationManager; }
120  void setApplicationManager(unity::shell::application::ApplicationManagerInterface*);
121 
122  unity::shell::application::SurfaceManagerInterface *surfaceManager() const { return m_surfaceManager; }
123  void setSurfaceManager(unity::shell::application::SurfaceManagerInterface*);
124 
125  int nextId() const { return m_nextId; }
126 
127 public:
136  Q_INVOKABLE unity::shell::application::MirSurfaceInterface *surfaceAt(int index) const;
137 
143  Q_INVOKABLE Window *windowAt(int index) const;
144 
148  Q_INVOKABLE unity::shell::application::ApplicationInfoInterface *applicationAt(int index) const;
149 
153  Q_INVOKABLE int idAt(int index) const;
154 
160  Q_INVOKABLE int indexForId(int id) const;
161 
165  Q_INVOKABLE void raiseId(int id);
166 
167 Q_SIGNALS:
168  void countChanged();
169  void inputMethodSurfaceChanged(unity::shell::application::MirSurfaceInterface* inputMethodSurface);
170  void focusedWindowChanged(Window *focusedWindow);
171  void applicationManagerChanged(unity::shell::application::ApplicationManagerInterface*);
172  void surfaceManagerChanged(unity::shell::application::SurfaceManagerInterface*);
173 
179  void listChanged();
180 
181  void nextIdChanged();
182 
183 private Q_SLOTS:
184  void onSurfaceCreated(unity::shell::application::MirSurfaceInterface *surface);
185  void onSurfacesRaised(const QVector<unity::shell::application::MirSurfaceInterface*> &surfaces);
186 
187  void onModificationsStarted();
188  void onModificationsEnded();
189 
190 private:
191  void doRaiseId(int id);
192  int generateId();
193  int nextFreeId(int candidateId, const int latestId);
194  int nextId(int id) const;
195  QString toString();
196  int indexOf(unity::shell::application::MirSurfaceInterface *surface);
197 
198  void setInputMethodWindow(Window *window);
199  void setFocusedWindow(Window *window);
200  void removeInputMethodWindow();
201  int findIndexOf(const unity::shell::application::MirSurfaceInterface *surface) const;
202  void deleteAt(int index);
203  void removeAt(int index);
204 
205  void addApplication(unity::shell::application::ApplicationInfoInterface *application);
206  void removeApplication(unity::shell::application::ApplicationInfoInterface *application);
207 
208  void prependPlaceholder(unity::shell::application::ApplicationInfoInterface *application);
209  void prependSurface(unity::shell::application::MirSurfaceInterface *surface,
210  unity::shell::application::ApplicationInfoInterface *application);
211  void prependSurfaceHelper(unity::shell::application::MirSurfaceInterface *surface,
212  unity::shell::application::ApplicationInfoInterface *application);
213  void prependWindow(Window *window, unity::shell::application::ApplicationInfoInterface *application);
214 
215  void connectWindow(Window *window);
216  void connectSurface(unity::shell::application::MirSurfaceInterface *surface);
217 
218  void onSurfaceDied(unity::shell::application::MirSurfaceInterface *surface);
219  void onSurfaceDestroyed(unity::shell::application::MirSurfaceInterface *surface);
220 
221  void move(int from, int to);
222 
223  void activateEmptyWindow(Window *window);
224 
225  void activateTopMostWindowWithoutId(int forbiddenId);
226 
227  Window *createWindow(unity::shell::application::MirSurfaceInterface *surface);
228 
229  struct ModelEntry {
230  ModelEntry() {}
231  ModelEntry(Window *window,
232  unity::shell::application::ApplicationInfoInterface *application)
233  : window(window), application(application) {}
234  Window *window{nullptr};
235  unity::shell::application::ApplicationInfoInterface *application{nullptr};
236  bool removeOnceSurfaceDestroyed{false};
237  };
238 
239  QVector<ModelEntry> m_windowModel;
240  Window* m_inputMethodWindow{nullptr};
241  Window* m_focusedWindow{nullptr};
242 
243  int m_nextId{1};
244  // Just something big enough that we don't risk running out of unused id numbers.
245  // Not sure if QML int type supports something close to std::numeric_limits<int>::max() and
246  // there's no reason to try out its limits.
247  static const int m_maxId{1000000};
248 
249  unity::shell::application::ApplicationManagerInterface* m_applicationManager{nullptr};
250  unity::shell::application::SurfaceManagerInterface *m_surfaceManager{nullptr};
251 
252  enum ModelState {
253  IdleState,
254  InsertingState,
255  RemovingState,
256  MovingState,
257  ResettingState
258  };
259  ModelState m_modelState{IdleState};
260 
261  // Valid between modificationsStarted and modificationsEnded
262  bool m_focusedWindowChanged{false};
263  Window *m_newlyFocusedWindow{nullptr};
264 };
265 
266 #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.