libqutim 0.2.80.1
|
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 <QtGui/QIcon> 00032 #include <QtGui/QAction> 00033 #include <QtCore/QEvent> 00034 #include <QAction> 00035 00036 namespace qutim_sdk_0_3 00037 { 00038 class ActionGenerator; 00039 class ActionGeneratorPrivate; 00040 class MenuController; 00041 class ActionToolBar; 00042 class ActionCollectionPrivate; 00043 class ActionValue; 00044 00045 //TBD list of all action roles 00046 enum ActionType 00047 { 00048 ActionTypeContactList = 0x01, //Actions in contactlist 00049 ActionTypeChatButton = 0x02, //Chat buttons 00050 ActionTypeAdditional = 0x04, //Additional modifier 00051 ActionTypeAccount = 0x8, 00052 ActionTypePreferences = 0x10, //QAction::PreferencesRole 00053 ActionTypeContactInfo = 0x20 00054 }; 00055 00056 Q_DECLARE_FLAGS(ActionsType,ActionType) 00057 00058 enum ActionHandlerType 00059 { 00060 ActionCreatedHandler, 00061 ActionVisibilityChangedHandler 00062 }; 00063 00064 Q_DECLARE_FLAGS(ActionHandlerTypes,ActionHandlerType) 00065 00066 class LIBQUTIM_EXPORT ActionCreatedEvent : public QEvent 00067 { 00068 public: 00069 ActionCreatedEvent(QAction *action, ActionGenerator *gen, QObject *controller); 00070 static QEvent::Type eventType(); 00071 QAction *action() const { return m_action; } 00072 ActionGenerator *generator() const { return m_gen; } 00073 QObject *controller() const {return m_con;} 00074 private: 00075 QAction *m_action; 00076 ActionGenerator *m_gen; 00077 QObject *m_con; 00078 }; 00079 00080 class LIBQUTIM_EXPORT ActionVisibilityChangedEvent : public QEvent 00081 { 00082 public: 00083 ActionVisibilityChangedEvent(QAction *action,QObject *controller, bool isVisible = true); 00084 static QEvent::Type eventType(); 00085 QAction *action() const { return m_action; } 00086 QObject *controller() const { return m_controller; } 00087 bool isVisible() const { return m_visible; } 00088 private: 00089 QAction *m_action; 00090 QObject *m_controller; 00091 bool m_visible; 00092 00093 }; 00094 00095 // TODO: Resolve problem with action groups, checkable actions and so one 00096 // Possible solutions: 00097 // * add flag for creating 'unique' actions with different instances for different objects 00098 // * add new quint64 property, named 'groupId', which should be unique for every action 00099 // and equal for action groups (use algorithm equal to message ids), if core founds 00100 // actions with similiar group ids it addes them to QActionGroup 00101 // 00102 // Also It should be usefull to create some class-listener for unique actions, for such 00103 // events as ActionCreated, ActionDestroyed and with ability for changing them during 00104 // their lifetime (making disabled/checked and so on) 00105 // It should be usefull to create method for getting QAction for MenuController passed 00106 // as argument. BTW, unique actions should be created only once for each object, use i.e. 00107 // QWeakPointer at cache and QSharedPointer at ActionContainer 00108 // 00109 // Try to remove any use of legacy ActionGenerator's members everywhere at qutIM 00110 class LIBQUTIM_EXPORT ActionGenerator : public ObjectGenerator 00111 { 00112 Q_DECLARE_PRIVATE(ActionGenerator) 00113 Q_DISABLE_COPY(ActionGenerator) 00114 Q_GADGET 00115 public: 00116 enum Type { StatusType = 0, GeneralType }; 00117 /* 00118 * \code 00119 void MyObject::onAction(QObject *obj) 00120 { 00121 Account *account = qobject_cast<Account*>(obj); 00122 Q_ASSERT(account); 00123 doStuff(); 00124 } 00125 \endcode 00126 */ 00127 ActionGenerator(const QIcon &icon, const LocalizedString &text, const QObject *receiver, const char *member); 00128 // Convience constructor for menus 00129 ActionGenerator(const QIcon &icon, const LocalizedString &text, const char *member); 00130 #ifndef Q_QDOC 00131 ActionGenerator(ActionGeneratorPrivate &priv); 00132 #endif 00133 virtual ~ActionGenerator(); 00134 QIcon icon() const; 00135 const LocalizedString &text() const; 00136 const QObject *receiver() const; 00137 const char *member() const; 00138 ActionGenerator *addProperty(const QByteArray &name, const QVariant &value); 00139 int type() const; 00140 ActionGenerator *setType(int type); 00141 int priority() const; 00142 ActionGenerator *setPriority(int priority); 00143 void setMenuController(MenuController *controller); 00144 void subscribe(QObject *object, const char *method); 00145 //void unsubscribe(QObject *object); //TODO implement me 00146 void addHandler(int type, QObject *obj); 00147 void removeHandler(int type, QObject *obj); 00148 void setCheckable(bool checkable); 00149 void setChecked(bool checked); 00150 void setToolTip(const LocalizedString &toolTip); 00151 void setShortcut(const QKeySequence &shortcut); 00152 void setShortcut(const QString &id); 00153 QString shortcut() const; 00154 void setMenuRole(QAction::MenuRole role); 00155 QAction::MenuRole menuRole() const; 00156 void setIconVisibleInMenu(bool visible); 00157 bool iconVisibleInMenu() const; 00158 QList<QAction*> actions(QObject *object) const; 00159 QMap<QObject*, QAction*> actions() const; 00160 static ActionGenerator *get(QAction *); 00161 protected: 00162 QAction *prepareAction(QAction *action) const; 00163 virtual QObject *generateHelper() const; 00164 virtual const QMetaObject *metaObject() const; 00165 virtual QList<QByteArray> interfaces() const; 00166 void create(QAction *action,QObject *obj) const; 00167 virtual void createImpl(QAction *action,QObject *obj) const; 00168 virtual void showImpl(QAction *action,QObject *obj); //obj usally is controller, default implementation do nothing 00169 virtual void hideImpl(QAction *action,QObject *obj); 00170 private: 00171 friend class MenuController; 00172 friend class ActionToolBar; 00173 friend class MenuControllerPrivate; 00174 friend class ActionValue; 00175 friend class ActionContainerPrivate; 00176 }; 00177 00178 class LIBQUTIM_EXPORT MenuActionGenerator : public ActionGenerator 00179 { 00180 Q_DECLARE_PRIVATE(ActionGenerator) 00181 Q_DISABLE_COPY(MenuActionGenerator) 00182 public: 00183 MenuActionGenerator(const QIcon &icon, const LocalizedString &text, QMenu *menu); 00184 MenuActionGenerator(const QIcon &icon, const LocalizedString &text, MenuController *controller); 00185 virtual ~MenuActionGenerator(); 00186 protected: 00187 virtual QObject *generateHelper() const; 00188 }; 00189 } 00190 00191 Q_DECLARE_METATYPE(qutim_sdk_0_3::ActionGenerator*) 00192 00193 #endif // ACTIONGENERATOR_H 00194