]> SALOME platform Git repositories - modules/gui.git/blob - src/Plot2d/Plot2d_Curve.cxx
Salome HOME
Merge from V6_main_20120808 08Aug12
[modules/gui.git] / src / Plot2d / Plot2d_Curve.cxx
1 // Copyright (C) 2007-2012  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.
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( QwtSymbol( ms, QBrush( aColor ), 
139                                 QPen( aColor ), 
140                                 QSize( markerS , markerS ) ) );
141
142   aCurve->setLegendPen(QPen(getColor(), getLineWidth(), ps ));
143   aCurve->setLegendSymbol( 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->setData( 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   }
163 }
164
165 /*!
166   Sets curve's color ( and resets AutoAssign flag )
167 */
168 void Plot2d_Curve::setColor( const QColor& color )
169 {
170   myColor = color;
171   setAutoAssign( false );
172 }
173
174 /*!
175   Gets curve's color
176 */
177 QColor Plot2d_Curve::getColor() const
178 {
179   return myColor;
180 }
181
182 /*!
183   Sets marker type and size ( and resets AutoAssign flag )
184 */
185 void Plot2d_Curve::setMarker( Plot2d::MarkerType marker, const int markerSize )
186 {
187   setMarker( marker );
188   setMarkerSize( markerSize );
189   setAutoAssign( false );
190 }
191
192 /*!
193   Sets marker type ( and resets AutoAssign flag )
194 */
195 void Plot2d_Curve::setMarker( Plot2d::MarkerType marker )
196 {
197   myMarker = marker;
198   setAutoAssign( false );
199 }
200
201 /* Sets Qwt marker type
202  */
203 void Plot2d_Curve::setMarkerStyle( QwtSymbol::Style style)
204 {
205   myMarkerStyle = style;
206 }
207
208 /*!
209   Gets marker type
210 */
211 Plot2d::MarkerType Plot2d_Curve::getMarker() const
212 {
213   return myMarker;
214 }
215
216
217 /* Gets Qwt marker type
218  */
219 QwtSymbol::Style Plot2d_Curve::getMarkerStyle() const
220 {
221   return myMarkerStyle;
222 }
223
224
225
226 /*!
227   Sets new marker size ( and resets AutoAssign flag )
228 */
229 void Plot2d_Curve::setMarkerSize( const int theSize )
230 {
231   myMarkerSize = theSize < 0 ? 0 : theSize;
232   setAutoAssign( false );
233 }
234
235 /*!
236   Gets marker size
237 */
238 int Plot2d_Curve::getMarkerSize() const
239 {
240   return myMarkerSize;
241 }
242
243 /*!
244   Sets line type and width ( and resets AutoAssign flag )
245   NOTE : A line width of 0 will produce a 1 pixel wide line using a fast algorithm for diagonals. 
246          A line width of 1 will also produce a 1 pixel wide line, but uses a slower more accurate 
247          algorithm for diagonals. 
248          For horizontal and vertical lines a line width of 0 is the same as a line width of 1.
249 */
250 void Plot2d_Curve::setLine( Plot2d::LineType line, const int lineWidth )
251 {
252   setLine( line );
253   setLineWidth( lineWidth );
254   setAutoAssign( false );
255 }
256
257 /*!
258   Sets line type ( and resets AutoAssign flag )
259 */
260 void Plot2d_Curve::setLine( Plot2d::LineType line )
261 {
262   myLine = line;
263   setAutoAssign( false );
264 }
265
266 /*!
267   Gets line type
268 */
269 Plot2d::LineType Plot2d_Curve::getLine() const
270 {
271   return myLine;
272 }
273
274 /*!
275   Sets line width ( and resets AutoAssign flag )
276 */
277 void Plot2d_Curve::setLineWidth( const int lineWidth )
278 {
279   myLineWidth = lineWidth < 0 ? 0 : lineWidth;
280   setAutoAssign( false );
281 }
282
283 /*!
284   Gets line width
285 */
286 int Plot2d_Curve::getLineWidth() const
287 {
288   return myLineWidth;
289 }
290 /*!
291   Sets deviation data on the curve.
292 */
293 void Plot2d_Curve::setDeviationData( const double* min, const double* max,const QList<int>& idx) {
294   for( int i = 0; i < idx.size(); i++ ) {
295     if(idx[i] < myPoints.size()) {
296       myPoints[idx[i]].setDeviation(min[i], max[i]);
297     }
298   }
299 }
300
301 /*!
302   Gets object's data
303 */
304 void Plot2d_Curve::getDeviationData( double*& theMin, double*& theMax, QList<int>& idx) const
305 {
306   int aNb = 0;
307   idx.clear();
308   for (int i = 0; i < nbPoints(); i++)
309     if(myPoints[i].hasDeviation())
310       aNb++;
311   if(aNb) {
312     double min, max;
313     theMin = new double[aNb];
314     theMax = new double[aNb];
315     for (int i = 0; i < nbPoints(); i++)
316       if(myPoints[i].hasDeviation()) {
317         myPoints[i].deviation(min,max);
318         theMin[i] = min;
319         theMax[i] = max;
320         idx.push_back(i);
321       }
322   }
323 }
324
325 /*!
326   Clear deviation data on the curve.
327 */
328 void Plot2d_Curve::clearDeviationData() {
329   for( int i=0; i < myPoints.size(); i++ )
330     myPoints[i].clearDeviation();
331 }
332
333 /*!
334   Gets object's minimal ordinate
335 */
336 double Plot2d_Curve::getMinY() const
337 {
338   double aMinY = 1e150;
339   pointList::const_iterator aIt;
340   double coeff = 0.0;
341   for (aIt = myPoints.begin(); aIt != myPoints.end(); ++aIt) {    
342     aMinY = qMin( aMinY, myScale * (*aIt).y );
343     if((*aIt).minDeviation(coeff))
344       aMinY = qMin( aMinY, coeff );
345   }
346   return aMinY;
347 }
348
349 /*!
350   Gets object's maximal ordinate
351 */
352 double Plot2d_Curve::getMaxY() const
353 {
354   double aMaxY = -1e150;
355   pointList::const_iterator aIt;
356   double coeff = 0.0;
357   for (aIt = myPoints.begin(); aIt != myPoints.end(); ++aIt) {
358     aMaxY = qMax( aMaxY, myScale * (*aIt).y);
359     if((*aIt).maxDeviation(coeff))
360       aMaxY = qMax( aMaxY, coeff);
361   }
362   return aMaxY;
363 }