Implement ability to add actions in the QMenu and hence create QMenuItem
This commit is contained in:
parent
2c6677aaf4
commit
cd6d910a2f
@ -18,25 +18,44 @@
|
|||||||
***************************************************************************/
|
***************************************************************************/
|
||||||
|
|
||||||
#include "qmenu.h"
|
#include "qmenu.h"
|
||||||
|
#include <QMenu>
|
||||||
|
#include <QDebug>
|
||||||
|
|
||||||
QMenuProxy::QMenuProxy (QObject *parent)
|
QMenuProxy::QMenuProxy (QObject *parent)
|
||||||
: QObject (parent)
|
: QObject (parent)
|
||||||
{
|
{
|
||||||
m_menu = new QMenu();
|
|
||||||
|
|
||||||
/* Test entries, should be removed later */
|
|
||||||
m_menu->addAction ("hello");
|
|
||||||
m_menu->addAction ("world");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
QMenuProxy::~QMenuProxy()
|
QMenuProxy::~QMenuProxy()
|
||||||
{
|
{
|
||||||
delete m_menu;
|
}
|
||||||
|
|
||||||
|
QDeclarativeListProperty<QMenuItem> QMenuProxy::actions()
|
||||||
|
{
|
||||||
|
return QDeclarativeListProperty<QMenuItem>(this, m_actions);
|
||||||
|
}
|
||||||
|
|
||||||
|
int QMenuProxy::actionCount() const
|
||||||
|
{
|
||||||
|
return m_actions.count();
|
||||||
|
}
|
||||||
|
|
||||||
|
QMenuItem *QMenuProxy::action(int index) const
|
||||||
|
{
|
||||||
|
return m_actions.at(index);
|
||||||
}
|
}
|
||||||
|
|
||||||
void QMenuProxy::showMenu(int x, int y)
|
void QMenuProxy::showMenu(int x, int y)
|
||||||
{
|
{
|
||||||
m_menu->popup(QPoint(x,y));
|
QList<QAction*> actions;
|
||||||
|
foreach(QMenuItem* item, m_actions) {
|
||||||
|
actions.append(item->nativeAction());
|
||||||
|
}
|
||||||
|
|
||||||
|
QAction *action = QMenu::exec(actions, QPoint(x,y));
|
||||||
|
if (action) {
|
||||||
|
emit actionTriggered(action->text());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#include "qmenu.moc"
|
#include "qmenu.moc"
|
||||||
|
@ -20,21 +20,68 @@
|
|||||||
#ifndef QMENU_PROXY_H
|
#ifndef QMENU_PROXY_H
|
||||||
#define QMENU_PROXY_H
|
#define QMENU_PROXY_H
|
||||||
|
|
||||||
#include <QObject>
|
#include <QAction>
|
||||||
#include <QMenu>
|
#include <QDeclarativeListProperty>
|
||||||
|
|
||||||
|
class QMenuItem : public QObject
|
||||||
|
{
|
||||||
|
Q_OBJECT
|
||||||
|
|
||||||
|
Q_PROPERTY(bool checkable READ checkable WRITE setCheckable)
|
||||||
|
Q_PROPERTY(bool checked READ checked WRITE setChecked)
|
||||||
|
Q_PROPERTY(bool enabled READ enabled WRITE setEnabled)
|
||||||
|
Q_PROPERTY(QString text READ text WRITE setText)
|
||||||
|
Q_PROPERTY(QIcon icon READ icon WRITE setIcon)
|
||||||
|
|
||||||
|
public:
|
||||||
|
QMenuItem(QObject *parent = 0) : QObject(parent) { m_action = new QAction(0); }
|
||||||
|
~QMenuItem() { }
|
||||||
|
|
||||||
|
bool enabled() const { return m_action->isEnabled(); }
|
||||||
|
void setEnabled(const bool enabled) { m_action->setEnabled(enabled); }
|
||||||
|
|
||||||
|
bool checkable() const { return m_action->isCheckable(); }
|
||||||
|
void setCheckable(const bool checkable) { m_action->setCheckable(checkable); }
|
||||||
|
|
||||||
|
bool checked() const { return m_action->isChecked(); }
|
||||||
|
void setChecked(const bool checked) { m_action->setChecked(checked); }
|
||||||
|
|
||||||
|
QString text() const { return m_action->text(); }
|
||||||
|
void setText(const QString &text) { m_action->setText(text); }
|
||||||
|
|
||||||
|
QIcon icon() const { return m_action->icon(); }
|
||||||
|
void setIcon(const QIcon &icon) { m_action->setIcon(icon); }
|
||||||
|
|
||||||
|
QAction *nativeAction() { return m_action; }
|
||||||
|
|
||||||
|
private:
|
||||||
|
QAction *m_action;
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef QList<QMenuItem*> QMenuItems;
|
||||||
|
|
||||||
class QMenuProxy : public QObject
|
class QMenuProxy : public QObject
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
|
Q_PROPERTY(QDeclarativeListProperty<QMenuItem> actions READ actions)
|
||||||
|
Q_CLASSINFO("DefaultProperty", "actions")
|
||||||
|
|
||||||
public:
|
public:
|
||||||
QMenuProxy(QObject *parent = 0);
|
QMenuProxy(QObject *parent = 0);
|
||||||
~QMenuProxy();
|
~QMenuProxy();
|
||||||
|
|
||||||
|
QDeclarativeListProperty<QMenuItem> actions();
|
||||||
|
int actionCount() const;
|
||||||
|
QMenuItem *action(int) const;
|
||||||
|
|
||||||
Q_INVOKABLE void showMenu(int x, int y);
|
Q_INVOKABLE void showMenu(int x, int y);
|
||||||
|
|
||||||
|
Q_SIGNALS:
|
||||||
|
void actionTriggered(QString itemName);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
QMenu *m_menu;
|
QMenuItems m_actions;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif //QMENU_PROXY_H
|
#endif //QMENU_PROXY_H
|
||||||
|
@ -35,6 +35,7 @@ void QtExtraComponentsPlugin::registerTypes(const char *uri)
|
|||||||
qmlRegisterType<QPixmapItem>(uri, 0, 1, "QPixmapItem");
|
qmlRegisterType<QPixmapItem>(uri, 0, 1, "QPixmapItem");
|
||||||
qmlRegisterType<QImageItem>(uri, 0, 1, "QImageItem");
|
qmlRegisterType<QImageItem>(uri, 0, 1, "QImageItem");
|
||||||
qmlRegisterType<QIconItem>(uri, 0, 1, "QIconItem");
|
qmlRegisterType<QIconItem>(uri, 0, 1, "QIconItem");
|
||||||
|
qmlRegisterType<QMenuItem>(uri, 0, 1, "QMenuItem");
|
||||||
qmlRegisterType<QMenuProxy>(uri, 0, 1, "QMenu");
|
qmlRegisterType<QMenuProxy>(uri, 0, 1, "QMenu");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user