Salome HOME
Upgrade to paraview 5.4
[modules/gui.git] / src / SPlot2d / SPlot2d_ViewModel.cxx
1 // Copyright (C) 2007-2016  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, or (at your option) any later version.
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
23 //  File   : SPlot2d_Viewer.cxx
24 //  Author : Sergey RUIN
25 //  Module : SUIT
26 //
27 #include "SPlot2d_ViewModel.h"
28
29 #include "SPlot2d_ViewWindow.h"
30
31 #include "SPlot2d_Prs.h"
32 #include "SPlot2d_Histogram.h"
33 #include "SUIT_Session.h"
34 #include "SUIT_Application.h"
35 #include "SUIT_ViewManager.h"
36
37 #include "SALOME_ListIO.hxx"
38
39 #include <QApplication>
40 #include <QToolBar>
41 #include <QToolButton>
42 #include <QCursor>
43 #include <QColorDialog>
44
45 //#include <qwt_math>
46 #include <qwt_plot_canvas.h>
47 #include <qwt_plot_curve.h>
48 #include <stdlib.h>
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   myDeselectAnalytical(true)
81 {
82 }
83
84 /*!
85   Destructor
86 */
87 SPlot2d_Viewer::~SPlot2d_Viewer()
88 {
89 }
90
91 /*!
92   Renames curve if it is found
93 */
94 void SPlot2d_Viewer::rename( const Handle(SALOME_InteractiveObject)& IObject,
95                              const QString& newName, Plot2d_ViewFrame* fr ) 
96 {
97   Plot2d_ViewFrame* aViewFrame = fr ? fr : getActiveViewFrame();
98   if( !aViewFrame )
99     return;
100
101   CurveDict aCurves = aViewFrame->getCurves();
102   CurveDict::Iterator it = aCurves.begin();
103   for( ; it != aCurves.end(); ++it )
104   {
105     SPlot2d_Curve* aCurve = dynamic_cast<SPlot2d_Curve*>( it.value() );
106     if( aCurve && aCurve->hasIO() && aCurve->getIO()->isSame( IObject ) )
107     {
108       aCurve->setVerTitle( newName );
109       it.key()->setTitle( newName );
110     }
111
112     if( aCurve && aCurve->hasTableIO() && aCurve->getTableIO()->isSame( IObject ) )
113       aCurve->getTableIO()->setName( newName.toLatin1() );
114   }
115   aViewFrame->updateTitles();
116 }
117
118 /*!
119   Renames all copies of object in all view windows
120   \param IObj - object to be renamed
121   \param name - new name
122 */
123 void SPlot2d_Viewer::renameAll( const Handle(SALOME_InteractiveObject)& IObj, const QString& name )
124 {
125   SUIT_ViewManager* vm = getViewManager();
126   if ( vm )
127   {
128     const QVector<SUIT_ViewWindow*>& wnds = vm->getViews();
129
130     for ( uint i = 0; i < wnds.size(); i++ )
131     {
132       Plot2d_ViewWindow* pwnd = dynamic_cast<Plot2d_ViewWindow*>( wnds.at( i ) );
133       rename( IObj, name, pwnd->getViewFrame() );
134     }
135   }
136 }
137
138 /*!
139   Returns true if interactive object is presented in the viewer
140 */
141 bool SPlot2d_Viewer::isInViewer( const Handle(SALOME_InteractiveObject)& IObject ) 
142 {
143   Plot2d_ViewFrame* aViewFrame = getActiveViewFrame();
144   if(aViewFrame == NULL) return 0;
145
146   if( getCurveByIO( IObject ) != NULL )
147     return 1;
148   else{
149     if(!IObject.IsNull()){
150       CurveDict aCurves = aViewFrame->getCurves();
151       CurveDict::Iterator it = aCurves.begin();
152       for( ; it != aCurves.end(); ++it ) {
153         SPlot2d_Curve* aCurve = dynamic_cast<SPlot2d_Curve*>( it.value() );
154         if(aCurve && aCurve->hasIO() && aCurve->getTableIO()->isSame(IObject))
155           return 1;
156       }
157     }
158   }
159   return 0;
160 }
161
162
163 /*!
164   Actually this method just re-displays curves which refers to the <IObject>
165 */
166 void SPlot2d_Viewer::Display( const Handle(SALOME_InteractiveObject)& IObject, bool update )
167 {
168   Plot2d_ViewFrame* aViewFrame = getActiveViewFrame();
169   if(aViewFrame == NULL) return;
170
171   SPlot2d_Curve* curve = getCurveByIO( IObject );
172   if ( curve )
173     aViewFrame->updateCurve( curve, update );
174 }
175
176 /*!
177   Actually this method just erases all curves which don't refer to <IOBject> 
178   and re-displays curve which is of <IObject>
179 */
180 void SPlot2d_Viewer::DisplayOnly( const Handle(SALOME_InteractiveObject)& IObject )
181 {
182   Plot2d_ViewFrame* aViewFrame = getActiveViewFrame();
183   if(aViewFrame == NULL) return;
184
185   Plot2d_Curve* curve = getCurveByIO( IObject );
186   CurveDict aCurves = aViewFrame->getCurves();
187   CurveDict::Iterator it = aCurves.begin();
188   for( ; it != aCurves.end(); ++it ) {
189     if(it.value() != curve)
190       aViewFrame->eraseCurve( curve );
191     else
192       aViewFrame->updateCurve( curve, false );
193   }
194
195   aViewFrame->Repaint();
196 }
197
198 /*!
199   Removes from the viewer the curves which refer to <IObject>
200 */
201 void SPlot2d_Viewer::Erase( const Handle(SALOME_InteractiveObject)& IObject, bool update )
202 {
203   Plot2d_ViewFrame* aViewFrame = getActiveViewFrame();
204   if(aViewFrame == NULL) return;
205
206   SPlot2d_Curve* curve = getCurveByIO( IObject );
207   if ( curve )
208     aViewFrame->eraseCurve( curve, update );
209
210   // it can be table or container object selected
211   //ASL: Temporary commented in order to avoid dependency on SALOMEDS
212 /*  _PTR(Study) aStudy = getStudyDS();
213   _PTR(SObject) aSO = aStudy->FindObjectID(IObject->getEntry());
214   if ( aSO ) {
215     _PTR(ChildIterator) aIter = aStudy->NewChildIterator( aSO );
216     for ( ; aIter->More(); aIter->Next() ) {
217       _PTR(SObject) aChildSO = aIter->Value();
218       _PTR(SObject) refSO;
219       if ( aChildSO->ReferencedObject( refSO ) && refSO )
220         aChildSO = refSO;
221       curve = getCurveByIO( new SALOME_InteractiveObject( aChildSO->GetID().c_str(), "") );
222       if ( curve )
223         aViewFrame->eraseCurve( curve, update );
224     }
225   }
226 */
227
228 }
229
230 /*!
231    Removes all curves from the view
232 */
233 void SPlot2d_Viewer::EraseAll(SALOME_Displayer* d, const bool forced) 
234 {
235   Plot2d_ViewFrame* aViewFrame = getActiveViewFrame();
236   if(aViewFrame) aViewFrame->EraseAll();
237   SALOME_View::EraseAll(d, forced);
238 }
239
240 /*!
241   Redraws Viewer contents
242 */
243 void SPlot2d_Viewer::Repaint()
244 {
245   Plot2d_ViewFrame* aViewFrame = getActiveViewFrame();
246   if(aViewFrame) aViewFrame->Repaint();
247 }
248
249 /*!
250   Display presentation
251 */
252 void SPlot2d_Viewer::Display( const SALOME_Prs2d* prs )
253 {
254   Plot2d_ViewFrame* aViewFrame = getActiveViewFrame();
255   Plot2d_Prs* aPrs = dynamic_cast<Plot2d_Prs*>(const_cast<SALOME_Prs2d*>(prs));
256   if(aViewFrame && aPrs) aViewFrame->Display(aPrs);
257 }
258
259 /*!
260   Erase presentation
261 */
262 void SPlot2d_Viewer::Erase( const SALOME_Prs2d* prs, const bool )
263 {
264   Plot2d_ViewFrame* aViewFrame = getActiveViewFrame();
265   Plot2d_Prs* aPrs = dynamic_cast<Plot2d_Prs*>(const_cast<SALOME_Prs2d*>(prs));
266   if(aViewFrame && aPrs) aViewFrame->Erase(aPrs);
267 }
268   
269 /*!
270   Create presentation by entry
271 */
272 SALOME_Prs* SPlot2d_Viewer::CreatePrs( const char* entry )
273 {
274   Plot2d_ViewFrame* aViewFrame = getActiveViewFrame();
275   SPlot2d_Prs *prs = new SPlot2d_Prs( entry );
276   if(aViewFrame)
277   {
278     CurveDict aCurves = aViewFrame->getCurves();
279     CurveDict::Iterator it = aCurves.begin();
280     for( ; it != aCurves.end(); ++it ) {
281       SPlot2d_Curve* aCurve = dynamic_cast<SPlot2d_Curve*>(it.value());
282       OwnerSet owners = aCurve->getOwners();
283       if(aCurve) {
284         if ( 
285             (aCurve->hasIO() && !strcmp( aCurve->getIO()->getEntry(), entry )) ||
286             (aCurve->hasTableIO() && !strcmp( aCurve->getTableIO()->getEntry(), entry )) ||
287             owners.contains(entry)
288             ) {
289           prs->AddObject(aCurve);
290         }
291       }      
292     }
293   }
294   return prs;
295 }
296
297 /*!
298   Returns true if interactive object is presented in the viewer and displayed
299 */
300 bool SPlot2d_Viewer::isVisible( const Handle(SALOME_InteractiveObject)& IObject ) 
301 {
302   Plot2d_ViewFrame* aViewFrame = getActiveViewFrame();
303   if(aViewFrame == NULL) return false;
304
305   SPlot2d_Curve* curve = getCurveByIO( IObject );
306   return aViewFrame->isVisible( curve );
307 }
308
309 /*!
310   \Collect objects visible in viewer
311   \param theList - visible objects collection
312 */
313 void SPlot2d_Viewer::GetVisible( SALOME_ListIO& theList )
314 {
315   Plot2d_ViewFrame* aViewFrame = getActiveViewFrame();
316   if(aViewFrame == NULL) return;
317   CurveDict aCurves = aViewFrame->getCurves();
318   CurveDict::Iterator it = aCurves.begin();
319   for( ; it != aCurves.end(); ++it ) {
320     SPlot2d_Curve* aCurve = dynamic_cast<SPlot2d_Curve*>(it.value()); 
321     if ( aCurve && aCurve->hasIO() && aViewFrame->isVisible( aCurve ) )
322       theList.Append( aCurve->getIO() );
323   }
324 }
325
326 /*!
327   Return interactive obeject if is presented in the viewer
328 */
329 Handle(SALOME_InteractiveObject) SPlot2d_Viewer::FindIObject( const char* Entry )
330 {
331   Handle(SALOME_InteractiveObject) anIO;
332   Plot2d_ViewFrame* aViewFrame = getActiveViewFrame();
333   if(aViewFrame == NULL) return anIO;
334
335   CurveDict aCurves = aViewFrame->getCurves();
336   CurveDict::Iterator it = aCurves.begin();
337   for( ; it != aCurves.end(); ++it ) {
338     SPlot2d_Curve* aCurve = dynamic_cast<SPlot2d_Curve*>(it.value()); 
339     if ( aCurve && aCurve->hasIO() && !strcmp( aCurve->getIO()->getEntry(), Entry ) ) {
340       anIO = aCurve->getIO();
341       break;
342     }
343   }
344   return anIO;
345 }
346
347 /*!
348   Returns an active Plot2d ViewFrame or NULL
349 */
350 Plot2d_ViewFrame* SPlot2d_Viewer::getActiveViewFrame()
351 {
352   SUIT_ViewManager* aViewMgr = getViewManager();
353   if(aViewMgr) {
354     Plot2d_ViewWindow* aViewWnd = dynamic_cast<Plot2d_ViewWindow*>(aViewMgr->getActiveView());
355     if(aViewWnd)
356       return aViewWnd->getViewFrame();
357   }
358
359   return NULL;
360 }
361
362 /*!
363   \return curve by object and viewframe
364   \param theIObject - object
365   \param fr - viewframe
366 */
367 SPlot2d_Curve* SPlot2d_Viewer::getCurveByIO( const Handle(SALOME_InteractiveObject)& theIObject,
368                                              Plot2d_ViewFrame* fr )
369 {
370   if ( !theIObject.IsNull() ) {
371     Plot2d_ViewFrame* aViewFrame = fr ? fr : getActiveViewFrame();
372     if(aViewFrame) {
373       CurveDict aCurves = aViewFrame->getCurves();
374       CurveDict::Iterator it = aCurves.begin();
375       for( ; it != aCurves.end(); ++it ) {
376         SPlot2d_Curve* aCurve = dynamic_cast<SPlot2d_Curve*>( it.value() );
377         if(aCurve) {
378           if ( aCurve->hasIO() && aCurve->getIO()->isSame( theIObject ) )
379             return aCurve;
380         }
381       }
382     }
383   }
384   return NULL;
385 }
386
387 /*!
388   create SPlot2d_ViewWindow
389 */
390 SUIT_ViewWindow* SPlot2d_Viewer::createView( SUIT_Desktop* theDesktop )
391 {
392   SPlot2d_ViewWindow* aPlot2dView = new SPlot2d_ViewWindow(theDesktop, this);
393   aPlot2dView->initLayout();
394   if (getPrs())
395     aPlot2dView->getViewFrame()->Display(getPrs());
396   return aPlot2dView;
397 }
398
399 /*!
400   SLOT: called when action "Legend Clicked" is activated.
401   override "onLegendClicked" method from Plot2d_ViewModel.
402 */
403 void SPlot2d_Viewer::onClicked( const QVariant& itemInfo, int index )
404 {
405   Plot2d_ViewFrame* aViewFrame = getActiveViewFrame();
406   if(aViewFrame == NULL) return;
407
408   QwtPlotItem* plotItem = aViewFrame->getPlot()->infoToItem( itemInfo );
409
410   bool isCurveSelected = false;
411   CurveDict aCurves = aViewFrame->getCurves();
412   for( CurveDict::Iterator it = aCurves.begin(); it != aCurves.end(); ++it )
413   {
414     if(plotItem == it.key()) {
415       isCurveSelected = true;
416       it.value()->setSelected(true);
417     } else {
418       it.value()->setSelected(false);
419     }
420   }
421
422   AnalyticalCurveList curves = aViewFrame->getAnalyticalCurves();
423    foreach ( Plot2d_AnalyticalCurve* curve, curves ) {
424            if(plotItem == curve->plotItem()) {
425           isCurveSelected = true;
426                   curve->setSelected(true);
427            } else {
428                   curve->setSelected(false);
429            }
430    }
431   if(isCurveSelected) {
432     for( CurveDict::Iterator it = aCurves.begin(); it != aCurves.end(); ++it )
433       aViewFrame->updateCurve( it.value() );
434
435         myDeselectAnalytical = false;
436         emit clearSelected();
437         aViewFrame->updateAnalyticalCurves();
438         myDeselectAnalytical = true;
439         return;
440   }
441
442   Plot2d_Object* anObject = aViewFrame->getPlotObject(plotItem);
443   if(anObject) {
444     
445     // Highlight object in Object Browser
446     QString anEntry;
447     if(SPlot2d_Curve* aSCurve = dynamic_cast<SPlot2d_Curve*>(anObject)) {
448       if(aSCurve->hasIO())
449         anEntry = aSCurve->getIO()->getEntry();
450     } else if( SPlot2d_Histogram* aSHisto = dynamic_cast<SPlot2d_Histogram*>(anObject)) {
451       if(aSHisto->hasIO())
452         anEntry = aSHisto->getIO()->getEntry();
453     }
454     
455     if(!anEntry.isEmpty())
456       emit legendSelected( anEntry );
457   }     
458 }
459
460 /*!
461   
462 */
463 void SPlot2d_Viewer::setObjectsSelected( SALOME_ListIO& theList ) {
464   Plot2d_ViewFrame* aViewFrame = getActiveViewFrame();
465   if(aViewFrame) {
466
467     objectList allObjects;
468     aViewFrame->getObjects( allObjects );
469     
470     bool isSelected = false;
471     SPlot2d_Histogram* h = 0;
472     SPlot2d_Curve* c =0;
473     
474     foreach ( Plot2d_Object* o, allObjects ) {
475       isSelected = false;
476       
477       Handle(SALOME_InteractiveObject) io;
478       if( (h = dynamic_cast<SPlot2d_Histogram*>(o)) && h->hasIO() ) {
479         io = h->getIO();
480       } else if((c = dynamic_cast<SPlot2d_Curve*>(o)) && c->hasIO()) {
481         io = c->getIO();
482       } else {
483         continue;
484       }
485
486       SALOME_ListIteratorOfListIO anIter( theList ); 
487       
488       for( ; anIter.More(); anIter.Next() ) {
489         if ( anIter.Value()->hasEntry() ) {
490           if( io->isSame(anIter.Value()) ) {
491             isSelected = o->isSelected();
492             if( !isSelected ) {
493               o->setSelected(true);
494               aViewFrame->updateObject(o);
495               theList.Remove(anIter);
496               isSelected = true;
497               break;
498             } else 
499               break;
500           }
501         }
502       }
503       if( !isSelected && o->isSelected() != false ) {   
504         o->setSelected(false);
505         aViewFrame->updateObject(o);
506       }
507     }
508         if( myDeselectAnalytical ) {
509                 aViewFrame->deselectAnalyticalCurves();
510                 aViewFrame->updateAnalyticalCurves(); 
511         }
512     aViewFrame->Repaint();
513   }
514 }