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