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