Skip to content

Commit

Permalink
Select the Element with its drawn shapes instead of boundingRect (#11928
Browse files Browse the repository at this point in the history
)

Helps with the issue #11866
  • Loading branch information
adeas31 committed Feb 1, 2024
1 parent 8fae7db commit 45f47c3
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 10 deletions.
1 change: 1 addition & 0 deletions OMEdit/OMEditLIB/Annotations/ShapeAnnotation.h
Expand Up @@ -164,6 +164,7 @@ class ShapeAnnotation : public QObject, public QGraphicsItem, public GraphicItem
void updateExtent(const int index, const QPointF point);
void setOriginItemPos(const QPointF point);
GraphicsView* getGraphicsView() {return mpGraphicsView;}
Element* getParentComponent() const {return mpParentComponent;}
OriginItem* getOriginItem() {return mpOriginItem;}
void setPoints(QVector<QPointF> points) {mPoints = points;}
const PointArrayAnnotation &getPoints() {return mPoints;}
Expand Down
16 changes: 16 additions & 0 deletions OMEdit/OMEditLIB/Element/Element.cpp
Expand Up @@ -4043,3 +4043,19 @@ QVariant Element::itemChange(GraphicsItemChange change, const QVariant &value)
}
return value;
}

/*!
* \brief Element::mousePressEvent
* The GraphicsView sets the Element under mouse object and then we use that to accept the mouse press event.
* Only accept the mouse press event for the Element set by GraphicsView and ignore all others.
* This allows us to select Element's by clicking on their drawn items instead of the bounding rectangle.
* \param event
*/
void Element::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
if (mpGraphicsView->getElementUnderMouse() == this) {
QGraphicsItem::mousePressEvent(event);
} else {
event->ignore();
}
}
2 changes: 2 additions & 0 deletions OMEdit/OMEditLIB/Element/Element.h
Expand Up @@ -440,8 +440,10 @@ public slots:
void showReplaceSubModelDialog();
void updateDynamicSelect(double time);
void resetDynamicSelect();
// QGraphicsItem interface
protected:
virtual QVariant itemChange(GraphicsItemChange change, const QVariant &value) override;
virtual void mousePressEvent(QGraphicsSceneMouseEvent *event) override;
};

#endif // ELEMENT_H
41 changes: 31 additions & 10 deletions OMEdit/OMEditLIB/Modeling/ModelWidgetContainer.cpp
Expand Up @@ -208,6 +208,7 @@ GraphicsView::GraphicsView(StringHandler::ViewType viewType, ModelWidget *pModel
mIsCreatingTextShape = false;
mIsCreatingBitmapShape = false;
mIsPanning = false;
mpElementUnderMouse = 0;
mLastMouseEventPos = QPoint(0, 0);
mpClickedComponent = 0;
mpClickedState = 0;
Expand Down Expand Up @@ -2939,7 +2940,7 @@ void GraphicsView::duplicateItems(const QString &action)
* A QGraphicsItem can be a Element or a ShapeAnnotation inside a Element.
* \return
*/
Element *GraphicsView::getElementFromQGraphicsItem(QGraphicsItem *pGraphicsItem)
Element* GraphicsView::getElementFromQGraphicsItem(QGraphicsItem *pGraphicsItem)
{
if (pGraphicsItem) {
Element *pElement = dynamic_cast<Element*>(pGraphicsItem);
Expand All @@ -2957,18 +2958,36 @@ Element *GraphicsView::getElementFromQGraphicsItem(QGraphicsItem *pGraphicsItem)
return 0;
}

/*!
* \brief GraphicsView::getShapeFromQGraphicsItem
* \param pGraphicsItem
* A QGraphicsItem can be a ShapeAnnotation.
* \return
*/
ShapeAnnotation* GraphicsView::getShapeFromQGraphicsItem(QGraphicsItem *pGraphicsItem)
{
if (pGraphicsItem) {
ShapeAnnotation *pShapeAnnotation = dynamic_cast<ShapeAnnotation*>(pGraphicsItem);
if (pShapeAnnotation && pShapeAnnotation->getParentComponent()) {
return pShapeAnnotation;
}
}
return 0;
}

/*!
* \brief GraphicsView::elementAtPosition
* Returns the first Element at the position.
* \param position
* \return
*/
Element *GraphicsView::elementAtPosition(QPoint position)
Element* GraphicsView::elementAtPosition(QPoint position)
{
QList<QGraphicsItem*> graphicsItems = items(position);
foreach (QGraphicsItem *pGraphicsItem, graphicsItems) {
Element *pElement = getElementFromQGraphicsItem(pGraphicsItem);
if (pElement) {
ShapeAnnotation *pShapeAnnotation = getShapeFromQGraphicsItem(pGraphicsItem);
if (pShapeAnnotation && pShapeAnnotation->getParentComponent()) {
Element *pElement = pShapeAnnotation->getParentComponent();
Element *pRootElement = pElement->getRootParentElement();
if (pRootElement && ((pRootElement->getLibraryTreeItem() && !pRootElement->getLibraryTreeItem()->isNonExisting()) || (mpModelWidget->isNewApi()))) {
return pRootElement;
Expand Down Expand Up @@ -4522,6 +4541,7 @@ void GraphicsView::mousePressEvent(QMouseEvent *event)
if (event->button() == Qt::RightButton) {
return;
}
mpElementUnderMouse = elementAtPosition(event->pos());
// if user is starting panning.
if (QApplication::keyboardModifiers() == Qt::ControlModifier) {
setIsPanning(true);
Expand All @@ -4530,12 +4550,13 @@ void GraphicsView::mousePressEvent(QMouseEvent *event)
return;
}
MainWindow *pMainWindow = MainWindow::instance();
QPointF snappedPoint = snapPointToGrid(mapToScene(event->pos()));
QPointF scenePos = mapToScene(event->pos());
QPointF snappedPoint = snapPointToGrid(scenePos);
bool eventConsumed = false;
// if left button presses and we are creating a connector
if (isCreatingConnection()) {
if (mpModelWidget->getLibraryTreeItem()->getLibraryType() == LibraryTreeItem::OMS) {
mpConnectionLineAnnotation->addPoint(roundPoint(mapToScene(event->pos())));
mpConnectionLineAnnotation->addPoint(roundPoint(scenePos));
} else {
mpConnectionLineAnnotation->addPoint(snappedPoint);
}
Expand Down Expand Up @@ -4596,15 +4617,15 @@ void GraphicsView::mousePressEvent(QMouseEvent *event)
pInitialStateLineAnnotation->setOldAnnotation(pInitialStateLineAnnotation->getOMCShapeAnnotation());
}
}
// if some item is clicked
// if connector is clicked
if (Element *pComponent = connectorElementAtPosition(event->pos())) {
if (!isCreatingConnection()) {
mpClickedComponent = pComponent;
} else if (isCreatingConnection()) {
addConnection(pComponent); // end the connection
eventConsumed = true; // consume the event so that connection line or end component will not become selected
}
} else if (Element *pComponent = stateElementAtPosition(event->pos())) {
} else if (Element *pComponent = stateElementAtPosition(event->pos())) { // if state is clicked
if (!isCreatingTransition()) {
mpClickedState = pComponent;
} else if (isCreatingTransition()) {
Expand Down Expand Up @@ -4704,6 +4725,7 @@ void GraphicsView::mouseReleaseEvent(QMouseEvent *event)
return;
}
setIsPanning(false);
mpElementUnderMouse = 0;
mpClickedComponent = 0;
mpClickedState = 0;

Expand Down Expand Up @@ -4761,9 +4783,8 @@ void GraphicsView::mouseReleaseEvent(QMouseEvent *event)

bool GraphicsView::handleDoubleClickOnComponent(QMouseEvent *event)
{
QGraphicsItem *pGraphicsItem = itemAt(event->pos());
bool shouldEnactQTDoubleClick = true;
Element *pComponent = getElementFromQGraphicsItem(pGraphicsItem);
Element *pComponent = elementAtPosition(event->pos());
if (pComponent) {
shouldEnactQTDoubleClick = false;
Element *pRootComponent = pComponent->getRootParentElement();
Expand Down
3 changes: 3 additions & 0 deletions OMEdit/OMEditLIB/Modeling/ModelWidgetContainer.h
Expand Up @@ -124,6 +124,7 @@ class GraphicsView : public QGraphicsView
bool mIsCreatingTextShape;
bool mIsCreatingBitmapShape;
bool mIsPanning;
Element *mpElementUnderMouse;
QPoint mLastMouseEventPos;
Element *mpClickedComponent;
Element *mpClickedState;
Expand Down Expand Up @@ -221,6 +222,7 @@ class GraphicsView : public QGraphicsView
void setIsCreatingPrologue(const bool enable);
void setIsPanning(bool enable);
bool isPanning() {return mIsPanning;}
Element* getElementUnderMouse() const {return mpElementUnderMouse;}
void setDragModeInternal(bool enable, bool updateCursor = false);
void setItemsFlags(bool enable);
void updateUndoRedoActions(bool enable);
Expand Down Expand Up @@ -365,6 +367,7 @@ class GraphicsView : public QGraphicsView
void duplicateItems(const QString &action);
bool isCreatingShape();
Element* getElementFromQGraphicsItem(QGraphicsItem *pGraphicsItem);
ShapeAnnotation* getShapeFromQGraphicsItem(QGraphicsItem *pGraphicsItem);
Element* elementAtPosition(QPoint position);
Element* connectorElementAtPosition(QPoint position);
Element* stateElementAtPosition(QPoint position);
Expand Down

0 comments on commit 45f47c3

Please sign in to comment.