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(const bool /*forced*/)
235 Plot2d_ViewFrame* aViewFrame = getActiveViewFrame();
236 if(aViewFrame) aViewFrame->EraseAll();
240 Redraws Viewer contents
242 void SPlot2d_Viewer::Repaint()
244 Plot2d_ViewFrame* aViewFrame = getActiveViewFrame();
245 if(aViewFrame) aViewFrame->Repaint();
251 void SPlot2d_Viewer::Display( const SALOME_Prs2d* prs )
253 Plot2d_ViewFrame* aViewFrame = getActiveViewFrame();
254 Plot2d_Prs* aPrs = dynamic_cast<Plot2d_Prs*>(const_cast<SALOME_Prs2d*>(prs));
255 if(aViewFrame && aPrs) aViewFrame->Display(aPrs);
261 void SPlot2d_Viewer::Erase( const SALOME_Prs2d* prs, const bool )
263 Plot2d_ViewFrame* aViewFrame = getActiveViewFrame();
264 Plot2d_Prs* aPrs = dynamic_cast<Plot2d_Prs*>(const_cast<SALOME_Prs2d*>(prs));
265 if(aViewFrame && aPrs) aViewFrame->Erase(aPrs);
269 Create presentation by entry
271 SALOME_Prs* SPlot2d_Viewer::CreatePrs( const char* entry )
273 Plot2d_ViewFrame* aViewFrame = getActiveViewFrame();
274 SPlot2d_Prs *prs = new SPlot2d_Prs();
277 CurveDict aCurves = aViewFrame->getCurves();
278 CurveDict::Iterator it = aCurves.begin();
279 for( ; it != aCurves.end(); ++it ) {
280 SPlot2d_Curve* aCurve = dynamic_cast<SPlot2d_Curve*>(it.value());
281 OwnerSet owners = aCurve->getOwners();
284 (aCurve->hasIO() && !strcmp( aCurve->getIO()->getEntry(), entry )) ||
285 (aCurve->hasTableIO() && !strcmp( aCurve->getTableIO()->getEntry(), entry )) ||
286 owners.contains(entry)
288 prs->AddObject(aCurve);
297 Returns true if interactive object is presented in the viewer and displayed
299 bool SPlot2d_Viewer::isVisible( const Handle(SALOME_InteractiveObject)& IObject )
301 Plot2d_ViewFrame* aViewFrame = getActiveViewFrame();
302 if(aViewFrame == NULL) return false;
304 SPlot2d_Curve* curve = getCurveByIO( IObject );
305 return aViewFrame->isVisible( curve );
309 \Collect objects visible in viewer
310 \param theList - visible objects collection
312 void SPlot2d_Viewer::GetVisible( SALOME_ListIO& theList )
314 Plot2d_ViewFrame* aViewFrame = getActiveViewFrame();
315 if(aViewFrame == NULL) return;
316 CurveDict aCurves = aViewFrame->getCurves();
317 CurveDict::Iterator it = aCurves.begin();
318 for( ; it != aCurves.end(); ++it ) {
319 SPlot2d_Curve* aCurve = dynamic_cast<SPlot2d_Curve*>(it.value());
320 if ( aCurve && aCurve->hasIO() && aViewFrame->isVisible( aCurve ) )
321 theList.Append( aCurve->getIO() );
326 Return interactive obeject if is presented in the viewer
328 Handle(SALOME_InteractiveObject) SPlot2d_Viewer::FindIObject( const char* Entry )
330 Handle(SALOME_InteractiveObject) anIO;
331 Plot2d_ViewFrame* aViewFrame = getActiveViewFrame();
332 if(aViewFrame == NULL) return anIO;
334 CurveDict aCurves = aViewFrame->getCurves();
335 CurveDict::Iterator it = aCurves.begin();
336 for( ; it != aCurves.end(); ++it ) {
337 SPlot2d_Curve* aCurve = dynamic_cast<SPlot2d_Curve*>(it.value());
338 if ( aCurve && aCurve->hasIO() && !strcmp( aCurve->getIO()->getEntry(), Entry ) ) {
339 anIO = aCurve->getIO();
347 Returns an active Plot2d ViewFrame or NULL
349 Plot2d_ViewFrame* SPlot2d_Viewer::getActiveViewFrame()
351 SUIT_ViewManager* aViewMgr = getViewManager();
353 Plot2d_ViewWindow* aViewWnd = dynamic_cast<Plot2d_ViewWindow*>(aViewMgr->getActiveView());
355 return aViewWnd->getViewFrame();
362 \return curve by object and viewframe
363 \param theIObject - object
364 \param fr - viewframe
366 SPlot2d_Curve* SPlot2d_Viewer::getCurveByIO( const Handle(SALOME_InteractiveObject)& theIObject,
367 Plot2d_ViewFrame* fr )
369 if ( !theIObject.IsNull() ) {
370 Plot2d_ViewFrame* aViewFrame = fr ? fr : getActiveViewFrame();
372 CurveDict aCurves = aViewFrame->getCurves();
373 CurveDict::Iterator it = aCurves.begin();
374 for( ; it != aCurves.end(); ++it ) {
375 SPlot2d_Curve* aCurve = dynamic_cast<SPlot2d_Curve*>( it.value() );
377 if ( aCurve->hasIO() && aCurve->getIO()->isSame( theIObject ) )
387 create SPlot2d_ViewWindow
389 SUIT_ViewWindow* SPlot2d_Viewer::createView( SUIT_Desktop* theDesktop )
391 SPlot2d_ViewWindow* aPlot2dView = new SPlot2d_ViewWindow(theDesktop, this);
392 aPlot2dView->initLayout();
394 aPlot2dView->getViewFrame()->Display(getPrs());
399 SLOT: called when action "Legend Clicked" is activated.
400 override "onLegendClicked" method from Plot2d_ViewModel.
402 void SPlot2d_Viewer::onClicked( const QVariant& itemInfo, int index )
404 Plot2d_ViewFrame* aViewFrame = getActiveViewFrame();
405 if(aViewFrame == NULL) return;
407 QwtPlotItem* plotItem = aViewFrame->getPlot()->infoToItem( itemInfo );
409 bool isAnalytical = false;
410 AnalyticalCurveList curves = aViewFrame->getAnalyticalCurves();
411 foreach ( Plot2d_AnalyticalCurve* curve, curves ) {
412 if(plotItem == curve->plotItem()) {
414 curve->setSelected(true);
416 curve->setSelected(false);
420 myDeselectAnalytical = false;
421 emit clearSelected();
422 aViewFrame->updateAnalyticalCurves();
423 myDeselectAnalytical = true;
427 Plot2d_Object* anObject = aViewFrame->getPlotObject(plotItem);
431 // Highlight object in Object Browser
433 if(SPlot2d_Curve* aSCurve = dynamic_cast<SPlot2d_Curve*>(anObject)) {
435 anEntry = aSCurve->getIO()->getEntry();
436 } else if( SPlot2d_Histogram* aSHisto = dynamic_cast<SPlot2d_Histogram*>(anObject)) {
438 anEntry = aSHisto->getIO()->getEntry();
441 if(!anEntry.isEmpty())
442 emit legendSelected( anEntry );
449 void SPlot2d_Viewer::setObjectsSelected( SALOME_ListIO& theList ) {
450 Plot2d_ViewFrame* aViewFrame = getActiveViewFrame();
453 objectList allObjects;
454 aViewFrame->getObjects( allObjects );
456 bool isSelected = false;
457 SPlot2d_Histogram* h = 0;
460 foreach ( Plot2d_Object* o, allObjects ) {
463 Handle(SALOME_InteractiveObject) io;
464 if( (h = dynamic_cast<SPlot2d_Histogram*>(o)) && h->hasIO() ) {
466 } else if((c = dynamic_cast<SPlot2d_Curve*>(o)) && c->hasIO()) {
472 SALOME_ListIteratorOfListIO anIter( theList );
474 for( ; anIter.More(); anIter.Next() ) {
475 if ( anIter.Value()->hasEntry() ) {
476 if( io->isSame(anIter.Value()) ) {
477 isSelected = o->isSelected();
479 o->setSelected(true);
480 aViewFrame->updateObject(o);
481 theList.Remove(anIter);
489 if( !isSelected && o->isSelected() != false ) {
490 o->setSelected(false);
491 aViewFrame->updateObject(o);
494 if( myDeselectAnalytical ) {
495 aViewFrame->deselectAnalyticalCurves();
496 aViewFrame->updateAnalyticalCurves();
498 aViewFrame->Repaint();