Positioning of Menu on screen

Actually quite a bit more lightweight than its QGV predecessor.
This commit is contained in:
Sebastian Kügler 2013-03-26 22:34:35 +01:00
parent 0ef4080e4f
commit a0fb4356dc
3 changed files with 5 additions and 120 deletions

View File

@ -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<QQuickItem *>(m_visualParent.data());
@ -174,130 +173,15 @@ void QMenuProxy::open(int x, int y)
parentItem = qobject_cast<QQuickItem *>(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<QGraphicsView*> 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<QQuickItem *>(m_visualParent.data());
} else {
parentItem = qobject_cast<QQuickItem *>(parent());
}
/*
if (!parentItem || !parentItem->scene()) {
m_menu->popup(QPoint(0, 0));
m_status = DialogStatus::Open;
emit statusChanged();
return;
}*/
/*
QList<QGraphicsView*> 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()
{

View File

@ -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);

View File

@ -1,5 +1,6 @@
/***************************************************************************
* Copyright 2011 Viranch Mehta <viranch.mehta@gmail.com> *
* Copyright 2013 Sebastian Kügler <sebas@kde.org> *
* *
* 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 *