Skip to content

Commit

Permalink
Handle prefixing of time units (#12370)
Browse files Browse the repository at this point in the history
#12352
Fixes the regression introduced in #12221
  • Loading branch information
adeas31 committed May 3, 2024
1 parent 5789400 commit 5fa03b4
Show file tree
Hide file tree
Showing 13 changed files with 210 additions and 178 deletions.
5 changes: 2 additions & 3 deletions OMEdit/OMEditLIB/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1669,10 +1669,9 @@ void MainWindow::PlotCallbackFunction(void *p, int externalWindow, const char* f
if (!fileInfo.exists()) return;
OMPlot::PlotWindow *pPlotWindow = pMainWindow->getPlotWindowContainer()->getCurrentWindow();
if (pPlotWindow && !externalWindow) {
if (pPlotWindow->getPlotType() == OMPlot::PlotWindow::PLOT && strcmp(plotType, "plotparametric") == 0) {
if (pPlotWindow->isPlot() && strcmp(plotType, "plotparametric") == 0) {
pMainWindow->getPlotWindowContainer()->addParametricPlotWindow();
} else if (pPlotWindow->getPlotType() == OMPlot::PlotWindow::PLOTPARAMETRIC &&
((strcmp(plotType, "plot") == 0) || (strcmp(plotType, "plotall") == 0))) {
} else if (pPlotWindow->isPlotParametric() && ((strcmp(plotType, "plot") == 0) || (strcmp(plotType, "plotall") == 0))) {
pMainWindow->getPlotWindowContainer()->addPlotWindow();
}
} else if (externalWindow || pMainWindow->getPlotWindowContainer()->subWindowList().size() == 0) {
Expand Down
2 changes: 1 addition & 1 deletion OMEdit/OMEditLIB/Plotting/PlotWindowContainer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,7 @@ void PlotWindowContainer::exportVariables()
QMessageBox::information(this, QString("%1 - %2").arg(Helper::applicationName, Helper::information), tr("No plot window is active for exporting variables."), Helper::ok);
return;
}
if (pPlotWindow->getPlotType() == PlotWindow::PLOTPARAMETRIC || pPlotWindow->getPlotType() == PlotWindow::PLOTARRAYPARAMETRIC) {
if (pPlotWindow->isPlotParametric() || pPlotWindow->isPlotArrayParametric()) {
QMessageBox::information(this, QString("%1 - %2").arg(Helper::applicationName, Helper::information), tr("Cannot export parametric plot."), Helper::ok);
return;
}
Expand Down
41 changes: 19 additions & 22 deletions OMEdit/OMEditLIB/Plotting/VariablesWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1584,7 +1584,7 @@ void VariablesWidget::variablesUpdated()
PlotWindow *pPlotWindow = qobject_cast<PlotWindow*>(pSubWindow->widget());
if (pPlotWindow) { // we can have an AnimateWindow there as well so always check
foreach (PlotCurve *pPlotCurve, pPlotWindow->getPlot()->getPlotCurvesList()) {
if (pPlotWindow->getPlotType() == PlotWindow::PLOT || pPlotWindow->getPlotType() == PlotWindow::PLOTARRAY) {
if (pPlotWindow->isPlot() || pPlotWindow->isPlotArray()) {
QString curveNameStructure = pPlotCurve->getNameStructure();
VariablesTreeItem *pVariableTreeItem;
pVariableTreeItem = mpVariablesTreeModel->findVariablesTreeItem(curveNameStructure, mpVariablesTreeModel->getRootVariablesTreeItem());
Expand All @@ -1599,7 +1599,7 @@ void VariablesWidget::variablesUpdated()
pPlotWindow->getPlot()->removeCurve(pPlotCurve);
pPlotCurve->detach();
}
} else if (pPlotWindow->getPlotType() == PlotWindow::PLOTPARAMETRIC || pPlotWindow->getPlotType() == PlotWindow::PLOTARRAYPARAMETRIC) {
} else if (pPlotWindow->isPlotParametric() || pPlotWindow->isPlotArrayParametric()) {
QString xVariable = QString(pPlotCurve->getFileName()).append(".").append(pPlotCurve->getXVariable());
VariablesTreeItem *pXVariableTreeItem;
pXVariableTreeItem = mpVariablesTreeModel->findVariablesTreeItem(xVariable, mpVariablesTreeModel->getRootVariablesTreeItem());
Expand Down Expand Up @@ -1656,13 +1656,13 @@ void VariablesWidget::updateVariablesTreeHelper(QMdiSubWindow *pSubWindow)
state = mpVariablesTreeModel->blockSignals(true);
foreach (PlotCurve *pPlotCurve, pPlotWindow->getPlot()->getPlotCurvesList()) {
VariablesTreeItem *pVariablesTreeItem;
if (pPlotWindow->getPlotType() == PlotWindow::PLOT || pPlotWindow->getPlotType() == PlotWindow::PLOTARRAY) {
if (pPlotWindow->isPlot() || pPlotWindow->isPlotArray()) {
QString variable = pPlotCurve->getNameStructure();
pVariablesTreeItem = mpVariablesTreeModel->findVariablesTreeItem(variable, mpVariablesTreeModel->getRootVariablesTreeItem());
if (pVariablesTreeItem) {
mpVariablesTreeModel->setData(mpVariablesTreeModel->variablesTreeItemIndex(pVariablesTreeItem), Qt::Checked, Qt::CheckStateRole);
}
} else if (pPlotWindow->getPlotType() == PlotWindow::PLOTPARAMETRIC || pPlotWindow->getPlotType() == PlotWindow::PLOTARRAYPARAMETRIC) {
} else if (pPlotWindow->isPlotParametric() || pPlotWindow->isPlotArrayParametric()) {
// check the xvariable
QString xVariable = QString(pPlotCurve->getFileName()).append(".").append(pPlotCurve->getXVariable());
pVariablesTreeItem = mpVariablesTreeModel->findVariablesTreeItem(xVariable, mpVariablesTreeModel->getRootVariablesTreeItem());
Expand All @@ -1673,7 +1673,7 @@ void VariablesWidget::updateVariablesTreeHelper(QMdiSubWindow *pSubWindow)
pVariablesTreeItem = mpVariablesTreeModel->findVariablesTreeItem(yVariable, mpVariablesTreeModel->getRootVariablesTreeItem());
if (pVariablesTreeItem)
mpVariablesTreeModel->setData(mpVariablesTreeModel->variablesTreeItemIndex(pVariablesTreeItem), Qt::Checked, Qt::CheckStateRole);
} else if (pPlotWindow->getPlotType() == PlotWindow::PLOTINTERACTIVE) {
} else if (pPlotWindow->isPlotInteractive()) {
QString variable = pPlotCurve->getNameStructure();
pVariablesTreeItem = mpVariablesTreeModel->findVariablesTreeItem(variable, mpVariablesTreeModel->getRootVariablesTreeItem());
if (pVariablesTreeItem) {
Expand Down Expand Up @@ -1931,7 +1931,7 @@ void VariablesWidget::plotVariables(const QModelIndex &index, qreal curveThickne
// if the variable is not an array and
// pPlotWindow is 0 or the plot's type is PLOTARRAY or PLOTARRAYPARAMETRIC
// then create a new plot window.
if (!pVariablesTreeItem->isMainArray() && (!pPlotWindow || pPlotWindow->getPlotType() == PlotWindow::PLOTARRAY || pPlotWindow->getPlotType() == PlotWindow::PLOTARRAYPARAMETRIC)) {
if (!pVariablesTreeItem->isMainArray() && (!pPlotWindow || pPlotWindow->isPlotArray() || pPlotWindow->isPlotArrayParametric())) {
bool checkedState = pVariablesTreeItem->isChecked();
MainWindow::instance()->getPlotWindowContainer()->addPlotWindow();
pPlotWindow = MainWindow::instance()->getPlotWindowContainer()->getCurrentWindow();
Expand All @@ -1940,7 +1940,7 @@ void VariablesWidget::plotVariables(const QModelIndex &index, qreal curveThickne
// if the variable is an array and
// pPlotWindow is 0 or the plot's type is PLOT or PLOTPARAMETRIC
// then create a new plot window.
else if (pVariablesTreeItem->isMainArray() && (!pPlotWindow || pPlotWindow->getPlotType() == PlotWindow::PLOT || pPlotWindow->getPlotType() == PlotWindow::PLOTPARAMETRIC)) {
else if (pVariablesTreeItem->isMainArray() && (!pPlotWindow || pPlotWindow->isPlot() || pPlotWindow->isPlotParametric())) {
bool checkedState = pVariablesTreeItem->isChecked();
MainWindow::instance()->getPlotWindowContainer()->addArrayPlotWindow();
pPlotWindow = MainWindow::instance()->getPlotWindowContainer()->getCurrentWindow();
Expand All @@ -1952,7 +1952,7 @@ void VariablesWidget::plotVariables(const QModelIndex &index, qreal curveThickne
return;
}
// if plottype is PLOT or PLOTARRAY then
if (pPlotWindow->getPlotType() == PlotWindow::PLOT || pPlotWindow->getPlotType() == PlotWindow::PLOTARRAY) {
if (pPlotWindow->isPlot() || pPlotWindow->isPlotArray()) {
// check the item checkstate
if (pVariablesTreeItem->isChecked()) {
VariablesTreeItem *pVariablesTreeRootItem = pVariablesTreeItem;
Expand All @@ -1970,7 +1970,7 @@ void VariablesWidget::plotVariables(const QModelIndex &index, qreal curveThickne
pPlotWindow->setVariablesList(QStringList(pVariablesTreeItem->getPlotVariable()));
pPlotWindow->setYUnit(Utilities::convertUnitToSymbol(pVariablesTreeItem->getUnit()));
pPlotWindow->setYDisplayUnit(Utilities::convertUnitToSymbol(pVariablesTreeItem->getDisplayUnit()));
if (pPlotWindow->getPlotType() == PlotWindow::PLOT) {
if (pPlotWindow->isPlot()) {
pPlotWindow->plot(pPlotCurve);
/* Ticket:5839
* Check the toggle sign of the curve and apply it in case of update.
Expand All @@ -1979,7 +1979,7 @@ void VariablesWidget::plotVariables(const QModelIndex &index, qreal curveThickne
pPlotCurve->setToggleSign(false);
pPlotWindow->toggleSign(pPlotCurve, true);
}
} else {/* ie. (pPlotWindow->getPlotType() == PlotWindow::PLOTARRAY)*/
} else {/* i.e., pPlotWindow->isPlotArray() */
double timePercent = mpTimeTextBox->text().toDouble();
pPlotWindow->plotArray(timePercent, pPlotCurve);
}
Expand Down Expand Up @@ -2030,7 +2030,7 @@ void VariablesWidget::plotVariables(const QModelIndex &index, qreal curveThickne
}
}
}
} else if (pPlotWindow->getPlotType() == PlotWindow::PLOTPARAMETRIC || pPlotWindow->getPlotType() == PlotWindow::PLOTARRAYPARAMETRIC) { // if plottype is PLOTPARAMETRIC or PLOTARRAYPARAMETRIC then
} else if (pPlotWindow->isPlotParametric() || pPlotWindow->isPlotArrayParametric()) { // if plottype is PLOTPARAMETRIC or PLOTARRAYPARAMETRIC then
// check the item checkstate
if (pVariablesTreeItem->isChecked()) {
VariablesTreeItem *pVariablesTreeRootItem = pVariablesTreeItem;
Expand Down Expand Up @@ -2087,7 +2087,7 @@ void VariablesWidget::plotVariables(const QModelIndex &index, qreal curveThickne
pPlotWindow->setXDisplayUnit(Utilities::convertUnitToSymbol(plotParametricCurve.xVariable.displayUnit));
pPlotWindow->setYUnit(Utilities::convertUnitToSymbol(plotParametricVariable.unit));
pPlotWindow->setYDisplayUnit(Utilities::convertUnitToSymbol(plotParametricVariable.displayUnit));
if (pPlotWindow->getPlotType() == PlotWindow::PLOTPARAMETRIC) {
if (pPlotWindow->isPlotParametric()) {
pPlotWindow->plotParametric(pPlotCurve);
/* Ticket:5839
* Check the toggle sign of the curve and apply it in case of update.
Expand All @@ -2096,7 +2096,7 @@ void VariablesWidget::plotVariables(const QModelIndex &index, qreal curveThickne
pPlotCurve->setToggleSign(false);
pPlotWindow->toggleSign(pPlotCurve, true);
}
} else { /* ie. (pPlotWindow->getPlotType() == PlotWindow::PLOTARRAYPARAMETRIC)*/
} else { /* i.e., pPlotWindow->isPlotArrayParametric() */
double timePercent = mpTimeTextBox->text().toDouble();
pPlotWindow->plotArrayParametric(timePercent, pPlotCurve);
}
Expand Down Expand Up @@ -2291,11 +2291,11 @@ void VariablesWidget::unitChanged(const QModelIndex &index)
}
pPlotCurve->plotData();
pPlotCurve->setYDisplayUnit(Utilities::convertUnitToSymbol(pVariablesTreeItem->getDisplayUnit()));
if (!(pPlotWindow->getPlotType() == PlotWindow::PLOTPARAMETRIC || pPlotWindow->getPlotType() == PlotWindow::PLOTARRAYPARAMETRIC)) {
if (!(pPlotWindow->isPlotParametric() || pPlotWindow->isPlotArrayParametric())) {
break;
}
}
if (pPlotWindow->getPlotType() == PlotWindow::PLOTPARAMETRIC || pPlotWindow->getPlotType() == PlotWindow::PLOTARRAYPARAMETRIC) {
if (pPlotWindow->isPlotParametric() || pPlotWindow->isPlotArrayParametric()) {
const QString xVariableName = QString("%1.%2").arg(pPlotCurve->getFileName(), pPlotCurve->getXVariable());
if (xVariableName.compare(pVariablesTreeItem->getVariableName()) == 0) {
for (int i = 0 ; i < pPlotCurve->mXAxisVector.size() ; i++) {
Expand Down Expand Up @@ -2366,15 +2366,14 @@ void VariablesWidget::updatePlotWindows()
try {
if (MainWindow::instance()->getPlotWindowContainer()->isPlotWindow(pSubWindow->widget())) {
PlotWindow *pPlotWindow = qobject_cast<PlotWindow*>(pSubWindow->widget());
PlotWindow::PlotType plotType = pPlotWindow->getPlotType();
if (plotType == PlotWindow::PLOTARRAY || plotType == PlotWindow::PLOTARRAYPARAMETRIC) {
if (pPlotWindow->isPlotArray() || pPlotWindow->isPlotArrayParametric()) {
QList<PlotCurve*> curves = pPlotWindow->getPlot()->getPlotCurvesList();
if (curves.isEmpty()) {
if (!pPlotWindow->getFooter().isEmpty()) {
pPlotWindow->setTime(time);
pPlotWindow->updateTimeText();
}
} else if (plotType == PlotWindow::PLOTARRAY) {
} else if (pPlotWindow->isPlotArray()) {
foreach (PlotCurve* curve, curves) {
QString varName = curve->getYVariable();
pPlotWindow->setVariablesList(QStringList(varName));
Expand Down Expand Up @@ -2634,12 +2633,10 @@ void VariablesWidget::timeUnitChanged(QString unit)
if (!pPlotWindow) {
return;
}
if (pPlotWindow->getPlotType() == PlotWindow::PLOTARRAY ||
pPlotWindow->getPlotType() == PlotWindow::PLOTARRAYPARAMETRIC) {
if (pPlotWindow->isPlotArray() || pPlotWindow->isPlotArrayParametric()) {
pPlotWindow->setTimeUnit(unit);
pPlotWindow->updateTimeText();
} else if (pPlotWindow->getPlotType() == PlotWindow::PLOT ||
pPlotWindow->getPlotType() == PlotWindow::PLOTINTERACTIVE) {
} else if (pPlotWindow->isPlot() || pPlotWindow->isPlotInteractive()) {
OMCInterface::convertUnits_res convertUnit = MainWindow::instance()->getOMCProxy()->convertUnits(pPlotWindow->getTimeUnit(), unit);
pPlotWindow->setTimeUnit(unit);
if (convertUnit.unitsCompatible) {
Expand Down
1 change: 1 addition & 0 deletions OMPlot/OMPlot/OMPlotGUI/OMPlot.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ class Plot : public QwtPlot
void setFontSizes(double titleFontSize, double verticalAxisTitleFontSize, double verticalAxisNumbersFontSize, double horizontalAxisTitleFontSize,
double horizontalAxisNumbersFontSize, double footerFontSize, double legendFontSize);
static bool prefixableUnit(const QString &unit);
static void getUnitPrefixAndExponent(double lowerBound, double upperBound, QString &unitPrefix, int &exponent);
public slots:
virtual void replot();
};
Expand Down
82 changes: 75 additions & 7 deletions OMPlot/OMPlot/OMPlotGUI/Plot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ Plot::Plot(PlotWindow *pParent)
setAxisScaleEngine(QwtPlot::yLeft, pYLinearScaleEngine);
setAxisAutoScale(QwtPlot::yLeft);
// create the scale draw
mpXScaleDraw = new ScaleDraw(this);
mpXScaleDraw = new ScaleDraw(true, this);
setAxisScaleDraw(QwtPlot::xBottom, mpXScaleDraw);
mpYScaleDraw = new ScaleDraw(this);
mpYScaleDraw = new ScaleDraw(false, this);
setAxisScaleDraw(QwtPlot::yLeft, mpYScaleDraw);
// create an instance of zoomer
mpPlotZoomer = new PlotZoomer(QwtPlot::xBottom, QwtPlot::yLeft, canvas());
Expand Down Expand Up @@ -276,9 +276,76 @@ bool Plot::prefixableUnit(const QString &unit)
return prefixableUnits.contains(unit);
}

/*!
* \brief Plot::getUnitPrefixAndExponent
* \param lowerBound
* \param upperBound
* \param unitPrefix
* \param exponent
*/
void Plot::getUnitPrefixAndExponent(double lowerBound, double upperBound, QString &unitPrefix, int &exponent)
{
/* Since log(1900) returns 3.278 so we need to round down for positive values to make it 3
* And log(0.0011) return -2.95 so we also need to round down for negative value to make it -3
* log(0) is undefined so avoid it
*/

if (!(qFuzzyCompare(lowerBound, 0.0) && qFuzzyCompare(upperBound, 0.0))) {
if (fabs(lowerBound) > fabs(upperBound)) {
exponent = qFloor(std::log10(fabs(lowerBound)));
} else {
exponent = qFloor(std::log10(fabs(upperBound)));
}

// We don't do anything for exponent values between -1 and 2.
if ((exponent < -1) || (exponent > 2)) {
if (exponent > 2) {
if (exponent >= 3 && exponent < 6) {
unitPrefix = "k";
exponent = 3;
} else if (exponent >= 6 && exponent < 9) {
unitPrefix = "M";
exponent = 6;
} else if (exponent >= 9 && exponent < 12) {
unitPrefix = "G";
exponent = 9;
} else if (exponent >= 12 && exponent < 15) {
unitPrefix = "T";
exponent = 12;
} else {
unitPrefix = "P";
exponent = 15;
}
} else if (exponent < -1) {
if (exponent <= -2 && exponent > -6) {
unitPrefix = "m";
exponent = -3;
} else if (exponent <= -6 && exponent > -9) {
unitPrefix = QChar(0x03BC);
exponent = -6;
} else if (exponent <= -9 && exponent > -12) {
unitPrefix = "n";
exponent = -9;
} else if (exponent <= -12 && exponent > -15) {
unitPrefix = "p";
exponent = -12;
} else {
unitPrefix = "f";
exponent = -15;
}
}
} else {
unitPrefix = "";
exponent = 0;
}
}
}

// just overloaded this function to get colors for curves.
void Plot::replot()
{
mpXScaleDraw->invalidateCache();
mpYScaleDraw->invalidateCache();
QwtPlot::replot();

// Now we need to again loop through curves to set the color and title.
Expand All @@ -293,12 +360,13 @@ void Plot::replot()
}

if (mpParentPlotWindow->getXCustomLabel().isEmpty()) {
QString timeUnit = mpParentPlotWindow->getTimeUnit();
if (mpParentPlotWindow->getPlotType() == PlotWindow::PLOT
|| mpParentPlotWindow->getPlotType() == PlotWindow::PLOTALL
|| mpParentPlotWindow->getPlotType() == PlotWindow::PLOTINTERACTIVE) {
// ScaleDraw::getUnitPrefix is only set when time is on x-axis
const QString timeUnit = mpXScaleDraw->getUnitPrefix() + mpParentPlotWindow->getTimeUnit();
if (mpParentPlotWindow->isPlot()
|| mpParentPlotWindow->isPlotAll()
|| mpParentPlotWindow->isPlotInteractive()) {
setAxisTitle(QwtPlot::xBottom, QString("%1 (%2)").arg(mpParentPlotWindow->getXLabel(), timeUnit));
} else if (mpParentPlotWindow->getPlotType() == PlotWindow::PLOTARRAY) {
} else if (mpParentPlotWindow->isPlotArray()) {
setAxisTitle(QwtPlot::xBottom, mpParentPlotWindow->getXLabel());
} else {
setAxisTitle(QwtPlot::xBottom, "");
Expand Down

0 comments on commit 5fa03b4

Please sign in to comment.