Skip to content

Commit

Permalink
Use the cursor position also when adding system and connector (#8276)
Browse files Browse the repository at this point in the history
Do not add geometry when adding the elements to the default position
  • Loading branch information
adeas31 committed Dec 7, 2021
1 parent 537b4a3 commit 3adf772
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 27 deletions.
5 changes: 4 additions & 1 deletion OMEdit/OMEditLIB/Modeling/ModelWidgetContainer.cpp
Expand Up @@ -96,7 +96,8 @@ GraphicsScene::GraphicsScene(StringHandler::ViewType viewType, ModelWidget *pMod
* \param visualizationView
*/
GraphicsView::GraphicsView(StringHandler::ViewType viewType, ModelWidget *pModelWidget, bool visualizationView)
: QGraphicsView(pModelWidget), mViewType(viewType), mVisualizationView(visualizationView), mSkipBackground(false), mContextMenuStartPosition(QPointF(0, 0))
: QGraphicsView(pModelWidget), mViewType(viewType), mVisualizationView(visualizationView), mSkipBackground(false), mContextMenuStartPosition(QPointF(0, 0)),
mContextMenuStartPositionValid(false)
{
/* Ticket #3275
* Set the scroll bars policy to always on to avoid unnecessary resize events.
Expand Down Expand Up @@ -3885,6 +3886,7 @@ void GraphicsView::contextMenuEvent(QContextMenuEvent *event)
if (!itemAt(event->pos())) {
QMenu menu;
mContextMenuStartPosition = mapToScene(mapFromGlobal(QCursor::pos()));
mContextMenuStartPositionValid = true;
if (mpModelWidget->getLibraryTreeItem()->getLibraryType() == LibraryTreeItem::Modelica) {
modelicaGraphicsViewContextMenu(&menu);
} else if (mpModelWidget->getLibraryTreeItem()->getLibraryType() == LibraryTreeItem::CompositeModel) {
Expand All @@ -3894,6 +3896,7 @@ void GraphicsView::contextMenuEvent(QContextMenuEvent *event)
}
menu.exec(event->globalPos());
mContextMenuStartPosition = QPointF(0, 0);
mContextMenuStartPositionValid = false;
return; // return from it because at a time we only want one context menu.
} else { // if we click on some item.
bool oneShapeSelected = false;
Expand Down
1 change: 1 addition & 0 deletions OMEdit/OMEditLIB/Modeling/ModelWidgetContainer.h
Expand Up @@ -159,6 +159,7 @@ class GraphicsView : public QGraphicsView
GraphicsView(StringHandler::ViewType viewType, ModelWidget *pModelWidget, bool visualizationView = false);
bool mSkipBackground; /* Do not draw the background rectangle */
QPointF mContextMenuStartPosition;
bool mContextMenuStartPositionValid;
StringHandler::ViewType getViewType() {return mViewType;}
ModelWidget* getModelWidget() {return mpModelWidget;}
bool isVisualizationView() {return mVisualizationView;}
Expand Down
12 changes: 12 additions & 0 deletions OMEdit/OMEditLIB/OMS/BusDialog.cpp
Expand Up @@ -576,6 +576,12 @@ void AddBusDialog::addBus()
accept();
} else { // add case
if (OMSProxy::instance()->addBus(bus)) {
if (mpGraphicsView->mContextMenuStartPositionValid) {
ssd_connector_geometry_t connectorGeometry;
connectorGeometry.x = Utilities::mapToCoOrdinateSystem(mpGraphicsView->mContextMenuStartPosition.x(), -100, 100, 0, 1);
connectorGeometry.y = Utilities::mapToCoOrdinateSystem(mpGraphicsView->mContextMenuStartPosition.y(), -100, 100, 0, 1);
OMSProxy::instance()->setBusGeometry(bus, &connectorGeometry);
}
foreach (QString connector, connectors) {
OMSProxy::instance()->addConnectorToBus(bus, connector);
}
Expand Down Expand Up @@ -869,6 +875,12 @@ void AddTLMBusDialog::addTLMBus()
} else { // add case
if (OMSProxy::instance()->addTLMBus(tlmBus, (oms_tlm_domain_t)mpDomainComboBox->itemData(mpDomainComboBox->currentIndex()).toInt(), mpDimensionSpinBox->value(),
(oms_tlm_interpolation_t)mpInterpolationComboBox->itemData(mpInterpolationComboBox->currentIndex()).toInt())) {
if (mpGraphicsView->mContextMenuStartPositionValid) {
ssd_connector_geometry_t connectorGeometry;
connectorGeometry.x = Utilities::mapToCoOrdinateSystem(mpGraphicsView->mContextMenuStartPosition.x(), -100, 100, 0, 1);
connectorGeometry.y = Utilities::mapToCoOrdinateSystem(mpGraphicsView->mContextMenuStartPosition.y(), -100, 100, 0, 1);
OMSProxy::instance()->setTLMBusGeometry(tlmBus, &connectorGeometry);
}
// add connectors to the bus
QPair<QString, QString> connector;
foreach (connector, connectors) {
Expand Down
40 changes: 15 additions & 25 deletions OMEdit/OMEditLIB/OMS/ModelDialog.cpp
Expand Up @@ -251,6 +251,9 @@ void AddSystemDialog::addSystem()
oms_system_enu_t systemType = (oms_system_enu_t)mpSystemWidget->getTypeComboBox()->itemData(mpSystemWidget->getTypeComboBox()->currentIndex()).toInt();
QString nameStructure = QString("%1.%2").arg(pParentLibraryTreeItem->getNameStructure()).arg(mpSystemWidget->getNameTextBox()->text());
if (OMSProxy::instance()->addSystem(nameStructure, systemType)) {
if (mpGraphicsView->mContextMenuStartPositionValid) {
OMSProxy::instance()->createElementGeometryUsingPosition(nameStructure, mpGraphicsView->mContextMenuStartPosition);
}
mpGraphicsView->getModelWidget()->createOMSimulatorUndoCommand(QString("Add system %1").arg(nameStructure));
mpGraphicsView->getModelWidget()->updateModelText();
accept();
Expand Down Expand Up @@ -346,29 +349,6 @@ QString AddSubModelDialog::browseSubModelPath(GraphicsView *pGraphicsView, QStri
return path;
}

/*!
* \brief AddSubModelDialog::setSubModelGeometry
* Sets the submodel geometry.
* \param nameStructure
*/
void AddSubModelDialog::setSubModelGeometry(const QString &nameStructure)
{
qreal x = mpGraphicsView->mContextMenuStartPosition.x();
qreal y = mpGraphicsView->mContextMenuStartPosition.y();

ssd_element_geometry_t elementGeometry;
elementGeometry.x1 = x - 10.0;
elementGeometry.y1 = y - 10.0;
elementGeometry.x2 = x + 10.0;
elementGeometry.y2 = y + 10.0;
elementGeometry.rotation = 0.0;
elementGeometry.iconSource = NULL;
elementGeometry.iconRotation = 0.0;
elementGeometry.iconFlip = false;
elementGeometry.iconFixedAspectRatio = false;
OMSProxy::instance()->setElementGeometry(nameStructure, &elementGeometry);
}

/*!
* \brief AddSubModelDialog::browseSubModelPath
* Slot activated when mpBrowsePathButton clicked signal is raised.
Expand Down Expand Up @@ -432,7 +412,9 @@ void AddSubModelDialog::addSubModel()
QString nameStructure = QString("%1.%2").arg(pParentLibraryTreeItem->getNameStructure()).arg(mpNameTextBox->text());
if (mpStartScriptTextBox->text().isEmpty()) {
if (OMSProxy::instance()->addSubModel(nameStructure, fileInfo.absoluteFilePath())) {
setSubModelGeometry(nameStructure);
if (mpGraphicsView->mContextMenuStartPositionValid) {
OMSProxy::instance()->createElementGeometryUsingPosition(nameStructure, mpGraphicsView->mContextMenuStartPosition);
}
mpGraphicsView->getModelWidget()->createOMSimulatorUndoCommand(QString("Add submodel %1").arg(nameStructure));
mpGraphicsView->getModelWidget()->updateModelText();
accept();
Expand All @@ -441,7 +423,9 @@ void AddSubModelDialog::addSubModel()
}
} else {
if (OMSProxy::instance()->addExternalTLMModel(nameStructure, mpStartScriptTextBox->text(), fileInfo.absoluteFilePath())) {
setSubModelGeometry(nameStructure);
if (mpGraphicsView->mContextMenuStartPositionValid) {
OMSProxy::instance()->createElementGeometryUsingPosition(nameStructure, mpGraphicsView->mContextMenuStartPosition);
}
mpGraphicsView->getModelWidget()->createOMSimulatorUndoCommand(QString("Add external tlm model %1").arg(nameStructure));
mpGraphicsView->getModelWidget()->updateModelText();
accept();
Expand Down Expand Up @@ -636,6 +620,12 @@ void AddConnectorDialog::addConnector()
oms_causality_enu_t causality = (oms_causality_enu_t)mpCausalityComboBox->itemData(mpCausalityComboBox->currentIndex()).toInt();
oms_signal_type_enu_t signalType = (oms_signal_type_enu_t)mpTypeComboBox->itemData(mpTypeComboBox->currentIndex()).toInt();
if (OMSProxy::instance()->addConnector(nameStructure, causality, signalType)) {
if (mpGraphicsView->mContextMenuStartPositionValid) {
ssd_connector_geometry_t connectorGeometry;
connectorGeometry.x = Utilities::mapToCoOrdinateSystem(mpGraphicsView->mContextMenuStartPosition.x(), -100, 100, 0, 1);
connectorGeometry.y = Utilities::mapToCoOrdinateSystem(mpGraphicsView->mContextMenuStartPosition.y(), -100, 100, 0, 1);
OMSProxy::instance()->setConnectorGeometry(nameStructure, &connectorGeometry);
}
mpGraphicsView->getModelWidget()->createOMSimulatorUndoCommand(QString("Add connector %1").arg(nameStructure));
mpGraphicsView->getModelWidget()->updateModelText();
accept();
Expand Down
1 change: 0 additions & 1 deletion OMEdit/OMEditLIB/OMS/ModelDialog.h
Expand Up @@ -116,7 +116,6 @@ class AddSubModelDialog : public QDialog
QPushButton *mpOkButton;
QPushButton *mpCancelButton;
QDialogButtonBox *mpButtonBox;
void setSubModelGeometry(const QString &nameStructure);
private slots:
void browseSubModelPath();
void browseStartScript();
Expand Down
24 changes: 24 additions & 0 deletions OMEdit/OMEditLIB/OMS/OMSProxy.cpp
Expand Up @@ -437,6 +437,30 @@ bool OMSProxy::addSubModel(QString cref, QString fmuPath)
return statusToBool(status);
}

/*!
* \brief OMSProxy::createElementGeometryUsingPosition
* Creates the element geometry using position.
* \param cref
* \param position
*/
void OMSProxy::createElementGeometryUsingPosition(const QString &cref, QPointF position)
{
qreal x = position.x();
qreal y = position.y();

ssd_element_geometry_t elementGeometry;
elementGeometry.x1 = x - 10.0;
elementGeometry.y1 = y - 10.0;
elementGeometry.x2 = x + 10.0;
elementGeometry.y2 = y + 10.0;
elementGeometry.rotation = 0.0;
elementGeometry.iconSource = NULL;
elementGeometry.iconRotation = 0.0;
elementGeometry.iconFlip = false;
elementGeometry.iconFixedAspectRatio = false;
setElementGeometry(cref, &elementGeometry);
}

bool OMSProxy::addExternalTLMModel(QString cref, QString startScript, QString modelPath)
{
QString command = "oms_addExternalModel";
Expand Down
1 change: 1 addition & 0 deletions OMEdit/OMEditLIB/OMS/OMSProxy.h
Expand Up @@ -78,6 +78,7 @@ class OMSProxy : public QObject
bool addConnectorToBus(QString busCref, QString connectorCref);
bool addConnectorToTLMBus(QString busCref, QString connectorCref, QString type);
bool addSubModel(QString cref, QString fmuPath);
void createElementGeometryUsingPosition(const QString &cref, QPointF position);
bool addExternalTLMModel(QString cref, QString startScript, QString modelPath);
bool addSystem(QString cref, oms_system_enu_t type);
bool addTLMBus(QString cref, oms_tlm_domain_t domain, int dimensions, const oms_tlm_interpolation_t interpolation);
Expand Down

0 comments on commit 3adf772

Please sign in to comment.