Salome HOME
useless #include "utilities.h" removed
[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, QString newName ) 
71 {
72   Plot2d_ViewFrame* aViewFrame = getActiveViewFrame();
73   if(aViewFrame == NULL) return;
74
75   Plot2d_Curve* curve = getCurveByIO( IObject );
76   if ( curve ) {
77     curve->setVerTitle( newName );
78     int key = aViewFrame->hasCurve( curve );
79     if ( key ) {
80       aViewFrame->setCurveTitle( key, newName );
81     }
82   }
83 }
84
85 /*!
86   Returns true if interactive object is presented in the viewer
87 */
88 bool SPlot2d_Viewer::isInViewer( const Handle(SALOME_InteractiveObject)& IObject ) 
89 {
90   Plot2d_ViewFrame* aViewFrame = getActiveViewFrame();
91   if(aViewFrame == NULL) return 0;
92
93   if( getCurveByIO( IObject ) != NULL )
94     return 1;
95   else{
96     if(!IObject.IsNull()){
97       QIntDictIterator<Plot2d_Curve> it(aViewFrame->getCurves());
98       for(; it.current();++it) {
99         SPlot2d_Curve* aCurve = dynamic_cast<SPlot2d_Curve*>(it.current()); 
100         if(aCurve->hasIO() && aCurve->getTableIO()->isSame(IObject))
101           return 1;
102       }
103     }
104   }
105   return 0;
106 }
107
108
109 /*!
110   Actually this method just re-displays curves which refers to the <IObject>
111 */
112 void SPlot2d_Viewer::Display( const Handle(SALOME_InteractiveObject)& IObject, bool update )
113 {
114   Plot2d_ViewFrame* aViewFrame = getActiveViewFrame();
115   if(aViewFrame == NULL) return;
116
117   SPlot2d_Curve* curve = getCurveByIO( IObject );
118   if ( curve )
119     aViewFrame->updateCurve( curve, update );
120 }
121
122 /*!
123   Actually this method just erases all curves which don't refer to <IOBject> 
124   and re-displays curve which is of <IObject>
125 */
126 void SPlot2d_Viewer::DisplayOnly( const Handle(SALOME_InteractiveObject)& IObject )
127 {
128   Plot2d_ViewFrame* aViewFrame = getActiveViewFrame();
129   if(aViewFrame == NULL) return;
130
131   Plot2d_Curve* curve = getCurveByIO( IObject );
132   QIntDictIterator<Plot2d_Curve> it( aViewFrame->getCurves() );
133   for ( ; it.current(); ++it ) {
134     if(it.current() != curve)
135       aViewFrame->eraseCurve( curve );
136     else
137       aViewFrame->updateCurve( curve, false );
138   }
139
140   aViewFrame->Repaint();
141 }
142
143 /*!
144   Removes from the viewer the curves which refer to <IObject>
145 */
146 void SPlot2d_Viewer::Erase( const Handle(SALOME_InteractiveObject)& IObject, bool update )
147 {
148   Plot2d_ViewFrame* aViewFrame = getActiveViewFrame();
149   if(aViewFrame == NULL) return;
150
151   SPlot2d_Curve* curve = getCurveByIO( IObject );
152   if ( curve )
153     aViewFrame->eraseCurve( curve, update );
154
155   // it can be table or container object selected
156   //ASL: Temporary commented in order to avoid dependency on SALOMEDS
157 /*  _PTR(Study) aStudy = getStudyDS();
158   _PTR(SObject) aSO = aStudy->FindObjectID(IObject->getEntry());
159   if ( aSO ) {
160     _PTR(ChildIterator) aIter = aStudy->NewChildIterator( aSO );
161     for ( ; aIter->More(); aIter->Next() ) {
162       _PTR(SObject) aChildSO = aIter->Value();
163       _PTR(SObject) refSO;
164       if ( aChildSO->ReferencedObject( refSO ) && refSO )
165         aChildSO = refSO;
166       curve = getCurveByIO( new SALOME_InteractiveObject( aChildSO->GetID().c_str(), "") );
167       if ( curve )
168         aViewFrame->eraseCurve( curve, update );
169     }
170   }
171 */
172
173 }
174
175 /*!
176    Removes all curves from the view
177 */
178 void SPlot2d_Viewer::EraseAll(const bool /*forced*/) 
179 {
180   Plot2d_ViewFrame* aViewFrame = getActiveViewFrame();
181   if(aViewFrame) aViewFrame->EraseAll();
182 }
183
184 /*!
185   Redraws Viewer contents
186 */
187 void SPlot2d_Viewer::Repaint()
188 {
189   Plot2d_ViewFrame* aViewFrame = getActiveViewFrame();
190   if(aViewFrame) aViewFrame->Repaint();
191 }
192
193 //==========================================================
194 /*!
195  *  SPlot2d_Viewer::Display
196  *  Display presentation
197  */
198 //==========================================================
199 void SPlot2d_Viewer::Display( const SALOME_Prs2d* prs )
200 {
201   Plot2d_ViewFrame* aViewFrame = getActiveViewFrame();
202   Plot2d_Prs* aPrs = dynamic_cast<Plot2d_Prs*>(const_cast<SALOME_Prs2d*>(prs));
203   if(aViewFrame && aPrs) aViewFrame->Display(aPrs);
204 }
205
206 //==========================================================
207 /*!
208  *  SPlot2d_Viewer::Erase
209  *  Erase presentation
210  */
211 //==========================================================
212 void SPlot2d_Viewer::Erase( const SALOME_Prs2d* prs, const bool )
213 {
214   Plot2d_ViewFrame* aViewFrame = getActiveViewFrame();
215   Plot2d_Prs* aPrs = dynamic_cast<Plot2d_Prs*>(const_cast<SALOME_Prs2d*>(prs));
216   if(aViewFrame && aPrs) aViewFrame->Erase(aPrs);
217 }
218   
219 //==========================================================
220 /*!
221  *  SPlot2d_Viewer::CreatePrs
222  *  Create presentation by entry
223  */
224 //==========================================================
225 SALOME_Prs* SPlot2d_Viewer::CreatePrs( const char* entry )
226 {
227   Plot2d_ViewFrame* aViewFrame = getActiveViewFrame();
228   if(aViewFrame)
229   {
230     Plot2d_Prs* prs = aViewFrame->CreatePrs(entry);
231     if( prs )
232       return new SPlot2d_Prs( prs );
233   }
234
235   return NULL;
236 }
237
238 //==========================================================
239 /*!
240  *  SPlot2d_Viewer::BeforeDisplay
241  *  Axiluary method called before displaying of objects
242  */
243 //==========================================================
244 void  SPlot2d_Viewer::BeforeDisplay( SALOME_Displayer* d )
245 {
246   d->BeforeDisplay( this, SALOME_Plot2dViewType() );
247 }
248
249 //==========================================================
250 /*!
251  *  SPlot2d_Viewer::AfterDisplay
252  *  Axiluary method called after displaying of objects
253  */
254 //==========================================================
255 void  SPlot2d_Viewer::AfterDisplay( SALOME_Displayer* d )
256 {
257   d->AfterDisplay( this, SALOME_Plot2dViewType() );
258 }
259
260 /*!
261   Returns true if interactive object is presented in the viewer and displayed
262 */
263 bool SPlot2d_Viewer::isVisible( const Handle(SALOME_InteractiveObject)& IObject ) 
264 {
265   Plot2d_ViewFrame* aViewFrame = getActiveViewFrame();
266   if(aViewFrame == NULL) return false;
267
268   SPlot2d_Curve* curve = getCurveByIO( IObject );
269   return aViewFrame->isVisible( curve );
270 }
271
272 /*!
273   Return interactive obeject if is presented in the viewer
274 */
275 Handle(SALOME_InteractiveObject) SPlot2d_Viewer::FindIObject( const char* Entry )
276 {
277   Handle(SALOME_InteractiveObject) anIO;
278   Plot2d_ViewFrame* aViewFrame = getActiveViewFrame();
279   if(aViewFrame == NULL) return anIO;
280
281   QIntDictIterator<Plot2d_Curve> it( aViewFrame->getCurves() );
282   for ( ; it.current(); ++it ) {
283     SPlot2d_Curve* aCurve = dynamic_cast<SPlot2d_Curve*>(it.current()); 
284     if ( aCurve->hasIO() && !strcmp( aCurve->getIO()->getEntry(), Entry ) ) {
285       anIO = aCurve->getIO();
286       break;
287     }
288   }
289   return anIO;
290 }
291
292 //==========================================================
293 /*!
294  *  SPlot2d_Viewer::getActiveViewFrame
295  *  Returns an active Plot2d ViewFrame or NULL
296  */
297 //==========================================================
298 Plot2d_ViewFrame* SPlot2d_Viewer::getActiveViewFrame()
299 {
300   SUIT_ViewManager* aViewMgr = getViewManager();
301   if(aViewMgr) {
302     Plot2d_ViewWindow* aViewWnd = dynamic_cast<Plot2d_ViewWindow*>(aViewMgr->getActiveView());
303     if(aViewWnd)
304       return aViewWnd->getViewFrame();
305   }
306
307   return NULL;
308 }
309
310 SPlot2d_Curve* SPlot2d_Viewer::getCurveByIO( const Handle(SALOME_InteractiveObject)& theIObject )
311 {
312   if ( !theIObject.IsNull() ) {
313     Plot2d_ViewFrame* aViewFrame = getActiveViewFrame();
314     if(aViewFrame) {
315       QIntDictIterator<Plot2d_Curve> it( aViewFrame->getCurves() );
316       for ( ; it.current(); ++it ) {
317         SPlot2d_Curve* aCurve = dynamic_cast<SPlot2d_Curve*>(it.current()); 
318         if(aCurve) {
319           if ( aCurve->hasIO() && aCurve->getIO()->isSame( theIObject ) )
320             return aCurve;
321         }
322       }
323     }
324   }
325   return NULL;
326 }
327
328 void SPlot2d_Viewer::onCloneView( Plot2d_ViewFrame* clonedVF, Plot2d_ViewFrame* newVF )
329 {
330   if( !clonedVF || !newVF )
331     return;
332
333   // 1) Copy all properties of view
334
335   newVF->copyPreferences( clonedVF );
336
337   // 2) Display all curves displayed in cloned view
338
339   QList<Plot2d_Curve> aCurves;
340   clonedVF->getCurves( aCurves );
341   QList<Plot2d_Curve>::const_iterator anIt = aCurves.begin(), aLast = aCurves.end();
342
343   for( ; anIt!=aLast; anIt++ )
344     if( clonedVF->isVisible( *anIt ) )
345       newVF->displayCurve( *anIt, false );
346   newVF->Repaint();
347 }