]> SALOME platform Git repositories - modules/gui.git/blob - src/SPlot2d/SPlot2d_ViewModel.cxx
Salome HOME
10afaed7ce3984292f4aa1cb956daa46e522d64b
[modules/gui.git] / src / SPlot2d / SPlot2d_ViewModel.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 //  This library is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU Lesser General Public
8 //  License as published by the Free Software Foundation; either
9 //  version 2.1 of the License.
10 //
11 //  This library is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 //  Lesser General Public License for more details.
15 //
16 //  You should have received a copy of the GNU Lesser General Public
17 //  License along with this library; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 //  File   : SPlot2d_Viewer.cxx
23 //  Author : Sergey RUIN
24 //  Module : SUIT
25 //
26 #include "SPlot2d_ViewModel.h"
27
28 #include "SPlot2d_ViewWindow.h"
29
30 #include "SPlot2d_Prs.h"
31 #include "SUIT_Session.h"
32 #include "SUIT_Application.h"
33 #include "SUIT_ViewManager.h"
34
35 #include "SALOME_ListIO.hxx"
36
37 #include <QApplication>
38 #include <QToolBar>
39 #include <QToolButton>
40 #include <QCursor>
41 #include <QColorDialog>
42
43 //#include <qwt_math>
44 #include <qwt_plot_canvas.h>
45 #include <qwt_plot_curve.h>
46 #include <stdlib.h>
47
48 using namespace std;
49
50 //ASL: Temporary commented in order to avoir dependency on SALOMEDS
51
52 //#include "SALOMEDSClient.hxx"
53 //#include "SALOMEDS_StudyManager.hxx"
54
55 // in order NOT TO link with SalomeApp, here the code returns SALOMEDS_Study.
56 // SalomeApp_Study::studyDS() does it as well, but -- here it is retrieved from
57 // SALOMEDS::StudyManager - no linkage with SalomeApp.  
58
59 /*static _PTR(Study) getStudyDS()
60 {
61   SALOMEDSClient_Study* aStudy = NULL;
62   _PTR(StudyManager) aMgr( new SALOMEDS_StudyManager() );
63
64   // get id of SUIT_Study, if it's a SalomeApp_Study, it will return
65   //    id of its underlying SALOMEDS::Study
66   SUIT_Application* app = SUIT_Session::session()->activeApplication();
67   if ( !app )  return _PTR(Study)(aStudy);
68   SUIT_Study* stud = app->activeStudy();
69   if ( !stud ) return _PTR(Study)(aStudy);
70   const int id = stud->id(); // virtual method, must return SALOMEDS_Study id
71   // get SALOMEDS_Study with this id from StudyMgr
72   return aMgr->GetStudyByID( id );
73 } */               
74
75 /*!
76   Constructor
77 */
78 SPlot2d_Viewer::SPlot2d_Viewer(  bool theAutoDel )
79 : Plot2d_Viewer( theAutoDel )    
80 {
81 }
82
83 /*!
84   Destructor
85 */
86 SPlot2d_Viewer::~SPlot2d_Viewer()
87 {
88 }
89
90 /*!
91   Renames curve if it is found
92 */
93 void SPlot2d_Viewer::rename( const Handle(SALOME_InteractiveObject)& IObject,
94                              const QString& newName, Plot2d_ViewFrame* fr ) 
95 {
96   Plot2d_ViewFrame* aViewFrame = fr ? fr : getActiveViewFrame();
97   if( !aViewFrame )
98     return;
99
100   CurveDict aCurves = aViewFrame->getCurves();
101   CurveDict::Iterator it = aCurves.begin();
102   for( ; it != aCurves.end(); ++it )
103   {
104     SPlot2d_Curve* aCurve = dynamic_cast<SPlot2d_Curve*>( it.value() );
105     if( aCurve && aCurve->hasIO() && aCurve->getIO()->isSame( IObject ) )
106     {
107       aCurve->setVerTitle( newName );
108       it.key()->setTitle( newName );
109     }
110
111     if( aCurve && aCurve->hasTableIO() && aCurve->getTableIO()->isSame( IObject ) )
112       aCurve->getTableIO()->setName( newName.toLatin1() );
113   }
114   aViewFrame->updateTitles();
115 }
116
117 /*!
118   Renames all copies of object in all view windows
119   \param IObj - object to be renamed
120   \param name - new name
121 */
122 void SPlot2d_Viewer::renameAll( const Handle(SALOME_InteractiveObject)& IObj, const QString& name )
123 {
124   SUIT_ViewManager* vm = getViewManager();
125   if ( vm )
126   {
127     const QVector<SUIT_ViewWindow*>& wnds = vm->getViews();
128
129     for ( uint i = 0; i < wnds.size(); i++ )
130     {
131       Plot2d_ViewWindow* pwnd = dynamic_cast<Plot2d_ViewWindow*>( wnds.at( i ) );
132       rename( IObj, name, pwnd->getViewFrame() );
133     }
134   }
135 }
136
137 /*!
138   Returns true if interactive object is presented in the viewer
139 */
140 bool SPlot2d_Viewer::isInViewer( const Handle(SALOME_InteractiveObject)& IObject ) 
141 {
142   Plot2d_ViewFrame* aViewFrame = getActiveViewFrame();
143   if(aViewFrame == NULL) return 0;
144
145   if( getCurveByIO( IObject ) != NULL )
146     return 1;
147   else{
148     if(!IObject.IsNull()){
149       CurveDict aCurves = aViewFrame->getCurves();
150       CurveDict::Iterator it = aCurves.begin();
151       for( ; it != aCurves.end(); ++it ) {
152         SPlot2d_Curve* aCurve = dynamic_cast<SPlot2d_Curve*>( it.value() );
153         if(aCurve && aCurve->hasIO() && aCurve->getTableIO()->isSame(IObject))
154           return 1;
155       }
156     }
157   }
158   return 0;
159 }
160
161
162 /*!
163   Actually this method just re-displays curves which refers to the <IObject>
164 */
165 void SPlot2d_Viewer::Display( const Handle(SALOME_InteractiveObject)& IObject, bool update )
166 {
167   Plot2d_ViewFrame* aViewFrame = getActiveViewFrame();
168   if(aViewFrame == NULL) return;
169
170   SPlot2d_Curve* curve = getCurveByIO( IObject );
171   if ( curve )
172     aViewFrame->updateCurve( curve, update );
173 }
174
175 /*!
176   Actually this method just erases all curves which don't refer to <IOBject> 
177   and re-displays curve which is of <IObject>
178 */
179 void SPlot2d_Viewer::DisplayOnly( const Handle(SALOME_InteractiveObject)& IObject )
180 {
181   Plot2d_ViewFrame* aViewFrame = getActiveViewFrame();
182   if(aViewFrame == NULL) return;
183
184   Plot2d_Curve* curve = getCurveByIO( IObject );
185   CurveDict aCurves = aViewFrame->getCurves();
186   CurveDict::Iterator it = aCurves.begin();
187   for( ; it != aCurves.end(); ++it ) {
188     if(it.value() != curve)
189       aViewFrame->eraseCurve( curve );
190     else
191       aViewFrame->updateCurve( curve, false );
192   }
193
194   aViewFrame->Repaint();
195 }
196
197 /*!
198   Removes from the viewer the curves which refer to <IObject>
199 */
200 void SPlot2d_Viewer::Erase( const Handle(SALOME_InteractiveObject)& IObject, bool update )
201 {
202   Plot2d_ViewFrame* aViewFrame = getActiveViewFrame();
203   if(aViewFrame == NULL) return;
204
205   SPlot2d_Curve* curve = getCurveByIO( IObject );
206   if ( curve )
207     aViewFrame->eraseCurve( curve, update );
208
209   // it can be table or container object selected
210   //ASL: Temporary commented in order to avoid dependency on SALOMEDS
211 /*  _PTR(Study) aStudy = getStudyDS();
212   _PTR(SObject) aSO = aStudy->FindObjectID(IObject->getEntry());
213   if ( aSO ) {
214     _PTR(ChildIterator) aIter = aStudy->NewChildIterator( aSO );
215     for ( ; aIter->More(); aIter->Next() ) {
216       _PTR(SObject) aChildSO = aIter->Value();
217       _PTR(SObject) refSO;
218       if ( aChildSO->ReferencedObject( refSO ) && refSO )
219         aChildSO = refSO;
220       curve = getCurveByIO( new SALOME_InteractiveObject( aChildSO->GetID().c_str(), "") );
221       if ( curve )
222         aViewFrame->eraseCurve( curve, update );
223     }
224   }
225 */
226
227 }
228
229 /*!
230    Removes all curves from the view
231 */
232 void SPlot2d_Viewer::EraseAll(const bool /*forced*/) 
233 {
234   Plot2d_ViewFrame* aViewFrame = getActiveViewFrame();
235   if(aViewFrame) aViewFrame->EraseAll();
236 }
237
238 /*!
239   Redraws Viewer contents
240 */
241 void SPlot2d_Viewer::Repaint()
242 {
243   Plot2d_ViewFrame* aViewFrame = getActiveViewFrame();
244   if(aViewFrame) aViewFrame->Repaint();
245 }
246
247 /*!
248   Display presentation
249 */
250 void SPlot2d_Viewer::Display( const SALOME_Prs2d* prs )
251 {
252   Plot2d_ViewFrame* aViewFrame = getActiveViewFrame();
253   Plot2d_Prs* aPrs = dynamic_cast<Plot2d_Prs*>(const_cast<SALOME_Prs2d*>(prs));
254   if(aViewFrame && aPrs) aViewFrame->Display(aPrs);
255 }
256
257 /*!
258   Erase presentation
259 */
260 void SPlot2d_Viewer::Erase( const SALOME_Prs2d* prs, const bool )
261 {
262   Plot2d_ViewFrame* aViewFrame = getActiveViewFrame();
263   Plot2d_Prs* aPrs = dynamic_cast<Plot2d_Prs*>(const_cast<SALOME_Prs2d*>(prs));
264   if(aViewFrame && aPrs) aViewFrame->Erase(aPrs);
265 }
266   
267 /*!
268   Create presentation by entry
269 */
270 SALOME_Prs* SPlot2d_Viewer::CreatePrs( const char* entry )
271 {
272   Plot2d_ViewFrame* aViewFrame = getActiveViewFrame();
273   if(aViewFrame)
274   {
275     Plot2d_Prs* prs = aViewFrame->CreatePrs(entry);
276     if( prs )
277       return new SPlot2d_Prs( prs );
278   }
279
280   return NULL;
281 }
282
283 /*!
284   Axiluary method called before displaying of objects
285 */
286 void  SPlot2d_Viewer::BeforeDisplay( SALOME_Displayer* d )
287 {
288   d->BeforeDisplay( this, SALOME_Plot2dViewType() );
289 }
290
291 /*!
292   Axiluary method called after displaying of objects
293 */
294 void  SPlot2d_Viewer::AfterDisplay( SALOME_Displayer* d )
295 {
296   d->AfterDisplay( this, SALOME_Plot2dViewType() );
297 }
298
299 /*!
300   Returns true if interactive object is presented in the viewer and displayed
301 */
302 bool SPlot2d_Viewer::isVisible( const Handle(SALOME_InteractiveObject)& IObject ) 
303 {
304   Plot2d_ViewFrame* aViewFrame = getActiveViewFrame();
305   if(aViewFrame == NULL) return false;
306
307   SPlot2d_Curve* curve = getCurveByIO( IObject );
308   return aViewFrame->isVisible( curve );
309 }
310
311 /*!
312   Return interactive obeject if is presented in the viewer
313 */
314 Handle(SALOME_InteractiveObject) SPlot2d_Viewer::FindIObject( const char* Entry )
315 {
316   Handle(SALOME_InteractiveObject) anIO;
317   Plot2d_ViewFrame* aViewFrame = getActiveViewFrame();
318   if(aViewFrame == NULL) return anIO;
319
320   CurveDict aCurves = aViewFrame->getCurves();
321   CurveDict::Iterator it = aCurves.begin();
322   for( ; it != aCurves.end(); ++it ) {
323     SPlot2d_Curve* aCurve = dynamic_cast<SPlot2d_Curve*>(it.value()); 
324     if ( aCurve && aCurve->hasIO() && !strcmp( aCurve->getIO()->getEntry(), Entry ) ) {
325       anIO = aCurve->getIO();
326       break;
327     }
328   }
329   return anIO;
330 }
331
332 /*!
333   Returns an active Plot2d ViewFrame or NULL
334 */
335 Plot2d_ViewFrame* SPlot2d_Viewer::getActiveViewFrame()
336 {
337   SUIT_ViewManager* aViewMgr = getViewManager();
338   if(aViewMgr) {
339     Plot2d_ViewWindow* aViewWnd = dynamic_cast<Plot2d_ViewWindow*>(aViewMgr->getActiveView());
340     if(aViewWnd)
341       return aViewWnd->getViewFrame();
342   }
343
344   return NULL;
345 }
346
347 /*!
348   \return curve by object and viewframe
349   \param theIObject - object
350   \param fr - viewframe
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       CurveDict aCurves = aViewFrame->getCurves();
359       CurveDict::Iterator it = aCurves.begin();
360       for( ; it != aCurves.end(); ++it ) {
361         SPlot2d_Curve* aCurve = dynamic_cast<SPlot2d_Curve*>( it.value() );
362         if(aCurve) {
363           if ( aCurve->hasIO() && aCurve->getIO()->isSame( theIObject ) )
364             return aCurve;
365         }
366       }
367     }
368   }
369   return NULL;
370 }
371
372 /*!
373   create SPlot2d_ViewWindow
374 */
375 SUIT_ViewWindow* SPlot2d_Viewer::createView( SUIT_Desktop* theDesktop )
376 {
377   SPlot2d_ViewWindow* aPlot2dView = new SPlot2d_ViewWindow(theDesktop, this);
378   if (getPrs())
379     aPlot2dView->getViewFrame()->Display(getPrs());
380   return aPlot2dView;
381 }
382
383 /*!
384   SLOT: called when action "Legend Clicked" is activated.
385   override "onLegendClicked" method from Plot2d_ViewModel.
386 */
387 void SPlot2d_Viewer::onLegendClicked( QwtPlotItem* plotItem )
388 {
389   Plot2d_ViewFrame* aViewFrame = getActiveViewFrame();
390   if(aViewFrame == NULL) return;
391
392   CurveDict aCurves = aViewFrame->getCurves();
393   SPlot2d_Curve* aSCurve;
394   CurveDict::Iterator it = aCurves.begin();
395   for( ; it != aCurves.end(); ++it )
396   {
397     if ( it.key() == plotItem ) {
398       aSCurve = dynamic_cast<SPlot2d_Curve*>( it.value() );
399       break;
400     }
401   }
402   // Highlight curve in Object Browser
403   if(aSCurve && aSCurve->hasIO()) {
404     QString anEntry = aSCurve->getIO()->getEntry();
405     emit legendSelected( anEntry );
406   }
407 }