Back to index

salome-gui  6.5.0
Plot2d_Histogram.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 //  File   : Plot2d_Histogram.cxx
00023 //  Author : Natalia ERMOLAEVA, Open CASCADE S.A.S. (natalia.donis@opencascade.com)
00024 
00025 #include "Plot2d_Histogram.h"
00026 #include "Plot2d_PlotItems.h"
00027 
00028 #include <qwt_plot_curve.h>
00029 
00030 const int MAX_ATTEMPTS = 10;     // max attempts
00031 
00035 Plot2d_Histogram::Plot2d_Histogram()
00036 : Plot2d_Object(),
00037   myColor( 0, 0, 0 ),
00038   myWidth( 0 ),
00039   myDefWidth( 0 )
00040 {
00041 }
00042 
00046 Plot2d_Histogram::~Plot2d_Histogram()
00047 {
00048 }
00049 
00053 Plot2d_Histogram::Plot2d_Histogram( const Plot2d_Histogram& hist )
00054 : Plot2d_Object( hist )
00055 {
00056   myColor      = hist.myColor;
00057   myWidth      = hist.myWidth;
00058   myDefWidth   = hist.myDefWidth;
00059 }
00060 
00064 Plot2d_Histogram& Plot2d_Histogram::operator=( const Plot2d_Histogram& hist )
00065 {
00066   Plot2d_Object::operator=(hist);
00067   myColor      = hist.myColor;
00068   myWidth      = hist.myWidth;
00069   myDefWidth   = hist.myDefWidth;
00070   return *this;
00071 }
00072 
00076 int Plot2d_Histogram::rtti()
00077 {
00078   return QwtPlotItem::Rtti_PlotHistogram;
00079 }
00080 
00084 QwtPlotItem* Plot2d_Histogram::createPlotItem()
00085 {
00086   Plot2d_HistogramItem* anItem = new Plot2d_HistogramItem();
00087   updatePlotItem( anItem );
00088   return anItem;
00089 }
00090 
00094 void Plot2d_Histogram::autoFill( const QwtPlot* thePlot )
00095 {
00096   setColor( getNextColor( thePlot ) );
00097 }
00098 
00102 void Plot2d_Histogram::updatePlotItem( QwtPlotItem* theItem )
00103 {
00104   if ( theItem->rtti() != rtti() )
00105     return;
00106 
00107   Plot2d_HistogramItem* anItem = dynamic_cast<Plot2d_HistogramItem*>( theItem );
00108   if ( !anItem )
00109     return;
00110 
00111   Plot2d_Object::updatePlotItem( theItem );
00112 
00113   anItem->setData( getData() );
00114   anItem->setLegendPen(getColor());
00115   anItem->setSelected(isSelected());
00116   anItem->setColor( isSelected() ? Plot2d_Object::selectionColor() : getColor() );
00117 }
00118 
00122 void Plot2d_Histogram::setData( const QList<double>& theXVals,
00123                             const QList<double>& theYVals )
00124 {
00125   pointList aPoints;
00126   int aSize = theXVals.size();
00127   for ( int i = 0; i < aSize; i++ )
00128     aPoints.append( Plot2d_Point( theXVals[i], theYVals[i] ) );
00129   setPointList( aPoints );
00130 
00131   myDefWidth = getMinInterval( theXVals )*(2./3.);
00132   myWidth = 0; // myDefWidth // VSR: width should not be automatically reset to myDefWidth
00133 }
00134 
00138 QwtIntervalData Plot2d_Histogram::getData() const
00139 {
00140   pointList aPoints = getPointList();
00141   int aSize = aPoints.size();
00142 
00143   QwtArray<QwtDoubleInterval> anIntervals( aSize );
00144   QwtArray<double> aValues( aSize );
00145   double aX;
00146   double aWidth = myWidth <= 0 ? myDefWidth : myWidth; // VSR: width is either manually assigned or auto-calculated
00147   for ( int i = 0; i < aSize; i++ ) {
00148     aX = aPoints[i].x;
00149     anIntervals[i] = QwtDoubleInterval( aX - aWidth/2, aX + aWidth/2 );
00150     aValues[i] = aPoints[i].y;
00151   }
00152 
00153   return QwtIntervalData( anIntervals, aValues );
00154 }
00155 
00159 void Plot2d_Histogram::setColor( const QColor& theColor )
00160 {
00161   myColor = theColor;
00162   setAutoAssign( false );
00163 }
00164 
00168 QColor Plot2d_Histogram::getColor() const
00169 {
00170   return myColor;
00171 }
00172 
00176 void Plot2d_Histogram::setWidth( const double theWidth )
00177 {
00178   myWidth = theWidth;
00179   //setAutoAssign( false ); // VSR: width attribute is not auto-assigned
00180 }
00181 
00185 double Plot2d_Histogram::getWidth( const bool isDef ) const
00186 {
00187   return isDef ? myDefWidth : myWidth;
00188 }
00189 
00193 QColor Plot2d_Histogram::getNextColor( const QwtPlot* thePlot )
00194 {
00195   bool bOk = false;
00196   int cnt = 0;
00197   QColor aColor;
00198   while ( !bOk ) {
00199     int aRed    = (int)( 256.0 * rand() / RAND_MAX);  // generate random color
00200     int aGreen  = (int)( 256.0 * rand() / RAND_MAX);  // ...
00201     int aBlue   = (int)( 256.0 * rand() / RAND_MAX);  // ...
00202     aColor      = QColor( aRed, aGreen, aBlue );
00203     bOk = ( ++cnt == MAX_ATTEMPTS ) || !existColor( thePlot, aColor );
00204   }
00205   return aColor;
00206 }
00207 
00211 bool Plot2d_Histogram::existColor( const QwtPlot* thePlot, const QColor& theColor )
00212 {
00213   bool ok = false;
00214   
00215   QColor bgColor = thePlot->palette().color( QPalette::Background );
00216   if ( Plot2d::closeColors( theColor, bgColor ) ) {
00217     ok = true;
00218   }
00219   else {
00220     QwtPlotItemList anItems = thePlot->itemList();
00221     QwtPlotItemIterator anIt = anItems.begin(), aLast = anItems.end();
00222     QwtPlotItem* anItem;
00223     for( ; anIt != aLast && !ok; anIt++ ) {
00224       anItem = *anIt;
00225       if ( !anItem )
00226        continue;
00227       if ( anItem->rtti() == rtti() ) {
00228        Plot2d_HistogramItem* aHItem = dynamic_cast<Plot2d_HistogramItem*>( anItem );
00229        ok = aHItem && Plot2d::closeColors( theColor, aHItem->color() );
00230       }
00231       else if ( anItem->rtti() == QwtPlotItem::Rtti_PlotCurve ) {
00232        QwtPlotCurve* aCurve = dynamic_cast<QwtPlotCurve*>( anItem );
00233        ok = aCurve && Plot2d::closeColors( theColor, aCurve->pen().color() );
00234       }
00235     }
00236   }
00237   return ok;
00238 }
00239 
00243 double Plot2d_Histogram::getMinInterval( const QList<double>& theVals )
00244 {
00245   double aValue = -1;
00246   int aSize = theVals.size();
00247   if ( aSize > 1 ) {
00248     aValue = qAbs( theVals[1] - theVals[0] );
00249     double aDelta;
00250     for ( int i = 2; i < aSize; i++ ) {
00251       aDelta = qAbs( theVals[i] - theVals[i-1] );
00252       aValue = qMin( aValue, qMax( aDelta, 0. ) );
00253     }
00254     aValue = aValue/2;
00255   }
00256   return aValue;
00257 }
00258