From 83ef6cc02887b3f73835819e259514d315e3ccd0 Mon Sep 17 00:00:00 2001 From: san Date: Wed, 21 Dec 2005 15:20:50 +0000 Subject: [PATCH] Rolling back protection against crash in logarithmic scale --- src/Plot2d/Plot2d_Curve.cxx | 30 ++++++++++++++++ src/Plot2d/Plot2d_Curve.h | 6 ++++ src/Plot2d/Plot2d_ViewFrame.cxx | 49 ++++++++++++++++++++++++--- src/Plot2d/Plot2d_ViewFrame.h | 6 ++++ src/Plot2d/resources/Plot2d_msg_en.po | 9 +++++ 5 files changed, 96 insertions(+), 4 deletions(-) diff --git a/src/Plot2d/Plot2d_Curve.cxx b/src/Plot2d/Plot2d_Curve.cxx index 325115aa2..c0a9abbb6 100755 --- a/src/Plot2d/Plot2d_Curve.cxx +++ b/src/Plot2d/Plot2d_Curve.cxx @@ -353,3 +353,33 @@ QwtPlot::Axis Plot2d_Curve::getYAxis() const { return myYAxis; } + +/*! + Gets curve's minimal abscissa +*/ +double Plot2d_Curve::getMinX() const +{ + QValueList::const_iterator aIt; + double aMinX = 1e150; + int aCurrent = 0; + for(aIt = myPoints.begin(); aIt != myPoints.end(); ++aIt) { + if ( (*aIt).x < aMinX ) + aMinX = (*aIt).x; + } + return aMinX; +} + +/*! + Gets curve's minimal ordinate +*/ +double Plot2d_Curve::getMinY() const +{ + QValueList::const_iterator aIt; + double aMinY = 1e150; + int aCurrent = 0; + for(aIt = myPoints.begin(); aIt != myPoints.end(); ++aIt) { + if ( (*aIt).y < aMinY ) + aMinY = (*aIt).y; + } + return aMinY; +} diff --git a/src/Plot2d/Plot2d_Curve.h b/src/Plot2d/Plot2d_Curve.h index e54009d5e..b17f7c327 100755 --- a/src/Plot2d/Plot2d_Curve.h +++ b/src/Plot2d/Plot2d_Curve.h @@ -82,6 +82,12 @@ public: void setYAxis(QwtPlot::Axis theYAxis); QwtPlot::Axis getYAxis() const; + // Protection against QwtCurve::drawLines() bug in Qwt 0.4.x: + // it crashes if switched to X/Y logarithmic mode, when one or more points have + // non-positive X/Y coordinate + double getMinX() const; + double getMinY() const; + protected: bool myAutoAssign; QString myHorTitle; diff --git a/src/Plot2d/Plot2d_ViewFrame.cxx b/src/Plot2d/Plot2d_ViewFrame.cxx index 4ef5df821..6d8546d35 100755 --- a/src/Plot2d/Plot2d_ViewFrame.cxx +++ b/src/Plot2d/Plot2d_ViewFrame.cxx @@ -1314,11 +1314,17 @@ void Plot2d_ViewFrame::setFont( const QFont& font, ObjectType type, bool update) */ void Plot2d_ViewFrame::setHorScaleMode( const int mode, bool update ) { + // san -- Protection against QwtCurve bug in Qwt 0.4.x: + // it crashes if switched to X/Y logarithmic mode, when one or more points have + // non-positive X/Y coordinate + if ( mode && !isXLogEnabled() ){ + SUIT_MessageBox::warn1(this, tr("WARNING"), tr("WRN_XLOG_NOT_ALLOWED"), tr("BUT_OK")); + return; + } + myXMode = mode; - if ( myXMode == 0 ) // linear - myPlot->changeAxisOptions( QwtPlot::xBottom, QwtAutoScale::Logarithmic, false ); - else // logarithmic - myPlot->changeAxisOptions( QwtPlot::xBottom, QwtAutoScale::Logarithmic, true ); + + myPlot->changeAxisOptions( QwtPlot::xBottom, QwtAutoScale::Logarithmic, myXMode != 0 ); if ( update ) fitAll(); @@ -1329,6 +1335,14 @@ void Plot2d_ViewFrame::setHorScaleMode( const int mode, bool update ) */ void Plot2d_ViewFrame::setVerScaleMode( const int mode, bool update ) { + // san -- Protection against QwtCurve bug in Qwt 0.4.x: + // it crashes if switched to X/Y logarithmic mode, when one or more points have + // non-positive X/Y coordinate + if ( mode && !isYLogEnabled() ){ + SUIT_MessageBox::warn1(this, tr("WARNING"), tr("WRN_YLOG_NOT_ALLOWED"), tr("BUT_OK")); + return; + } + myYMode = mode; if ( myYMode == 0 ) { // linear myPlot->changeAxisOptions( QwtPlot::yLeft, QwtAutoScale::Logarithmic, false ); @@ -1345,6 +1359,7 @@ void Plot2d_ViewFrame::setVerScaleMode( const int mode, bool update ) emit vpModeVerChanged(); } + /*! Return, scale mode for horizontal axis */ @@ -1557,6 +1572,32 @@ void Plot2d_ViewFrame::onViewGlobalPan() qApp->installEventFilter( this ); } +/*! + Precaution for logarithmic X scale +*/ +bool Plot2d_ViewFrame::isXLogEnabled() const +{ + bool allPositive = true; + QIntDictIterator it( myCurves ); + for ( ; allPositive && it.current(); ++it ) { + allPositive = ( it.current()->getMinX() > 0. ); + } + return allPositive; +} + +/*! + Precaution for logarithmic Y scale +*/ +bool Plot2d_ViewFrame::isYLogEnabled() const +{ + bool allPositive = true; + QIntDictIterator it( myCurves ); + for ( ; allPositive && it.current(); ++it ) { + allPositive = ( it.current()->getMinY() > 0. ); + } + return allPositive; +} + //================================================================================= // Plot2d_Plot2d implementation //================================================================================= diff --git a/src/Plot2d/Plot2d_ViewFrame.h b/src/Plot2d/Plot2d_ViewFrame.h index 3629ed1ca..ff39f51b9 100755 --- a/src/Plot2d/Plot2d_ViewFrame.h +++ b/src/Plot2d/Plot2d_ViewFrame.h @@ -110,6 +110,12 @@ public: bool isModeVerLinear(); bool isLegendShow() { return myShowLegend; }; + // Protection against QwtCurve::drawLines() bug in Qwt 0.4.x: + // it crashes if switched to X/Y logarithmic mode, when one or more points have + // non-positive X/Y coordinate + bool isXLogEnabled() const; + bool isYLogEnabled() const; + protected: int testOperation( const QMouseEvent& ); void readPreferences(); diff --git a/src/Plot2d/resources/Plot2d_msg_en.po b/src/Plot2d/resources/Plot2d_msg_en.po index fec033631..b9b3c9b81 100755 --- a/src/Plot2d/resources/Plot2d_msg_en.po +++ b/src/Plot2d/resources/Plot2d_msg_en.po @@ -141,6 +141,15 @@ msgstr "Error" msgid "BUT_OK" msgstr "Ok" +msgid "WARNING" +msgstr "Warning" + +msgid "WRN_XLOG_NOT_ALLOWED" +msgstr "Some points with non-positive abscissa values have been detected.\nLogarithmic scale for abscissa axis is not allowed." + +msgid "WRN_YLOG_NOT_ALLOWED" +msgstr "Some points with non-positive ordinate values have been detected.\nLogarithmic scale for ordinate axis is not allowed." + # ------------------------------------ # Plot2d_SetupViewDlg # ------------------------------------ -- 2.39.2