diff --git a/declarativeimports/qtextracomponents/CMakeLists.txt b/declarativeimports/qtextracomponents/CMakeLists.txt index 7c3f5c5c9..f395e632f 100644 --- a/declarativeimports/qtextracomponents/CMakeLists.txt +++ b/declarativeimports/qtextracomponents/CMakeLists.txt @@ -8,6 +8,7 @@ set(qtextracomponents_SRCS qimageitem.cpp qiconitem.cpp qmenu.cpp + qmenuitem.cpp ) INCLUDE_DIRECTORIES( diff --git a/declarativeimports/qtextracomponents/qmenu.cpp b/declarativeimports/qtextracomponents/qmenu.cpp index cc51fafcf..a6a41ff1b 100644 --- a/declarativeimports/qtextracomponents/qmenu.cpp +++ b/declarativeimports/qtextracomponents/qmenu.cpp @@ -19,15 +19,17 @@ #include "qmenu.h" #include -#include +#include QMenuProxy::QMenuProxy (QObject *parent) : QObject (parent) { + m_menu = new QMenu(0); } QMenuProxy::~QMenuProxy() { + delete m_menu; } QDeclarativeListProperty QMenuProxy::actions() @@ -47,15 +49,13 @@ QMenuItem *QMenuProxy::action(int index) const void QMenuProxy::showMenu(int x, int y) { - QList actions; + m_menu->clear(); foreach(QMenuItem* item, m_actions) { - actions.append(item->nativeAction()); + m_menu->addAction(item->nativeAction()); } - QAction *action = QMenu::exec(actions, QPoint(x,y)); - if (action) { - emit actionTriggered(action->text()); - } + QPoint screenPos = QApplication::activeWindow()->mapToGlobal(QPoint(x, y)); + m_menu->popup(screenPos); } #include "qmenu.moc" diff --git a/declarativeimports/qtextracomponents/qmenu.h b/declarativeimports/qtextracomponents/qmenu.h index 73e928f8a..489e4d5c6 100644 --- a/declarativeimports/qtextracomponents/qmenu.h +++ b/declarativeimports/qtextracomponents/qmenu.h @@ -20,45 +20,8 @@ #ifndef QMENU_PROXY_H #define QMENU_PROXY_H -#include #include - -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 QMenuItems; +#include "qmenuitem.h" class QMenuProxy : public QObject { @@ -81,7 +44,8 @@ Q_SIGNALS: void actionTriggered(QString itemName); private: - QMenuItems m_actions; + QList m_actions; + QMenu *m_menu; }; #endif //QMENU_PROXY_H diff --git a/declarativeimports/qtextracomponents/qmenuitem.cpp b/declarativeimports/qtextracomponents/qmenuitem.cpp new file mode 100644 index 000000000..bf4ba9b30 --- /dev/null +++ b/declarativeimports/qtextracomponents/qmenuitem.cpp @@ -0,0 +1,41 @@ +/*************************************************************************** + * Copyright 2011 Viranch Mehta * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA . * + ***************************************************************************/ + +#include "qmenuitem.h" + +QMenuItem::QMenuItem(QObject *parent) + : QObject(parent) +{ + m_action = new QAction(0); + connect (m_action, SIGNAL(triggered(bool)), + this, SLOT(emitTriggered(bool))); +} + +QMenuItem::~QMenuItem() +{ + delete m_action; +} + +void QMenuItem::emitTriggered(bool checked) +{ + emit triggered(); +} + +#include "qmenuitem.moc" + diff --git a/declarativeimports/qtextracomponents/qmenuitem.h b/declarativeimports/qtextracomponents/qmenuitem.h new file mode 100644 index 000000000..2cd12b0b3 --- /dev/null +++ b/declarativeimports/qtextracomponents/qmenuitem.h @@ -0,0 +1,67 @@ +/*************************************************************************** + * Copyright 2011 Viranch Mehta * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA . * + ***************************************************************************/ + +#ifndef QMENUITEM_H +#define QMENUITEM_H + +#include + +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); + ~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; } + +public Q_SLOTS: + void emitTriggered(bool checked); + +Q_SIGNALS: + void triggered(); + +private: + QAction *m_action; +}; + +#endif // QMENUITEM_H + diff --git a/declarativeimports/qtextracomponents/qtextracomponentsplugin.cpp b/declarativeimports/qtextracomponents/qtextracomponentsplugin.cpp index 821db06a1..5bb8529dc 100644 --- a/declarativeimports/qtextracomponents/qtextracomponentsplugin.cpp +++ b/declarativeimports/qtextracomponents/qtextracomponentsplugin.cpp @@ -27,6 +27,7 @@ #include "qimageitem.h" #include "qiconitem.h" #include "qmenu.h" +#include "qmenuitem.h" void QtExtraComponentsPlugin::registerTypes(const char *uri) { @@ -35,8 +36,8 @@ void QtExtraComponentsPlugin::registerTypes(const char *uri) qmlRegisterType(uri, 0, 1, "QPixmapItem"); qmlRegisterType(uri, 0, 1, "QImageItem"); qmlRegisterType(uri, 0, 1, "QIconItem"); - qmlRegisterType(uri, 0, 1, "QMenuItem"); qmlRegisterType(uri, 0, 1, "QMenu"); + qmlRegisterType(uri, 0, 1, "QMenuItem"); }