]> SALOME platform Git repositories - modules/gui.git/blob - src/Plot2d/Plot2d.cxx
Salome HOME
2be404b088590d188a38641816cfdb009dda42a8
[modules/gui.git] / src / Plot2d / Plot2d.cxx
1 //  Copyright (C) 2007-2008  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.cxx
23 // Author : Vadim SANDLER, Open CASCADE S.A.S. (vadim.sandler@opencascade.com)
24 //
25 #include "Plot2d.h"
26
27 #include <QPainter>
28
29 /*!
30   \brief Convert Plot2d marker type to Qwt marker type.
31   \param m Plot2d marker type
32   \return Qwt marker type
33 */
34 QwtSymbol::Style Plot2d::plot2qwtMarker( Plot2d::MarkerType m )
35 {
36   QwtSymbol::Style ms = QwtSymbol::NoSymbol;  
37   switch ( m ) {
38   case Plot2d::Circle:
39     ms = QwtSymbol::Ellipse;   break;
40   case Plot2d::Rectangle:
41     ms = QwtSymbol::Rect;      break;
42   case Plot2d::Diamond:
43     ms = QwtSymbol::Diamond;   break;
44   case Plot2d::DTriangle:
45     ms = QwtSymbol::DTriangle; break;
46   case Plot2d::UTriangle:
47     ms = QwtSymbol::UTriangle; break;
48   case Plot2d::LTriangle:
49     ms = QwtSymbol::LTriangle; break;
50   case Plot2d::RTriangle:
51     ms = QwtSymbol::RTriangle; break;
52   case Plot2d::Cross:
53     ms = QwtSymbol::Cross;     break;
54   case Plot2d::XCross:
55     ms = QwtSymbol::XCross;    break;
56   case Plot2d::None:
57   default:
58     ms = QwtSymbol::NoSymbol;  break;
59   }
60   return ms;
61 }
62
63 /*!
64   \brief Convert Qwt marker type to Plot2d marker type.
65   \param m Qwt marker type
66   \return Plot2d marker type
67 */
68 Plot2d::MarkerType Plot2d::qwt2plotMarker( QwtSymbol::Style m )
69 {
70   Plot2d::MarkerType ms = Plot2d::None;  
71   switch ( m ) {
72   case QwtSymbol::Ellipse:
73     ms = Plot2d::Circle;    break;
74   case QwtSymbol::Rect:
75     ms = Plot2d::Rectangle; break;
76   case QwtSymbol::Diamond:
77     ms = Plot2d::Diamond;   break;
78   case QwtSymbol::DTriangle:
79     ms = Plot2d::DTriangle; break;
80   case QwtSymbol::UTriangle:
81     ms = Plot2d::UTriangle; break;
82   case QwtSymbol::RTriangle:
83     ms = Plot2d::RTriangle; break;
84   case QwtSymbol::LTriangle:
85     ms = Plot2d::LTriangle; break;
86   case QwtSymbol::Cross:
87     ms = Plot2d::Cross;     break;
88   case QwtSymbol::XCross:
89     ms = Plot2d::XCross;    break;
90   case QwtSymbol::NoSymbol:
91   default:
92     ms = Plot2d::None;      break;
93   }
94   return ms;
95 }
96
97 /*!
98   \brief Convert Plot2d line type to Qt/Qwt line type.
99   \param p Plot2d line type
100   \return Qt/Qwt line type
101 */
102 Qt::PenStyle Plot2d::plot2qwtLine( Plot2d::LineType p )
103 {
104   Qt::PenStyle ps = Qt::NoPen;
105   switch ( p ) {
106   case Plot2d::Solid:
107     ps = Qt::SolidLine;      break;
108   case Plot2d::Dash:
109     ps = Qt::DashLine;       break;
110   case Plot2d::Dot:
111     ps = Qt::DotLine;        break;
112   case Plot2d::DashDot:
113     ps = Qt::DashDotLine;    break;
114   case Plot2d::DashDotDot:
115     ps = Qt::DashDotDotLine; break;
116   case Plot2d::NoPen:
117   default:
118     ps = Qt::NoPen;          break;
119   }
120   return ps;
121 }
122
123 /*!
124   \brief Convert Qt/Qwt line type to Plot2d line type.
125   \param p Qt/Qwt line type
126   \return Plot2d line type
127 */
128 Plot2d::LineType Plot2d::qwt2plotLine( Qt::PenStyle p )
129 {
130   Plot2d::LineType ps = Plot2d::NoPen;
131   switch ( p ) {
132   case Qt::SolidLine:
133     ps = Plot2d::Solid;      break;
134   case Qt::DashLine:
135     ps = Plot2d::Dash;       break;
136   case Qt::DotLine:
137     ps = Plot2d::Dot;        break;
138   case Qt::DashDotLine:
139     ps = Plot2d::DashDot;    break;
140   case Qt::DashDotDotLine:
141     ps = Plot2d::DashDotDot; break;
142   case Qt::NoPen:
143   default:
144     ps = Plot2d::NoPen;      break;
145   }
146   return ps;
147 }
148
149 /*!
150   \brief Draw line.
151   \param painter painter
152   \param p1 starting point
153   \param p2 ending point
154   \param type line type
155   \param color line color
156   \param width line width
157 */
158 void Plot2d::drawLine( QPainter* painter, const QPoint& p1, const QPoint& p2, 
159                        Qt::PenStyle type, const QColor& color, int width )
160 {
161   painter->save();
162   QPen pen( type );
163   pen.setColor( color );
164   pen.setWidth( width );
165   painter->setPen( pen );
166   painter->drawLine( p1, p2 );
167   painter->restore();
168 }
169
170 /*!
171   \brief Draw line.
172   \param painter painter
173   \param p1 starting point
174   \param p2 ending point
175   \param type line type
176   \param color line color
177   \param width line width
178 */
179 void Plot2d::drawLine( QPainter* painter, const QPoint& p1, const QPoint& p2, 
180                        Plot2d::LineType type, const QColor& color, int width )
181 {
182   drawLine( painter, p1, p2, plot2qwtLine( type ), color, width );
183 }
184
185 /*!
186   \brief Draw line.
187   \param painter painter
188   \param x1 X coordinate of the starting point
189   \param y1 Y coordinate of the starting point
190   \param x2 X coordinate of the ending point
191   \param y2 Y coordinate of the ending point
192   \param type line type
193   \param color line color
194   \param width line width
195 */
196 void Plot2d::drawLine( QPainter* painter, int x1, int y1, int x2, int y2,
197                        Qt::PenStyle type, const QColor& color, int width )
198 {
199   drawLine( painter, QPoint( x1, y1 ), QPoint( x2, y2 ), type, color, width );
200 }
201
202 /*!
203   \brief Draw line.
204   \param painter painter
205   \param x1 X coordinate of the starting point
206   \param y1 Y coordinate of the starting point
207   \param x2 X coordinate of the ending point
208   \param y2 Y coordinate of the ending point
209   \param type line type
210   \param color line color
211   \param width line width
212 */
213 void Plot2d::drawLine( QPainter* painter, int x1, int y1, int x2, int y2,
214                        Plot2d::LineType type, const QColor& color, int width )
215 {
216   drawLine( painter, QPoint( x1, y1 ), QPoint( x2, y2 ), 
217             plot2qwtLine( type), color, width );
218 }
219
220 /*!
221   \brief Draw marker.
222   \param painter painter
223   \param p central point
224   \param r marker rectangle
225   \param type marker type
226   \param color marker color
227 */
228 void Plot2d::drawMarker( QPainter* painter, const QPoint& p, const QRect& r,
229                          QwtSymbol::Style type, const QColor& color )
230 {
231   painter->save();
232   painter->setPen( color );
233   painter->setBrush( color );
234   
235   QRect ar = r;
236   ar.moveCenter( p );
237   const int w2 = ar.width()  / 2;
238   const int h2 = ar.height() / 2;
239
240   switch( type ) {
241   case QwtSymbol::Ellipse:
242     painter->drawEllipse( ar );
243     break;
244   case QwtSymbol::Rect:
245     painter->drawRect( ar );
246     painter->fillRect( ar, QBrush( color ) );
247     break;
248   case QwtSymbol::Diamond:
249     {
250       QPolygon polygon;
251       polygon << QPoint( ar.x() + w2, ar.y() );
252       polygon << QPoint( ar.right(), ar.y() + h2 );
253       polygon << QPoint( ar.x() + w2, ar.bottom() );
254       polygon << QPoint( ar.x(), ar.y() + h2 );
255       painter->drawPolygon( polygon );
256       break;
257     }
258   case QwtSymbol::Cross:
259     painter->drawLine( ar.left() + w2, ar.top(), ar.left() + w2, ar.bottom() );
260     painter->drawLine( ar.left(), ar.top() + h2, ar.right(), ar.top() + h2 );
261     break;
262   case QwtSymbol::XCross:
263     painter->drawLine( ar.left(), ar.top(), ar.right(), ar.bottom() );
264     painter->drawLine( ar.left(), ar.bottom(), ar.right(), ar.top() );
265     break;
266   case QwtSymbol::UTriangle:
267     {
268       QPolygon polygon;
269       polygon << QPoint( ar.left() + w2, ar.top() );
270       polygon << QPoint( ar.right(), ar.bottom() );
271       polygon << QPoint( ar.left(), ar.bottom() );
272       painter->drawPolygon( polygon );
273       break;
274     }
275   case QwtSymbol::DTriangle:
276     {
277       QPolygon polygon;
278       polygon << QPoint( ar.left() + w2, ar.bottom() );
279       polygon << QPoint( ar.right(), ar.top() );
280       polygon << QPoint( ar.left(), ar.top() );
281       painter->drawPolygon( polygon );
282       break;
283     }
284   case QwtSymbol::RTriangle:
285     {
286       QPolygon polygon;
287       polygon << QPoint( ar.left(), ar.top() );
288       polygon << QPoint( ar.right(), ar.top() + h2 );
289       polygon << QPoint( ar.left(), ar.bottom() );
290       painter->drawPolygon( polygon );
291       break;
292     }
293   case QwtSymbol::LTriangle:
294     {
295       QPolygon polygon;
296       polygon << QPoint( ar.left(), ar.top() + h2 );
297       polygon << QPoint( ar.right(), ar.top() );
298       polygon << QPoint( ar.right(), ar.bottom() );
299       painter->drawPolygon( polygon );
300       break;
301     }
302   default:
303     break;
304   }
305   painter->restore();
306 }
307
308 /*!
309   \brief Draw marker.
310   \param painter painter
311   \param p central point
312   \param r marker rectangle
313   \param type marker type
314   \param color marker color
315 */
316 void Plot2d::drawMarker( QPainter* painter, const QPoint& p, const QRect& r,
317                          Plot2d::MarkerType type, const QColor& color )
318 {
319   drawMarker( painter, p, r, plot2qwtMarker( type ), color ); 
320 }
321
322 /*!
323   \brief Draw marker.
324   \param painter painter
325   \param x X coordinate of the central point
326   \param y Y coordinate of the central point
327   \param w marker rectangle width
328   \param h marker rectangle height
329   \param type marker type
330   \param color marker color
331 */
332 void Plot2d::drawMarker( QPainter* painter, int x, int y, int w, int h,
333                          QwtSymbol::Style type, const QColor& color )
334 {
335   drawMarker( painter, QPoint( x, y ), QRect( 0, 0, w, h ), type, color ); 
336 }
337
338 /*!
339   \brief Draw marker.
340   \param painter painter
341   \param x X coordinate of the central point
342   \param y Y coordinate of the central point
343   \param w marker rectangle width
344   \param h marker rectangle height
345   \param type marker type
346   \param color marker color
347 */
348 void Plot2d::drawMarker( QPainter* painter, int x, int y, int w, int h,
349                          Plot2d::MarkerType type, const QColor& color )
350 {
351   drawMarker( painter, QPoint( x, y ), QRect( 0, 0, w, h ), plot2qwtMarker( type ), color ); 
352 }