Skip to content

Commit

Permalink
Merge pull request #45 from rfranke/dyntext
Browse files Browse the repository at this point in the history
DynamicSelect for text annotations
  • Loading branch information
adeas31 committed Oct 26, 2016
2 parents 71c8892 + 9ac8442 commit f9c1597
Show file tree
Hide file tree
Showing 8 changed files with 153 additions and 27 deletions.
1 change: 1 addition & 0 deletions OMEdit/OMEditGUI/Annotations/ShapeAnnotation.cpp
Expand Up @@ -372,6 +372,7 @@ void ShapeAnnotation::setDefaults(ShapeAnnotation *pShapeAnnotation)
mClassFileName = pShapeAnnotation->mClassFileName;
mImageSource = pShapeAnnotation->mImageSource;
mImage = pShapeAnnotation->mImage;
mDynamicSelect = pShapeAnnotation->mDynamicSelect;
}

/*!
Expand Down
1 change: 1 addition & 0 deletions OMEdit/OMEditGUI/Annotations/ShapeAnnotation.h
Expand Up @@ -254,6 +254,7 @@ public slots:
QString mClassFileName; /* Used to find the bitmap relative locations. */
QString mImageSource;
QImage mImage;
QStringList mDynamicSelect; /* list of DynamicSelect arguments */
QList<CornerItem*> mCornerItemsList;
virtual void contextMenuEvent(QGraphicsSceneContextMenuEvent *pEvent);
virtual QVariant itemChange(GraphicsItemChange change, const QVariant &value);
Expand Down
31 changes: 29 additions & 2 deletions OMEdit/OMEditGUI/Annotations/TextAnnotation.cpp
Expand Up @@ -34,6 +34,7 @@

#include "TextAnnotation.h"
#include "Commands.h"
#include "VariablesWidget.h"

/*!
* \class TextAnnotation
Expand Down Expand Up @@ -123,7 +124,15 @@ void TextAnnotation::parseShapeAnnotation(QString annotation)
mExtents.replace(i, QPointF(extentPoints.at(0).toFloat(), extentPoints.at(1).toFloat()));
}
// 10th item of the list contains the textString.
mOriginalTextString = StringHandler::removeFirstLastQuotes(list.at(9));
if (list.at(9).startsWith("{")) {
// DynamicSelect
mDynamicSelect = StringHandler::getStrings(StringHandler::removeFirstLastCurlBrackets(list.at(9)));
if (mDynamicSelect.count() >= 1)
mOriginalTextString = StringHandler::removeFirstLastQuotes(mDynamicSelect.at(0));
}
else {
mOriginalTextString = StringHandler::removeFirstLastQuotes(list.at(9));
}
mTextString = mOriginalTextString;
initUpdateTextString();
// 11th item of the list contains the fontSize.
Expand Down Expand Up @@ -420,7 +429,7 @@ void TextAnnotation::updateShape(ShapeAnnotation *pShapeAnnotation)
void TextAnnotation::initUpdateTextString()
{
if (mpComponent) {
if (mOriginalTextString.contains("%")) {
if (mOriginalTextString.contains("%") || mDynamicSelect.count() >= 2) {
updateTextString();
connect(mpComponent, SIGNAL(displayTextChanged()), SLOT(updateTextString()), Qt::UniqueConnection);
}
Expand Down Expand Up @@ -470,6 +479,24 @@ void TextAnnotation::updateTextString()
* - %name replaced by the name of the component (i.e. the identifier for it in in the enclosing class).
* - %class replaced by the name of the class.
*/
// first check for available results and DynamicSelect
ModelWidget *pModelWidget = mpComponent->getGraphicsView()->getModelWidget();
if (!pModelWidget->getResultFileName().isEmpty() && mDynamicSelect.count() >= 2) {
MainWindow *pMainWindow = pModelWidget->getModelWidgetContainer()->getMainWindow();
VariablesTreeModel *pVariablesTreeModel = pMainWindow->getVariablesWidget()->getVariablesTreeModel();
VariablesTreeItem *pVariablesTreeItem = pVariablesTreeModel->findVariablesTreeItem(pModelWidget->getResultFileName() + "." + mpComponent->getComponentInfo()->getName() + "." + mDynamicSelect.at(1), pVariablesTreeModel->getRootVariablesTreeItem());
if (pVariablesTreeItem != NULL) {
mTextString = pVariablesTreeItem->getValue(pVariablesTreeItem->getUnit(), pMainWindow->getOMCProxy()).toString();
if (mDynamicSelect.count() >= 3) {
int digits = mDynamicSelect.at(2).toInt();
mTextString = QString::number(mTextString.toDouble(), 'g', digits);
}
}
else
mTextString = mDynamicSelect.at(1); // default value if result not found
return;
}
// alternatively use model value
mTextString = mOriginalTextString;
if (!mTextString.contains("%")) {
return;
Expand Down
2 changes: 2 additions & 0 deletions OMEdit/OMEditGUI/Component/ComponentProperties.cpp
Expand Up @@ -1134,6 +1134,8 @@ void ComponentParameters::updateComponentParameters()
newComponentExtendsModifiersMap);
mpComponent->getGraphicsView()->getModelWidget()->getUndoStack()->push(pUpdateComponentParametersCommand);
mpComponent->getGraphicsView()->getModelWidget()->updateModelText();
// remove possible dynamic results to see the new static values
mpComponent->getGraphicsView()->getModelWidget()->removeDynamicResults();
}
accept();
}
Expand Down
61 changes: 61 additions & 0 deletions OMEdit/OMEditGUI/Modeling/ModelWidgetContainer.cpp
Expand Up @@ -41,6 +41,7 @@
#include "ComponentProperties.h"
#include "Commands.h"
#include "FetchInterfaceDataDialog.h"
#include "VariablesWidget.h"

//! @class GraphicsScene
//! @brief The GraphicsScene class is a container for graphicsl components in a simulationmodel.
Expand Down Expand Up @@ -231,6 +232,7 @@ bool GraphicsView::addComponent(QString className, QPointF position)
if (!pLibraryTreeItem) {
return false;
}
mpModelWidget->removeDynamicResults(); // show static values during editing
QStringList dialogAnnotation;
// if we are dropping something on meta-model editor then we can skip Modelica stuff.
if (mpModelWidget->getLibraryTreeItem()->getLibraryType() == LibraryTreeItem::MetaModel) {
Expand Down Expand Up @@ -2440,6 +2442,9 @@ ModelWidget::ModelWidget(LibraryTreeItem* pLibraryTreeItem, ModelWidgetContainer
}
mpLibraryTreeItem->setClassText(contents);
}
// Clean up model widget if results are removed from variables browser
connect(mpModelWidgetContainer->getMainWindow()->getVariablesWidget()->getVariablesTreeModel(),
SIGNAL(variableTreeItemRemoved(QString)), this, SLOT(removeDynamicResults(QString)));
}

/*!
Expand Down Expand Up @@ -3032,6 +3037,7 @@ bool ModelWidget::modelicaEditorTextChanged(LibraryTreeItem **pLibraryTreeItem)
QString modelicaText = pModelicaEditor->getPlainText();
QString stringToLoad;
LibraryTreeItem *pParentLibraryTreeItem = mpModelWidgetContainer->getMainWindow()->getLibraryWidget()->getLibraryTreeModel()->getContainingFileParentLibraryTreeItem(mpLibraryTreeItem);
removeDynamicResults(); // show static values during editing
if (pParentLibraryTreeItem != mpLibraryTreeItem) {
stringToLoad = mpLibraryTreeItem->getClassTextBefore() + modelicaText + mpLibraryTreeItem->getClassTextAfter();
} else {
Expand Down Expand Up @@ -3233,6 +3239,24 @@ void ModelWidget::updateUndoRedoActions()
}
}

/*!
* \brief ModelWidget::updateDynamicResults
* Update the model widget with values from resultFile.
* Skip update for empty resultFileName -- use removeDynamicResults.
*/
void ModelWidget::updateDynamicResults(QString resultFileName)
{
mResultFileName = resultFileName;
if (!resultFileName.isEmpty()) {
foreach (Component *component, mpDiagramGraphicsView->getInheritedComponentsList()) {
component->componentParameterHasChanged();
}
foreach (Component *component, mpDiagramGraphicsView->getComponentsList()) {
component->componentParameterHasChanged();
}
}
}

/*!
* \brief ModelWidget::getModelInheritedClasses
* Gets the class inherited classes.
Expand Down Expand Up @@ -4009,6 +4033,29 @@ void ModelWidget::showTextView(bool checked)
updateUndoRedoActions();
}

/*!
* \brief ModelWidget::removeDynamicResults
* Check if resultFileName is empty or equals mResultFileName.
* Update the model widget with static values from the model
* and empty mResultFileName.
*/
void ModelWidget::removeDynamicResults(QString resultFileName)
{
if (mResultFileName.isEmpty()) {
// nothing to do
return;
}
if (resultFileName.isEmpty() or resultFileName == mResultFileName) {
mResultFileName = "";
foreach (Component *component, mpDiagramGraphicsView->getInheritedComponentsList()) {
component->componentParameterHasChanged();
}
foreach (Component *component, mpDiagramGraphicsView->getComponentsList()) {
component->componentParameterHasChanged();
}
}
}

void ModelWidget::makeFileWritAble()
{
const QString &fileName = mpLibraryTreeItem->getFileName();
Expand Down Expand Up @@ -4217,6 +4264,20 @@ ModelWidget* ModelWidgetContainer::getCurrentModelWidget()
}
}

/*!
* \brief ModelWidgetContainer::getModelWidget
* Returns the ModelWidget for className or NULL if not found.
*/
ModelWidget* ModelWidgetContainer::getModelWidget(const QString& className)
{
foreach (QMdiSubWindow *pSubWindow, subWindowList()) {
ModelWidget *pModelWidget = qobject_cast<ModelWidget*>(pSubWindow->widget());
if (className == pModelWidget->getLibraryTreeItem()->getNameStructure())
return pModelWidget;
}
return NULL;
}

/*!
* \brief ModelWidgetContainer::getCurrentMdiSubWindow
* Returns the current QMdiSubWindow.
Expand Down
5 changes: 5 additions & 0 deletions OMEdit/OMEditGUI/Modeling/ModelWidgetContainer.h
Expand Up @@ -369,6 +369,8 @@ class ModelWidget : public QWidget
void updateModelText();
void updateModelicaTextManually(QString contents);
void updateUndoRedoActions();
void updateDynamicResults(QString resultFileName);
QString getResultFileName() {return mResultFileName;}
private:
ModelWidgetContainer *mpModelWidgetContainer;
LibraryTreeItem *mpLibraryTreeItem;
Expand Down Expand Up @@ -400,6 +402,7 @@ class ModelWidget : public QWidget
QList<LibraryTreeItem*> mInheritedClassesList;
QList<ComponentInfo*> mComponentsList;
QStringList mComponentsAnnotationsList;
QString mResultFileName;

void getModelInheritedClasses();
void drawModelInheritedClassShapes(ModelWidget *pModelWidget, StringHandler::ViewType viewType);
Expand Down Expand Up @@ -427,6 +430,7 @@ public slots:
bool metaModelEditorTextChanged();
void handleCanUndoChanged(bool canUndo);
void handleCanRedoChanged(bool canRedo);
void removeDynamicResults(QString resultFileName = "");
protected:
virtual void closeEvent(QCloseEvent *event);
};
Expand All @@ -439,6 +443,7 @@ class ModelWidgetContainer : public MdiArea
ModelWidgetContainer(MainWindow *pParent);
void addModelWidget(ModelWidget *pModelWidget, bool checkPreferedView = true);
ModelWidget* getCurrentModelWidget();
ModelWidget* getModelWidget(const QString &className);
QMdiSubWindow* getCurrentMdiSubWindow();
QMdiSubWindow* getMdiSubWindow(ModelWidget *pModelWidget);
void setPreviousViewType(StringHandler::ViewType viewType) {mPreviousViewType = viewType;}
Expand Down
78 changes: 53 additions & 25 deletions OMEdit/OMEditGUI/Plotting/VariablesWidget.cpp
Expand Up @@ -239,6 +239,31 @@ VariablesTreeItem* VariablesTreeItem::rootParent()
return pVariablesTreeItem1;
}

/*!
* \brief VariablesTreeItem::getValue
* Returns the value in the desired unit or
* an empty value in case of conversion error.
*/
QVariant VariablesTreeItem::getValue(QString unit, OMCProxy *pOMCProxy)
{
QString value = "";
if (mPreviousUnit.compare(unit) == 0) {
value = mValue;
}
else {
OMCInterface::convertUnits_res convertUnit = pOMCProxy->convertUnits(mPreviousUnit, unit);
if (convertUnit.unitsCompatible) {
bool ok = false;
qreal realValue = mValue.toDouble(&ok);
if (ok) {
realValue = Utilities::convertUnit(realValue, convertUnit.offset, convertUnit.scaleFactor);
value = QString::number(realValue);
}
}
}
return value;
}

VariablesTreeModel::VariablesTreeModel(VariablesTreeView *pVariablesTreeView)
: QAbstractItemModel(pVariablesTreeView)
{
Expand Down Expand Up @@ -701,8 +726,9 @@ void VariablesTreeModel::getVariableInformation(ModelicaMatReader *pMatReader, Q
QHash<QString, QString> hash = mScalarVariablesList.value(variableToFind);
if (hash["name"].compare(variableToFind) == 0) {
*changeAble = (hash["isValueChangeable"].compare("true") == 0) ? true : false;
if (*changeAble) {
*value = hash["start"];
QString start = hash["start"];
if (*changeAble && !start.isEmpty()) {
*value = start;
} else { /* if the variable is not a tunable parameter then read the final value of the variable. Only mat result files are supported. */
if ((pMatReader->file != NULL) && strcmp(pMatReader->fileName, "")) {
*value = "";
Expand Down Expand Up @@ -894,6 +920,23 @@ void VariablesWidget::insertVariablesItemsToTree(QString fileName, QString fileP
/* In order to improve the response time of insertVariablesItems function we should clear the filter and collapse all the items. */
mpVariableTreeProxyModel->setFilterRegExp(QRegExp(""));
mpVariablesTreeView->collapseAll();
/* Show results in model diagram if it is present in ModelWidgetContainer
and if switch to plotting perspective is disabled */
ModelWidget *pModelWidget = NULL;
if (!mpMainWindow->getOptionsDialog()->getSimulationPage()->getSwitchToPlottingPerspectiveCheckBox()->isChecked()) {
pModelWidget = mpMainWindow->getModelWidgetContainer()->getModelWidget(simulationOptions.getClassName());
}
if (pModelWidget != NULL) {
if (simulationOptions.isReSimulate() && pModelWidget->getResultFileName().isEmpty()) {
/* skip update of model view if the model has changed */
pModelWidget = NULL;
}
else {
/* prevent update during removeVariableTreeItem
because the model will be updated below anyway */
pModelWidget->updateDynamicResults("");
}
}
/* Remove the simulation result if we already had it in tree */
bool variableItemDeleted = mpVariablesTreeModel->removeVariableTreeItem(fileName);
/* add the plot variables */
Expand All @@ -902,6 +945,10 @@ void VariablesWidget::insertVariablesItemsToTree(QString fileName, QString fileP
if (variableItemDeleted) {
variablesUpdated();
}
/* update the model widget */
if (pModelWidget != NULL) {
pModelWidget->updateDynamicResults(fileName);
}
mpVariablesTreeView->setSortingEnabled(true);
mpVariablesTreeView->sortByColumn(0, Qt::AscendingOrder);
/* since we cleared the filter above so we need to apply it back. */
Expand Down Expand Up @@ -1023,20 +1070,11 @@ void VariablesWidget::readVariablesAndUpdateXML(VariablesTreeItem *pVariablesTre
for (int i = 0 ; i < pVariablesTreeItem->getChildren().size() ; i++) {
VariablesTreeItem *pChildVariablesTreeItem = pVariablesTreeItem->child(i);
if (pChildVariablesTreeItem->isEditable() && pChildVariablesTreeItem->isValueChanged()) {
QString value = pChildVariablesTreeItem->data(1, Qt::DisplayRole).toString();
//QString value = pChildVariablesTreeItem->data(1, Qt::DisplayRole).toString();
/* Ticket #2250, 4031
* We need to convert the value to base unit since the values stored in init xml are always in base unit.
*/
if (pChildVariablesTreeItem->getUnit().compare(pChildVariablesTreeItem->getDisplayUnit()) != 0) {
OMCInterface::convertUnits_res convertUnit = mpMainWindow->getOMCProxy()->convertUnits(pChildVariablesTreeItem->getDisplayUnit(),
pChildVariablesTreeItem->getUnit());
if (convertUnit.unitsCompatible) {
bool ok = true;
qreal realValue = value.toDouble(&ok);
realValue = Utilities::convertUnit(realValue, convertUnit.offset, convertUnit.scaleFactor);
value = QString::number(realValue);
}
}
QString value = pChildVariablesTreeItem->getValue(pChildVariablesTreeItem->getUnit(), mpMainWindow->getOMCProxy()).toString();
QString variableToFind = pChildVariablesTreeItem->getVariableName();
variableToFind.remove(QRegExp(outputFileName + "."));
QHash<QString, QString> hash;
Expand Down Expand Up @@ -1195,18 +1233,8 @@ void VariablesWidget::plotVariables(const QModelIndex &index, qreal curveThickne
* Update the value of Variables Browser display unit according to the display unit of already plotted curve.
*/
pVariablesTreeItem->setData(3, pPlotCurve->getDisplayUnit(), Qt::EditRole);
OMCInterface::convertUnits_res convertUnit = mpMainWindow->getOMCProxy()->convertUnits(pVariablesTreeItem->getPreviousUnit(),
pVariablesTreeItem->getDisplayUnit());
if (convertUnit.unitsCompatible) {
/* update value */
QVariant stringValue = pVariablesTreeItem->data(1, Qt::EditRole);
bool ok = true;
qreal realValue = stringValue.toDouble(&ok);
if (ok) {
realValue = Utilities::convertUnit(realValue, convertUnit.offset, convertUnit.scaleFactor);
pVariablesTreeItem->setData(1, QString::number(realValue), Qt::EditRole);
}
}
QString value = pVariablesTreeItem->getValue(pVariablesTreeItem->getDisplayUnit(), mpMainWindow->getOMCProxy()).toString();
pVariablesTreeItem->setData(1, value, Qt::EditRole);
if (pPlotCurve && pVariablesTreeItem->getUnit().compare(pVariablesTreeItem->getDisplayUnit()) != 0) {
OMCInterface::convertUnits_res convertUnit = mpMainWindow->getOMCProxy()->convertUnits(pVariablesTreeItem->getUnit(),
pVariablesTreeItem->getDisplayUnit());
Expand Down
1 change: 1 addition & 0 deletions OMEdit/OMEditGUI/Plotting/VariablesWidget.h
Expand Up @@ -73,6 +73,7 @@ class VariablesTreeItem
int row() const;
VariablesTreeItem* parent();
VariablesTreeItem* rootParent();
QVariant getValue(QString unit, OMCProxy *pOMCProxy);
private:
QList<VariablesTreeItem*> mChildren;
VariablesTreeItem *mpParentVariablesTreeItem;
Expand Down

0 comments on commit f9c1597

Please sign in to comment.