Salome HOME
Join modifications from BR_Dev_For_4_0 tag V4_1_1.
[modules/gui.git] / src / SPlot2d / SPlot2d_ViewModel.cxx
1 // Copyright (C) 2005  OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
2 // 
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either 
6 // version 2.1 of the License.
7 // 
8 // This library is distributed in the hope that it will be useful 
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of 
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public  
14 // License along with this library; if not, write to the Free Software 
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 //  File   : SPlot2d_Viewer.cxx
20 //  Author : Sergey RUIN
21 //  Module : SUIT
22
23 #include "SPlot2d_ViewModel.h"
24
25 #include "SPlot2d_ViewWindow.h"
26
27 #include "SPlot2d_Prs.h"
28 #include "SUIT_Session.h"
29 #include "SUIT_Application.h"
30
31 //#include "utilities.h"
32 #include "qapplication.h"
33 #include <qtoolbar.h>
34 #include <qtoolbutton.h>
35 #include <qcursor.h>
36 #include <qcolordialog.h>
37 #include <qwt_math.h>
38 #include <qwt_plot_canvas.h>
39 #include <stdlib.h>
40
41 using namespace std;
42
43 //ASL: Temporary commented in order to avoir dependency on SALOMEDS
44
45 //#include "SALOMEDSClient.hxx"
46 //#include "SALOMEDS_StudyManager.hxx"
47
48 // in order NOT TO link with SalomeApp, here the code returns SALOMEDS_Study.
49 // SalomeApp_Study::studyDS() does it as well, but -- here it is retrieved from
50 // SALOMEDS::StudyManager - no linkage with SalomeApp.  
51
52 /*static _PTR(Study) getStudyDS()
53 {
54   SALOMEDSClient_Study* aStudy = NULL;
55   _PTR(StudyManager) aMgr( new SALOMEDS_StudyManager() );
56
57   // get id of SUIT_Study, if it's a SalomeApp_Study, it will return
58   //    id of its underlying SALOMEDS::Study
59   SUIT_Application* app = SUIT_Session::session()->activeApplication();
60   if ( !app )  return _PTR(Study)(aStudy);
61   SUIT_Study* stud = app->activeStudy();
62   if ( !stud ) return _PTR(Study)(aStudy);
63   const int id = stud->id(); // virtual method, must return SALOMEDS_Study id
64   // get SALOMEDS_Study with this id from StudyMgr
65   return aMgr->GetStudyByID( id );
66 } */               
67
68 /*!
69   Constructor
70 */
71 SPlot2d_Viewer::SPlot2d_Viewer(  bool theAutoDel )
72 : Plot2d_Viewer( theAutoDel )    
73 {
74 }
75
76 /*!
77   Destructor
78 */
79 SPlot2d_Viewer::~SPlot2d_Viewer()
80 {
81 }
82
83 /*!
84   Renames curve if it is found
85 */
86 void SPlot2d_Viewer::rename( const Handle(SALOME_InteractiveObject)& IObject,
87                              const QString& newName, Plot2d_ViewFrame* fr ) 
88 {
89   Plot2d_ViewFrame* aViewFrame = fr ? fr : getActiveViewFrame();
90   if( !aViewFrame )
91     return;
92
93   QIntDictIterator<Plot2d_Curve> it( aViewFrame->getCurves() );
94   for( ; it.current(); ++it )
95   {
96     SPlot2d_Curve* aCurve = dynamic_cast<SPlot2d_Curve*>( it.current() );
97     if( aCurve && aCurve->hasIO() && aCurve->getIO()->isSame( IObject ) )
98     {
99       aCurve->setVerTitle( newName );
100       int key = aViewFrame->hasCurve( aCurve );
101       if( key )
102         aViewFrame->setCurveTitle( key, newName );
103     }
104
105     if( aCurve && aCurve->hasTableIO() && aCurve->getTableIO()->isSame( IObject ) )
106       aCurve->getTableIO()->setName( newName.latin1() );
107   }
108   aViewFrame->updateTitles();
109 }
110
111 /*!
112   Renames all copies of object in all view windows
113   \param IObj - object to be renamed
114   \param name - new name
115 */
116 void SPlot2d_Viewer::renameAll( const Handle(SALOME_InteractiveObject)& IObj, const QString& name )
117 {
118   SUIT_ViewManager* vm = getViewManager();
119   if ( vm )
120   {
121     const QPtrVector<SUIT_ViewWindow>& wnds = vm->getViews();
122     for ( uint i = 0; i < wnds.size(); i++ )
123     {
124       Plot2d_ViewWindow* pwnd = dynamic_cast<Plot2d_ViewWindow*>( wnds.at( i ) );
125       rename( IObj, name, pwnd->getViewFrame() );
126     }
127   }
128 }
129
130 /*!
131   Returns true if interactive object is presented in the viewer
132 */
133 bool SPlot2d_Viewer::isInViewer( const Handle(SALOME_InteractiveObject)& IObject ) 
134 {
135   Plot2d_ViewFrame* aViewFrame = getActiveViewFrame();
136   if(aViewFrame == NULL) return 0;
137
138   if( getCurveByIO( IObject ) != NULL )
139     return 1;
140   else{
141     if(!IObject.IsNull()){
142       QIntDictIterator<Plot2d_Curve> it(aViewFrame->getCurves());
143       for(; it.current();++it) {
144         SPlot2d_Curve* aCurve = dynamic_cast<SPlot2d_Curve*>(it.current()); 
145         if(aCurve->hasIO() && aCurve->getTableIO()->isSame(IObject))
146           return 1;
147       }
148     }
149   }
150   return 0;
151 }
152
153
154 /*!
155   Actually this method just re-displays curves which refers to the <IObject>
156 */
157 void SPlot2d_Viewer::Display( const Handle(SALOME_InteractiveObject)& IObject, bool update )
158 {
159   Plot2d_ViewFrame* aViewFrame = getActiveViewFrame();
160   if(aViewFrame == NULL) return;
161
162   SPlot2d_Curve* curve = getCurveByIO( IObject );
163   if ( curve )
164     aViewFrame->updateCurve( curve, update );
165 }
166
167 /*!
168   Actually this method just erases all curves which don't refer to <IOBject> 
169   and re-displays curve which is of <IObject>
170 */
171 void SPlot2d_Viewer::DisplayOnly( const Handle(SALOME_InteractiveObject)& IObject )
172 {
173   Plot2d_ViewFrame* aViewFrame = getActiveViewFrame();
174   if(aViewFrame == NULL) return;
175
176   Plot2d_Curve* curve = getCurveByIO( IObject );
177   QIntDictIterator<Plot2d_Curve> it( aViewFrame->getCurves() );
178   for ( ; it.current(); ++it ) {
179     if(it.current() != curve)
180       aViewFrame->eraseCurve( curve );
181     else
182       aViewFrame->updateCurve( curve, false );
183   }
184
185   aViewFrame->Repaint();
186 }
187
188 /*!
189   Removes from the viewer the curves which refer to <IObject>
190 */
191 void SPlot2d_Viewer::Erase( const Handle(SALOME_InteractiveObject)& IObject, bool update )
192 {
193   Plot2d_ViewFrame* aViewFrame = getActiveViewFrame();
194   if(aViewFrame == NULL) return;
195
196   SPlot2d_Curve* curve = getCurveByIO( IObject );
197   if ( curve )
198     aViewFrame->eraseCurve( curve, update );
199
200   // it can be table or container object selected
201   //ASL: Temporary commented in order to avoid dependency on SALOMEDS
202 /*  _PTR(Study) aStudy = getStudyDS();
203   _PTR(SObject) aSO = aStudy->FindObjectID(IObject->getEntry());
204   if ( aSO ) {
205     _PTR(ChildIterator) aIter = aStudy->NewChildIterator( aSO );
206     for ( ; aIter->More(); aIter->Next() ) {
207       _PTR(SObject) aChildSO = aIter->Value();
208       _PTR(SObject) refSO;
209       if ( aChildSO->ReferencedObject( refSO ) && refSO )
210         aChildSO = refSO;
211       curve = getCurveByIO( new SALOME_InteractiveObject( aChildSO->GetID().c_str(), "") );
212       if ( curve )
213         aViewFrame->eraseCurve( curve, update );
214     }
215   }
216 */
217
218 }
219
220 /*!
221    Removes all curves from the view
222 */
223 void SPlot2d_Viewer::EraseAll(const bool /*forced*/) 
224 {
225   Plot2d_ViewFrame* aViewFrame = getActiveViewFrame();
226   if(aViewFrame) aViewFrame->EraseAll();
227 }
228
229 /*!
230   Redraws Viewer contents
231 */
232 void SPlot2d_Viewer::Repaint()
233 {
234   Plot2d_ViewFrame* aViewFrame = getActiveViewFrame();
235   if(aViewFrame) aViewFrame->Repaint();
236 }
237
238 /*!
239   Display presentation
240 */
241 void SPlot2d_Viewer::Display( const SALOME_Prs2d* prs )
242 {
243   Plot2d_ViewFrame* aViewFrame = getActiveViewFrame();
244   Plot2d_Prs* aPrs = dynamic_cast<Plot2d_Prs*>(const_cast<SALOME_Prs2d*>(prs));
245   if(aViewFrame && aPrs) aViewFrame->Display(aPrs);
246 }
247
248 /*!
249   Erase presentation
250 */
251 void SPlot2d_Viewer::Erase( const SALOME_Prs2d* prs, const bool )
252 {
253   Plot2d_ViewFrame* aViewFrame = getActiveViewFrame();
254   Plot2d_Prs* aPrs = dynamic_cast<Plot2d_Prs*>(const_cast<SALOME_Prs2d*>(prs));
255   if(aViewFrame && aPrs) aViewFrame->Erase(aPrs);
256 }
257   
258 /*!
259   Create presentation by entry
260 */
261 SALOME_Prs* SPlot2d_Viewer::CreatePrs( const char* entry )
262 {
263   Plot2d_ViewFrame* aViewFrame = getActiveViewFrame();
264   if(aViewFrame)
265   {
266     Plot2d_Prs* prs = aViewFrame->CreatePrs(entry);
267     if( prs )
268       return new SPlot2d_Prs( prs );
269   }
270
271   return NULL;
272 }
273
274 /*!
275   Axiluary method called before displaying of objects
276 */
277 void  SPlot2d_Viewer::BeforeDisplay( SALOME_Displayer* d )
278 {
279   d->BeforeDisplay( this, SALOME_Plot2dViewType() );
280 }
281
282 /*!
283   Axiluary method called after displaying of objects
284 */
285 void  SPlot2d_Viewer::AfterDisplay( SALOME_Displayer* d )
286 {
287   d->AfterDisplay( this, SALOME_Plot2dViewType() );
288 }
289
290 /*!
291   Returns true if interactive object is presented in the viewer and displayed
292 */
293 bool SPlot2d_Viewer::isVisible( const Handle(SALOME_InteractiveObject)& IObject ) 
294 {
295   Plot2d_ViewFrame* aViewFrame = getActiveViewFrame();
296   if(aViewFrame == NULL) return false;
297
298   SPlot2d_Curve* curve = getCurveByIO( IObject );
299   return aViewFrame->isVisible( curve );
300 }
301
302 /*!
303   Return interactive obeject if is presented in the viewer
304 */
305 Handle(SALOME_InteractiveObject) SPlot2d_Viewer::FindIObject( const char* Entry )
306 {
307   Handle(SALOME_InteractiveObject) anIO;
308   Plot2d_ViewFrame* aViewFrame = getActiveViewFrame();
309   if(aViewFrame == NULL) return anIO;
310
311   QIntDictIterator<Plot2d_Curve> it( aViewFrame->getCurves() );
312   for ( ; it.current(); ++it ) {
313     SPlot2d_Curve* aCurve = dynamic_cast<SPlot2d_Curve*>(it.current()); 
314     if ( aCurve->hasIO() && !strcmp( aCurve->getIO()->getEntry(), Entry ) ) {
315       anIO = aCurve->getIO();
316       break;
317     }
318   }
319   return anIO;
320 }
321
322 /*!
323   Returns an active Plot2d ViewFrame or NULL
324 */
325 Plot2d_ViewFrame* SPlot2d_Viewer::getActiveViewFrame()
326 {
327   SUIT_ViewManager* aViewMgr = getViewManager();
328   if(aViewMgr) {
329     Plot2d_ViewWindow* aViewWnd = dynamic_cast<Plot2d_ViewWindow*>(aViewMgr->getActiveView());
330     if(aViewWnd)
331       return aViewWnd->getViewFrame();
332   }
333
334   return NULL;
335 }
336
337 /*!
338   \return curve by object and viewframe
339   \param theIObject - object
340   \param fr - viewframe
341 */
342 SPlot2d_Curve* SPlot2d_Viewer::getCurveByIO( const Handle(SALOME_InteractiveObject)& theIObject,
343                                              Plot2d_ViewFrame* fr )
344 {
345   if ( !theIObject.IsNull() ) {
346     Plot2d_ViewFrame* aViewFrame = fr ? fr : getActiveViewFrame();
347     if(aViewFrame) {
348       QIntDictIterator<Plot2d_Curve> it( aViewFrame->getCurves() );
349       for ( ; it.current(); ++it ) {
350         SPlot2d_Curve* aCurve = dynamic_cast<SPlot2d_Curve*>(it.current()); 
351         if(aCurve) {
352           if ( aCurve->hasIO() && aCurve->getIO()->isSame( theIObject ) )
353             return aCurve;
354         }
355       }
356     }
357   }
358   return NULL;
359 }
360
361 /*!
362   SLOT: called when action "Clone view" is activated
363 */
364 void SPlot2d_Viewer::onCloneView( Plot2d_ViewFrame* clonedVF, Plot2d_ViewFrame* newVF )
365 {
366   if( !clonedVF || !newVF )
367     return;
368
369   // 1) Copy all properties of view
370
371   newVF->copyPreferences( clonedVF );
372
373   // 2) Display all curves displayed in cloned view
374
375   QList<Plot2d_Curve> aCurves;
376   clonedVF->getCurves( aCurves );
377   QList<Plot2d_Curve>::const_iterator anIt = aCurves.begin(), aLast = aCurves.end();
378
379   for( ; anIt!=aLast; anIt++ )
380     if( clonedVF->isVisible( *anIt ) )
381       newVF->displayCurve( *anIt, false );
382   newVF->Repaint();
383 }
384
385 /*!
386   create SPlot2d_ViewWindow
387 */
388 SUIT_ViewWindow* SPlot2d_Viewer::createView( SUIT_Desktop* theDesktop )
389 {
390   SPlot2d_ViewWindow* aPlot2dView = new SPlot2d_ViewWindow(theDesktop, this);
391   if (getPrs())
392     aPlot2dView->getViewFrame()->Display(getPrs());
393   return aPlot2dView;
394 }