libqutim 0.3.1.0
menucontroller.h
Go to the documentation of this file.
00001 /****************************************************************************
00002 **
00003 ** qutIM - instant messenger
00004 **
00005 ** Copyright © 2011 Ruslan Nigmatullin <euroelessar@yandex.ru>
00006 ** Copyright © 2011 Aleksey Sidorov <gorthauer87@yandex.ru>
00007 **
00008 *****************************************************************************
00009 **
00010 ** $QUTIM_BEGIN_LICENSE$
00011 ** This program is free software: you can redistribute it and/or modify
00012 ** it under the terms of the GNU General Public License as published by
00013 ** the Free Software Foundation, either version 3 of the License, or
00014 ** (at your option) any later version.
00015 **
00016 ** This program is distributed in the hope that it will be useful,
00017 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
00018 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00019 ** See the GNU General Public License for more details.
00020 **
00021 ** You should have received a copy of the GNU General Public License
00022 ** along with this program.  If not, see http://www.gnu.org/licenses/.
00023 ** $QUTIM_END_LICENSE$
00024 **
00025 ****************************************************************************/
00026 
00027 #ifndef MENUUNIT_H
00028 #define MENUUNIT_H
00029 
00030 #include "actiongenerator.h"
00031 #include <QtGui/QMenu>
00032 #include "debug.h"
00033 
00034 namespace qutim_sdk_0_3
00035 {
00036 
00037 class SettingsItem;
00038 typedef QList<SettingsItem *> SettingsItemList;
00039 
00040 class MenuControllerPrivate;
00041 struct ActionInfo;
00042 
00076 class LIBQUTIM_EXPORT MenuController : public QObject
00077 {
00078     Q_OBJECT
00079     Q_DISABLE_COPY(MenuController)
00080     Q_DECLARE_PRIVATE(MenuController)
00081 public:
00082     enum MenuFlag
00083     {
00084         ShowSelfActions     = 0x01,
00085         ShowSuperActions    = 0x02,
00086         ShowOwnerActions    = 0x04
00087     };
00088     Q_DECLARE_FLAGS(MenuFlags, MenuFlag)
00089 
00090 #ifndef Q_QDOC
00091     struct Action
00092     {
00093         ActionGenerator *generator;
00094         QList<QByteArray> menu;
00095     };
00096     typedef QList<Action> ActionList;
00097 #endif
00098 
00101     MenuController(QObject *parent = 0);
00102 #ifndef Q_QDOC
00103     MenuController(MenuControllerPrivate &p, QObject *parent = 0);
00104 #endif
00105 
00108     virtual ~MenuController();
00113     QMenu *menu(bool deleteOnClose = true) const;
00121     void addAction(const ActionGenerator *gen, const QList<QByteArray> &menu = QList<QByteArray>());
00129     template <int N>
00130     void addAction(const ActionGenerator *gen, const char (&menu)[N]);
00131     bool removeAction(const ActionGenerator *gen);
00138     static void addAction(const ActionGenerator *gen, const QMetaObject *meta,
00139                           const QList<QByteArray> &menu = QList<QByteArray>());
00146     template <typename T>
00147     static void addAction(const ActionGenerator *gen,
00148                           const QList<QByteArray> &menu = QList<QByteArray>());
00156     template <typename T, int N>
00157     static void addAction(const ActionGenerator *gen, const char (&menu)[N]);
00165     template <int N>
00166     static void addAction(const ActionGenerator *gen, const QMetaObject *meta, const char (&menu)[N]);
00180     static QObject *get(QAction *);
00181     template <typename T>
00182     static T get(QAction *);
00183 public slots:
00188     void showMenu(const QPoint &pos);
00189 protected:
00193     void setMenuOwner(MenuController *controller);
00194     void setMenuFlags(const MenuFlags &flags);
00195 #ifndef Q_QDOC
00196     virtual ActionList dynamicActions() const;
00197 #endif
00198     virtual void virtual_hook(int id, void *data);
00199     QScopedPointer<MenuControllerPrivate> d_ptr;
00200 };
00201 
00202 // TODO: Implement class
00203 // Warning: All legacy QAction's created for this container MUST be destroyed at it's death
00204 // Notice: You should use QActionGroup as at DynamicMenu for actions triggering
00205 // Think: May be wh should use this container inside DynamicMenu?
00206 // Think: May be we should use filters also for menus?
00207 class ActionContainerPrivate;
00208 
00209 class LIBQUTIM_EXPORT ActionHandler
00210 {
00211 public:
00212     virtual ~ActionHandler();
00213     
00214     virtual void actionAdded(QAction *action, int index) = 0;
00215     virtual void actionRemoved(int index) = 0;
00216     virtual void actionsCleared() = 0;
00217 };
00218 
00219 class LIBQUTIM_EXPORT ActionContainer
00220 {
00221     Q_DISABLE_COPY(ActionContainer)
00222     Q_DECLARE_PRIVATE(ActionContainer)
00223 public:
00224     enum Filter { Invalid = -1, TypeMatch, TypeMismatch };
00225     // Constructor
00226     // Get all actions
00227     ActionContainer();
00228     ActionContainer(Filter filter, const QVariant &data);
00229     ActionContainer(MenuController *controller);
00230     // Get only actions, which satisfy filter, i.e. filter=TypeMatch, data=1
00231     // for getting all actions with type == 1
00232     ActionContainer(MenuController *controller, Filter filter, const QVariant &data);
00233     // Destructor, I think it shouldn't be virtual
00234     ~ActionContainer();
00235     
00236     void setController(MenuController *controller);
00237     void show();
00238     void hide();
00239     
00240     void addHandler(ActionHandler *handler);
00241     void removeHandler(ActionHandler *handler);
00242 
00243     // Access to actions, they should be sorted by qutim_sdk_0_3::actionLessThan
00244     int count() const;
00245     int size() const;
00246     // Can be accessed only after first ref's call
00247     QAction *action(int index) const;
00248     QList<QByteArray> menu(int index) const;
00249     const ActionGenerator *generator(int index) const;
00250 
00251 private:
00252     QScopedPointer<ActionContainerPrivate> d_ptr;
00253 };
00254 
00255 template <int N>
00256 Q_INLINE_TEMPLATE void MenuController::addAction(const ActionGenerator *gen, const char (&menu)[N])
00257 {
00258     addAction(gen, QByteArray::fromRawData(menu, N - 1).split('\0'));
00259 }
00260 
00261 template <typename T>
00262 Q_INLINE_TEMPLATE void MenuController::addAction(const ActionGenerator *gen, const QList<QByteArray> &menu)
00263 {
00264     addAction(gen, &T::staticMetaObject, menu);
00265 }
00266 
00267 template <typename T, int N>
00268 Q_INLINE_TEMPLATE void MenuController::addAction(const ActionGenerator *gen, const char (&menu)[N])
00269 {
00270     addAction(gen, &T::staticMetaObject, QByteArray::fromRawData(menu, N - 1).split('\0'));
00271 }
00272 
00273 template <int N>
00274 Q_INLINE_TEMPLATE void MenuController::addAction(const ActionGenerator *gen,
00275                                                  const QMetaObject *meta,
00276                                                  const char (&menu)[N])
00277 {
00278     addAction(gen, meta, QByteArray::fromRawData(menu, N - 1).split('\0'));
00279 }
00280 
00281 }
00282 
00283 Q_DECLARE_METATYPE(qutim_sdk_0_3::MenuController*)
00284 Q_DECLARE_OPERATORS_FOR_FLAGS(qutim_sdk_0_3::MenuController::MenuFlags)
00285 
00286 namespace qutim_sdk_0_3
00287 {
00288 
00289 template<typename T>
00290 Q_INLINE_TEMPLATE T MenuController::get(QAction *action)
00291 {
00292     return qobject_cast<T>(MenuController::get(action));
00293 }
00294 
00295 }
00296 
00297 #endif // MENUUNIT_H
00298