Unity 8
TopLevelSurfaceList.h
1 /*
2  * Copyright (C) 2016 Canonical, Ltd.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; version 3.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  */
16 
17 #ifndef TOPLEVELSURFACELIST_H
18 #define TOPLEVELSURFACELIST_H
19 
20 #include <QAbstractListModel>
21 #include <QList>
22 #include <QLoggingCategory>
23 
24 Q_DECLARE_LOGGING_CATEGORY(UNITY_TOPSURFACELIST)
25 
26 namespace unity {
27  namespace shell {
28  namespace application {
29  class ApplicationInfoInterface;
30  class MirSurfaceInterface;
31  }
32  }
33 }
34 
47 class TopLevelSurfaceList : public QAbstractListModel
48 {
49 
50  Q_OBJECT
51 
57  Q_PROPERTY(QAbstractListModel* applicationsModel READ applicationsModel
58  WRITE setApplicationsModel
59  NOTIFY applicationsModelChanged)
60 
61 
66  Q_PROPERTY(int count READ rowCount NOTIFY countChanged)
67 
72  Q_PROPERTY(int nextId READ nextId NOTIFY nextIdChanged)
73 public:
74 
82  enum Roles {
83  SurfaceRole = Qt::UserRole,
84  ApplicationRole = Qt::UserRole + 1,
85  IdRole = Qt::UserRole + 2,
86  };
87 
88  explicit TopLevelSurfaceList(QObject *parent = nullptr);
89  virtual ~TopLevelSurfaceList();
90 
91  // QAbstractItemModel methods
92  int rowCount(const QModelIndex &parent = QModelIndex()) const override;
93  QVariant data(const QModelIndex& index, int role) const override;
94  QHash<int, QByteArray> roleNames() const override {
95  QHash<int, QByteArray> roleNames { {SurfaceRole, "surface"},
96  {ApplicationRole, "application"},
97  {IdRole, "id"} };
98  return roleNames;
99  }
100 
101  int nextId() const { return m_nextId; }
102 
103  QAbstractListModel *applicationsModel() const;
104  void setApplicationsModel(QAbstractListModel*);
105 
106 public Q_SLOTS:
113  unity::shell::application::MirSurfaceInterface *surfaceAt(int index) const;
114 
118  unity::shell::application::ApplicationInfoInterface *applicationAt(int index) const;
119 
123  int idAt(int index) const;
124 
130  int indexForId(int id) const;
131 
135  void raiseId(int id);
136 
137  void doRaiseId(int id);
138 
139 Q_SIGNALS:
140  void countChanged();
141 
147  void listChanged();
148 
149  void nextIdChanged();
150 
151  void applicationsModelChanged();
152 
153 private:
154  void addApplication(unity::shell::application::ApplicationInfoInterface *application);
155  void removeApplication(unity::shell::application::ApplicationInfoInterface *application);
156 
157  int indexOf(unity::shell::application::MirSurfaceInterface *surface);
158  void raise(unity::shell::application::MirSurfaceInterface *surface);
159  void move(int from, int to);
160  void appendSurfaceHelper(unity::shell::application::MirSurfaceInterface *surface,
161  unity::shell::application::ApplicationInfoInterface *application);
162  void connectSurface(unity::shell::application::MirSurfaceInterface *surface);
163  int generateId();
164  int nextFreeId(int candidateId);
165  QString toString();
166  void onSurfaceDestroyed(unity::shell::application::MirSurfaceInterface *surface);
167  void onSurfaceDied(unity::shell::application::MirSurfaceInterface *surface);
168  void removeAt(int index);
169  void findApplicationRole();
170 
171  unity::shell::application::ApplicationInfoInterface *getApplicationFromModelAt(int index);
172 
173  /*
174  Placeholder for a future surface from a starting or running application.
175  Enables shell to give immediate feedback to the user by showing, eg,
176  a splash screen.
177 
178  It's a model row containing a null surface and the given application.
179  */
180  void appendPlaceholder(unity::shell::application::ApplicationInfoInterface *application);
181 
182  /*
183  Adds a model row with the given surface and application
184 
185  Alternatively, if a placeholder exists for the given application it's
186  filled with the given surface instead.
187  */
188  void appendSurface(unity::shell::application::MirSurfaceInterface *surface,
189  unity::shell::application::ApplicationInfoInterface *application);
190 
191  struct ModelEntry {
192  ModelEntry(unity::shell::application::MirSurfaceInterface *surface, unity::shell::application::ApplicationInfoInterface *application, int id)
193  : surface(surface), application(application), id(id) {}
194  unity::shell::application::MirSurfaceInterface *surface;
195  unity::shell::application::ApplicationInfoInterface *application;
196  int id;
197  bool removeOnceSurfaceDestroyed{false};
198  };
199 
200  QList<ModelEntry> m_surfaceList;
201  int m_nextId{1};
202  static const int m_maxId{1000000};
203 
204  // applications that are being monitored
205  QList<unity::shell::application::ApplicationInfoInterface *> m_applications;
206 
207  QAbstractListModel* m_applicationsModel{nullptr};
208  int m_applicationRole{-1};
209 
210  enum ModelState {
211  IdleState,
212  InsertingState,
213  RemovingState,
214  MovingState,
215  ResettingState
216  };
217  ModelState m_modelState{IdleState};
218 };
219 
220 Q_DECLARE_METATYPE(TopLevelSurfaceList*)
221 Q_DECLARE_METATYPE(QAbstractListModel*)
222 
223 #endif // TOPLEVELSURFACELIST_H
Roles
The Roles supported by the model.
A model of top-level surfaces.