Unity 8
TopLevelWindowModel.h
1 /*
2  * Copyright (C) 2016 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 Q_DECLARE_LOGGING_CATEGORY(TOPLEVELWINDOWMODEL)
24 
25 class Window;
26 
27 namespace unity {
28  namespace shell {
29  namespace application {
30  class ApplicationInfoInterface;
31  class ApplicationManagerInterface;
32  class MirSurfaceInterface;
33  class SurfaceManagerInterface;
34  }
35  }
36 }
37 
50 class TopLevelWindowModel : public QAbstractListModel
51 {
52  Q_OBJECT
53 
59  Q_PROPERTY(int count READ rowCount NOTIFY countChanged)
60 
61 
66  Q_PROPERTY(unity::shell::application::MirSurfaceInterface* inputMethodSurface READ inputMethodSurface NOTIFY inputMethodSurfaceChanged)
67 
71  Q_PROPERTY(Window* focusedWindow READ focusedWindow NOTIFY focusedWindowChanged)
72 
73  Q_PROPERTY(unity::shell::application::SurfaceManagerInterface* surfaceManager
74  READ surfaceManager
75  WRITE setSurfaceManager
76  NOTIFY surfaceManagerChanged)
77 
78  Q_PROPERTY(unity::shell::application::ApplicationManagerInterface* applicationManager
79  READ applicationManager
80  WRITE setApplicationManager
81  NOTIFY applicationManagerChanged)
82 
87  Q_PROPERTY(int nextId READ nextId NOTIFY nextIdChanged)
88 
89 public:
96  enum Roles {
97  WindowRole = Qt::UserRole,
98  ApplicationRole = Qt::UserRole + 1,
99  };
100 
102 
103  // From QAbstractItemModel
104  int rowCount(const QModelIndex &parent = QModelIndex()) const override;
105  QVariant data(const QModelIndex& index, int role) const override;
106  QHash<int, QByteArray> roleNames() const override {
107  QHash<int, QByteArray> roleNames { {WindowRole, "window"},
108  {ApplicationRole, "application"} };
109  return roleNames;
110  }
111 
112  // Own API
113 
114  unity::shell::application::MirSurfaceInterface* inputMethodSurface() const;
115  Window* focusedWindow() const;
116 
117  unity::shell::application::ApplicationManagerInterface *applicationManager() const { return m_applicationManager; }
118  void setApplicationManager(unity::shell::application::ApplicationManagerInterface*);
119 
120  unity::shell::application::SurfaceManagerInterface *surfaceManager() const { return m_surfaceManager; }
121  void setSurfaceManager(unity::shell::application::SurfaceManagerInterface*);
122 
123  int nextId() const { return m_nextId; }
124 
125 public:
134  Q_INVOKABLE unity::shell::application::MirSurfaceInterface *surfaceAt(int index) const;
135 
141  Q_INVOKABLE Window *windowAt(int index) const;
142 
146  Q_INVOKABLE unity::shell::application::ApplicationInfoInterface *applicationAt(int index) const;
147 
151  Q_INVOKABLE int idAt(int index) const;
152 
158  Q_INVOKABLE int indexForId(int id) const;
159 
163  Q_INVOKABLE void raiseId(int id);
164 
165 Q_SIGNALS:
166  void countChanged();
167  void inputMethodSurfaceChanged(unity::shell::application::MirSurfaceInterface* inputMethodSurface);
168  void focusedWindowChanged(Window *focusedWindow);
169  void applicationManagerChanged(unity::shell::application::ApplicationManagerInterface*);
170  void surfaceManagerChanged(unity::shell::application::SurfaceManagerInterface*);
171 
177  void listChanged();
178 
179  void nextIdChanged();
180 
181 private Q_SLOTS:
182  void onSurfaceCreated(unity::shell::application::MirSurfaceInterface *surface);
183  void onSurfacesRaised(const QVector<unity::shell::application::MirSurfaceInterface*> &surfaces);
184 
185  void onModificationsStarted();
186  void onModificationsEnded();
187 
188 private:
189  void doRaiseId(int id);
190  int generateId();
191  int nextFreeId(int candidateId, const int latestId);
192  int nextId(int id) const;
193  QString toString();
194  int indexOf(unity::shell::application::MirSurfaceInterface *surface);
195 
196  void setInputMethodWindow(Window *window);
197  void setFocusedWindow(Window *window);
198  void removeInputMethodWindow();
199  int findIndexOf(const unity::shell::application::MirSurfaceInterface *surface) const;
200  void removeAt(int index);
201 
202  void addApplication(unity::shell::application::ApplicationInfoInterface *application);
203  void removeApplication(unity::shell::application::ApplicationInfoInterface *application);
204 
205  void prependPlaceholder(unity::shell::application::ApplicationInfoInterface *application);
206  void prependSurface(unity::shell::application::MirSurfaceInterface *surface,
207  unity::shell::application::ApplicationInfoInterface *application);
208  void prependSurfaceHelper(unity::shell::application::MirSurfaceInterface *surface,
209  unity::shell::application::ApplicationInfoInterface *application);
210 
211  void connectWindow(Window *window);
212  void connectSurface(unity::shell::application::MirSurfaceInterface *surface);
213 
214  void onSurfaceDied(unity::shell::application::MirSurfaceInterface *surface);
215  void onSurfaceDestroyed(unity::shell::application::MirSurfaceInterface *surface);
216 
217  void move(int from, int to);
218 
219  void activateEmptyWindow(Window *window);
220 
221  void activateTopMostWindowWithoutId(int forbiddenId);
222 
223  struct ModelEntry {
224  ModelEntry() {}
225  ModelEntry(Window *window,
226  unity::shell::application::ApplicationInfoInterface *application)
227  : window(window), application(application) {}
228  Window *window{nullptr};
229  unity::shell::application::ApplicationInfoInterface *application{nullptr};
230  bool removeOnceSurfaceDestroyed{false};
231  };
232 
233  QVector<ModelEntry> m_windowModel;
234  Window* m_inputMethodWindow{nullptr};
235  Window* m_focusedWindow{nullptr};
236 
237  int m_nextId{1};
238  // Just something big enough that we don't risk running out of unused id numbers.
239  // Not sure if QML int type supports something close to std::numeric_limits<int>::max() and
240  // there's no reason to try out its limits.
241  static const int m_maxId{1000000};
242 
243  unity::shell::application::ApplicationManagerInterface* m_applicationManager{nullptr};
244  unity::shell::application::SurfaceManagerInterface *m_surfaceManager{nullptr};
245 
246  enum ModelState {
247  IdleState,
248  InsertingState,
249  RemovingState,
250  MovingState,
251  ResettingState
252  };
253  ModelState m_modelState{IdleState};
254 
255  // Valid between modificationsStarted and modificationsEnded
256  bool m_focusedWindowChanged{false};
257  Window *m_newlyFocusedWindow{nullptr};
258 };
259 
260 #endif // TOPLEVELWINDOWMODEL_H
A slightly higher concept than MirSurface.
Definition: Window.h:46
Roles
The Roles supported by the model.
A model of top-level surfaces.