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