2011-08-02 16:13:48 +02:00
|
|
|
/***************************************************************************
|
|
|
|
* 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 "qmenu.h"
|
2011-10-28 18:57:34 +02:00
|
|
|
|
2011-08-16 23:52:25 +02:00
|
|
|
#include <QApplication>
|
2011-10-28 20:14:38 +02:00
|
|
|
#include <QDebug>
|
|
|
|
#include <QDesktopWidget>
|
2011-10-28 18:57:34 +02:00
|
|
|
#include <QGraphicsObject>
|
|
|
|
#include <QGraphicsView>
|
2011-11-08 23:07:42 +01:00
|
|
|
#include <QDeclarativeItem>
|
2011-08-02 16:13:48 +02:00
|
|
|
|
2012-01-09 11:41:28 +01:00
|
|
|
#include "plasmacomponentsplugin.h"
|
2011-08-02 16:13:48 +02:00
|
|
|
QMenuProxy::QMenuProxy (QObject *parent)
|
2011-10-28 19:40:04 +02:00
|
|
|
: QObject(parent),
|
|
|
|
m_status(DialogStatus::Closed)
|
2012-01-09 12:41:21 +01:00
|
|
|
{
|
2011-08-16 23:52:25 +02:00
|
|
|
m_menu = new QMenu(0);
|
2011-12-21 19:52:35 +01:00
|
|
|
connect(m_menu, SIGNAL(triggered(QAction *)),
|
|
|
|
this, SLOT(itemTriggered(QAction *)));
|
2011-08-02 16:13:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QMenuProxy::~QMenuProxy()
|
|
|
|
{
|
2011-08-16 23:52:25 +02:00
|
|
|
delete m_menu;
|
2011-08-07 01:41:21 +02:00
|
|
|
}
|
|
|
|
|
2011-12-29 15:09:58 +01:00
|
|
|
QDeclarativeListProperty<QMenuItem> QMenuProxy::content()
|
2011-08-07 01:41:21 +02:00
|
|
|
{
|
2011-10-28 19:40:04 +02:00
|
|
|
return QDeclarativeListProperty<QMenuItem>(this, m_items);
|
2011-08-07 01:41:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int QMenuProxy::actionCount() const
|
|
|
|
{
|
2011-10-28 19:40:04 +02:00
|
|
|
return m_items.count();
|
2011-08-07 01:41:21 +02:00
|
|
|
}
|
|
|
|
|
2011-10-28 18:57:34 +02:00
|
|
|
QMenuItem *QMenuProxy::action(int index) const
|
2011-08-07 01:41:21 +02:00
|
|
|
{
|
2011-10-28 19:40:04 +02:00
|
|
|
return m_items.at(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
DialogStatus::Status QMenuProxy::status() const
|
|
|
|
{
|
|
|
|
return m_status;
|
2011-08-02 16:13:48 +02:00
|
|
|
}
|
|
|
|
|
2011-12-30 16:35:55 +01:00
|
|
|
QObject *QMenuProxy::visualParent() const
|
2011-11-08 23:07:42 +01:00
|
|
|
{
|
|
|
|
return m_visualParent.data();
|
|
|
|
}
|
|
|
|
|
2011-12-30 16:35:55 +01:00
|
|
|
void QMenuProxy::setVisualParent(QObject *parent)
|
2011-11-08 23:07:42 +01:00
|
|
|
{
|
|
|
|
if (m_visualParent.data() == parent) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-12-30 16:35:55 +01:00
|
|
|
//if the old parent was a QAction, disconnect the menu from it
|
|
|
|
QAction *action = qobject_cast<QAction *>(m_visualParent.data());
|
|
|
|
if (action) {
|
|
|
|
action->setMenu(0);
|
|
|
|
m_menu->clear();
|
|
|
|
}
|
|
|
|
//if parent is a QAction, become a submenu
|
|
|
|
action = qobject_cast<QAction *>(parent);
|
|
|
|
if (action) {
|
|
|
|
action->setMenu(m_menu);
|
|
|
|
m_menu->clear();
|
|
|
|
foreach(QMenuItem* item, m_items) {
|
2011-12-30 17:48:14 +01:00
|
|
|
m_menu->addAction(item);
|
2011-12-30 16:35:55 +01:00
|
|
|
}
|
|
|
|
m_menu->updateGeometry();
|
|
|
|
}
|
|
|
|
|
2011-11-08 23:07:42 +01:00
|
|
|
m_visualParent = parent;
|
|
|
|
emit visualParentChanged();
|
|
|
|
}
|
|
|
|
|
2011-12-29 15:09:58 +01:00
|
|
|
bool QMenuProxy::event(QEvent *event)
|
|
|
|
{
|
|
|
|
switch (event->type()) {
|
|
|
|
case QEvent::ChildAdded: {
|
|
|
|
QChildEvent *ce = static_cast<QChildEvent *>(event);
|
|
|
|
QMenuItem *mi = qobject_cast<QMenuItem *>(ce->child());
|
|
|
|
//FIXME: linear complexity here
|
|
|
|
if (mi && !m_items.contains(mi)) {
|
2011-12-30 17:48:14 +01:00
|
|
|
m_menu->addAction(mi);
|
2011-12-29 15:09:58 +01:00
|
|
|
m_items << mi;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case QEvent::ChildRemoved: {
|
|
|
|
QChildEvent *ce = static_cast<QChildEvent *>(event);
|
|
|
|
QMenuItem *mi = qobject_cast<QMenuItem *>(ce->child());
|
|
|
|
|
|
|
|
//FIXME: linear complexity here
|
|
|
|
if (mi) {
|
2011-12-30 17:48:14 +01:00
|
|
|
m_menu->removeAction(mi);
|
2011-12-29 15:09:58 +01:00
|
|
|
m_items.removeAll(mi);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return QObject::event(event);
|
|
|
|
}
|
|
|
|
|
2011-12-20 15:36:26 +01:00
|
|
|
void QMenuProxy::clearMenuItems()
|
|
|
|
{
|
|
|
|
qDeleteAll(m_items);
|
|
|
|
m_items.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void QMenuProxy::addMenuItem(const QString &text)
|
|
|
|
{
|
|
|
|
QMenuItem *item = new QMenuItem(this);
|
|
|
|
item->setText(text);
|
2011-12-30 17:48:14 +01:00
|
|
|
m_menu->addAction(item);
|
2011-12-20 15:36:26 +01:00
|
|
|
m_items << item;
|
|
|
|
}
|
|
|
|
|
2011-12-29 15:09:58 +01:00
|
|
|
void QMenuProxy::addMenuItem(QMenuItem *item)
|
|
|
|
{
|
2011-12-30 17:48:14 +01:00
|
|
|
m_menu->addAction(item);
|
2011-12-29 15:09:58 +01:00
|
|
|
m_items << item;
|
|
|
|
}
|
|
|
|
|
2011-12-21 19:52:35 +01:00
|
|
|
void QMenuProxy::itemTriggered(QAction *action)
|
|
|
|
{
|
|
|
|
QMenuItem *item = qobject_cast<QMenuItem *>(action);
|
|
|
|
if (item) {
|
|
|
|
emit triggered(item);
|
2011-12-21 21:55:32 +01:00
|
|
|
int index = m_items.indexOf(item);
|
|
|
|
if (index > -1) {
|
|
|
|
emit triggeredIndex(index);
|
|
|
|
}
|
2011-12-21 19:52:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-14 23:11:16 +02:00
|
|
|
void QMenuProxy::open(int x, int y)
|
2011-08-02 16:13:48 +02:00
|
|
|
{
|
2011-08-16 23:52:25 +02:00
|
|
|
m_menu->clear();
|
2011-10-28 19:40:04 +02:00
|
|
|
foreach(QMenuItem* item, m_items) {
|
2011-08-17 17:00:53 +02:00
|
|
|
m_menu->addAction (item);
|
2011-08-07 01:41:21 +02:00
|
|
|
}
|
|
|
|
|
2012-09-20 16:28:49 +02:00
|
|
|
QPoint screenPos;
|
|
|
|
|
|
|
|
QGraphicsObject *parentItem;
|
|
|
|
if (m_visualParent) {
|
|
|
|
parentItem = qobject_cast<QGraphicsObject *>(m_visualParent.data());
|
|
|
|
} else {
|
|
|
|
parentItem = qobject_cast<QGraphicsObject *>(parent());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!parentItem || !parentItem->scene()) {
|
2012-09-21 14:23:41 +02:00
|
|
|
m_menu->popup(QPoint(0, 0));
|
|
|
|
m_status = DialogStatus::Open;
|
|
|
|
emit statusChanged();
|
2012-09-20 16:28:49 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QList<QGraphicsView*> views = parentItem->scene()->views();
|
|
|
|
|
|
|
|
if (views.size() < 1) {
|
2012-09-21 14:23:41 +02:00
|
|
|
m_menu->popup(QPoint(0, 0));
|
|
|
|
m_status = DialogStatus::Open;
|
|
|
|
emit statusChanged();
|
2012-09-20 16:28:49 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QGraphicsView *view = 0;
|
|
|
|
if (views.size() == 1) {
|
|
|
|
view = views[0];
|
|
|
|
} else {
|
|
|
|
QGraphicsView *found = 0;
|
|
|
|
QGraphicsView *possibleFind = 0;
|
|
|
|
foreach (QGraphicsView *v, views) {
|
|
|
|
if (v->sceneRect().intersects(parentItem->sceneBoundingRect()) ||
|
|
|
|
v->sceneRect().contains(parentItem->scenePos())) {
|
|
|
|
if (v->isActiveWindow()) {
|
|
|
|
found = v;
|
|
|
|
} else {
|
|
|
|
possibleFind = v;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
view = found ? found : possibleFind;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (view) {
|
|
|
|
screenPos = view->mapToGlobal(view->mapFromScene(parentItem->scenePos()+ QPoint(x, y)));
|
|
|
|
} else {
|
|
|
|
screenPos = QApplication::activeWindow()->mapToGlobal(QPoint(x, y));
|
|
|
|
}
|
|
|
|
|
2011-08-16 23:52:25 +02:00
|
|
|
m_menu->popup(screenPos);
|
2011-10-28 19:40:04 +02:00
|
|
|
m_status = DialogStatus::Open;
|
|
|
|
emit statusChanged();
|
2011-08-02 16:13:48 +02:00
|
|
|
}
|
|
|
|
|
2011-10-28 18:57:34 +02:00
|
|
|
void QMenuProxy::open()
|
|
|
|
{
|
2011-10-28 20:14:38 +02:00
|
|
|
m_menu->clear();
|
|
|
|
|
|
|
|
foreach(QMenuItem* item, m_items) {
|
|
|
|
m_menu->addAction (item);
|
|
|
|
}
|
|
|
|
m_menu->updateGeometry();
|
|
|
|
|
2011-11-08 23:07:42 +01:00
|
|
|
QGraphicsObject *parentItem;
|
|
|
|
if (m_visualParent) {
|
2011-12-30 16:35:55 +01:00
|
|
|
parentItem = qobject_cast<QGraphicsObject *>(m_visualParent.data());
|
2011-12-27 18:26:05 +01:00
|
|
|
} else {
|
|
|
|
parentItem = qobject_cast<QGraphicsObject *>(parent());
|
2011-11-08 23:07:42 +01:00
|
|
|
}
|
2011-10-28 18:57:34 +02:00
|
|
|
|
|
|
|
if (!parentItem || !parentItem->scene()) {
|
2012-09-21 14:23:41 +02:00
|
|
|
m_menu->popup(QPoint(0, 0));
|
|
|
|
m_status = DialogStatus::Open;
|
|
|
|
emit statusChanged();
|
2011-10-28 18:57:34 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QList<QGraphicsView*> views = parentItem->scene()->views();
|
|
|
|
|
|
|
|
if (views.size() < 1) {
|
2012-09-21 14:23:41 +02:00
|
|
|
m_menu->popup(QPoint(0, 0));
|
|
|
|
m_status = DialogStatus::Open;
|
|
|
|
emit statusChanged();
|
2011-10-28 18:57:34 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QGraphicsView *view = 0;
|
|
|
|
if (views.size() == 1) {
|
|
|
|
view = views[0];
|
|
|
|
} else {
|
|
|
|
QGraphicsView *found = 0;
|
|
|
|
QGraphicsView *possibleFind = 0;
|
|
|
|
foreach (QGraphicsView *v, views) {
|
|
|
|
if (v->sceneRect().intersects(parentItem->sceneBoundingRect()) ||
|
|
|
|
v->sceneRect().contains(parentItem->scenePos())) {
|
|
|
|
if (v->isActiveWindow()) {
|
|
|
|
found = v;
|
|
|
|
} else {
|
|
|
|
possibleFind = v;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
view = found ? found : possibleFind;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!view) {
|
2012-09-21 14:23:41 +02:00
|
|
|
m_menu->popup(QPoint(0, 0));
|
|
|
|
m_status = DialogStatus::Open;
|
|
|
|
emit statusChanged();
|
2011-10-28 18:57:34 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-10-28 20:14:38 +02:00
|
|
|
const QRect avail = QApplication::desktop()->availableGeometry(view);
|
|
|
|
QPoint menuPos = view->mapToGlobal(view->mapFromScene(parentItem->scenePos()+QPoint(0, parentItem->boundingRect().height())));
|
|
|
|
|
2011-10-28 20:34:46 +02:00
|
|
|
if (menuPos.y() + m_menu->sizeHint().height() > avail.bottom()) {
|
|
|
|
menuPos = view->mapToGlobal(view->mapFromScene(parentItem->scenePos() - QPoint(0, m_menu->sizeHint().height())));
|
2011-10-28 20:14:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
m_menu->popup(menuPos);
|
|
|
|
m_status = DialogStatus::Open;
|
|
|
|
emit statusChanged();
|
2011-10-28 18:57:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void QMenuProxy::close()
|
|
|
|
{
|
|
|
|
m_menu->hide();
|
2011-10-28 19:40:04 +02:00
|
|
|
m_status = DialogStatus::Closed;
|
|
|
|
emit statusChanged();
|
2011-10-28 18:57:34 +02:00
|
|
|
}
|
|
|
|
|
2011-08-02 16:13:48 +02:00
|
|
|
#include "qmenu.moc"
|
|
|
|
|