Positioning of Menu on screen
Actually quite a bit more lightweight than its QGV predecessor.
This commit is contained in:
parent
0ef4080e4f
commit
a0fb4356dc
@ -159,14 +159,13 @@ void QMenuProxy::itemTriggered(QAction *action)
|
|||||||
|
|
||||||
void QMenuProxy::open(int x, int y)
|
void QMenuProxy::open(int x, int y)
|
||||||
{
|
{
|
||||||
|
qDebug() << "opening menu at " << x << y;
|
||||||
m_menu->clear();
|
m_menu->clear();
|
||||||
foreach(QMenuItem* item, m_items) {
|
foreach(QMenuItem* item, m_items) {
|
||||||
qDebug() <<"Adding action: " << item->text();
|
qDebug() <<"Adding action: " << item->text();
|
||||||
m_menu->addAction(item->action());
|
m_menu->addAction(item->action());
|
||||||
}
|
}
|
||||||
|
|
||||||
QPoint screenPos;
|
|
||||||
|
|
||||||
QQuickItem *parentItem;
|
QQuickItem *parentItem;
|
||||||
if (m_visualParent) {
|
if (m_visualParent) {
|
||||||
parentItem = qobject_cast<QQuickItem *>(m_visualParent.data());
|
parentItem = qobject_cast<QQuickItem *>(m_visualParent.data());
|
||||||
@ -174,130 +173,15 @@ void QMenuProxy::open(int x, int y)
|
|||||||
parentItem = qobject_cast<QQuickItem *>(parent());
|
parentItem = qobject_cast<QQuickItem *>(parent());
|
||||||
}
|
}
|
||||||
QPointF pos = parentItem->mapToScene(QPointF(x, y));
|
QPointF pos = parentItem->mapToScene(QPointF(x, y));
|
||||||
//qDebug() << "I've an Item at " << pos;
|
|
||||||
if (parentItem->window() && parentItem->window()->screen()) {
|
if (parentItem->window() && parentItem->window()->screen()) {
|
||||||
pos = parentItem->window()->mapToGlobal(pos.toPoint());
|
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(pos.toPoint());
|
||||||
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_status = DialogStatus::Open;
|
m_status = DialogStatus::Open;
|
||||||
emit statusChanged();
|
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()
|
void QMenuProxy::close()
|
||||||
{
|
{
|
||||||
|
@ -53,8 +53,8 @@ public:
|
|||||||
QObject *visualParent() const;
|
QObject *visualParent() const;
|
||||||
void setVisualParent(QObject *parent);
|
void setVisualParent(QObject *parent);
|
||||||
|
|
||||||
Q_INVOKABLE void open(int x, int y);
|
Q_INVOKABLE void open(int x = 0, int y = 0);
|
||||||
Q_INVOKABLE void open();
|
//Q_INVOKABLE void open();
|
||||||
Q_INVOKABLE void close();
|
Q_INVOKABLE void close();
|
||||||
Q_INVOKABLE void clearMenuItems();
|
Q_INVOKABLE void clearMenuItems();
|
||||||
Q_INVOKABLE void addMenuItem(const QString &text);
|
Q_INVOKABLE void addMenuItem(const QString &text);
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
/***************************************************************************
|
/***************************************************************************
|
||||||
* Copyright 2011 Viranch Mehta <viranch.mehta@gmail.com> *
|
* 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 *
|
* 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 *
|
* it under the terms of the GNU General Public License as published by *
|
||||||
|
Loading…
Reference in New Issue
Block a user