libqutim  0.3.2.0
actiongenerator.h
Go to the documentation of this file.
00001 /****************************************************************************
00002 **
00003 ** qutIM - instant messenger
00004 **
00005 ** Copyright © 2011 Ruslan Nigmatullin <euroelessar@yandex.ru>
00006 **
00007 *****************************************************************************
00008 **
00009 ** $QUTIM_BEGIN_LICENSE$
00010 ** This program is free software: you can redistribute it and/or modify
00011 ** it under the terms of the GNU General Public License as published by
00012 ** the Free Software Foundation, either version 3 of the License, or
00013 ** (at your option) any later version.
00014 **
00015 ** This program is distributed in the hope that it will be useful,
00016 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00018 ** See the GNU General Public License for more details.
00019 **
00020 ** You should have received a copy of the GNU General Public License
00021 ** along with this program.  If not, see http://www.gnu.org/licenses/.
00022 ** $QUTIM_END_LICENSE$
00023 **
00024 ****************************************************************************/
00025 
00026 #ifndef ACTIONGENERATOR_H
00027 #define ACTIONGENERATOR_H
00028 
00029 #include "objectgenerator.h"
00030 #include "localizedstring.h"
00031 #include <QIcon>
00032 #include <QEvent>
00033 #include <QAction>
00034 
00035 namespace qutim_sdk_0_3
00036 {
00037 class ActionGenerator;
00038 class ActionGeneratorPrivate;
00039 class MenuController;
00040 class ActionToolBar;
00041 class ActionCollectionPrivate;
00042 class ActionValue;
00043 
00044 //TBD list of all action roles
00045 enum ActionType
00046 {
00047     ActionTypeContactList = 0x01, //Actions in contactlist
00048     ActionTypeChatButton = 0x02, //Chat buttons
00049     ActionTypeAdditional = 0x04, //Additional modifier
00050     ActionTypeAccount = 0x8,
00051     ActionTypePreferences = 0x10, //QAction::PreferencesRole
00052     ActionTypeContactInfo = 0x20
00053 };
00054 
00055 Q_DECLARE_FLAGS(ActionsType,ActionType)
00056 
00057 enum ActionHandlerType
00058 {
00059     ActionCreatedHandler,
00060     ActionVisibilityChangedHandler
00061 };
00062 
00063 Q_DECLARE_FLAGS(ActionHandlerTypes,ActionHandlerType)
00064 
00065 class LIBQUTIM_EXPORT ActionCreatedEvent : public QEvent
00066 {
00067 public:
00068     ActionCreatedEvent(QAction *action, ActionGenerator *gen, QObject *controller);
00069     static QEvent::Type eventType();
00070     QAction *action() const { return m_action; }
00071     ActionGenerator *generator() const { return m_gen; }
00072     QObject *controller() const {return m_con;}
00073 private:
00074     QAction *m_action;
00075     ActionGenerator *m_gen;
00076     QObject *m_con;
00077 };
00078 
00079 class LIBQUTIM_EXPORT ActionVisibilityChangedEvent : public QEvent
00080 {
00081 public:
00082     ActionVisibilityChangedEvent(QAction *action,QObject *controller, bool isVisible = true);
00083     static QEvent::Type eventType();
00084     QAction *action() const { return m_action; }
00085     QObject *controller() const { return m_controller; }
00086     bool isVisible() const { return m_visible; }
00087 private:
00088     QAction *m_action;
00089     QObject *m_controller;
00090     bool m_visible;
00091 
00092 };
00093 
00094 // TODO: Resolve problem with action groups, checkable actions and so one
00095 // Possible solutions:
00096 // * add flag for creating 'unique' actions with different instances for different objects
00097 // * add new quint64 property, named 'groupId', which should be unique for every action
00098 //   and equal for action groups (use algorithm equal to message ids), if core founds
00099 //   actions with similiar group ids it addes them to QActionGroup
00100 //
00101 // Also It should be usefull to create some class-listener for unique actions, for such
00102 // events as ActionCreated, ActionDestroyed and with ability for changing them during
00103 // their lifetime (making disabled/checked and so on)
00104 // It should be usefull to create method for getting QAction for MenuController passed
00105 // as argument. BTW, unique actions should be created only once for each object, use i.e.
00106 // QPointer at cache and QSharedPointer at ActionContainer
00107 //
00108 // Try to remove any use of legacy ActionGenerator's members everywhere at qutIM
00109 class LIBQUTIM_EXPORT ActionGenerator : public ObjectGenerator
00110 {
00111     Q_DECLARE_PRIVATE(ActionGenerator)
00112     Q_DISABLE_COPY(ActionGenerator)
00113     Q_GADGET
00114 public:
00115     enum Type { StatusType = 0, GeneralType };
00116     /*
00117    * \code
00118 void MyObject::onAction(QObject *obj)
00119 {
00120  Account *account = qobject_cast<Account*>(obj);
00121  Q_ASSERT(account);
00122  doStuff();
00123 }
00124     \endcode
00125    */
00126     ActionGenerator(const QIcon &icon, const LocalizedString &text, const QObject *receiver, const char *member);
00127     // Convience constructor for menus
00128     ActionGenerator(const QIcon &icon, const LocalizedString &text, const char *member);
00129 #ifndef Q_QDOC
00130     ActionGenerator(ActionGeneratorPrivate &priv);
00131 #endif
00132     virtual ~ActionGenerator();
00133     QIcon icon() const;
00134     const LocalizedString &text() const;
00135     const QObject *receiver() const;
00136     const char *member() const;
00137     ActionGenerator *addProperty(const QByteArray &name, const QVariant &value);
00138     int type() const;
00139     ActionGenerator *setType(int type);
00140     int priority() const;
00141     ActionGenerator *setPriority(int priority);
00142     void setMenuController(MenuController *controller);
00143     void subscribe(QObject *object, const char *method);
00144     //void unsubscribe(QObject *object); //TODO implement me
00145     void addHandler(int type, QObject *obj);
00146     void removeHandler(int type, QObject *obj);
00147     void setCheckable(bool checkable);
00148     void setChecked(bool checked);
00149     void setToolTip(const LocalizedString &toolTip);
00150     void setShortcut(const QKeySequence &shortcut);
00151     void setShortcut(const QString &id);
00152     QString shortcut() const;
00153     void setMenuRole(QAction::MenuRole role);
00154     QAction::MenuRole menuRole() const;
00155     void setIconVisibleInMenu(bool visible);
00156     bool iconVisibleInMenu() const;
00157     QList<QAction*> actions(QObject *object) const;
00158     QMap<QObject*, QAction*> actions() const;
00159     static ActionGenerator *get(QAction *);
00160 protected:
00161     QAction *prepareAction(QAction *action) const;
00162     virtual QObject *generateHelper() const;
00163     virtual const QMetaObject *metaObject() const;
00164     virtual QList<QByteArray> interfaces() const;
00165     void create(QAction *action,QObject *obj) const;
00166     virtual void createImpl(QAction *action,QObject *obj) const;
00167     virtual void showImpl(QAction *action,QObject *obj); //obj usally is controller, default implementation do nothing
00168     virtual void hideImpl(QAction *action,QObject *obj);
00169 private:
00170     friend class MenuController;
00171     friend class ActionToolBar;
00172     friend class MenuControllerPrivate;
00173     friend class ActionValue;
00174     friend class ActionContainerPrivate;
00175 };
00176 
00177 class LIBQUTIM_EXPORT MenuActionGenerator : public ActionGenerator
00178 {
00179     Q_DECLARE_PRIVATE(ActionGenerator)
00180     Q_DISABLE_COPY(MenuActionGenerator)
00181 public:
00182     MenuActionGenerator(const QIcon &icon, const LocalizedString &text, QMenu *menu);
00183     MenuActionGenerator(const QIcon &icon, const LocalizedString &text, MenuController *controller);
00184     virtual ~MenuActionGenerator();
00185 protected:
00186     virtual QObject *generateHelper() const;
00187 };
00188 }
00189 
00190 Q_DECLARE_METATYPE(qutim_sdk_0_3::ActionGenerator*)
00191 
00192 #endif // ACTIONGENERATOR_H
00193