Make QMenu fairly usable in QML plasmoids, separate out QMenu and QMenuItem classes

This commit is contained in:
Viranch Mehta 2011-08-17 03:22:25 +05:30
parent cd6d910a2f
commit c34d4ea456
6 changed files with 121 additions and 47 deletions

View File

@ -8,6 +8,7 @@ set(qtextracomponents_SRCS
qimageitem.cpp qimageitem.cpp
qiconitem.cpp qiconitem.cpp
qmenu.cpp qmenu.cpp
qmenuitem.cpp
) )
INCLUDE_DIRECTORIES( INCLUDE_DIRECTORIES(

View File

@ -19,15 +19,17 @@
#include "qmenu.h" #include "qmenu.h"
#include <QMenu> #include <QMenu>
#include <QDebug> #include <QApplication>
QMenuProxy::QMenuProxy (QObject *parent) QMenuProxy::QMenuProxy (QObject *parent)
: QObject (parent) : QObject (parent)
{ {
m_menu = new QMenu(0);
} }
QMenuProxy::~QMenuProxy() QMenuProxy::~QMenuProxy()
{ {
delete m_menu;
} }
QDeclarativeListProperty<QMenuItem> QMenuProxy::actions() QDeclarativeListProperty<QMenuItem> QMenuProxy::actions()
@ -47,15 +49,13 @@ QMenuItem *QMenuProxy::action(int index) const
void QMenuProxy::showMenu(int x, int y) void QMenuProxy::showMenu(int x, int y)
{ {
QList<QAction*> actions; m_menu->clear();
foreach(QMenuItem* item, m_actions) { foreach(QMenuItem* item, m_actions) {
actions.append(item->nativeAction()); m_menu->addAction(item->nativeAction());
} }
QAction *action = QMenu::exec(actions, QPoint(x,y)); QPoint screenPos = QApplication::activeWindow()->mapToGlobal(QPoint(x, y));
if (action) { m_menu->popup(screenPos);
emit actionTriggered(action->text());
}
} }
#include "qmenu.moc" #include "qmenu.moc"

View File

@ -20,45 +20,8 @@
#ifndef QMENU_PROXY_H #ifndef QMENU_PROXY_H
#define QMENU_PROXY_H #define QMENU_PROXY_H
#include <QAction>
#include <QDeclarativeListProperty> #include <QDeclarativeListProperty>
#include "qmenuitem.h"
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
{ {
@ -81,7 +44,8 @@ Q_SIGNALS:
void actionTriggered(QString itemName); void actionTriggered(QString itemName);
private: private:
QMenuItems m_actions; QList<QMenuItem*> m_actions;
QMenu *m_menu;
}; };
#endif //QMENU_PROXY_H #endif //QMENU_PROXY_H

View File

@ -0,0 +1,41 @@
/***************************************************************************
* Copyright 2011 Viranch Mehta <viranch.mehta@gmail.com> *
* *
* 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"

View File

@ -0,0 +1,67 @@
/***************************************************************************
* Copyright 2011 Viranch Mehta <viranch.mehta@gmail.com> *
* *
* 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 <QAction>
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

View File

@ -27,6 +27,7 @@
#include "qimageitem.h" #include "qimageitem.h"
#include "qiconitem.h" #include "qiconitem.h"
#include "qmenu.h" #include "qmenu.h"
#include "qmenuitem.h"
void QtExtraComponentsPlugin::registerTypes(const char *uri) void QtExtraComponentsPlugin::registerTypes(const char *uri)
{ {
@ -35,8 +36,8 @@ 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");
qmlRegisterType<QMenuItem>(uri, 0, 1, "QMenuItem");
} }