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