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