Salome HOME
Join modifications from BR_Dev_For_4_0 tag V4_1_1.
[modules/gui.git] / src / QxGraph / QxGraph_Prs.cxx
1 //  SALOME QxGraph : build Supervisor viewer into desktop
2 //
3 //  Copyright (C) 2003  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
23 #include "QxGraph_Prs.h"
24
25 #include "QxGraph_Canvas.h"
26 #include "QxGraph_Def.h" // for debug only
27
28 #include "SUIT_Session.h" // for debug only
29
30 /*!
31   Constructor
32 */
33 QxGraph_Prs::QxGraph_Prs(QxGraph_Canvas* theCanvas):
34   myCanvas(theCanvas),
35   myDMode(0),
36   needUpdate(true)
37 {
38   myCanvas->addPrs(this);
39 }
40
41 /*!
42   Destructor
43 */
44 QxGraph_Prs::~QxGraph_Prs()
45 {
46   for ( DMode2ItemList::iterator it1 = myDisplayMap.begin();
47         it1 != myDisplayMap.end();
48         it1++ )
49   {
50     for ( std::list<QCanvasItem*>::iterator it2 = (*it1).second.begin();
51           it2 != (*it1).second.end();
52           it2++ )
53     {
54       QCanvasItem* anItem = *it2;
55       if ( anItem ) delete anItem;
56     }
57   }
58       
59   myDisplayMap.clear();
60 }
61
62 /*!
63   Add item to display in the view with index theDMode
64 */
65 void QxGraph_Prs::addItem(QCanvasItem* theItem, int theDMode)
66 {
67   if ( theDMode == -1 ) // add item for the current display mode
68     myDisplayMap[myDMode].push_back(theItem);
69   else
70     myDisplayMap[theDMode].push_back(theItem);
71 }
72
73 /*!
74   Remove item from the view with index theDMode
75 */
76 void QxGraph_Prs::removeItem(QCanvasItem* theItem, int theDMode)
77 {
78   if ( theDMode == -1 ) // remove item from the current display mode
79     myDisplayMap[myDMode].remove(theItem);
80   else
81     myDisplayMap[theDMode].remove(theItem);
82 }
83
84 /*! Adds all the items of this presentation for the current display mode
85  *  to the canvas.
86  */
87 void QxGraph_Prs::show()
88 {
89   if ( isToUpdate() ) 
90     update();
91
92   for ( std::list<QCanvasItem*>::iterator it = myDisplayMap[myDMode].begin();
93         it != myDisplayMap[myDMode].end();
94         it++ )
95   {
96     QCanvasItem* anItem = *it;
97     if ( anItem )
98     {
99       anItem->setCanvas( myCanvas );
100       anItem->show();
101     }
102   }
103 }
104
105 /*! Removes all the items belonging to this presentation from the canvas.
106  */
107 void QxGraph_Prs::hide()
108 {
109   for ( DMode2ItemList::iterator it1 = myDisplayMap.begin();
110         it1 != myDisplayMap.end();
111         it1++ )
112   {
113     for ( std::list<QCanvasItem*>::iterator it2 = (*it1).second.begin();
114           it2 != (*it1).second.end();
115           it2++ )
116     {
117       QCanvasItem* anItem = *it2;
118       if ( anItem )
119       {
120         anItem->setCanvas( 0 );
121       }
122     }
123   }
124 }
125
126 /*! Prepare for full recomputation of the presentation
127  */
128 void QxGraph_Prs::setToUpdate( const bool theFlag )
129 {
130   needUpdate = theFlag;
131 }
132
133 /*! Re-fills the presentation with items.
134  *  Base implementation just resets <needUpdate> flag.
135  *  It should be called at the end by re-implementations.
136  */
137 void QxGraph_Prs::update()
138 {
139   setToUpdate( false );
140 }
141
142 /*!
143   Add a QCanvasRectangle item for display mode DMode
144 */
145 QCanvasItem* QxGraph_Prs::addRectangleItem(QRect theRect, int theDMode)
146 {
147   QCanvasRectangle* aRectItem;
148   if ( myCanvas )
149   {
150     QCanvasRectangle* aRectItem = new QCanvasRectangle(theRect, myCanvas);
151     aRectItem->setZ(0);
152     aRectItem->show();
153     myCanvas->update();
154     
155     // test drawing features: brush, pen ...
156     QBrush aBr(SUIT_Session::session()->resourceMgr()->colorValue( "QxGraph", "NodeBody", RECTANGLE_BODY ));
157     aRectItem->setBrush(aBr);
158   }
159   addItem(aRectItem);
160   return aRectItem;
161 }
162
163 /*!
164   Add a QCanvasPolygon item for display mode DMode
165 */
166 QCanvasItem* QxGraph_Prs::addPolygonItem(QPointArray thePA, int theDMode)
167 {
168   QCanvasPolygon* aPolyItem;
169   if ( myCanvas )
170   {
171     aPolyItem = new QCanvasPolygon(myCanvas);
172     aPolyItem->setZ(0);
173     aPolyItem->setPoints(thePA);
174     aPolyItem->show();
175     myCanvas->update();
176     
177     // test drawing features: brush, pen ...
178     QBrush aBr(SUIT_Session::session()->resourceMgr()->colorValue( "QxGraph", "NodeBody", RECTANGLE_BODY ));
179     aPolyItem->setBrush(aBr);
180     QPen aPen(Qt::black,2);
181     aPolyItem->setPen(aPen);
182   }
183   addItem(aPolyItem);
184   return aPolyItem;
185 }
186
187 /*!
188   Add a QCanvasLine item for display mode DMode
189 */
190 QCanvasItem* QxGraph_Prs::addLineItem(QPoint theStart, QPoint theEnd, int theDMode)
191 {
192   QCanvasLine* aLineItem;
193   if ( myCanvas )
194   {
195     aLineItem = new QCanvasLine(myCanvas);
196     aLineItem->setZ(0);
197     aLineItem->setPoints(theStart.x(), theStart.y(), theEnd.x(), theEnd.y());
198     aLineItem->show();
199     myCanvas->update();
200   
201     // test drawing features: brush, pen ...
202     QPen aPen(Qt::black,2);
203     aLineItem->setPen(aPen);
204   }
205   addItem(aLineItem);
206   return aLineItem;
207 }
208
209 /*!
210   Add a QCanvasEllipse item for display mode DMode
211 */
212 QCanvasItem* QxGraph_Prs::addEllipseItem(int theW, int theH, int theStartAngle, int theAngle, int theDMode)
213 {
214   QCanvasEllipse* aEllipseItem;
215   if ( myCanvas )
216   {
217     aEllipseItem = new QCanvasEllipse(theW, theH, theStartAngle, theAngle, myCanvas);
218     aEllipseItem->setZ(0);
219     aEllipseItem->show();
220     myCanvas->update();
221     
222     // test drawing features: brush, pen ...
223     QBrush aBr(SUIT_Session::session()->resourceMgr()->colorValue( "QxGraph", "NodeBody", RECTANGLE_BODY ));
224     aEllipseItem->setBrush(aBr);
225     QPen aPen(Qt::black,2);
226     aEllipseItem->setPen(aPen);
227   }
228   addItem(aEllipseItem);
229   return aEllipseItem;
230 }
231
232 /*!
233   Add a QCanvasText item for display mode DMode
234 */
235 QCanvasItem* QxGraph_Prs::addTextItem(QString theText, int theDMode)
236 {
237   QCanvasText* aTextItem;
238   if ( myCanvas )
239   {
240     aTextItem = new QCanvasText(theText, myCanvas);
241     aTextItem->setZ(0);
242     aTextItem->show();
243     myCanvas->update();
244     
245     // test drawing features: font, color, text flags ...
246     aTextItem->setColor(Qt::darkBlue);
247     //aTextItem->setFont(QFont("Times"/*"Helvetica"*/, 14, QFont::Normal, true));
248   }
249   addItem(aTextItem);
250   return aTextItem;
251 }