Skip to content

Commit

Permalink
Fixes ticket:4842 Export the values with the actual unit
Browse files Browse the repository at this point in the history
Belonging to [master]:
  - #108
  • Loading branch information
adeas31 authored and OpenModelica-Hudson committed Apr 2, 2019
1 parent db70cfa commit 3823fc8
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 42 deletions.
9 changes: 8 additions & 1 deletion OMEdit/OMEdit/OMEditGUI/Plotting/PlotWindowContainer.cpp
Expand Up @@ -535,7 +535,14 @@ void PlotWindowContainer::exportVariables()
// write time data
data << QString::number(pPlotWindow->getPlot()->getPlotCurvesList().at(0)->mXAxisVector.at(i));
for (int j = 0; j < headers.size() - 1; ++j) {
data << QString::number(pPlotWindow->getPlot()->getPlotCurvesList().at(j)->mYAxisVector.at(i));
PlotCurve *pPlotCurve = pPlotWindow->getPlot()->getPlotCurvesList().at(j);
OMCInterface::convertUnits_res convertUnit = MainWindow::instance()->getOMCProxy()->convertUnits(pPlotCurve->getDisplayUnit(),
pPlotCurve->getUnit());
if (convertUnit.unitsCompatible) {
data << QString::number(Utilities::convertUnit(pPlotCurve->mYAxisVector.at(i), convertUnit.offset, convertUnit.scaleFactor));
} else {
data << QString::number(pPlotCurve->mYAxisVector.at(i));
}
}
contents.append(data.join(",")).append("\n");
}
Expand Down
88 changes: 49 additions & 39 deletions OMEdit/OMEdit/OMEditGUI/Plotting/VariablesWidget.cpp
Expand Up @@ -484,60 +484,34 @@ QModelIndex VariablesTreeModel::variablesTreeItemIndexHelper(const VariablesTree
return QModelIndex();
}

/*!
* \brief VariablesTreeModel::parseInitXml
* Parses the model_init.xml file and returns the scalar variables information.
* \param xmlReader
* \return
*/
void VariablesTreeModel::parseInitXml(QXmlStreamReader &xmlReader)
{
mScalarVariablesList.clear();
/* We'll parse the XML until we reach end of it.*/
while(!xmlReader.atEnd() && !xmlReader.hasError())
{
while (!xmlReader.atEnd() && !xmlReader.hasError()) {
/* Read next element.*/
QXmlStreamReader::TokenType token = xmlReader.readNext();
/* If token is just StartDocument, we'll go to next.*/
if(token == QXmlStreamReader::StartDocument)
if (token == QXmlStreamReader::StartDocument) {
continue;
}
/* If token is StartElement, we'll see if we can read it.*/
if(token == QXmlStreamReader::StartElement)
{
if (token == QXmlStreamReader::StartElement) {
/* If it's named ScalarVariable, we'll dig the information from there.*/
if(xmlReader.name() == "ScalarVariable")
{
if (xmlReader.name() == "ScalarVariable") {
QHash<QString, QString> scalarVariable = parseScalarVariable(xmlReader);
mScalarVariablesList.insert(scalarVariable.value("name"),scalarVariable);
mScalarVariablesHash.insert(scalarVariable.value("name"),scalarVariable);
}
}
}
xmlReader.clear();
}

QHash<QString, QString> VariablesTreeModel::parseScalarVariable(QXmlStreamReader &xmlReader)
{
QHash<QString, QString> scalarVariable;
/* Let's check that we're really getting a ScalarVariable. */
if(xmlReader.tokenType() != QXmlStreamReader::StartElement && xmlReader.name() == "ScalarVariable")
return scalarVariable;
/* Let's get the attributes for ScalarVariable */
QXmlStreamAttributes attributes = xmlReader.attributes();
/* Read the ScalarVariable attributes. */
scalarVariable["name"] = attributes.value("name").toString();
scalarVariable["description"] = attributes.value("description").toString();
scalarVariable["isValueChangeable"] = attributes.value("isValueChangeable").toString();
scalarVariable["variability"] = attributes.value("variability").toString();
/* Read the next element i.e Real, Integer, Boolean etc. */
xmlReader.readNext();
while(!(xmlReader.tokenType() == QXmlStreamReader::EndElement && xmlReader.name() == "ScalarVariable"))
{
if(xmlReader.tokenType() == QXmlStreamReader::StartElement)
{
QXmlStreamAttributes attributes = xmlReader.attributes();
scalarVariable["start"] = attributes.value("start").toString();
scalarVariable["unit"] = attributes.value("unit").toString();
scalarVariable["displayUnit"] = attributes.value("displayUnit").toString();
}
xmlReader.readNext();
}
return scalarVariable;
}

/*!
* \brief VariablesTreeModel::insertVariablesItems
* Inserts the variables in the Variables Browser.
Expand Down Expand Up @@ -573,6 +547,7 @@ void VariablesTreeModel::insertVariablesItems(QString fileName, QString filePath
pTopVariablesTreeItem->setActive();
}
/* open the model_init.xml file for reading */
mScalarVariablesHash.clear();
QString initFileName;
if (simulationOptions.isValid()) {
initFileName = QString(simulationOptions.getOutputFileName()).append("_init.xml");
Expand Down Expand Up @@ -850,6 +825,41 @@ void VariablesTreeModel::plotAllVariables(VariablesTreeItem *pVariablesTreeItem,
}
}

/*!
* \brief VariablesTreeModel::parseScalarVariable
* Parses the scalar variable.
* Helper function for VariablesTreeModel::parseInitXml
* \param xmlReader
* \return
*/
QHash<QString, QString> VariablesTreeModel::parseScalarVariable(QXmlStreamReader &xmlReader)
{
QHash<QString, QString> scalarVariable;
/* Let's check that we're really getting a ScalarVariable. */
if (xmlReader.tokenType() != QXmlStreamReader::StartElement && xmlReader.name() == "ScalarVariable") {
return scalarVariable;
}
/* Let's get the attributes for ScalarVariable */
QXmlStreamAttributes attributes = xmlReader.attributes();
/* Read the ScalarVariable attributes. */
scalarVariable["name"] = attributes.value("name").toString();
scalarVariable["description"] = attributes.value("description").toString();
scalarVariable["isValueChangeable"] = attributes.value("isValueChangeable").toString();
scalarVariable["variability"] = attributes.value("variability").toString();
/* Read the next element i.e Real, Integer, Boolean etc. */
xmlReader.readNext();
while (!(xmlReader.tokenType() == QXmlStreamReader::EndElement && xmlReader.name() == "ScalarVariable")) {
if (xmlReader.tokenType() == QXmlStreamReader::StartElement) {
QXmlStreamAttributes attributes = xmlReader.attributes();
scalarVariable["start"] = attributes.value("start").toString();
scalarVariable["unit"] = attributes.value("unit").toString();
scalarVariable["displayUnit"] = attributes.value("displayUnit").toString();
}
xmlReader.readNext();
}
return scalarVariable;
}

/*!
* \brief VariablesTreeModel::getVariableInformation
* Returns the variable information like value, unit, displayunit and description.
Expand All @@ -865,7 +875,7 @@ void VariablesTreeModel::plotAllVariables(VariablesTreeItem *pVariablesTreeItem,
void VariablesTreeModel::getVariableInformation(ModelicaMatReader *pMatReader, QString variableToFind, QString *value, bool *changeAble,
QString *variability, QString *unit, QString *displayUnit, QString *description)
{
QHash<QString, QString> hash = mScalarVariablesList.value(variableToFind);
QHash<QString, QString> hash = mScalarVariablesHash.value(variableToFind);
if (hash["name"].compare(variableToFind) == 0) {
*changeAble = (hash["isValueChangeable"].compare("true") == 0) ? true : false;
*variability = hash["variability"];
Expand Down
4 changes: 2 additions & 2 deletions OMEdit/OMEdit/OMEditGUI/Plotting/VariablesWidget.h
Expand Up @@ -131,7 +131,6 @@ class VariablesTreeModel : public QAbstractItemModel
QModelIndex variablesTreeItemIndexHelper(const VariablesTreeItem *pVariablesTreeItem, const VariablesTreeItem *pParentVariablesTreeItem,
const QModelIndex &parentIndex) const;
void parseInitXml(QXmlStreamReader &xmlReader);
QHash<QString, QString> parseScalarVariable(QXmlStreamReader &xmlReader);
void insertVariablesItems(QString fileName, QString filePath, QStringList variablesList, SimulationOptions simulationOptions);
bool removeVariableTreeItem(QString variable);
void unCheckVariables(VariablesTreeItem *pVariablesTreeItem);
Expand All @@ -140,7 +139,8 @@ class VariablesTreeModel : public QAbstractItemModel
VariablesTreeView *mpVariablesTreeView;
VariablesTreeItem *mpRootVariablesTreeItem;
VariablesTreeItem *mpActiveVariablesTreeItem;
QHash<QString, QHash<QString,QString> > mScalarVariablesList;
QHash<QString, QHash<QString,QString> > mScalarVariablesHash;
QHash<QString, QString> parseScalarVariable(QXmlStreamReader &xmlReader);
void getVariableInformation(ModelicaMatReader *pMatReader, QString variableToFind, QString *value, bool *changeAble, QString *variability,
QString *unit, QString *displayUnit, QString *description);
signals:
Expand Down

0 comments on commit 3823fc8

Please sign in to comment.