From f3fd2114e3da7713621cb18bcdd46c40ccb94855 Mon Sep 17 00:00:00 2001 From: mgn Date: Wed, 29 Apr 2015 10:30:38 +0300 Subject: [PATCH] 0002790: External 20778 Colorization of plot2d --- src/Plot2d/Plot2d_Curve.cxx | 2 +- src/Plot2d/Plot2d_ViewFrame.cxx | 156 +++++++++++++++++++++++++++++++- src/Plot2d/Plot2d_ViewFrame.h | 25 +++++ 3 files changed, 178 insertions(+), 5 deletions(-) diff --git a/src/Plot2d/Plot2d_Curve.cxx b/src/Plot2d/Plot2d_Curve.cxx index 8c3eb8cd9..c1807dbe6 100755 --- a/src/Plot2d/Plot2d_Curve.cxx +++ b/src/Plot2d/Plot2d_Curve.cxx @@ -618,4 +618,4 @@ void Plot2d_Curve::buildSymbolsColorMap( const QWidget* theBackWidget, const int mySymbolsColorMap[ (*aIt).colorId ] = aColor; } } -} \ No newline at end of file +} diff --git a/src/Plot2d/Plot2d_ViewFrame.cxx b/src/Plot2d/Plot2d_ViewFrame.cxx index e21dc8a65..cd3d72ec0 100755 --- a/src/Plot2d/Plot2d_ViewFrame.cxx +++ b/src/Plot2d/Plot2d_ViewFrame.cxx @@ -179,7 +179,8 @@ Plot2d_ViewFrame::Plot2d_ViewFrame( QWidget* parent, const QString& title ) myXGridMaxMinor( 5 ), myYGridMaxMinor( 5 ), myY2GridMaxMinor( 5 ), myXMode( 0 ), myYMode( 0 ), mySecondY( false ), myTitleAutoUpdate( true ), myXTitleAutoUpdate( true ), myYTitleAutoUpdate( true ), - myTitleChangedByUser( false ), myXTitleChangedByUser( false ), myYTitleChangedByUser( false ) + myTitleChangedByUser( false ), myXTitleChangedByUser( false ), myYTitleChangedByUser( false ), + myIsTimeColorization( false ), myTimePosition( -1 ), myInactiveColor( Qt::gray ) { setObjectName( title ); /* Plot 2d View */ @@ -614,6 +615,9 @@ void Plot2d_ViewFrame::displayCurve( Plot2d_Curve* curve, bool update ) aPCurve->attach( myPlot ); //myPlot->setCurveYAxis(curveKey, curve->getYAxis()); + // Colorize the curves by current time + aPCurve->setTimeColorization( myIsTimeColorization, myTimePosition, myInactiveColor ); + myPlot->getCurves().insert( aPCurve, curve ); if ( curve->isAutoAssign() ) { QwtSymbol::Style typeMarker; @@ -720,9 +724,13 @@ void Plot2d_ViewFrame::updateCurve( Plot2d_Curve* curve, bool update ) { if ( !curve ) return; - if ( hasPlotCurve( curve ) ) { - QwtPlotCurve* aPCurve = getPlotCurve( curve ); - if ( !curve->isAutoAssign() ) { + + if ( hasPlotCurve( curve ) ) + { + QwtPlotCurve* aPCurve = getPlotCurve( curve ); + + if ( !curve->isAutoAssign() ) + { Qt::PenStyle ps = Plot2d::plot2qwtLine( curve->getLine() ); QwtSymbol::Style ms = Plot2d::plot2qwtMarker( curve->getMarker() ); aPCurve->setPen ( QPen( curve->getColor(), curve->getLineWidth(), ps ) ); @@ -733,7 +741,14 @@ void Plot2d_ViewFrame::updateCurve( Plot2d_Curve* curve, bool update ) aPCurve->setData( curve->horData(), curve->verData(), curve->nbPoints() ); Plot2d_PlotCurve* aPlot2dCurve = dynamic_cast< Plot2d_PlotCurve* >( aPCurve ); if ( aPlot2dCurve ) + { aPlot2dCurve->setNbMarkers( curve->getNbMarkers() ); + + // Colorize the curves by current time + aPlot2dCurve->setTimeColorization( myIsTimeColorization, + myTimePosition, + myInactiveColor ); + } } aPCurve->setTitle( curve->getVerTitle() ); aPCurve->setVisible( true ); @@ -1966,6 +1981,70 @@ void Plot2d_PlotCurve::setSymbolsColorMap( const colorMap& theMap ) mySymbolsColorMap = theMap; } +/*! + Set the current time position to colorize. +*/ +void Plot2d_PlotCurve::setTimeColorization( bool isTimeColorization, + const double theTimePosition, + const QColor& theColor ) +{ + myIsTimeColorization = isTimeColorization; + myTimePosition = myIsTimeColorization ? theTimePosition : -1; + myInactiveColor = myIsTimeColorization ? theColor : Qt::gray; +} + +/*! + Return true if the curve is colored by the current time, false otherwise. +*/ +bool Plot2d_PlotCurve::isTimeColorization() const +{ + if ( myIsTimeColorization && (myTimePosition != -1) && myInactiveColor.isValid() ) + return true; + return false; +} + +/*! + Return the time position. +*/ +double Plot2d_PlotCurve::getTimePosition() const +{ + return myTimePosition; +} + +/*! + Return the color is in inactive part on the plot. +*/ +QColor Plot2d_PlotCurve::getInactiveColor() const +{ + return myInactiveColor; +} + +/*! + Draw the line part (without symbols) of a curve interval. +*/ +void Plot2d_PlotCurve::drawCurve( QPainter *p, int style, + const QwtScaleMap &xMap, const QwtScaleMap &yMap, + int from, int to) const +{ + if ( isTimeColorization() ) + { + for ( int i = 1; i <= to; i++ ) + { + // Paint the inactive part of curve + if ( x(i) > getTimePosition() ) + { + p->setBrush( getInactiveColor() ); + p->setPen( getInactiveColor() ); + } + QwtPlotCurve::drawCurve( p, style, xMap, yMap, i-1, i ); + } + } + else + { + QwtPlotCurve::drawCurve( p, style, xMap, yMap, from, to ); + } +} + /*! Draws curve's markers */ @@ -1983,6 +2062,29 @@ void Plot2d_PlotCurve::drawSymbols( QPainter *p, const QwtSymbol &symbol, { QwtPlotCurve::drawSymbols( p, symbol, xMap, yMap, from, to ); } + else if ( isTimeColorization() ) + { + QRect aRect; + const QwtMetricsMap &aMetricsMap = QwtPainter::metricsMap(); + aRect.setSize( aMetricsMap.screenToLayout( symbol.size() ) ); + + for ( int i = from; i <= to; i++ ) + { + const int xi = xMap.transform( x(i) ); + const int yi = yMap.transform( y(i) ); + + // Paint the inactive part of curve + if ( x(i) > getTimePosition() ) + { + p->setBrush( getInactiveColor() ); + p->setPen( getInactiveColor() ); + } + + // Draw the symbol + aRect.moveCenter( QPoint( xi, yi ) ); + symbol.draw( p, aRect ); + } + } else { double aTail = 0.0; @@ -2851,3 +2953,49 @@ void Plot2d_ViewFrame::updateSymbols() } } } + +/*! + Set the given time position to colorize the curves. +*/ +void Plot2d_ViewFrame::setTimeColorization( bool isTimeColorization, + const double theTimeValue, + const QColor& theColor ) +{ + myIsTimeColorization = isTimeColorization; + myTimePosition = myIsTimeColorization ? theTimeValue : -1; + myInactiveColor = myIsTimeColorization ? theColor : Qt::gray; +} + +/*! + Slot: Update curves by given theTimeValue time. +*/ +void Plot2d_ViewFrame::onTimeColorizationUpdated( double theTimeValue ) +{ + if ( isTimeColorization() ) + { + setTimeValue( theTimeValue ); + for ( CurveDict::Iterator it = myPlot->getCurves().begin(); + it != myPlot->getCurves().end(); it++ ) + updateCurve( it.value(), false ); + + myPlot->replot(); + } +} + +/*! + Set the time position. +*/ +void Plot2d_ViewFrame::setTimeValue( const double theTimeValue ) +{ + myTimePosition = theTimeValue; +} + +/*! + Return true if the view widget colorizes curves by current time. +*/ +bool Plot2d_ViewFrame::isTimeColorization() +{ + if ( myIsTimeColorization && ( myTimePosition != -1 ) && myInactiveColor.isValid() ) + return true; + return false; +} diff --git a/src/Plot2d/Plot2d_ViewFrame.h b/src/Plot2d/Plot2d_ViewFrame.h index a276e2196..f17abb9c9 100755 --- a/src/Plot2d/Plot2d_ViewFrame.h +++ b/src/Plot2d/Plot2d_ViewFrame.h @@ -89,6 +89,11 @@ public: double& yMin, double& yMax, double& y2Min, double& y2Max); + void setTimeColorization( bool, const double theTimeValue = -1, + const QColor& theColor = Qt::gray ); + void setTimeValue( const double theTimeValue ); + bool isTimeColorization(); + /* view parameters */ void copyPreferences( Plot2d_ViewFrame* ); void setCurveType( int curveType, bool update = true ); @@ -174,6 +179,7 @@ public slots: void onPanDown(); void onZoomIn(); void onZoomOut(); + void onTimeColorizationUpdated( double ); protected: virtual void customEvent( QEvent* ); @@ -226,6 +232,10 @@ protected: static bool myPrefTitleChangedByUser; static bool myXPrefTitleChangedByUser; static bool myYPrefTitleChangedByUser; + + bool myIsTimeColorization; + double myTimePosition; + QColor myInactiveColor; }; class Plot2d_Plot2d : public QwtPlot @@ -300,8 +310,19 @@ public: void setSymbolsColorData( const int *cData, int size ); void setSymbolsColorMap( const colorMap& theMap ); + void setTimeColorization( bool isTimeColorization, + const double theTimeValue = -1, + const QColor& theColor = Qt::gray ); + bool isTimeColorization() const; + double getTimePosition() const; + QColor getInactiveColor() const; + protected: + virtual void drawCurve( QPainter *p, int style, + const QwtScaleMap &xMap, const QwtScaleMap &yMap, + int from, int to) const; + virtual void drawSymbols(QPainter *p, const QwtSymbol &, const QwtScaleMap &xMap, const QwtScaleMap &yMap, int from, int to) const; @@ -310,6 +331,10 @@ private: double myNbMarkers; QwtArray mySymbolsColorIds; colorMap mySymbolsColorMap; + + bool myIsTimeColorization; + double myTimePosition; + QColor myInactiveColor; }; #endif -- 2.39.2