1 // Copyright (C) 2007-2008 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.
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
22 // File : SPlot2d_Viewer.cxx
23 // Author : Sergey RUIN
26 #include "SPlot2d_ViewModel.h"
28 #include "SPlot2d_ViewWindow.h"
30 #include "SPlot2d_Prs.h"
31 #include "SUIT_Session.h"
32 #include "SUIT_Application.h"
33 #include "SUIT_ViewManager.h"
35 #include "SALOME_ListIO.hxx"
37 #include <QApplication>
39 #include <QToolButton>
41 #include <QColorDialog>
44 #include <qwt_plot_canvas.h>
45 #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 )
86 SPlot2d_Viewer::~SPlot2d_Viewer()
91 Renames curve if it is found
93 void SPlot2d_Viewer::rename( const Handle(SALOME_InteractiveObject)& IObject,
94 const QString& newName, Plot2d_ViewFrame* fr )
96 Plot2d_ViewFrame* aViewFrame = fr ? fr : getActiveViewFrame();
100 CurveDict aCurves = aViewFrame->getCurves();
101 CurveDict::Iterator it = aCurves.begin();
102 for( ; it != aCurves.end(); ++it )
104 SPlot2d_Curve* aCurve = dynamic_cast<SPlot2d_Curve*>( it.value() );
105 if( aCurve && aCurve->hasIO() && aCurve->getIO()->isSame( IObject ) )
107 aCurve->setVerTitle( newName );
108 it.key()->setTitle( newName );
111 if( aCurve && aCurve->hasTableIO() && aCurve->getTableIO()->isSame( IObject ) )
112 aCurve->getTableIO()->setName( newName.toLatin1() );
114 aViewFrame->updateTitles();
118 Renames all copies of object in all view windows
119 \param IObj - object to be renamed
120 \param name - new name
122 void SPlot2d_Viewer::renameAll( const Handle(SALOME_InteractiveObject)& IObj, const QString& name )
124 SUIT_ViewManager* vm = getViewManager();
127 const QVector<SUIT_ViewWindow*>& wnds = vm->getViews();
129 for ( uint i = 0; i < wnds.size(); i++ )
131 Plot2d_ViewWindow* pwnd = dynamic_cast<Plot2d_ViewWindow*>( wnds.at( i ) );
132 rename( IObj, name, pwnd->getViewFrame() );
138 Returns true if interactive object is presented in the viewer
140 bool SPlot2d_Viewer::isInViewer( const Handle(SALOME_InteractiveObject)& IObject )
142 Plot2d_ViewFrame* aViewFrame = getActiveViewFrame();
143 if(aViewFrame == NULL) return 0;
145 if( getCurveByIO( IObject ) != NULL )
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))
163 Actually this method just re-displays curves which refers to the <IObject>
165 void SPlot2d_Viewer::Display( const Handle(SALOME_InteractiveObject)& IObject, bool update )
167 Plot2d_ViewFrame* aViewFrame = getActiveViewFrame();
168 if(aViewFrame == NULL) return;
170 SPlot2d_Curve* curve = getCurveByIO( IObject );
172 aViewFrame->updateCurve( curve, update );
176 Actually this method just erases all curves which don't refer to <IOBject>
177 and re-displays curve which is of <IObject>
179 void SPlot2d_Viewer::DisplayOnly( const Handle(SALOME_InteractiveObject)& IObject )
181 Plot2d_ViewFrame* aViewFrame = getActiveViewFrame();
182 if(aViewFrame == NULL) return;
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 );
191 aViewFrame->updateCurve( curve, false );
194 aViewFrame->Repaint();
198 Removes from the viewer the curves which refer to <IObject>
200 void SPlot2d_Viewer::Erase( const Handle(SALOME_InteractiveObject)& IObject, bool update )
202 Plot2d_ViewFrame* aViewFrame = getActiveViewFrame();
203 if(aViewFrame == NULL) return;
205 SPlot2d_Curve* curve = getCurveByIO( IObject );
207 aViewFrame->eraseCurve( curve, update );
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());
214 _PTR(ChildIterator) aIter = aStudy->NewChildIterator( aSO );
215 for ( ; aIter->More(); aIter->Next() ) {
216 _PTR(SObject) aChildSO = aIter->Value();
218 if ( aChildSO->ReferencedObject( refSO ) && refSO )
220 curve = getCurveByIO( new SALOME_InteractiveObject( aChildSO->GetID().c_str(), "") );
222 aViewFrame->eraseCurve( curve, update );
230 Removes all curves from the view
232 void SPlot2d_Viewer::EraseAll(const bool /*forced*/)
234 Plot2d_ViewFrame* aViewFrame = getActiveViewFrame();
235 if(aViewFrame) aViewFrame->EraseAll();
239 Redraws Viewer contents
241 void SPlot2d_Viewer::Repaint()
243 Plot2d_ViewFrame* aViewFrame = getActiveViewFrame();
244 if(aViewFrame) aViewFrame->Repaint();
250 void SPlot2d_Viewer::Display( const SALOME_Prs2d* prs )
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);
260 void SPlot2d_Viewer::Erase( const SALOME_Prs2d* prs, const bool )
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);
268 Create presentation by entry
270 SALOME_Prs* SPlot2d_Viewer::CreatePrs( const char* entry )
272 Plot2d_ViewFrame* aViewFrame = getActiveViewFrame();
275 Plot2d_Prs* prs = aViewFrame->CreatePrs(entry);
277 return new SPlot2d_Prs( prs );
284 Axiluary method called before displaying of objects
286 void SPlot2d_Viewer::BeforeDisplay( SALOME_Displayer* d )
288 d->BeforeDisplay( this, SALOME_Plot2dViewType() );
292 Axiluary method called after displaying of objects
294 void SPlot2d_Viewer::AfterDisplay( SALOME_Displayer* d )
296 d->AfterDisplay( this, SALOME_Plot2dViewType() );
300 Returns true if interactive object is presented in the viewer and displayed
302 bool SPlot2d_Viewer::isVisible( const Handle(SALOME_InteractiveObject)& IObject )
304 Plot2d_ViewFrame* aViewFrame = getActiveViewFrame();
305 if(aViewFrame == NULL) return false;
307 SPlot2d_Curve* curve = getCurveByIO( IObject );
308 return aViewFrame->isVisible( curve );
312 Return interactive obeject if is presented in the viewer
314 Handle(SALOME_InteractiveObject) SPlot2d_Viewer::FindIObject( const char* Entry )
316 Handle(SALOME_InteractiveObject) anIO;
317 Plot2d_ViewFrame* aViewFrame = getActiveViewFrame();
318 if(aViewFrame == NULL) return anIO;
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();
333 Returns an active Plot2d ViewFrame or NULL
335 Plot2d_ViewFrame* SPlot2d_Viewer::getActiveViewFrame()
337 SUIT_ViewManager* aViewMgr = getViewManager();
339 Plot2d_ViewWindow* aViewWnd = dynamic_cast<Plot2d_ViewWindow*>(aViewMgr->getActiveView());
341 return aViewWnd->getViewFrame();
348 \return curve by object and viewframe
349 \param theIObject - object
350 \param fr - viewframe
352 SPlot2d_Curve* SPlot2d_Viewer::getCurveByIO( const Handle(SALOME_InteractiveObject)& theIObject,
353 Plot2d_ViewFrame* fr )
355 if ( !theIObject.IsNull() ) {
356 Plot2d_ViewFrame* aViewFrame = fr ? fr : getActiveViewFrame();
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() );
363 if ( aCurve->hasIO() && aCurve->getIO()->isSame( theIObject ) )
373 create SPlot2d_ViewWindow
375 SUIT_ViewWindow* SPlot2d_Viewer::createView( SUIT_Desktop* theDesktop )
377 SPlot2d_ViewWindow* aPlot2dView = new SPlot2d_ViewWindow(theDesktop, this);
379 aPlot2dView->getViewFrame()->Display(getPrs());
384 SLOT: called when action "Legend Clicked" is activated.
385 override "onLegendClicked" method from Plot2d_ViewModel.
387 void SPlot2d_Viewer::onLegendClicked( QwtPlotItem* plotItem )
389 Plot2d_ViewFrame* aViewFrame = getActiveViewFrame();
390 if(aViewFrame == NULL) return;
392 CurveDict aCurves = aViewFrame->getCurves();
393 SPlot2d_Curve* aSCurve;
394 CurveDict::Iterator it = aCurves.begin();
395 for( ; it != aCurves.end(); ++it )
397 if ( it.key() == plotItem ) {
398 aSCurve = dynamic_cast<SPlot2d_Curve*>( it.value() );
402 // Highlight curve in Object Browser
403 if(aSCurve && aSCurve->hasIO()) {
404 QString anEntry = aSCurve->getIO()->getEntry();
405 emit legendSelected( anEntry );