Salome HOME
Copyright update 2022
[modules/gui.git] / src / Plot2d / Plot2d_Curve.cxx
1 // Copyright (C) 2007-2022  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 //  File   : Plot2d_Curve.cxx
23 //  Author : Vadim SANDLER, Open CASCADE S.A.S. (vadim.sandler@opencascade.com)
24
25 #include "Plot2d_Curve.h"
26 #include "Plot2d_PlotItems.h"
27 #include <qwt_plot_curve.h>
28
29 const int DEFAULT_LINE_WIDTH  =  0;     // (default) line width
30 const int DEFAULT_MARKER_SIZE =  9;     // default marker size
31
32 /*!
33   Constructor
34 */
35 Plot2d_Curve::Plot2d_Curve()
36 : Plot2d_Object(),
37   myColor( 0, 0, 0 ), 
38   myMarker( Plot2d::Circle ), 
39   myMarkerSize( 0 ), 
40   myLine( Plot2d::Solid ), 
41   myLineWidth( 0 )
42 {
43 }
44
45 /*!
46   Destructor
47 */
48 Plot2d_Curve::~Plot2d_Curve()
49 {
50 }
51
52 /*!
53   Copy constructor. Makes deep copy of data
54 */
55 Plot2d_Curve::Plot2d_Curve( const Plot2d_Curve& curve )
56 : Plot2d_Object( curve )
57 {
58   myColor      = curve.getColor();
59   myMarker     = curve.getMarker();
60   myMarkerSize = curve.getMarkerSize();
61   myLine       = curve.getLine();
62   myLineWidth  = curve.getLineWidth();
63 }
64
65 /*!
66   operator=. Makes deep copy of data
67 */
68 Plot2d_Curve& Plot2d_Curve::operator=( const Plot2d_Curve& curve )
69 {
70   Plot2d_Object::operator=(curve);
71   myColor      = curve.getColor();
72   myMarker     = curve.getMarker();
73   myMarkerSize = curve.getMarkerSize();
74   myLine       = curve.getLine();
75   myLineWidth  = curve.getLineWidth();
76   return *this;
77 }
78
79 /*!
80   Get typeid for the plot2d curve class
81 */
82 int Plot2d_Curve::rtti()
83 {
84   return QwtPlotItem::Rtti_PlotCurve;
85 }
86
87 /*!
88   Create plot object for the curve
89 */
90 QwtPlotItem* Plot2d_Curve::createPlotItem()
91 {
92   QwtPlotCurve* aCurve = new Plot2d_QwtPlotCurve( getVerTitle(), getYAxis() );
93   updatePlotItem( aCurve );
94   return aCurve;
95 }
96
97 /*!
98   Auto fill parameters of object by plot view
99 */
100 void Plot2d_Curve::autoFill( const QwtPlot* thePlot )
101 {
102   QwtSymbol::Style typeMarker;
103   QColor           color;
104   Qt::PenStyle     typeLine;
105   Plot2d::getNextMarker( rtti(), thePlot, typeMarker, color, typeLine );
106
107   setColor( color );
108   setLine( Plot2d::qwt2plotLine( typeLine ), DEFAULT_LINE_WIDTH );
109   setMarker( Plot2d::qwt2plotMarker( typeMarker ) );
110 }
111
112 /*!
113   Updates curve fields
114 */
115 void Plot2d_Curve::updatePlotItem( QwtPlotItem* theItem )
116 {
117   if ( theItem->rtti() != rtti() )
118     return;
119
120   Plot2d_QwtPlotCurve* aCurve = dynamic_cast<Plot2d_QwtPlotCurve*>( theItem );
121   if ( !aCurve )
122     return;
123
124   Plot2d_Object::updatePlotItem( theItem );
125
126   Qt::PenStyle     ps = Plot2d::plot2qwtLine( getLine() );
127   QwtSymbol::Style ms = Plot2d::plot2qwtMarker( getMarker() );
128   
129   QColor aColor = isSelected() ?  Plot2d_Object::selectionColor() : getColor();
130   int lineW = getLineWidth(); 
131   if ( isSelected() ) lineW += (lineW == 0 ? 3 : 2);
132
133   int markerS = isSelected() ? getMarkerSize() + 2 : getMarkerSize();
134
135   aCurve->setSelected(isSelected());
136
137   aCurve->setPen( QPen(aColor , lineW, ps ) );
138   aCurve->setSymbol( new QwtSymbol( ms, QBrush( aColor ), 
139                                     QPen( aColor ),
140                                     QSize( markerS , markerS ) ) );
141
142   aCurve->setLegendPen(QPen(getColor(), getLineWidth(), ps ));
143   aCurve->setLegendSymbol( new QwtSymbol( ms, QBrush( getColor() ),
144                                           QPen( getColor() ),
145                                           QSize( getMarkerSize() , getMarkerSize() )));
146   
147   double *x, *y, *min, *max;
148   long nb = getData( &x, &y );
149   if(nb > 0 && x && y) {
150     aCurve->setSamples( x, y, nb );
151     delete [] x;
152     delete [] y;
153     QList<int> idx;
154     getDeviationData(min, max, idx);
155     if(idx.size() > 0 && min && max) {
156       aCurve->setDeviationData(min,max,idx);
157       delete min;
158       delete max;
159     } else {
160       aCurve->clearDeviationData();
161     }
162   } else {
163     aCurve->setSamples( NULL, NULL, 0 );
164   }
165 }
166
167 /*!
168   Sets curve's color ( and resets AutoAssign flag )
169 */
170 void Plot2d_Curve::setColor( const QColor& color )
171 {
172   myColor = color;
173   setAutoAssign( false );
174 }
175
176 /*!
177   Gets curve's color
178 */
179 QColor Plot2d_Curve::getColor() const
180 {
181   return myColor;
182 }
183
184 /*!
185   Sets marker type and size ( and resets AutoAssign flag )
186 */
187 void Plot2d_Curve::setMarker( Plot2d::MarkerType marker, const int markerSize )
188 {
189   setMarker( marker );
190   setMarkerSize( markerSize );
191   setAutoAssign( false );
192 }
193
194 /*!
195   Sets marker type ( and resets AutoAssign flag )
196 */
197 void Plot2d_Curve::setMarker( Plot2d::MarkerType marker )
198 {
199   myMarker = marker;
200   setAutoAssign( false );
201 }
202
203 /* Sets Qwt marker type
204  */
205 void Plot2d_Curve::setMarkerStyle( QwtSymbol::Style style)
206 {
207   myMarkerStyle = style;
208 }
209
210 /*!
211   Gets marker type
212 */
213 Plot2d::MarkerType Plot2d_Curve::getMarker() const
214 {
215   return myMarker;
216 }
217
218
219 /* Gets Qwt marker type
220  */
221 QwtSymbol::Style Plot2d_Curve::getMarkerStyle() const
222 {
223   return myMarkerStyle;
224 }
225
226
227
228 /*!
229   Sets new marker size ( and resets AutoAssign flag )
230 */
231 void Plot2d_Curve::setMarkerSize( const int theSize )
232 {
233   myMarkerSize = theSize < 0 ? 0 : theSize;
234   setAutoAssign( false );
235 }
236
237 /*!
238   Gets marker size
239 */
240 int Plot2d_Curve::getMarkerSize() const
241 {
242   return myMarkerSize;
243 }
244
245 /*!
246   Sets line type and width ( and resets AutoAssign flag )
247   NOTE : A line width of 0 will produce a 1 pixel wide line using a fast algorithm for diagonals. 
248          A line width of 1 will also produce a 1 pixel wide line, but uses a slower more accurate 
249          algorithm for diagonals. 
250          For horizontal and vertical lines a line width of 0 is the same as a line width of 1.
251 */
252 void Plot2d_Curve::setLine( Plot2d::LineType line, const int lineWidth )
253 {
254   setLine( line );
255   setLineWidth( lineWidth );
256   setAutoAssign( false );
257 }
258
259 /*!
260   Sets line type ( and resets AutoAssign flag )
261 */
262 void Plot2d_Curve::setLine( Plot2d::LineType line )
263 {
264   myLine = line;
265   setAutoAssign( false );
266 }
267
268 /*!
269   Gets line type
270 */
271 Plot2d::LineType Plot2d_Curve::getLine() const
272 {
273   return myLine;
274 }
275
276 /*!
277   Sets line width ( and resets AutoAssign flag )
278 */
279 void Plot2d_Curve::setLineWidth( const int lineWidth )
280 {
281   myLineWidth = lineWidth < 0 ? 0 : lineWidth;
282   setAutoAssign( false );
283 }
284
285 /*!
286   Gets line width
287 */
288 int Plot2d_Curve::getLineWidth() const
289 {
290   return myLineWidth;
291 }
292 /*!
293   Sets deviation data on the curve.
294 */
295 void Plot2d_Curve::setDeviationData( const double* min, const double* max,const QList<int>& idx) {
296   for( int i = 0; i < idx.size(); i++ ) {
297     if(idx[i] < myPoints.size()) {
298       myPoints[idx[i]].setDeviation(min[i], max[i]);
299     }
300   }
301 }
302
303 /*!
304   Gets object's data
305 */
306 void Plot2d_Curve::getDeviationData( double*& theMin, double*& theMax, QList<int>& idx) const
307 {
308   int aNb = 0;
309   idx.clear();
310   for (int i = 0; i < nbPoints(); i++)
311     if(myPoints[i].hasDeviation())
312       aNb++;
313   if(aNb) {
314     double min, max;
315     theMin = new double[aNb];
316     theMax = new double[aNb];
317     for (int i = 0; i < nbPoints(); i++)
318       if(myPoints[i].hasDeviation()) {
319         myPoints[i].deviation(min,max);
320         theMin[i] = min;
321         theMax[i] = max;
322         idx.push_back(i);
323       }
324   }
325 }
326
327 /*!
328   Clear deviation data on the curve.
329 */
330 void Plot2d_Curve::clearDeviationData() {
331   for( int i=0; i < myPoints.size(); i++ )
332     myPoints[i].clearDeviation();
333 }
334
335 /*!
336   Gets object's minimal ordinate
337 */
338 double Plot2d_Curve::getMinY() const
339 {
340   double aMinY = 1e150;
341   pointList::const_iterator aIt;
342   double coeff = 0.0;
343   for (aIt = myPoints.begin(); aIt != myPoints.end(); ++aIt) {    
344     aMinY = qMin( aMinY, myScale * (*aIt).y );
345     if((*aIt).minDeviation(coeff))
346       aMinY = qMin( aMinY, coeff );
347   }
348   return aMinY;
349 }
350
351 /*!
352   Gets object's maximal ordinate
353 */
354 double Plot2d_Curve::getMaxY() const
355 {
356   double aMaxY = -1e150;
357   pointList::const_iterator aIt;
358   double coeff = 0.0;
359   for (aIt = myPoints.begin(); aIt != myPoints.end(); ++aIt) {
360     aMaxY = qMax( aMaxY, myScale * (*aIt).y);
361     if((*aIt).maxDeviation(coeff))
362       aMaxY = qMax( aMaxY, coeff);
363   }
364   return aMaxY;
365 }