libqutim  0.3.1.0
menucontroller.h
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** qutIM - instant messenger
4 **
5 ** Copyright © 2011 Ruslan Nigmatullin <euroelessar@yandex.ru>
6 ** Copyright © 2011 Aleksey Sidorov <gorthauer87@yandex.ru>
7 **
8 *****************************************************************************
9 **
10 ** $QUTIM_BEGIN_LICENSE$
11 ** This program is free software: you can redistribute it and/or modify
12 ** it under the terms of the GNU General Public License as published by
13 ** the Free Software Foundation, either version 3 of the License, or
14 ** (at your option) any later version.
15 **
16 ** This program is distributed in the hope that it will be useful,
17 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19 ** See the GNU General Public License for more details.
20 **
21 ** You should have received a copy of the GNU General Public License
22 ** along with this program. If not, see http://www.gnu.org/licenses/.
23 ** $QUTIM_END_LICENSE$
24 **
25 ****************************************************************************/
26 
27 #ifndef MENUUNIT_H
28 #define MENUUNIT_H
29 
30 #include "actiongenerator.h"
31 #include <QMenu>
32 #include "debug.h"
33 
34 namespace qutim_sdk_0_3
35 {
36 
38 typedef QList<SettingsItem *> SettingsItemList;
39 
40 class MenuControllerPrivate;
41 struct ActionInfo;
42 
76 class LIBQUTIM_EXPORT MenuController : public QObject
77 {
78  Q_OBJECT
79  Q_DISABLE_COPY(MenuController)
80  Q_DECLARE_PRIVATE(MenuController)
81 public:
82  enum MenuFlag
83  {
84  ShowSelfActions = 0x01,
85  ShowSuperActions = 0x02,
86  ShowOwnerActions = 0x04
87  };
88  Q_DECLARE_FLAGS(MenuFlags, MenuFlag)
89 
90 #ifndef Q_QDOC
91  struct Action
92  {
93  ActionGenerator *generator;
94  QList<QByteArray> menu;
95  };
96  typedef QList<Action> ActionList;
97 #endif
98 
101  MenuController(QObject *parent = 0);
102 #ifndef Q_QDOC
103  MenuController(MenuControllerPrivate &p, QObject *parent = 0);
104 #endif
105 
108  virtual ~MenuController();
113  QMenu *menu(bool deleteOnClose = true) const;
121  void addAction(const ActionGenerator *gen, const QList<QByteArray> &menu = QList<QByteArray>());
129  template <int N>
130  void addAction(const ActionGenerator *gen, const char (&menu)[N]);
131  bool removeAction(const ActionGenerator *gen);
138  static void addAction(const ActionGenerator *gen, const QMetaObject *meta,
139  const QList<QByteArray> &menu = QList<QByteArray>());
146  template <typename T>
147  static void addAction(const ActionGenerator *gen,
148  const QList<QByteArray> &menu = QList<QByteArray>());
156  template <typename T, int N>
157  static void addAction(const ActionGenerator *gen, const char (&menu)[N]);
165  template <int N>
166  static void addAction(const ActionGenerator *gen, const QMetaObject *meta, const char (&menu)[N]);
180  static QObject *get(QAction *);
181  template <typename T>
182  static T get(QAction *);
183 public slots:
188  void showMenu(const QPoint &pos);
189 protected:
193  void setMenuOwner(MenuController *controller);
194  void setMenuFlags(const MenuFlags &flags);
195 #ifndef Q_QDOC
196  virtual ActionList dynamicActions() const;
197 #endif
198  virtual void virtual_hook(int id, void *data);
199  QScopedPointer<MenuControllerPrivate> d_ptr;
200 };
201 
202 // TODO: Implement class
203 // Warning: All legacy QAction's created for this container MUST be destroyed at it's death
204 // Notice: You should use QActionGroup as at DynamicMenu for actions triggering
205 // Think: May be wh should use this container inside DynamicMenu?
206 // Think: May be we should use filters also for menus?
207 class ActionContainerPrivate;
208 
210 {
211 public:
212  virtual ~ActionHandler();
213 
214  virtual void actionAdded(QAction *action, int index) = 0;
215  virtual void actionRemoved(int index) = 0;
216  virtual void actionsCleared() = 0;
217 };
218 
220 {
221  Q_DISABLE_COPY(ActionContainer)
222  Q_DECLARE_PRIVATE(ActionContainer)
223 public:
224  enum Filter { Invalid = -1, TypeMatch, TypeMismatch };
225  // Constructor
226  // Get all actions
227  ActionContainer();
228  ActionContainer(Filter filter, const QVariant &data);
229  ActionContainer(MenuController *controller);
230  // Get only actions, which satisfy filter, i.e. filter=TypeMatch, data=1
231  // for getting all actions with type == 1
232  ActionContainer(MenuController *controller, Filter filter, const QVariant &data);
233  // Destructor, I think it shouldn't be virtual
234  ~ActionContainer();
235 
236  void setController(MenuController *controller);
237  void show();
238  void hide();
239 
240  void addHandler(ActionHandler *handler);
241  void removeHandler(ActionHandler *handler);
242 
243  // Access to actions, they should be sorted by qutim_sdk_0_3::actionLessThan
244  int count() const;
245  int size() const;
246  // Can be accessed only after first ref's call
247  QAction *action(int index) const;
248  QList<QByteArray> menu(int index) const;
249  const ActionGenerator *generator(int index) const;
250 
251 private:
252  QScopedPointer<ActionContainerPrivate> d_ptr;
253 };
254 
255 template <int N>
256 Q_INLINE_TEMPLATE void MenuController::addAction(const ActionGenerator *gen, const char (&menu)[N])
257 {
258  addAction(gen, QByteArray::fromRawData(menu, N - 1).split('\0'));
259 }
260 
261 template <typename T>
262 Q_INLINE_TEMPLATE void MenuController::addAction(const ActionGenerator *gen, const QList<QByteArray> &menu)
263 {
264  addAction(gen, &T::staticMetaObject, menu);
265 }
266 
267 template <typename T, int N>
268 Q_INLINE_TEMPLATE void MenuController::addAction(const ActionGenerator *gen, const char (&menu)[N])
269 {
270  addAction(gen, &T::staticMetaObject, QByteArray::fromRawData(menu, N - 1).split('\0'));
271 }
272 
273 template <int N>
274 Q_INLINE_TEMPLATE void MenuController::addAction(const ActionGenerator *gen,
275  const QMetaObject *meta,
276  const char (&menu)[N])
277 {
278  addAction(gen, meta, QByteArray::fromRawData(menu, N - 1).split('\0'));
279 }
280 
281 }
282 
284 Q_DECLARE_OPERATORS_FOR_FLAGS(qutim_sdk_0_3::MenuController::MenuFlags)
285 
286 namespace qutim_sdk_0_3
287 {
288 
289 template<typename T>
290 Q_INLINE_TEMPLATE T MenuController::get(QAction *action)
291 {
292  return qobject_cast<T>(MenuController::get(action));
293 }
294 
295 }
296 
297 #endif // MENUUNIT_H
298 

Generated by Doxygen