1 // Copyright (C) 2007-2014 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 // File : SPlot2d_Viewer.cxx
24 // Author : Sergey RUIN
27 #include "SPlot2d_ViewModel.h"
29 #include "SPlot2d_ViewWindow.h"
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"
37 #include "SALOME_ListIO.hxx"
39 #include <QApplication>
41 #include <QToolButton>
43 #include <QColorDialog>
46 #include <qwt_plot_canvas.h>
47 #include <qwt_plot_curve.h>
50 //ASL: Temporary commented in order to avoir dependency on SALOMEDS
52 //#include "SALOMEDSClient.hxx"
53 //#include "SALOMEDS_StudyManager.hxx"
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.
59 /*static _PTR(Study) getStudyDS()
61 SALOMEDSClient_Study* aStudy = NULL;
62 _PTR(StudyManager) aMgr( new SALOMEDS_StudyManager() );
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 );
78 SPlot2d_Viewer::SPlot2d_Viewer( bool theAutoDel )
79 : Plot2d_Viewer( theAutoDel ),
80 myDeselectAnalytical(true)
87 SPlot2d_Viewer::~SPlot2d_Viewer()
92 Renames curve if it is found
94 void SPlot2d_Viewer::rename( const Handle(SALOME_InteractiveObject)& IObject,
95 const QString& newName, Plot2d_ViewFrame* fr )
97 Plot2d_ViewFrame* aViewFrame = fr ? fr : getActiveViewFrame();
101 CurveDict aCurves = aViewFrame->getCurves();
102 CurveDict::Iterator it = aCurves.begin();
103 for( ; it != aCurves.end(); ++it )
105 SPlot2d_Curve* aCurve = dynamic_cast<SPlot2d_Curve*>( it.value() );
106 if( aCurve && aCurve->hasIO() && aCurve->getIO()->isSame( IObject ) )
108 aCurve->setVerTitle( newName );
109 it.key()->setTitle( newName );
112 if( aCurve && aCurve->hasTableIO() && aCurve->getTableIO()->isSame( IObject ) )
113 aCurve->getTableIO()->setName( newName.toLatin1() );
115 aViewFrame->updateTitles();
119 Renames all copies of object in all view windows
120 \param IObj - object to be renamed
121 \param name - new name
123 void SPlot2d_Viewer::renameAll( const Handle(SALOME_InteractiveObject)& IObj, const QString& name )
125 SUIT_ViewManager* vm = getViewManager();
128 const QVector<SUIT_ViewWindow*>& wnds = vm->getViews();
130 for ( uint i = 0; i < wnds.size(); i++ )
132 Plot2d_ViewWindow* pwnd = dynamic_cast<Plot2d_ViewWindow*>( wnds.at( i ) );
133 rename( IObj, name, pwnd->getViewFrame() );
139 Returns true if interactive object is presented in the viewer
141 bool SPlot2d_Viewer::isInViewer( const Handle(SALOME_InteractiveObject)& IObject )
143 Plot2d_ViewFrame* aViewFrame = getActiveViewFrame();
144 if(aViewFrame == NULL) return 0;
146 if( getCurveByIO( IObject ) != NULL )
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))
164 Actually this method just re-displays curves which refers to the <IObject>
166 void SPlot2d_Viewer::Display( const Handle(SALOME_InteractiveObject)& IObject, bool update )
168 Plot2d_ViewFrame* aViewFrame = getActiveViewFrame();
169 if(aViewFrame == NULL) return;
171 SPlot2d_Curve* curve = getCurveByIO( IObject );
173 aViewFrame->updateCurve( curve, update );
177 Actually this method just erases all curves which don't refer to <IOBject>
178 and re-displays curve which is of <IObject>
180 void SPlot2d_Viewer::DisplayOnly( const Handle(SALOME_InteractiveObject)& IObject )
182 Plot2d_ViewFrame* aViewFrame = getActiveViewFrame();
183 if(aViewFrame == NULL) return;
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 );
192 aViewFrame->updateCurve( curve, false );
195 aViewFrame->Repaint();
199 Removes from the viewer the curves which refer to <IObject>
201 void SPlot2d_Viewer::Erase( const Handle(SALOME_InteractiveObject)& IObject, bool update )
203 Plot2d_ViewFrame* aViewFrame = getActiveViewFrame();
204 if(aViewFrame == NULL) return;
206 SPlot2d_Curve* curve = getCurveByIO( IObject );
208 aViewFrame->eraseCurve( curve, update );
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());
215 _PTR(ChildIterator) aIter = aStudy->NewChildIterator( aSO );
216 for ( ; aIter->More(); aIter->Next() ) {
217 _PTR(SObject) aChildSO = aIter->Value();
219 if ( aChildSO->ReferencedObject( refSO ) && refSO )
221 curve = getCurveByIO( new SALOME_InteractiveObject( aChildSO->GetID().c_str(), "") );
223 aViewFrame->eraseCurve( curve, update );
231 Removes all curves from the view
233 void SPlot2d_Viewer::EraseAll(SALOME_Displayer* d, const bool forced)
235 Plot2d_ViewFrame* aViewFrame = getActiveViewFrame();
236 if(aViewFrame) aViewFrame->EraseAll();
237 SALOME_View::EraseAll(d, forced);
241 Redraws Viewer contents
243 void SPlot2d_Viewer::Repaint()
245 Plot2d_ViewFrame* aViewFrame = getActiveViewFrame();
246 if(aViewFrame) aViewFrame->Repaint();
252 void SPlot2d_Viewer::Display( const SALOME_Prs2d* prs )
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);
262 void SPlot2d_Viewer::Erase( const SALOME_Prs2d* prs, const bool )
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);
270 Create presentation by entry
272 SALOME_Prs* SPlot2d_Viewer::CreatePrs( const char* entry )
274 Plot2d_ViewFrame* aViewFrame = getActiveViewFrame();
275 SPlot2d_Prs *prs = new SPlot2d_Prs( entry );
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();
285 (aCurve->hasIO() && !strcmp( aCurve->getIO()->getEntry(), entry )) ||
286 (aCurve->hasTableIO() && !strcmp( aCurve->getTableIO()->getEntry(), entry )) ||
287 owners.contains(entry)
289 prs->AddObject(aCurve);
298 Returns true if interactive object is presented in the viewer and displayed
300 bool SPlot2d_Viewer::isVisible( const Handle(SALOME_InteractiveObject)& IObject )
302 Plot2d_ViewFrame* aViewFrame = getActiveViewFrame();
303 if(aViewFrame == NULL) return false;
305 SPlot2d_Curve* curve = getCurveByIO( IObject );
306 return aViewFrame->isVisible( curve );
310 \Collect objects visible in viewer
311 \param theList - visible objects collection
313 void SPlot2d_Viewer::GetVisible( SALOME_ListIO& theList )
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() );
327 Return interactive obeject if is presented in the viewer
329 Handle(SALOME_InteractiveObject) SPlot2d_Viewer::FindIObject( const char* Entry )
331 Handle(SALOME_InteractiveObject) anIO;
332 Plot2d_ViewFrame* aViewFrame = getActiveViewFrame();
333 if(aViewFrame == NULL) return anIO;
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();
348 Returns an active Plot2d ViewFrame or NULL
350 Plot2d_ViewFrame* SPlot2d_Viewer::getActiveViewFrame()
352 SUIT_ViewManager* aViewMgr = getViewManager();
354 Plot2d_ViewWindow* aViewWnd = dynamic_cast<Plot2d_ViewWindow*>(aViewMgr->getActiveView());
356 return aViewWnd->getViewFrame();
363 \return curve by object and viewframe
364 \param theIObject - object
365 \param fr - viewframe
367 SPlot2d_Curve* SPlot2d_Viewer::getCurveByIO( const Handle(SALOME_InteractiveObject)& theIObject,
368 Plot2d_ViewFrame* fr )
370 if ( !theIObject.IsNull() ) {
371 Plot2d_ViewFrame* aViewFrame = fr ? fr : getActiveViewFrame();
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() );
378 if ( aCurve->hasIO() && aCurve->getIO()->isSame( theIObject ) )
388 create SPlot2d_ViewWindow
390 SUIT_ViewWindow* SPlot2d_Viewer::createView( SUIT_Desktop* theDesktop )
392 SPlot2d_ViewWindow* aPlot2dView = new SPlot2d_ViewWindow(theDesktop, this);
393 aPlot2dView->initLayout();
395 aPlot2dView->getViewFrame()->Display(getPrs());
400 SLOT: called when action "Legend Clicked" is activated.
401 override "onLegendClicked" method from Plot2d_ViewModel.
403 void SPlot2d_Viewer::onClicked( const QVariant& itemInfo, int index )
405 Plot2d_ViewFrame* aViewFrame = getActiveViewFrame();
406 if(aViewFrame == NULL) return;
408 QwtPlotItem* plotItem = aViewFrame->getPlot()->infoToItem( itemInfo );
410 bool isAnalytical = false;
411 AnalyticalCurveList curves = aViewFrame->getAnalyticalCurves();
412 foreach ( Plot2d_AnalyticalCurve* curve, curves ) {
413 if(plotItem == curve->plotItem()) {
415 curve->setSelected(true);
417 curve->setSelected(false);
421 myDeselectAnalytical = false;
422 emit clearSelected();
423 aViewFrame->updateAnalyticalCurves();
424 myDeselectAnalytical = true;
428 Plot2d_Object* anObject = aViewFrame->getPlotObject(plotItem);
432 // Highlight object in Object Browser
434 if(SPlot2d_Curve* aSCurve = dynamic_cast<SPlot2d_Curve*>(anObject)) {
436 anEntry = aSCurve->getIO()->getEntry();
437 } else if( SPlot2d_Histogram* aSHisto = dynamic_cast<SPlot2d_Histogram*>(anObject)) {
439 anEntry = aSHisto->getIO()->getEntry();
442 if(!anEntry.isEmpty())
443 emit legendSelected( anEntry );
450 void SPlot2d_Viewer::setObjectsSelected( SALOME_ListIO& theList ) {
451 Plot2d_ViewFrame* aViewFrame = getActiveViewFrame();
454 objectList allObjects;
455 aViewFrame->getObjects( allObjects );
457 bool isSelected = false;
458 SPlot2d_Histogram* h = 0;
461 foreach ( Plot2d_Object* o, allObjects ) {
464 Handle(SALOME_InteractiveObject) io;
465 if( (h = dynamic_cast<SPlot2d_Histogram*>(o)) && h->hasIO() ) {
467 } else if((c = dynamic_cast<SPlot2d_Curve*>(o)) && c->hasIO()) {
473 SALOME_ListIteratorOfListIO anIter( theList );
475 for( ; anIter.More(); anIter.Next() ) {
476 if ( anIter.Value()->hasEntry() ) {
477 if( io->isSame(anIter.Value()) ) {
478 isSelected = o->isSelected();
480 o->setSelected(true);
481 aViewFrame->updateObject(o);
482 theList.Remove(anIter);
490 if( !isSelected && o->isSelected() != false ) {
491 o->setSelected(false);
492 aViewFrame->updateObject(o);
495 if( myDeselectAnalytical ) {
496 aViewFrame->deselectAnalyticalCurves();
497 aViewFrame->updateAnalyticalCurves();
499 aViewFrame->Repaint();