From a0fb4356dcf4ceb2befaba7a8dbb7b794c8d60ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20K=C3=BCgler?= Date: Tue, 26 Mar 2013 22:34:35 +0100 Subject: [PATCH] Positioning of Menu on screen Actually quite a bit more lightweight than its QGV predecessor. --- .../plasmacomponents/qmenu.cpp | 120 +----------------- .../plasmacomponents/qmenu.h | 4 +- .../plasmacomponents/qmenuitem.cpp | 1 + 3 files changed, 5 insertions(+), 120 deletions(-) diff --git a/src/declarativeimports/plasmacomponents/qmenu.cpp b/src/declarativeimports/plasmacomponents/qmenu.cpp index 64c584f9c..7cd5eacd7 100644 --- a/src/declarativeimports/plasmacomponents/qmenu.cpp +++ b/src/declarativeimports/plasmacomponents/qmenu.cpp @@ -159,14 +159,13 @@ void QMenuProxy::itemTriggered(QAction *action) void QMenuProxy::open(int x, int y) { + qDebug() << "opening menu at " << x << y; m_menu->clear(); foreach(QMenuItem* item, m_items) { qDebug() <<"Adding action: " << item->text(); m_menu->addAction(item->action()); } - QPoint screenPos; - QQuickItem *parentItem; if (m_visualParent) { parentItem = qobject_cast(m_visualParent.data()); @@ -174,130 +173,15 @@ void QMenuProxy::open(int x, int y) parentItem = qobject_cast(parent()); } QPointF pos = parentItem->mapToScene(QPointF(x, y)); - //qDebug() << "I've an Item at " << pos; if (parentItem->window() && parentItem->window()->screen()) { pos = parentItem->window()->mapToGlobal(pos.toPoint()); } - screenPos = pos.toPoint(); - /* - if (!parentItem || !parentItem->scene()) { - m_menu->popup(QPoint(0, 0)); - m_status = DialogStatus::Open; - emit statusChanged(); - return; - } - QList views = parentItem->scene()->views(); - if (views.size() < 1) { - m_menu->popup(QPoint(0, 0)); - m_status = DialogStatus::Open; - emit statusChanged(); - 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)); - } - */ - m_menu->popup(screenPos); + m_menu->popup(pos.toPoint()); m_status = DialogStatus::Open; emit statusChanged(); } -void QMenuProxy::open() -{ - m_menu->clear(); - - foreach(QMenuItem* item, m_items) { - m_menu->addAction(item->action()); - } - m_menu->updateGeometry(); - - QQuickItem *parentItem; - if (m_visualParent) { - parentItem = qobject_cast(m_visualParent.data()); - } else { - parentItem = qobject_cast(parent()); - } - -/* - if (!parentItem || !parentItem->scene()) { - m_menu->popup(QPoint(0, 0)); - m_status = DialogStatus::Open; - emit statusChanged(); - return; - }*/ - /* - QList views = parentItem->scene()->views(); - - if (views.size() < 1) { - m_menu->popup(QPoint(0, 0)); - m_status = DialogStatus::Open; - emit statusChanged(); - 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) { - */ - m_menu->popup(QPoint(0, 0)); - m_status = DialogStatus::Open; - emit statusChanged(); - return; - /* - } - - const QRect avail = QApplication::desktop()->availableGeometry(view); - QPoint menuPos = view->mapToGlobal(view->mapFromScene(parentItem->scenePos()+QPoint(0, parentItem->boundingRect().height()))); - - if (menuPos.y() + m_menu->sizeHint().height() > avail.bottom()) { - menuPos = view->mapToGlobal(view->mapFromScene(parentItem->scenePos() - QPoint(0, m_menu->sizeHint().height()))); - } - - m_menu->popup(menuPos); - m_status = DialogStatus::Open; - emit statusChanged(); - */ -} void QMenuProxy::close() { diff --git a/src/declarativeimports/plasmacomponents/qmenu.h b/src/declarativeimports/plasmacomponents/qmenu.h index c712d4b92..78aadeca8 100644 --- a/src/declarativeimports/plasmacomponents/qmenu.h +++ b/src/declarativeimports/plasmacomponents/qmenu.h @@ -53,8 +53,8 @@ public: QObject *visualParent() const; void setVisualParent(QObject *parent); - Q_INVOKABLE void open(int x, int y); - Q_INVOKABLE void open(); + Q_INVOKABLE void open(int x = 0, int y = 0); + //Q_INVOKABLE void open(); Q_INVOKABLE void close(); Q_INVOKABLE void clearMenuItems(); Q_INVOKABLE void addMenuItem(const QString &text); diff --git a/src/declarativeimports/plasmacomponents/qmenuitem.cpp b/src/declarativeimports/plasmacomponents/qmenuitem.cpp index 791ce4b53..fb7e9be30 100644 --- a/src/declarativeimports/plasmacomponents/qmenuitem.cpp +++ b/src/declarativeimports/plasmacomponents/qmenuitem.cpp @@ -1,5 +1,6 @@ /*************************************************************************** * Copyright 2011 Viranch Mehta * + * Copyright 2013 Sebastian Kügler * * * * 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 *