Commit 0634fabd authored by Alex Diev's avatar Alex Diev
Browse files

Merge branch 'sergiy-issue-173' into 'master'

Resolve "Impossible to show the context menu by pressing the context menu keyboard button."

Closes #173

See merge request esa/taste3!230
parents b79dd0e3 1eb84fcc
Pipeline #1577 failed with stage
......@@ -102,21 +102,27 @@ bool CreatorTool::eventFilter(QObject *watched, QEvent *event)
break;
}
}
if (m_view == watched && event->type() == QEvent::KeyPress) {
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
switch (keyEvent->key()) {
case Qt::Key_Delete: {
removeSelectedItems();
} break;
case Qt::Key_Escape: {
if (toolType() == ToolType::Pointer) {
if (auto scene = m_view->scene())
scene->clearSelection();
} else {
clearPreviewItem();
emit created();
if (m_view == watched) {
if (event->type() == QEvent::ContextMenu)
return onContextMenu(static_cast<QContextMenuEvent *>(event));
if (event->type() == QEvent::KeyPress) {
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
switch (keyEvent->key()) {
case Qt::Key_Delete: {
removeSelectedItems();
} break;
case Qt::Key_Escape: {
if (toolType() == ToolType::Pointer) {
if (auto scene = m_view->scene())
scene->clearSelection();
} else {
clearPreviewItem();
emit created();
}
} break;
}
} break;
}
}
......@@ -214,24 +220,14 @@ bool CreatorTool::onMouseRelease(QMouseEvent *e)
if (!m_view)
return false;
const QPointF scenePos = cursorInScene(e->globalPos());
if (m_toolType == ToolType::Pointer) {
if ((e->button() & Qt::RightButton) && e->modifiers() == Qt::NoModifier) {
if (QMenu *menu = populateContextMenu(scenePos)) {
connect(menu, &QMenu::aboutToHide, this, [this]() {
if (m_previewItem)
m_previewItem->setVisible(false);
});
menu->exec(e->globalPos());
clearPreviewItem();
return true;
}
}
if ((e->button() & Qt::RightButton) && e->modifiers() == Qt::NoModifier)
return showContextMenu(e->globalPos());
} else {
const bool hasPreview = m_previewItem || m_previewConnectionItem;
const bool isIface = m_toolType == ToolType::ProvidedInterface || m_toolType == ToolType::RequiredInterface;
if (hasPreview || isIface) {
const QPointF &scenePos = cursorInScene(e->globalPos());
handleToolType(m_toolType, scenePos);
return true;
}
......@@ -294,6 +290,33 @@ bool CreatorTool::onMouseMove(QMouseEvent *e)
return false;
}
bool CreatorTool::onContextMenu(QContextMenuEvent *e)
{
if (!m_view || e->reason() == QContextMenuEvent::Mouse)
return false;
QGraphicsScene *scene = m_view->scene();
if (!scene)
return false;
QPoint viewPos, globalPos;
if (!scene->selectedItems().isEmpty()) {
QGraphicsItem *selectedItem = scene->selectedItems().first();
const QPointF &scenePos = selectedItem->mapToScene(selectedItem->boundingRect().bottomRight());
viewPos = m_view->mapFromScene(scenePos);
globalPos = m_view->mapToGlobal(viewPos);
} else {
globalPos = QCursor::pos();
viewPos = m_view->mapFromGlobal(globalPos);
}
// onMousePress is needed to set an apppropriate m_previewItem
QMouseEvent mouseEvent(QEvent::MouseButtonPress, viewPos, Qt::RightButton, Qt::RightButton, 0);
onMousePress(&mouseEvent);
return showContextMenu(globalPos);
}
QPointF CreatorTool::cursorInScene() const
{
return cursorInScene(QCursor::pos()); // TODO: add current screen detection
......@@ -939,5 +962,19 @@ bool CreatorTool::warnConnectionPreview(const QPointF &pos)
return warn;
}
bool CreatorTool::showContextMenu(const QPoint &globalPos)
{
if (QMenu *menu = populateContextMenu(cursorInScene(globalPos))) {
connect(menu, &QMenu::aboutToHide, this, [this]() {
if (m_previewItem)
m_previewItem->setVisible(false);
});
menu->exec(globalPos);
clearPreviewItem();
return true;
}
return false;
}
} // namespace aadl
} // namespace taste3
......@@ -26,6 +26,7 @@
#include <QVector>
class QMouseEvent;
class QContextMenuEvent;
class QGraphicsView;
class QGraphicsRectItem;
class QGraphicsPathItem;
......@@ -79,6 +80,7 @@ protected:
virtual bool onMousePress(QMouseEvent *e);
virtual bool onMouseRelease(QMouseEvent *e);
virtual bool onMouseMove(QMouseEvent *e);
virtual bool onContextMenu(QContextMenuEvent *e);
QPointF cursorInScene() const;
QPointF cursorInScene(const QPoint &screenPos) const;
......@@ -103,6 +105,8 @@ private:
bool warnConnectionPreview(const QPointF &pos);
bool showContextMenu(const QPoint &globalPos);
private:
CreatorTool::ToolType m_toolType { ToolType::Pointer };
QSet<InteractiveObject *> m_collidedItems;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment