Skip to content

Commit

Permalink
Fix updating the element name (#8207)
Browse files Browse the repository at this point in the history
Fixes #8180
Use element instead of component
Code refactoring
  • Loading branch information
adeas31 committed Nov 23, 2021
1 parent 9f0cb45 commit 953d678
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 67 deletions.
10 changes: 5 additions & 5 deletions OMEdit/OMEditLIB/Annotations/TextAnnotation.cpp
Expand Up @@ -312,8 +312,8 @@ void TextAnnotation::drawTextAnnotation(QPainter *painter)
// map the existing bounding rect to new transformation but with positive width and height so that font metrics can work
QRectF absMappedBoundingRect = QRectF(boundingRect().x() * sx, boundingRect().y() * sy, qAbs(boundingRect().width() * sx), qAbs(boundingRect().height() * sy));
// normalize the text for drawing
mTextString = StringHandler::removeFirstLastQuotes(mTextString);
mTextString = StringHandler::unparse(QString("\"").append(mTextString).append("\""));
QString textString = StringHandler::removeFirstLastQuotes(mTextString);
textString = StringHandler::unparse(QString("\"").append(mTextString).append("\""));
// Don't create new QFont instead get a font from painter and set the values on it and set it back.
QFont font = painter->font();
font.setFamily(mFontName);
Expand All @@ -333,7 +333,7 @@ void TextAnnotation::drawTextAnnotation(QPainter *painter)
// if absolute font size is defined and is greater than 0 then we don't need to calculate the font size.
if (mFontSize <= 0) {
QFontMetrics fontMetrics(painter->font());
QRect fontBoundRect = fontMetrics.boundingRect(absMappedBoundingRect.toRect(), Qt::TextDontClip, mTextString);
QRect fontBoundRect = fontMetrics.boundingRect(absMappedBoundingRect.toRect(), Qt::TextDontClip, textString);
const qreal xFactor = absMappedBoundingRect.width() / fontBoundRect.width();
const qreal yFactor = absMappedBoundingRect.height() / fontBoundRect.height();
/* Ticket:4256
Expand All @@ -349,10 +349,10 @@ void TextAnnotation::drawTextAnnotation(QPainter *painter)
/* Try to get the elided text if calculated font size <= Helper::minimumTextFontSize
* OR if font size is absolute.
*/
QString textToDraw = mTextString;
QString textToDraw = textString;
if (absMappedBoundingRect.width() > 1 && ((mFontSize <= 0 && painter->font().pointSizeF() <= Helper::minimumTextFontSize) || mFontSize > 0)) {
QFontMetrics fontMetrics(painter->font());
textToDraw = fontMetrics.elidedText(mTextString, Qt::ElideRight, absMappedBoundingRect.width());
textToDraw = fontMetrics.elidedText(textString, Qt::ElideRight, absMappedBoundingRect.width());
// if we get "..." i.e., QChar(0x2026) as textToDraw then don't draw anything
if (textToDraw.compare(QChar(0x2026)) == 0) {
textToDraw = "";
Expand Down
80 changes: 39 additions & 41 deletions OMEdit/OMEditLIB/Element/ElementProperties.cpp
Expand Up @@ -1387,21 +1387,20 @@ void ElementParameters::updateComponentParameters()

/*!
* \class ElementAttributes
* \brief A dialog for displaying components attributes like visibility, stream, casuality etc.
* \brief A dialog for displaying elements attributes like visibility, stream, casuality etc.
*/
/*!
* \brief ElementAttributes::ElementAttributes
* \param pComponent
* \param pElement
* \param pParent
*/
ElementAttributes::ElementAttributes(Element *pComponent, QWidget *pParent)
ElementAttributes::ElementAttributes(Element *pElement, QWidget *pParent)
: QDialog(pParent)
{
QString className = pComponent->getGraphicsView()->getModelWidget()->getLibraryTreeItem()->getNameStructure();
setWindowTitle(tr("%1 - %2 - %3 in %4").arg(Helper::applicationName).arg(tr("Element Attributes")).arg(pComponent->getName())
.arg(className));
QString className = pElement->getGraphicsView()->getModelWidget()->getLibraryTreeItem()->getNameStructure();
setWindowTitle(tr("%1 - %2 - %3 in %4").arg(Helper::applicationName, tr("Element Attributes"), pElement->getName(), className));
setAttribute(Qt::WA_DeleteOnClose);
mpComponent = pComponent;
mpElement = pElement;
setUpDialog();
initializeDialog();
}
Expand Down Expand Up @@ -1490,13 +1489,13 @@ void ElementAttributes::setUpDialog()
// Create the buttons
mpOkButton = new QPushButton(Helper::ok);
mpOkButton->setAutoDefault(true);
connect(mpOkButton, SIGNAL(clicked()), this, SLOT(updateComponentAttributes()));
connect(mpOkButton, SIGNAL(clicked()), this, SLOT(updateElementAttributes()));
mpCancelButton = new QPushButton(Helper::cancel);
mpCancelButton->setAutoDefault(false);
connect(mpCancelButton, SIGNAL(clicked()), this, SLOT(reject()));
mpButtonBox = new QDialogButtonBox(Qt::Horizontal);
mpButtonBox->addButton(mpOkButton, QDialogButtonBox::ActionRole);
if (mpComponent->getGraphicsView()->getModelWidget()->getLibraryTreeItem()->isSystemLibrary() || mpComponent->isInheritedComponent()) {
if (mpElement->getGraphicsView()->getModelWidget()->getLibraryTreeItem()->isSystemLibrary() || mpElement->isInheritedComponent()) {
mpOkButton->setDisabled(true);
}
mpButtonBox->addButton(mpCancelButton, QDialogButtonBox::ActionRole);
Expand All @@ -1521,79 +1520,78 @@ void ElementAttributes::setUpDialog()
void ElementAttributes::initializeDialog()
{
// get Class Name
mpNameTextBox->setText(mpComponent->getComponentInfo()->getName());
mpNameTextBox->setText(mpElement->getComponentInfo()->getName());
mpNameTextBox->setCursorPosition(0);
// get dimensions
QString dimensions = mpComponent->getComponentInfo()->getArrayIndex();
QString dimensions = mpElement->getComponentInfo()->getArrayIndex();
mpDimensionsTextBox->setText(QString("[%1]").arg(dimensions));
// get Comment
mpCommentTextBox->setText(mpComponent->getComponentInfo()->getComment());
mpCommentTextBox->setText(mpElement->getComponentInfo()->getComment());
mpCommentTextBox->setCursorPosition(0);
// get classname
mpPathTextBox->setText(mpComponent->getComponentInfo()->getClassName());
mpPathTextBox->setText(mpElement->getComponentInfo()->getClassName());
// get Variability
if (mpComponent->getComponentInfo()->getVariablity() == "constant") {
if (mpElement->getComponentInfo()->getVariablity() == "constant") {
mpConstantRadio->setChecked(true);
} else if (mpComponent->getComponentInfo()->getVariablity() == "parameter") {
} else if (mpElement->getComponentInfo()->getVariablity() == "parameter") {
mpParameterRadio->setChecked(true);
} else if (mpComponent->getComponentInfo()->getVariablity() == "discrete") {
} else if (mpElement->getComponentInfo()->getVariablity() == "discrete") {
mpDiscreteRadio->setChecked(true);
} else {
mpDefaultRadio->setChecked(true);
}
// get Properties
mpFinalCheckBox->setChecked(mpComponent->getComponentInfo()->getFinal());
mpProtectedCheckBox->setChecked(mpComponent->getComponentInfo()->getProtected());
mpReplaceAbleCheckBox->setChecked(mpComponent->getComponentInfo()->getReplaceable());
mIsFlow = mpComponent->getComponentInfo()->getFlow() ? "true" : "false";
mpFinalCheckBox->setChecked(mpElement->getComponentInfo()->getFinal());
mpProtectedCheckBox->setChecked(mpElement->getComponentInfo()->getProtected());
mpReplaceAbleCheckBox->setChecked(mpElement->getComponentInfo()->getReplaceable());
mIsFlow = mpElement->getComponentInfo()->getFlow() ? "true" : "false";
// get Casuality
if (mpComponent->getComponentInfo()->getCausality() == "input") {
if (mpElement->getComponentInfo()->getCausality() == "input") {
mpInputRadio->setChecked(true);
} else if (mpComponent->getComponentInfo()->getCausality() == "output") {
} else if (mpElement->getComponentInfo()->getCausality() == "output") {
mpOutputRadio->setChecked(true);
} else {
mpNoneRadio->setChecked(true);
}
// get InnerOuter
mpInnerCheckBox->setChecked(mpComponent->getComponentInfo()->getInner());
mpOuterCheckBox->setChecked(mpComponent->getComponentInfo()->getOuter());
mpInnerCheckBox->setChecked(mpElement->getComponentInfo()->getInner());
mpOuterCheckBox->setChecked(mpElement->getComponentInfo()->getOuter());
}

/*!
* \brief ElementAttributes::updateComponentAttributes
* \brief ElementAttributes::updateElementAttributes
* Slot activated when mpOkButton clicked signal is raised.\n
* Updates the component attributes.
* Updates the element attributes.
*/
void ElementAttributes::updateComponentAttributes()
void ElementAttributes::updateElementAttributes()
{
ModelWidget *pModelWidget = mpComponent->getGraphicsView()->getModelWidget();
ModelWidget *pModelWidget = mpElement->getGraphicsView()->getModelWidget();
/* Check the same component name problem before setting any attributes. */
if (mpComponent->getComponentInfo()->getName().compare(mpNameTextBox->text()) != 0) {
if (!mpComponent->getGraphicsView()->checkElementName(mpNameTextBox->text())) {
QMessageBox::information(MainWindow::instance(), QString(Helper::applicationName).append(" - ").append(Helper::information),
if (mpElement->getComponentInfo()->getName().compare(mpNameTextBox->text()) != 0) {
if (!mpElement->getGraphicsView()->checkElementName(mpNameTextBox->text())) {
QMessageBox::information(MainWindow::instance(), QString("%1 - %2").arg(Helper::applicationName, Helper::information),
GUIMessages::getMessage(GUIMessages::SAME_COMPONENT_NAME).arg(mpNameTextBox->text()), Helper::ok);
return;
}
}
// check for spaces
if (StringHandler::containsSpace(mpNameTextBox->text())) {
QMessageBox::critical(MainWindow::instance(), QString("%1 - %2").arg(Helper::applicationName).arg(Helper::error),
QMessageBox::critical(MainWindow::instance(), QString("%1 - %2").arg(Helper::applicationName, Helper::error),
tr("A component name should not have spaces. Please choose another name."), Helper::ok);
return;
}
// check for comma
if (mpNameTextBox->text().contains(',')) {
QMessageBox::critical(MainWindow::instance(), QString("%1 - %2").arg(Helper::applicationName).arg(Helper::error),
QMessageBox::critical(MainWindow::instance(), QString("%1 - %2").arg(Helper::applicationName, Helper::error),
GUIMessages::getMessage(GUIMessages::INVALID_INSTANCE_NAME).arg(mpNameTextBox->text()), Helper::ok);
return;
}
// check for invalid names
MainWindow::instance()->getOMCProxy()->setLoggingEnabled(false);
QList<QString> result = MainWindow::instance()->getOMCProxy()->parseString(QString("model M N %1; end M;").arg(mpNameTextBox->text()),
"M", false);
QList<QString> result = MainWindow::instance()->getOMCProxy()->parseString(QString("model M N %1; end M;").arg(mpNameTextBox->text()), "M", false);
MainWindow::instance()->getOMCProxy()->setLoggingEnabled(true);
if (result.isEmpty()) {
QMessageBox::critical(MainWindow::instance(), QString("%1 - %2").arg(Helper::applicationName).arg(Helper::error),
QMessageBox::critical(MainWindow::instance(), QString("%1 - %2").arg(Helper::applicationName, Helper::error),
GUIMessages::getMessage(GUIMessages::INVALID_INSTANCE_NAME).arg(mpNameTextBox->text()), Helper::ok);
return;
}
Expand All @@ -1616,16 +1614,16 @@ void ElementAttributes::updateComponentAttributes()
causality = "";
}
// save the old ElementInfo
ElementInfo oldComponentInfo(mpComponent->getComponentInfo());
ElementInfo oldComponentInfo(mpElement->getComponentInfo());
// Create a new ElementInfo
ElementInfo newComponentInfo;
newComponentInfo.setClassName(mpComponent->getComponentInfo()->getClassName());
newComponentInfo.setClassName(mpElement->getComponentInfo()->getClassName());
newComponentInfo.setName(mpNameTextBox->text());
newComponentInfo.setComment(mpCommentTextBox->text());
newComponentInfo.setProtected(mpProtectedCheckBox->isChecked());
newComponentInfo.setFinal(mpFinalCheckBox->isChecked());
newComponentInfo.setFlow(mpComponent->getComponentInfo()->getFlow());
newComponentInfo.setStream(mpComponent->getComponentInfo()->getStream());
newComponentInfo.setFlow(mpElement->getComponentInfo()->getFlow());
newComponentInfo.setStream(mpElement->getComponentInfo()->getStream());
newComponentInfo.setReplaceable(mpReplaceAbleCheckBox->isChecked());
newComponentInfo.setVariablity(variability);
newComponentInfo.setInner(mpInnerCheckBox->isChecked());
Expand All @@ -1636,7 +1634,7 @@ void ElementAttributes::updateComponentAttributes()
/* If user has really changed the Component's attributes then push that change on the stack.
*/
if (oldComponentInfo != newComponentInfo) {
UpdateComponentAttributesCommand *pUpdateComponentAttributesCommand = new UpdateComponentAttributesCommand(mpComponent, oldComponentInfo, newComponentInfo);
UpdateComponentAttributesCommand *pUpdateComponentAttributesCommand = new UpdateComponentAttributesCommand(mpElement, oldComponentInfo, newComponentInfo);
pModelWidget->getUndoStack()->push(pUpdateComponentAttributesCommand);
pModelWidget->updateModelText();
}
Expand Down
6 changes: 3 additions & 3 deletions OMEdit/OMEditLIB/Element/ElementProperties.h
Expand Up @@ -199,11 +199,11 @@ class ElementAttributes : public QDialog
{
Q_OBJECT
public:
ElementAttributes(Element *pComponent, QWidget *pParent = 0);
ElementAttributes(Element *pElement, QWidget *pParent = 0);
void setUpDialog();
void initializeDialog();
private:
Element *mpComponent;
Element *mpElement;
Label *mpAttributesHeading;
QFrame *mHorizontalLine;
QGroupBox *mpTypeGroupBox;
Expand Down Expand Up @@ -238,7 +238,7 @@ class ElementAttributes : public QDialog
QPushButton *mpCancelButton;
QDialogButtonBox *mpButtonBox;
public slots:
void updateComponentAttributes();
void updateElementAttributes();
};

class CompositeModelSubModelAttributes : public QDialog
Expand Down
27 changes: 9 additions & 18 deletions OMEdit/OMEditLIB/Modeling/Commands.cpp
Expand Up @@ -440,8 +440,7 @@ void UpdateComponentAttributesCommand::updateComponentAttributes(Element *pCompo

OMCProxy *pOMCProxy = MainWindow::instance()->getOMCProxy();
// update component attributes
if (pOMCProxy->setComponentProperties(modelName, pComponent->getComponentInfo()->getName(), isFinal, flow, isProtected, isReplaceAble,
variability, isInner, isOuter, causality)) {
if (pOMCProxy->setComponentProperties(modelName, pComponent->getComponentInfo()->getName(), isFinal, flow, isProtected, isReplaceAble, variability, isInner, isOuter, causality)) {
pComponent->getComponentInfo()->setFinal(componentInfo.getFinal());
pComponent->getComponentInfo()->setProtected(componentInfo.getProtected());
pComponent->getComponentInfo()->setReplaceable(componentInfo.getReplaceable());
Expand Down Expand Up @@ -471,8 +470,7 @@ void UpdateComponentAttributesCommand::updateComponentAttributes(Element *pCompo
}
}
} else {
QMessageBox::critical(MainWindow::instance(),
QString(Helper::applicationName).append(" - ").append(Helper::error), pOMCProxy->getResult(), Helper::ok);
QMessageBox::critical(MainWindow::instance(), QString("%1 - %2").arg(Helper::applicationName, Helper::error), pOMCProxy->getResult(), Helper::ok);
pOMCProxy->printMessagesStringInternal();
}
// update the component comment only if its changed.
Expand All @@ -483,22 +481,19 @@ void UpdateComponentAttributesCommand::updateComponentAttributes(Element *pCompo
pComponent->componentCommentHasChanged();
if (pComponent->getLibraryTreeItem()->isConnector()) {
if (pComponent->getGraphicsView()->getViewType() == StringHandler::Icon) {
Element *pDiagramComponent = 0;
pDiagramComponent = pComponent->getGraphicsView()->getModelWidget()->getDiagramGraphicsView()->getElementObject(pComponent->getName());
Element *pDiagramComponent = pComponent->getGraphicsView()->getModelWidget()->getDiagramGraphicsView()->getElementObject(pComponent->getName());
if (pDiagramComponent) {
pDiagramComponent->componentCommentHasChanged();
}
} else {
Element *pIconComponent = 0;
pIconComponent = pComponent->getGraphicsView()->getModelWidget()->getIconGraphicsView()->getElementObject(pComponent->getName());
Element *pIconComponent = pComponent->getGraphicsView()->getModelWidget()->getIconGraphicsView()->getElementObject(pComponent->getName());
if (pIconComponent) {
pIconComponent->componentCommentHasChanged();
}
}
}
} else {
QMessageBox::critical(MainWindow::instance(),
QString(Helper::applicationName).append(" - ").append(Helper::error), pOMCProxy->getResult(), Helper::ok);
QMessageBox::critical(MainWindow::instance(), QString("%1 - %2").arg(Helper::applicationName, Helper::error), pOMCProxy->getResult(), Helper::ok);
pOMCProxy->printMessagesStringInternal();
}
}
Expand All @@ -511,22 +506,19 @@ void UpdateComponentAttributesCommand::updateComponentAttributes(Element *pCompo
pComponent->componentNameHasChanged();
if (pComponent->getLibraryTreeItem()->isConnector()) {
if (pComponent->getGraphicsView()->getViewType() == StringHandler::Icon) {
Element *pDiagramComponent = 0;
pDiagramComponent = pComponent->getGraphicsView()->getModelWidget()->getDiagramGraphicsView()->getElementObject(pComponent->getName());
Element *pDiagramComponent = pComponent->getGraphicsView()->getModelWidget()->getDiagramGraphicsView()->getElementObject(pComponent->getName());
if (pDiagramComponent) {
pDiagramComponent->componentNameHasChanged();
}
} else {
Element *pIconComponent = 0;
pIconComponent = pComponent->getGraphicsView()->getModelWidget()->getIconGraphicsView()->getElementObject(pComponent->getName());
Element *pIconComponent = pComponent->getGraphicsView()->getModelWidget()->getIconGraphicsView()->getElementObject(pComponent->getName());
if (pIconComponent) {
pIconComponent->componentNameHasChanged();
}
}
}
} else {
QMessageBox::critical(MainWindow::instance(),
QString(Helper::applicationName).append(" - ").append(Helper::error), pOMCProxy->getResult(), Helper::ok);
QMessageBox::critical(MainWindow::instance(), QString("%1 - %2").arg(Helper::applicationName, Helper::error), pOMCProxy->getResult(), Helper::ok);
pOMCProxy->printMessagesStringInternal();
}
}
Expand All @@ -536,8 +528,7 @@ void UpdateComponentAttributesCommand::updateComponentAttributes(Element *pCompo
if (pOMCProxy->setComponentDimensions(modelName, pComponent->getComponentInfo()->getName(), arrayIndex)) {
pComponent->getComponentInfo()->setArrayIndex(arrayIndex);
} else {
QMessageBox::critical(MainWindow::instance(),
QString(Helper::applicationName).append(" - ").append(Helper::error), pOMCProxy->getResult(), Helper::ok);
QMessageBox::critical(MainWindow::instance(), QString("%1 - %2").arg(Helper::applicationName, Helper::error), pOMCProxy->getResult(), Helper::ok);
pOMCProxy->printMessagesStringInternal();
}
}
Expand Down

0 comments on commit 953d678

Please sign in to comment.