Unity 8
menucontentactivator.h
1 /*
2  * Copyright (C) 2013 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  * Author: Nick Dedekind <nick.dedekind@canonical.com>
17  */
18 
19 #ifndef MENUCONTENTACTIVATOR_H
20 #define MENUCONTENTACTIVATOR_H
21 
22 #include "unityindicatorsglobal.h"
23 
24 #include <QObject>
25 #include <QTimer>
26 #include <QQmlListProperty>
27 
28 
29 namespace UnityIndicators {
30 /* Defines an interface for a Timer. */
31 class UNITYINDICATORS_EXPORT AbstractTimer : public QObject {
32  Q_OBJECT
33 public:
34  AbstractTimer(QObject *parent) : QObject(parent), m_isRunning(false) {}
35  virtual int interval() const = 0;
36  virtual void setInterval(int msecs) = 0;
37  virtual void start() { m_isRunning = true; };
38  virtual void stop() { m_isRunning = false; }
39  bool isRunning() const { return m_isRunning; }
40 Q_SIGNALS:
41  void timeout();
42 private:
43  bool m_isRunning;
44 };
45 }
46 
47 /* Defines a object to express the active state of a menu. */
48 class UNITYINDICATORS_EXPORT MenuContentState : public QObject
49 {
50  Q_OBJECT
51  Q_PROPERTY(bool active READ isActive NOTIFY activeChanged)
52 public:
53  MenuContentState(bool active);
54 
55  bool isActive() const;
56  void setActive(bool active);
57 
58 Q_SIGNALS:
59  void activeChanged();
60 private:
61  bool m_active;
62 };
63 
64 class MenuContentActivatorPrivate;
65 
66 class UNITYINDICATORS_EXPORT MenuContentActivator : public QObject
67 {
68  Q_OBJECT
69  Q_PROPERTY(int baseIndex READ baseIndex WRITE setBaseIndex NOTIFY baseIndexChanged)
70  Q_PROPERTY(bool running READ isRunning WRITE setRunning NOTIFY runningChanged)
71  Q_PROPERTY(int count READ count WRITE setCount NOTIFY countChanged)
72  Q_PROPERTY(QQmlListProperty<MenuContentState> content READ content NOTIFY contentChanged DESIGNABLE false)
73 public:
74  MenuContentActivator(QObject* parent = nullptr);
75  ~MenuContentActivator();
76 
77  Q_INVOKABLE void restart();
78  Q_INVOKABLE void stop();
79  Q_INVOKABLE void clear();
80  Q_INVOKABLE bool isMenuContentActive(int index) const;
81 
82  void setRunning(bool running);
83  bool isRunning() const;
84 
85  void setBaseIndex(int index);
86  int baseIndex() const;
87 
88  void setCount(int index);
89  int count() const;
90 
91  void setDelta(int index);
92  int delta() const;
93 
94  QQmlListProperty<MenuContentState> content();
95 
96  // Replaces the existing Timer with the given one.
97  //
98  // Useful for providing a fake timer when testing.
99  void setContentTimer(UnityIndicators::AbstractTimer *timer);
100  void setMenuContentState(int index, bool active);
101 
102 Q_SIGNALS:
103  void baseIndexChanged(int baseIndex);
104  void deltaChanged(int delta);
105  void runningChanged(bool running);
106  void countChanged(int count);
107  void contentChanged();
108 
109 private Q_SLOTS:
110  void onTimeout();
111 
112 private:
113  MenuContentActivatorPrivate* d;
114  friend class MenuContentActivatorPrivate;
115 };
116 
117 #endif // MENUCONTENTACTIVATOR_H