Salome HOME
ptv, do not send mouse release event just after mouse double click
[modules/gui.git] / src / SPlot2d / SPlot2d_ViewModel.cxx
1 //  File   : SPlot2d_Viewer.cxx
2 //  Author : Sergey RUIN
3 //  Module : SUIT
4
5 #include "SPlot2d_ViewModel.h"
6
7 #include "SPlot2d_Prs.h"
8 #include "SUIT_Session.h"
9 #include "SUIT_Application.h"
10
11 #include "utilities.h"
12 #include "qapplication.h"
13 #include <qtoolbar.h>
14 #include <qtoolbutton.h>
15 #include <qcursor.h>
16 #include <qcolordialog.h>
17 #include <qwt_math.h>
18 #include <qwt_plot_canvas.h>
19 #include <stdlib.h>
20 #include "utilities.h"
21
22 using namespace std;
23
24 #include "SALOMEDSClient.hxx"
25 #include "SALOMEDS_StudyManager.hxx"
26
27 // in order NOT TO link with SalomeApp, here the code returns SALOMEDS_Study.
28 // SalomeApp_Study::studyDS() does it as well, but -- here it is retrieved from
29 // SALOMEDS::StudyManager - no linkage with SalomeApp.  
30
31 static _PTR(Study) getStudyDS()
32 {
33   SALOMEDSClient_Study* aStudy = NULL;
34   _PTR(StudyManager) aMgr( new SALOMEDS_StudyManager() );
35
36   // get id of SUIT_Study, if it's a SalomeApp_Study, it will return
37   //    id of its underlying SALOMEDS::Study
38   SUIT_Application* app = SUIT_Session::session()->activeApplication();
39   if ( !app )  return _PTR(Study)(aStudy);
40   SUIT_Study* stud = app->activeStudy();
41   if ( !stud ) return _PTR(Study)(aStudy);
42   const int id = stud->id(); // virtual method, must return SALOMEDS_Study id
43   // get SALOMEDS_Study with this id from StudyMgr
44   return aMgr->GetStudyByID( id );
45 }                
46
47 //=================================================================================
48 // SPlot2d_Viewer implementation
49 //=================================================================================
50
51 /*!
52   Constructor
53 */
54 SPlot2d_Viewer::SPlot2d_Viewer(  bool theAutoDel )
55 : Plot2d_Viewer( theAutoDel )    
56 {
57 }
58
59 /*!
60   Destructor
61 */
62 SPlot2d_Viewer::~SPlot2d_Viewer()
63 {
64 }
65
66 /*!
67   Renames curve if it is found
68 */
69 void SPlot2d_Viewer::rename( const Handle(SALOME_InteractiveObject)& IObject, QString newName ) 
70 {
71   Plot2d_ViewFrame* aViewFrame = getActiveViewFrame();
72   if(aViewFrame == NULL) return;
73
74   Plot2d_Curve* curve = getCurveByIO( IObject );
75   if ( curve ) {
76     curve->setVerTitle( newName );
77     int key = aViewFrame->hasCurve( curve );
78     if ( key ) {
79       aViewFrame->setCurveTitle( key, newName );
80     }
81   }
82 }
83
84 /*!
85   Returns true if interactive object is presented in the viewer
86 */
87 bool SPlot2d_Viewer::isInViewer( const Handle(SALOME_InteractiveObject)& IObject ) 
88 {
89   Plot2d_ViewFrame* aViewFrame = getActiveViewFrame();
90   if(aViewFrame == NULL) return 0;
91
92   if( getCurveByIO( IObject ) != NULL )
93     return 1;
94   else{
95     if(!IObject.IsNull()){
96       QIntDictIterator<Plot2d_Curve> it(aViewFrame->getCurves());
97       for(; it.current();++it) {
98         SPlot2d_Curve* aCurve = dynamic_cast<SPlot2d_Curve*>(it.current()); 
99         if(aCurve->hasIO() && aCurve->getTableIO()->isSame(IObject))
100           return 1;
101       }
102     }
103   }
104   return 0;
105 }
106
107
108 /*!
109   Actually this method just re-displays curves which refers to the <IObject>
110 */
111 void SPlot2d_Viewer::Display( const Handle(SALOME_InteractiveObject)& IObject, bool update )
112 {
113   Plot2d_ViewFrame* aViewFrame = getActiveViewFrame();
114   if(aViewFrame == NULL) return;
115
116   SPlot2d_Curve* curve = getCurveByIO( IObject );
117   if ( curve )
118     aViewFrame->updateCurve( curve, update );
119 }
120
121 /*!
122   Actually this method just erases all curves which don't refer to <IOBject> 
123   and re-displays curve which is of <IObject>
124 */
125 void SPlot2d_Viewer::DisplayOnly( const Handle(SALOME_InteractiveObject)& IObject )
126 {
127   Plot2d_ViewFrame* aViewFrame = getActiveViewFrame();
128   if(aViewFrame == NULL) return;
129
130   Plot2d_Curve* curve = getCurveByIO( IObject );
131   QIntDictIterator<Plot2d_Curve> it( aViewFrame->getCurves() );
132   for ( ; it.current(); ++it ) {
133     if(it.current() != curve)
134       aViewFrame->eraseCurve( curve );
135     else
136       aViewFrame->updateCurve( curve, false );
137   }
138
139   aViewFrame->Repaint();
140 }
141
142 /*!
143   Removes from the viewer the curves which refer to <IObject>
144 */
145 void SPlot2d_Viewer::Erase( const Handle(SALOME_InteractiveObject)& IObject, bool update )
146 {
147   Plot2d_ViewFrame* aViewFrame = getActiveViewFrame();
148   if(aViewFrame == NULL) return;
149
150   SPlot2d_Curve* curve = getCurveByIO( IObject );
151   if ( curve )
152     aViewFrame->eraseCurve( curve, update );
153
154   // it can be table or container object selected
155   _PTR(Study) aStudy = getStudyDS();
156   _PTR(SObject) aSO = aStudy->FindObjectID(IObject->getEntry());
157   if ( aSO ) {
158     _PTR(ChildIterator) aIter = aStudy->NewChildIterator( aSO );
159     for ( ; aIter->More(); aIter->Next() ) {
160       _PTR(SObject) aChildSO = aIter->Value();
161       _PTR(SObject) refSO;
162       if ( aChildSO->ReferencedObject( refSO ) && refSO )
163         aChildSO = refSO;
164       curve = getCurveByIO( new SALOME_InteractiveObject( aChildSO->GetID().c_str(), "") );
165       if ( curve )
166         aViewFrame->eraseCurve( curve, update );
167     }
168   }
169
170
171 }
172
173 /*!
174    Removes all curves from the view
175 */
176 void SPlot2d_Viewer::EraseAll(const bool /*forced*/) 
177 {
178   Plot2d_ViewFrame* aViewFrame = getActiveViewFrame();
179   if(aViewFrame) aViewFrame->EraseAll();
180 }
181
182 /*!
183   Redraws Viewer contents
184 */
185 void SPlot2d_Viewer::Repaint()
186 {
187   Plot2d_ViewFrame* aViewFrame = getActiveViewFrame();
188   if(aViewFrame) aViewFrame->Repaint();
189 }
190
191 //==========================================================
192 /*!
193  *  SPlot2d_Viewer::Display
194  *  Display presentation
195  */
196 //==========================================================
197 void SPlot2d_Viewer::Display( const SALOME_Prs2d* prs )
198 {
199   Plot2d_ViewFrame* aViewFrame = getActiveViewFrame();
200   Plot2d_Prs* aPrs = dynamic_cast<Plot2d_Prs*>(const_cast<SALOME_Prs2d*>(prs));
201   if(aViewFrame && aPrs) aViewFrame->Display(aPrs);
202 }
203
204 //==========================================================
205 /*!
206  *  SPlot2d_Viewer::Erase
207  *  Erase presentation
208  */
209 //==========================================================
210 void SPlot2d_Viewer::Erase( const SALOME_Prs2d* prs, const bool )
211 {
212   Plot2d_ViewFrame* aViewFrame = getActiveViewFrame();
213   Plot2d_Prs* aPrs = dynamic_cast<Plot2d_Prs*>(const_cast<SALOME_Prs2d*>(prs));
214   if(aViewFrame && aPrs) aViewFrame->Erase(aPrs);
215 }
216   
217 //==========================================================
218 /*!
219  *  SPlot2d_Viewer::CreatePrs
220  *  Create presentation by entry
221  */
222 //==========================================================
223 SALOME_Prs* SPlot2d_Viewer::CreatePrs( const char* entry )
224 {
225   Plot2d_ViewFrame* aViewFrame = getActiveViewFrame();
226   if(aViewFrame) {
227     return new SPlot2d_Prs(aViewFrame->CreatePrs(entry));
228   }
229
230   return NULL;
231 }
232
233 //==========================================================
234 /*!
235  *  SPlot2d_Viewer::BeforeDisplay
236  *  Axiluary method called before displaying of objects
237  */
238 //==========================================================
239 void  SPlot2d_Viewer::BeforeDisplay( SALOME_Displayer* d )
240 {
241   d->BeforeDisplay( this, SALOME_Plot2dViewType() );
242 }
243
244 //==========================================================
245 /*!
246  *  SPlot2d_Viewer::AfterDisplay
247  *  Axiluary method called after displaying of objects
248  */
249 //==========================================================
250 void  SPlot2d_Viewer::AfterDisplay( SALOME_Displayer* d )
251 {
252   d->AfterDisplay( this, SALOME_Plot2dViewType() );
253 }
254
255 /*!
256   Returns true if interactive object is presented in the viewer and displayed
257 */
258 bool SPlot2d_Viewer::isVisible( const Handle(SALOME_InteractiveObject)& IObject ) 
259 {
260   Plot2d_ViewFrame* aViewFrame = getActiveViewFrame();
261   if(aViewFrame == NULL) return false;
262
263   SPlot2d_Curve* curve = getCurveByIO( IObject );
264   return aViewFrame->isVisible( curve );
265 }
266
267 /*!
268   Return interactive obeject if is presented in the viewer
269 */
270 Handle(SALOME_InteractiveObject) SPlot2d_Viewer::FindIObject( const char* Entry )
271 {
272   Handle(SALOME_InteractiveObject) anIO;
273   Plot2d_ViewFrame* aViewFrame = getActiveViewFrame();
274   if(aViewFrame == NULL) return anIO;
275
276   QIntDictIterator<Plot2d_Curve> it( aViewFrame->getCurves() );
277   for ( ; it.current(); ++it ) {
278     SPlot2d_Curve* aCurve = dynamic_cast<SPlot2d_Curve*>(it.current()); 
279     if ( aCurve->hasIO() && !strcmp( aCurve->getIO()->getEntry(), Entry ) ) {
280       anIO = aCurve->getIO();
281       break;
282     }
283   }
284   return anIO;
285 }
286
287 //==========================================================
288 /*!
289  *  SPlot2d_Viewer::getActiveViewFrame
290  *  Returns an active Plot2d ViewFrame or NULL
291  */
292 //==========================================================
293 Plot2d_ViewFrame* SPlot2d_Viewer::getActiveViewFrame()
294 {
295   SUIT_ViewManager* aViewMgr = getViewManager();
296   if(aViewMgr) {
297     Plot2d_ViewWindow* aViewWnd = dynamic_cast<Plot2d_ViewWindow*>(aViewMgr->getActiveView());
298     if(aViewWnd)
299       return aViewWnd->getViewFrame();
300   }
301
302   return NULL;
303 }
304
305 SPlot2d_Curve* SPlot2d_Viewer::getCurveByIO( const Handle(SALOME_InteractiveObject)& theIObject )
306 {
307   if ( !theIObject.IsNull() ) {
308     Plot2d_ViewFrame* aViewFrame = getActiveViewFrame();
309     if(aViewFrame) {
310       QIntDictIterator<Plot2d_Curve> it( aViewFrame->getCurves() );
311       for ( ; it.current(); ++it ) {
312         SPlot2d_Curve* aCurve = dynamic_cast<SPlot2d_Curve*>(it.current()); 
313         if(aCurve) {
314           if ( aCurve->hasIO() && aCurve->getIO()->isSame( theIObject ) )
315             return aCurve;
316         }
317       }
318     }
319   }
320   return NULL;
321 }
322