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