Salome HOME
Merge branch 'hydro/imps_2017'
[modules/geom.git] / src / GEOMGUI / GEOMGUI_AnnotationMgr.cxx
1 // Copyright (C) 2007-2016  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "GEOMGUI_AnnotationMgr.h"
21
22 #include <GEOMGUI_AnnotationAttrs.h>
23 #include <GEOM_Annotation.hxx>
24 #include <GEOM_Client.hxx>
25 #include <GEOM_Constants.h>
26 #include <GEOM_Displayer.h>
27 #include <GEOMGUI_TextTreeWdg.h>
28
29 #include <GeometryGUI.h>
30
31 #include <SalomeApp_Application.h>
32 #include <SalomeApp_Study.h>
33 #include <SALOME_Prs.h>
34
35 #include <SUIT_ResourceMgr.h>
36 #include <SUIT_Session.h>
37 #include <SUIT_Desktop.h>
38 #include <SUIT_ViewWindow.h>
39 #include <SUIT_ViewManager.h>
40
41 #include <SOCC_Prs.h>
42 #include <SOCC_ViewWindow.h>
43
44 #include <Quantity_Color.hxx>
45 #include <TCollection_AsciiString.hxx>
46
47 #include <TopExp.hxx>
48 #include <TopoDS_Shape.hxx>
49 #include <TopTools_IndexedMapOfShape.hxx>
50 #include <gp_Ax3.hxx>
51 #include <AIS_ListIteratorOfListOfInteractive.hxx>
52
53 #include <QFont>
54 #include <QColor>
55
56
57 GEOMGUI_AnnotationMgr::GEOMGUI_AnnotationMgr( SalomeApp_Application* theApplication )
58 : myApplication( theApplication )
59 {
60 }
61
62 QString GEOMGUI_AnnotationMgr::GetEntrySeparator()
63 {
64         return "_annotation:";
65 }
66
67 //================================================================
68 // Function : CreatePresentation
69 // Purpose  :
70 //================================================================
71 SALOME_Prs* GEOMGUI_AnnotationMgr::CreatePresentation( const GEOMGUI_AnnotationAttrs::Properties& theProperty,
72                                                        GEOM::GEOM_Object_ptr theObject,
73                                                        SOCC_Viewer* theView,
74                                                        const QString& theEntry )
75 {
76   SOCC_Viewer* aView = viewOrActiveView( theView );
77   if ( !aView ) {
78     return NULL;
79   }
80
81   Handle ( GEOM_Annotation ) aPresentation = new GEOM_Annotation();
82   if ( !theEntry.isEmpty() ) {
83     // owner should be set to provide selection mechanizm
84     Handle( SALOME_InteractiveObject ) anIO = new SALOME_InteractiveObject();
85     anIO->setEntry( theEntry.toLatin1().constData() );
86     aPresentation->SetOwner( anIO );
87   }
88
89   aPresentation->SetIsScreenFixed( theProperty.IsScreenFixed );
90
91   TopoDS_Shape aShape = GEOM_Client::get_client().GetShape( GeometryGUI::GetGeomGen(), theObject );
92   if ( !aShape.IsNull() ) {
93
94     gp_Ax3 aShapeLCS = gp_Ax3().Transformed( aShape.Location().Transformation() );
95
96     GEOMGUI_AnnotationAttrs::SetupPresentation( aPresentation, theProperty, aShapeLCS );
97
98     if ( theProperty.ShapeType == TopAbs_SHAPE ) {
99
100       aPresentation->SetHilightShape( aShape );
101     }
102     else if ( theProperty.ShapeIndex > 0 ) {
103
104       TopTools_IndexedMapOfShape aSubShapeMap;
105       TopExp::MapShapes( aShape, static_cast<TopAbs_ShapeEnum>( theProperty.ShapeType ), aSubShapeMap );
106       if ( theProperty.ShapeIndex <= aSubShapeMap.Extent() ) {
107
108         aPresentation->SetHilightShape( aSubShapeMap( theProperty.ShapeIndex ) );
109       }
110     }
111   }
112
113   setDisplayProperties( aPresentation, aView, getEntry( theObject ).c_str() );
114
115   // add Prs to preview
116   SUIT_ViewWindow* vw = getApplication()->desktop()->activeWindow();
117   SOCC_Prs* aPrs =
118       dynamic_cast<SOCC_Prs*>( ( aView )->CreatePrs( 0 ) );
119
120   if ( aPrs )
121     aPrs->AddObject( aPresentation );
122
123   return aPrs;
124 }
125
126 bool GEOMGUI_AnnotationMgr::IsDisplayed( const QString& theEntry, const int theIndex, SOCC_Viewer* theView ) const
127 {
128   SOCC_Viewer* aView = viewOrActiveView( theView );
129   if ( !aView || !myVisualized.contains( aView ) )
130     return false;
131
132   EntryToAnnotations anEntryToAnnotation = myVisualized[aView];
133   if ( !anEntryToAnnotation.contains( theEntry ) )
134     return false;
135
136   AnnotationToPrs anAnnotationToPrs = anEntryToAnnotation[theEntry];
137   if ( !anAnnotationToPrs.contains( theIndex ) )
138     return false;
139
140   return true;
141 }
142
143 //=======================================================================
144 // function : GEOMGUI_AnnotationMgr::Display
145 // purpose  : Displays annotation shape presentation in view. It creates an annotation presentation
146 // and stores it in an internal container
147 //=======================================================================
148 void GEOMGUI_AnnotationMgr::Display( const QString& theEntry, const int theIndex, SOCC_Viewer* theView,
149                                      const bool isStoreViewState, const bool isUpdateViewer )
150 {
151   SOCC_Viewer* aView = viewOrActiveView( theView );
152   if ( !aView )
153     return;
154
155   if ( IsDisplayed( theEntry, theIndex, aView ) )
156     return;
157
158   GEOMGUI_AnnotationAttrs::Properties aProperty;
159   GEOM::GEOM_Object_ptr anObject;
160   getObject( theEntry, theIndex, anObject, aProperty );
161
162   // display presentation in the viewer
163   QString anEntry = QString("%1%2%3").arg(theEntry).arg(GetEntrySeparator()).arg(theIndex);
164   SALOME_Prs* aPrs = CreatePresentation( aProperty, anObject, aView, anEntry );
165   ((SALOME_View*)aView)->Display( getDisplayer(), aPrs );
166   if ( isUpdateViewer )
167     getDisplayer()->UpdateViewer();
168
169   EntryToAnnotations anEntryToMap;
170   if ( myVisualized.contains( aView ) )
171     anEntryToMap = myVisualized[aView];
172
173   // store displayed parameters to an internal container
174   AnnotationToPrs anAnnotationToPrsMap;
175   if ( anEntryToMap.contains( theEntry ) )
176     anAnnotationToPrsMap = anEntryToMap[theEntry];
177   anAnnotationToPrsMap[theIndex] = aPrs;
178   anEntryToMap[theEntry] = anAnnotationToPrsMap;
179   myVisualized[aView] = anEntryToMap;
180
181   if ( isStoreViewState ) {
182     // change persistent for the entry: set visible state in true for indices which presentations are shown
183     storeVisibleState( theEntry, theView );
184     storeFixedPosition( theEntry, theView );
185   }
186 }
187
188 void GEOMGUI_AnnotationMgr::Redisplay( const QString& theEntry, const int theIndex,
189                                        const GEOMGUI_AnnotationAttrs::Properties& theProperty )
190 {
191   SUIT_Session* ses = SUIT_Session::session();
192   SUIT_Application* app = ses->activeApplication();
193   if ( app )
194   {
195     SUIT_Desktop* desk = app->desktop();
196     QList<SUIT_ViewWindow*> wnds = desk->windows();
197     SUIT_ViewWindow* wnd;
198     QListIterator<SUIT_ViewWindow*> it( wnds );
199     while ( it.hasNext() && (wnd = it.next()) )
200     {
201       SUIT_ViewManager* vman = wnd->getViewManager();
202       if ( vman )
203       {
204         SUIT_ViewModel* vmodel = vman->getViewModel();
205         if ( vmodel )
206         {
207           SOCC_Viewer* aView = dynamic_cast<SOCC_Viewer*>(vmodel);
208           if ( aView )
209             Redisplay( theEntry, theIndex, theProperty, aView );
210         }
211       }
212     }
213   }
214 }
215
216 void GEOMGUI_AnnotationMgr::Redisplay( const QString& theEntry, const int theIndex,
217                                        const GEOMGUI_AnnotationAttrs::Properties& theProperty,
218                                        SOCC_Viewer* theView )
219 {
220   SOCC_Viewer* aView = viewOrActiveView( theView );
221   if ( !aView )
222     return;
223
224   if ( !myVisualized.contains( aView ) )
225     return;
226
227   EntryToAnnotations anEntryToAnnotation = myVisualized[aView];
228   if ( !anEntryToAnnotation.contains( theEntry ) )
229     return;
230
231   AnnotationToPrs anAnnotationToPrs = anEntryToAnnotation[theEntry];
232   if ( !anAnnotationToPrs.contains( theIndex ) )
233     return;
234
235   GEOMGUI_AnnotationAttrs::Properties aProperty;
236   GEOM::GEOM_Object_ptr anObject;
237   getObject( theEntry, theIndex, anObject, aProperty );
238   TopoDS_Shape aShape = GEOM_Client::get_client().GetShape( GeometryGUI::GetGeomGen(), anObject );
239   gp_Ax3 aShapeLCS = gp_Ax3().Transformed( aShape.Location().Transformation() );
240
241   // erase presentation from the viewer
242   SALOME_Prs* aPrs = anAnnotationToPrs[theIndex];
243   SOCC_Prs* anOCCPrs = dynamic_cast<SOCC_Prs*>( aPrs );
244   if ( anOCCPrs ) {
245     AIS_ListOfInteractive anIOs;
246     anOCCPrs->GetObjects( anIOs );
247     AIS_ListIteratorOfListOfInteractive anIter( anIOs );
248
249     for ( ; anIter.More(); anIter.Next() ) {
250       Handle(AIS_InteractiveObject) aPrs = anIter.Value();
251       Handle(GEOM_Annotation) aPresentation = Handle(GEOM_Annotation)::DownCast( aPrs );
252
253       GEOMGUI_AnnotationAttrs::SetupPresentation( aPresentation, theProperty, aShapeLCS );
254       aView->getAISContext()->Redisplay( aPresentation );
255     }
256   }
257 }
258
259 void GEOMGUI_AnnotationMgr::Erase( const QString& theEntry, const int theIndex, SOCC_Viewer* theView,
260                                    const bool isUpdateViewer )
261 {
262   SOCC_Viewer* aView = viewOrActiveView( theView );
263   if ( !aView )
264     return;
265
266   if ( !myVisualized.contains( aView ) )
267     return;
268
269   EntryToAnnotations anEntryToAnnotation = myVisualized[aView];
270   if ( !anEntryToAnnotation.contains( theEntry ) )
271     return;
272
273   AnnotationToPrs anAnnotationToPrs = anEntryToAnnotation[theEntry];
274   if ( !anAnnotationToPrs.contains( theIndex ) )
275     return;
276
277   // erase presentation from the viewer
278   SALOME_Prs* aPrs = anAnnotationToPrs[theIndex];
279   ((SALOME_View*)aView)->Erase( getDisplayer(), aPrs );
280   if ( isUpdateViewer )
281     getDisplayer()->UpdateViewer();
282
283   // remove displayed parameters from an internal container
284   anAnnotationToPrs.remove( theIndex );
285   anEntryToAnnotation[theEntry] = anAnnotationToPrs;
286   if (anAnnotationToPrs.isEmpty()) {
287     anEntryToAnnotation.remove( theEntry );
288   }
289   else {
290     anEntryToAnnotation[theEntry] = anAnnotationToPrs;
291   }
292   myVisualized[aView] = anEntryToAnnotation;
293
294   // change persistent for the entry: set visible state in true for indices which presentations are shown
295   storeVisibleState( theEntry, theView );
296 }
297
298 void GEOMGUI_AnnotationMgr::DisplayVisibleAnnotations( const QString& theEntry, SOCC_Viewer* theView,
299                                                        const bool isUpdateViewer )
300 {
301   SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>( getApplication()->activeStudy() );
302   _PTR(SObject) aSObj = aStudy->studyDS()->FindObjectID( theEntry.toStdString() );
303   const Handle(GEOMGUI_AnnotationAttrs) aShapeAnnotations = GEOMGUI_AnnotationAttrs::FindAttributes( aSObj );
304   if ( !aShapeAnnotations.IsNull() ) {
305     const int aCount = aShapeAnnotations->GetNbAnnotation();
306     std::vector<bool> isVisible( aCount );
307     for ( int anIndex = 0; anIndex < aCount; ++anIndex )
308     {
309       isVisible[anIndex] = aShapeAnnotations->GetIsVisible( anIndex );
310     }
311     for ( int anIndex = 0; anIndex < aCount; ++anIndex )
312     {
313       if ( isVisible[anIndex] )
314         Display( theEntry, anIndex, theView, true, isUpdateViewer );
315     }
316   }
317 }
318
319 void GEOMGUI_AnnotationMgr::EraseVisibleAnnotations( const QString& theEntry, SOCC_Viewer* theView,
320                                                      const bool isUpdateViewer )
321 {
322   SOCC_Viewer* aView = viewOrActiveView( theView );
323   if ( !myVisualized.contains( aView ) )
324     return;
325
326   EntryToAnnotations anEntryToAnnotation = myVisualized[aView];
327   if ( !anEntryToAnnotation.contains( theEntry ) )
328     return;
329   AnnotationToPrs anAnnotationToPrs = anEntryToAnnotation[theEntry];
330
331   SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>( getApplication()->activeStudy() );
332   _PTR(SObject) aSObj = aStudy->studyDS()->FindObjectID( theEntry.toStdString() );
333   const Handle(GEOMGUI_AnnotationAttrs) aShapeAnnotations = GEOMGUI_AnnotationAttrs::FindAttributes( aSObj );
334
335   const int aCount = aShapeAnnotations->GetNbAnnotation();
336   for ( int anIndex = 0; anIndex < aCount; ++anIndex )
337   {
338     if ( !anAnnotationToPrs.contains( anIndex ) )
339       continue;
340
341     // erase presentation from the viewer
342     SALOME_Prs* aPrs = anAnnotationToPrs[anIndex];
343     ((SALOME_View*)aView)->Erase( getDisplayer(), aPrs );
344   }
345   if ( isUpdateViewer )
346     getDisplayer()->UpdateViewer();
347   anEntryToAnnotation.remove( theEntry );
348   myVisualized[aView] = anEntryToAnnotation;
349 }
350
351 //=======================================================================
352 // function : GEOMGUI_AnnotationMgr::EraseRemovedAnnotation
353 // purpose  : Method to update internal maps after removing an
354 //  annotation from the object.
355 //=======================================================================
356 void GEOMGUI_AnnotationMgr::EraseRemovedAnnotation( const QString& theEntry, const int theIndex )
357 {
358   QMap<SOCC_Viewer*, EntryToAnnotations>::iterator aEntryMapIt = myVisualized.begin();
359   for ( ; aEntryMapIt != myVisualized.end(); ++aEntryMapIt ) {
360     SOCC_Viewer* aView = aEntryMapIt.key();
361     EntryToAnnotations& anEntryToAnnotation = aEntryMapIt.value();
362     if ( !anEntryToAnnotation.contains( theEntry ) )
363       continue;
364
365     AnnotationToPrs aUpdatedToPrs;
366     AnnotationToPrs& anAnnotationToPrs = anEntryToAnnotation[theEntry];
367     AnnotationToPrs::iterator anAnnotationIt = anAnnotationToPrs.begin();
368     for ( ; anAnnotationIt != anAnnotationToPrs.end(); ++anAnnotationIt ) {
369
370       const int aIndex = anAnnotationIt.key();
371       SALOME_Prs* aPrs = anAnnotationIt.value();
372       if ( aIndex > theIndex ) {
373         aUpdatedToPrs[aIndex - 1] = aPrs;
374       }
375       else if ( aIndex != theIndex ) {
376         aUpdatedToPrs[aIndex] = aPrs;
377       }
378       else {
379         ((SALOME_View*)aView)->Erase( getDisplayer(), aPrs );
380       }
381     }
382
383     anAnnotationToPrs = aUpdatedToPrs;
384   }
385   getDisplayer()->UpdateViewer();
386 }
387
388 //=======================================================================
389 // function : GEOMGUI_AnnotationMgr::UpdateVisibleAnnotations
390 // purpose  : 
391 //=======================================================================
392 void GEOMGUI_AnnotationMgr::UpdateVisibleAnnotations( const QString& theEntry, SOCC_Viewer* theView )
393 {
394   SOCC_Viewer* aView = viewOrActiveView( theView );
395   if ( !myVisualized.contains( aView ) )
396     return;
397
398   EntryToAnnotations& anEntryToAnnotation = myVisualized[aView];
399   if ( !anEntryToAnnotation.contains( theEntry ) )
400     return;
401
402   AnnotationToPrs& anAnnotationToPrs = anEntryToAnnotation[theEntry];
403   AnnotationToPrs::iterator anIt = anAnnotationToPrs.begin();
404   for (; anIt != anAnnotationToPrs.end(); ++anIt ) {
405     SOCC_Prs* aPrs =
406       dynamic_cast<SOCC_Prs*> (anIt.value());
407
408     GEOMGUI_AnnotationAttrs::Properties aProperty;
409     GEOM::GEOM_Object_ptr anObject;
410     getObject( theEntry, anIt.key(), anObject, aProperty );
411     TopoDS_Shape aShape = GEOM_Client::get_client().GetShape( GeometryGUI::GetGeomGen(), anObject );
412     gp_Ax3 aShapeLCS = gp_Ax3().Transformed( aShape.Location().Transformation() );
413
414     AIS_ListOfInteractive aIObjects;
415     aPrs->GetObjects( aIObjects );
416     AIS_ListOfInteractive::Iterator aIOIt( aIObjects );
417     for ( ; aIOIt.More(); aIOIt.Next() ) {
418
419       Handle(GEOM_Annotation) aPresentation =
420         Handle(GEOM_Annotation)::DownCast( aIOIt.Value() );
421
422       if ( aPresentation.IsNull() )
423         continue;
424
425       if ( !aShape.IsNull() ) {
426
427         gp_Ax3 aShapeLCS = gp_Ax3().Transformed( aShape.Location().Transformation() );
428         GEOMGUI_AnnotationAttrs::SetupPresentation( aPresentation, aProperty, aShapeLCS );
429         if ( aProperty.ShapeType == TopAbs_SHAPE ) {
430           aPresentation->SetHilightShape( aShape );
431         }
432         else if ( aProperty.ShapeIndex > 0 ) {
433           TopTools_IndexedMapOfShape aSubShapeMap;
434           TopExp::MapShapes( aShape, static_cast<TopAbs_ShapeEnum>( aProperty.ShapeType ), aSubShapeMap );
435           if ( aProperty.ShapeIndex <= aSubShapeMap.Extent() ) {
436             aPresentation->SetHilightShape( aSubShapeMap( aProperty.ShapeIndex ) );
437           }
438         }
439       }
440
441       setDisplayProperties( aPresentation, aView, theEntry );
442
443       aView->getAISContext()->Redisplay( aPresentation );
444     }
445   }
446   getDisplayer()->UpdateViewer();
447 }
448
449 void GEOMGUI_AnnotationMgr::DisplayAllAnnotations( SOCC_Viewer* theView )
450 {
451   SOCC_Viewer* aView = viewOrActiveView( theView );
452   if ( !myVisualized.contains( aView ) )
453     return;
454
455   GeometryGUI* aModule = dynamic_cast<GeometryGUI*>( getApplication()->activeModule() );
456   GEOMGUI_TextTreeWdg* aTextWidget = aModule->GetTextTreeWdg();
457   QList<QString> anEntries = aTextWidget->getAllEntries( GEOMGUI_TextTreeWdg::AnnotationShape );
458
459   SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>( getApplication()->activeStudy() );
460   for ( int i = 0, aCount = anEntries.size(); i < aCount; i++ ) {
461      QString anEntry = anEntries[i];
462
463     _PTR(SObject) aSObj = aStudy->studyDS()->FindObjectID( anEntry.toStdString() );
464     if ( !aSObj )
465       continue;
466
467     const Handle(GEOMGUI_AnnotationAttrs) aShapeAnnotations = GEOMGUI_AnnotationAttrs::FindAttributes( aSObj );
468     if ( aShapeAnnotations.IsNull() )
469       continue;
470
471     int anAnnotationsCount = aShapeAnnotations->GetNbAnnotation();
472     for ( int anIndex = 0; anIndex < anAnnotationsCount; ++anIndex )
473     {
474       Display( anEntry, anIndex, aView, false, false );
475     }
476     getDisplayer()->UpdateViewer();
477     storeVisibleState( anEntry, aView );
478     storeFixedPosition( anEntry, aView );
479   }
480 }
481
482 void GEOMGUI_AnnotationMgr::EraseAllAnnotations( SOCC_Viewer* theView )
483 {
484   SOCC_Viewer* aView = viewOrActiveView( theView );
485   if ( !myVisualized.contains( aView ) )
486     return;
487
488   GeometryGUI* aModule = dynamic_cast<GeometryGUI*>( getApplication()->activeModule() );
489   GEOMGUI_TextTreeWdg* aTextWidget = aModule->GetTextTreeWdg();
490   QList<QString> anEntries = aTextWidget->getAllEntries( GEOMGUI_TextTreeWdg::AnnotationShape );
491
492   for ( int i = 0, aCount = anEntries.size(); i < aCount; i++ ) {
493     QString anEntry = anEntries[i];
494     EraseVisibleAnnotations( anEntry, aView, false );
495     storeVisibleState( anEntry, aView );
496   }
497   getDisplayer()->UpdateViewer();
498 }
499
500 void GEOMGUI_AnnotationMgr::SetPreviewStyle( const QString& theEntry, const int theIndex, const bool theIsPreview )
501 {
502   SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
503   const QColor aFontColor = aResMgr->colorValue( "Geometry", "shape_annotation_font_color", QColor( 255, 255, 255 ) );
504   const QColor aLineColor = aResMgr->colorValue( "Geometry", "shape_annotation_line_color", QColor( 255, 255, 255 ) );
505
506   const Quantity_Color aOcctFontColor( aFontColor.redF(), aFontColor.greenF(), aFontColor.blueF(), Quantity_TOC_RGB );
507   const Quantity_Color aOcctLineColor( aLineColor.redF(), aLineColor.greenF(), aLineColor.blueF(), Quantity_TOC_RGB );
508
509   QMap<SOCC_Viewer*, EntryToAnnotations>::Iterator aViewIt = myVisualized.begin();
510   for (; aViewIt != myVisualized.end(); ++aViewIt ) {
511
512     Handle(GEOM_Annotation) aPresentation = getAISPresentation ( theEntry, theIndex, aViewIt.key() );
513     if ( aPresentation.IsNull() ) {
514       continue;
515     }
516
517     if ( theIsPreview ) {
518       aPresentation->SetTextColor( Quantity_NOC_VIOLET );
519       aPresentation->SetLineColor( Quantity_NOC_VIOLET );
520       aPresentation->SetDepthCulling( Standard_False );
521     }
522     else {
523       aPresentation->SetTextColor( aOcctFontColor );
524       aPresentation->SetLineColor( aOcctLineColor );
525       aPresentation->SetDepthCulling( Standard_True );
526     }
527   }
528   getDisplayer()->UpdateViewer();
529 }
530
531 Handle(SALOME_InteractiveObject) GEOMGUI_AnnotationMgr::FindInteractiveObject( const QString& theEntry,
532                                                                                const int theIndex,
533                                                                                SOCC_Viewer* theView ) const
534 {
535   Handle(SALOME_InteractiveObject) anIO;
536
537   SOCC_Viewer* aView = viewOrActiveView( theView );
538   if ( !myVisualized.contains( aView ) )
539     return anIO;
540
541   EntryToAnnotations anEntryToAnnotation = myVisualized[aView];
542   if ( !anEntryToAnnotation.contains( theEntry ) )
543     return anIO;
544
545   AnnotationToPrs anAnnotationToPrs = anEntryToAnnotation[theEntry];
546   if ( !anAnnotationToPrs.contains(theIndex) )
547     return anIO;
548
549   SALOME_Prs* aPrs = anAnnotationToPrs[theIndex];
550   SOCC_Prs* anOCCPrs = dynamic_cast<SOCC_Prs*>( aPrs );
551   if ( !anOCCPrs )
552     return anIO;
553
554   AIS_ListOfInteractive anIOs;
555   anOCCPrs->GetObjects( anIOs );
556   AIS_ListIteratorOfListOfInteractive anIter( anIOs );
557   for ( ; anIter.More() && anIO.IsNull(); anIter.Next() ) {
558     Handle(AIS_InteractiveObject) aPrs = anIter.Value();
559     if ( aPrs->GetOwner() )
560       anIO = Handle(SALOME_InteractiveObject)::DownCast( aPrs->GetOwner() );
561   }
562   return anIO;
563 }
564
565 int GEOMGUI_AnnotationMgr::FindAnnotationIndex( Handle(SALOME_InteractiveObject) theIO,
566                                                 SOCC_Viewer* theView )
567 {
568   int anIndex = -1;
569
570   SOCC_Viewer* aView = viewOrActiveView( theView );
571   if ( !myVisualized.contains( aView ) )
572     return anIndex;
573
574   QString anEntry = theIO->getEntry();
575
576   EntryToAnnotations anEntryToAnnotation = myVisualized[aView];
577   if ( !anEntryToAnnotation.contains( anEntry ) )
578     return anIndex;
579
580   AnnotationToPrs anAnnotationToPrs = anEntryToAnnotation[anEntry];
581   //typedef QMap<int, SALOME_Prs*> AnnotationToPrs;
582   AnnotationToPrs::const_iterator anIt = anAnnotationToPrs.begin(),
583                                   aLast = anAnnotationToPrs.end();
584   for (; anIt != aLast && anIndex < 0; anIt++) {
585     SALOME_Prs* aPrs = anIt.value();
586     SOCC_Prs* anOCCPrs = dynamic_cast<SOCC_Prs*>( aPrs );
587     if ( !anOCCPrs )
588       continue;
589
590     AIS_ListOfInteractive anIOs;
591     anOCCPrs->GetObjects( anIOs );
592     AIS_ListIteratorOfListOfInteractive anIter( anIOs );
593     for ( ; anIter.More() && anIndex < 0; anIter.Next() ) {
594       Handle(AIS_InteractiveObject) aPrs = anIter.Value();
595       if ( aPrs->GetOwner() ) {
596         Handle(SALOME_InteractiveObject) aPrsOwner = Handle(SALOME_InteractiveObject)::DownCast(aPrs->GetOwner());
597         if ( aPrsOwner == theIO )
598           anIndex = anIt.key();
599       }
600     }
601   }
602
603   return anIndex;
604 }
605
606 //=======================================================================
607 // function : GEOMGUI_AnnotationMgr::RemoveView
608 // purpose  : 
609 //=======================================================================
610 void GEOMGUI_AnnotationMgr::RemoveView( SOCC_Viewer* theView )
611 {
612   if ( !theView && myVisualized.contains( theView ) )
613     myVisualized.remove( theView );
614 }
615
616 QString GEOMGUI_AnnotationMgr::getDisplayedIndicesInfo( const QString& theEntry, SOCC_Viewer* theView ) const
617 {
618   QString aDisplayedIndices;
619
620   SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>( getApplication()->activeStudy() );
621   _PTR(SObject) aSObj = aStudy->studyDS()->FindObjectID( theEntry.toStdString() );
622   if ( !aSObj )
623   {
624     return aDisplayedIndices;
625   }
626   const Handle(GEOMGUI_AnnotationAttrs) aShapeAnnotations = GEOMGUI_AnnotationAttrs::FindAttributes( aSObj );
627   if ( !aShapeAnnotations.IsNull() )
628   {
629     const int aCount = aShapeAnnotations->GetNbAnnotation();
630     QStringList anIndices;
631     for ( int anIndex = 0; anIndex < aCount; ++anIndex )
632     {
633       if (IsDisplayed( theEntry, anIndex, theView ) )
634         anIndices.append( QString::number(anIndex) );
635     }
636     aDisplayedIndices = anIndices.join(";");
637   }
638   return aDisplayedIndices;
639 }
640
641 void GEOMGUI_AnnotationMgr::setDisplayedIndicesInfo( const QString& theEntry, SOCC_Viewer* theView,
642                                                      const QString theIndicesInfo )
643 {
644   if ( theIndicesInfo.isEmpty() )
645     return;
646
647   QStringList anIndices = theIndicesInfo.split( ";" );
648   for ( int i = 0, aCount = anIndices.size(); i < aCount; i++ ) {
649     Display( theEntry, anIndices[i].toInt(), theView );
650   }
651 }
652
653 GEOM_Displayer* GEOMGUI_AnnotationMgr::getDisplayer() const
654 {
655   LightApp_Module* aModule = dynamic_cast<LightApp_Module*>( getApplication()->activeModule() );
656   return dynamic_cast<GEOM_Displayer*>( aModule->displayer() );
657 }
658
659 SOCC_Viewer* GEOMGUI_AnnotationMgr::viewOrActiveView( SOCC_Viewer* theView ) const
660 {
661   SOCC_Viewer* aView = theView;
662   if ( !aView ) {
663     SalomeApp_Application* anApp = getApplication();
664     SUIT_ViewWindow* anActiveWindow = anApp->desktop()->activeWindow();
665     if ( anActiveWindow ) {
666       aView = dynamic_cast<SOCC_Viewer*>( anActiveWindow->getViewManager()->getViewModel() );
667     }
668   }
669   return aView;
670 }
671
672 QString GEOMGUI_AnnotationMgr::makeAnnotationEntry( const QString& theEntry, const int theIndex )
673 {
674   return QString("%1%2%3").arg(theEntry).arg(GetEntrySeparator()).arg(theIndex);
675 }
676
677 bool GEOMGUI_AnnotationMgr::getIndexFromEntry( const QString& theEntry, QString& theObjEntry, int& theIndex )
678 {
679   QStringList aSplit = theEntry.split( GetEntrySeparator() );
680   if ( aSplit.size() < 2 )
681     return false;
682
683   bool isOk = true;
684   theObjEntry = aSplit.at( 0 );
685   theIndex = aSplit.at( 1 ).toInt( &isOk );
686   return isOk;
687 }
688
689 void GEOMGUI_AnnotationMgr::getObject( const QString& theEntry, const int theIndex,
690                                        GEOM::GEOM_Object_ptr& theObject,
691                                        GEOMGUI_AnnotationAttrs::Properties& theProperty )
692 {
693   SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>( getApplication()->activeStudy() );
694   _PTR(SObject) aSObj = aStudy->studyDS()->FindObjectID( theEntry.toStdString() );
695   const Handle(GEOMGUI_AnnotationAttrs) aShapeAnnotations = GEOMGUI_AnnotationAttrs::FindAttributes( aSObj );
696   if ( !aShapeAnnotations.IsNull() ) {
697     aShapeAnnotations->GetProperties( theIndex, theProperty );
698
699     theObject = GEOM::GEOM_Object::_narrow( GeometryGUI::ClientSObjectToObject(aSObj) );
700   }
701 }
702
703 void GEOMGUI_AnnotationMgr::storeFixedPosition( const QString& theEntry, SOCC_Viewer* theView )
704 {
705   SOCC_Viewer* aView = viewOrActiveView( theView );
706   if ( !aView || !myVisualized.contains( aView ) )
707     return;
708
709   SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>( getApplication()->activeStudy() );
710   _PTR(SObject) aSObj = aStudy->studyDS()->FindObjectID( theEntry.toStdString() );
711   const Handle(GEOMGUI_AnnotationAttrs) aShapeAnnotations = GEOMGUI_AnnotationAttrs::FindAttributes( aSObj );
712   if ( aShapeAnnotations.IsNull() )
713     return;
714
715
716   EntryToAnnotations anEntryToAnnotation = myVisualized[aView];
717   AnnotationToPrs anAnnotationToPrs;
718   if ( anEntryToAnnotation.contains( theEntry ) )
719     anAnnotationToPrs = anEntryToAnnotation[theEntry];
720
721   AnnotationToPrs::iterator anIt = anAnnotationToPrs.begin();
722   for (; anIt != anAnnotationToPrs.end(); ++anIt ) {
723     int anIndex = anIt.key();
724     bool isFixedAnnotation = aShapeAnnotations->GetIsScreenFixed( anIndex );
725     if ( !isFixedAnnotation )
726       continue;
727
728     SOCC_Prs* aPrs = dynamic_cast<SOCC_Prs*> (anIt.value());
729     Handle(GEOM_Annotation) anAnnotationPresentation;
730
731     AIS_ListOfInteractive aIObjects;
732     aPrs->GetObjects( aIObjects );
733     AIS_ListOfInteractive::Iterator aIOIt( aIObjects );
734     for ( ; aIOIt.More(); aIOIt.Next() ) {
735       anAnnotationPresentation = Handle(GEOM_Annotation)::DownCast( aIOIt.Value() );
736       if ( !anAnnotationPresentation.IsNull() )
737         break;
738     }
739     if ( !anAnnotationPresentation.IsNull() )
740       aShapeAnnotations->SetPosition( anIndex, anAnnotationPresentation->GetPosition() );
741   }
742 }
743
744 void GEOMGUI_AnnotationMgr::storeVisibleState( const QString& theEntry, SOCC_Viewer* theView )
745 {
746   SOCC_Viewer* aView = viewOrActiveView( theView );
747   if ( !aView || !myVisualized.contains( aView ) )
748     return;
749
750   EntryToAnnotations anEntryToAnnotation = myVisualized[aView];
751   AnnotationToPrs anAnnotationToPrs;
752   if ( anEntryToAnnotation.contains( theEntry ) )
753     anAnnotationToPrs = anEntryToAnnotation[theEntry];
754
755
756   SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>( getApplication()->activeStudy() );
757   _PTR(SObject) aSObj = aStudy->studyDS()->FindObjectID( theEntry.toStdString() );
758   const Handle(GEOMGUI_AnnotationAttrs) aShapeAnnotations = GEOMGUI_AnnotationAttrs::FindAttributes( aSObj );
759   if ( !aShapeAnnotations.IsNull() ) {
760     const int aCount = aShapeAnnotations->GetNbAnnotation();
761     for ( int anIndex = 0; anIndex < aCount; ++anIndex ) {
762       bool aVisible = anAnnotationToPrs.contains( anIndex );
763       aShapeAnnotations->SetIsVisible( anIndex, aVisible );
764     }
765   }
766 }
767
768 //=======================================================================
769 // function : GEOMGUI_AnnotationMgr::getEntry
770 // purpose  : 
771 //=======================================================================
772 std::string GEOMGUI_AnnotationMgr::getEntry( const GEOM::GEOM_Object_ptr theObject )
773 {
774   SUIT_Session* session = SUIT_Session::session();
775   SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>( session->activeApplication() );
776   if ( app )
777   {
778     CORBA::String_var IOR = app->orb()->object_to_string( theObject );
779     if ( strcmp(IOR.in(), "") != 0 )
780     {
781       SalomeApp_Study* study = ( SalomeApp_Study* )app->activeStudy();
782       _PTR(SObject) SO ( study->studyDS()->FindObjectIOR( std::string(IOR) ) );
783       if ( SO )
784         return SO->GetID();
785     }
786   }
787   return std::string();
788 }
789
790 //=======================================================================
791 // function : GEOMGUI_AnnotationMgr::getName
792 // purpose  : 
793 //=======================================================================
794 std::string GEOMGUI_AnnotationMgr::getName( const GEOM::GEOM_Object_ptr theObject )
795 {
796   SUIT_Session* session = SUIT_Session::session();
797   SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>( session->activeApplication() );
798   if ( app )
799   {
800     CORBA::String_var IOR = app->orb()->object_to_string( theObject );
801     if ( strcmp(IOR.in(), "") != 0 )
802     {
803       SalomeApp_Study* study = ( SalomeApp_Study* )app->activeStudy();
804       _PTR(SObject) aSObj ( study->studyDS()->FindObjectIOR( std::string(IOR) ) );
805
806       _PTR(GenericAttribute) anAttr;
807
808       if ( aSObj && aSObj->FindAttribute( anAttr, "AttributeName") )
809       {
810         _PTR(AttributeName) aNameAttr( anAttr );
811         return aNameAttr->Value();
812       }
813     }
814   }
815   return std::string();
816 }
817
818 //=======================================================================
819 // function : GEOMGUI_AnnotationMgr::setDisplayProperties
820 // purpose  : 
821 //=======================================================================
822 void GEOMGUI_AnnotationMgr::setDisplayProperties( const Handle(GEOM_Annotation)& thePrs,
823                                                   SOCC_Viewer* theView,
824                                                   const QString& theEntry )
825 {
826   SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
827   const QFont  aFont      = aResMgr->fontValue( "Geometry", "shape_annotation_font", QFont( "Y14.5M-2009", 24 ) );
828   const QColor aFontColor = aResMgr->colorValue( "Geometry", "shape_annotation_font_color", QColor( 0, 0, 127 ) );
829   const QColor aLineColor = aResMgr->colorValue( "Geometry", "shape_annotation_line_color", QColor( 0, 0, 127 ) );
830   const double aLineWidth = aResMgr->doubleValue( "Geometry", "shape_annotation_line_width", 1.0 );
831   const int aLineStyle    = aResMgr->integerValue( "Geometry", "shape_annotation_line_style", 0 );
832   const bool isAutoHide   = aResMgr->booleanValue( "Geometry", "shape_annotation_autohide", false );
833
834   const Quantity_Color aOcctFontColor( aFontColor.redF(), aFontColor.greenF(), aFontColor.blueF(), Quantity_TOC_RGB );
835   const Quantity_Color aOcctLineColor( aLineColor.redF(), aLineColor.greenF(), aLineColor.blueF(), Quantity_TOC_RGB );
836   const Standard_Real aFontHeight = aFont.pixelSize() != -1 ? aFont.pixelSize() : aFont.pointSize();
837
838   thePrs->SetFont( TCollection_AsciiString( aFont.family().toLatin1().data() ) );
839   thePrs->SetTextHeight( aFontHeight );
840   thePrs->SetTextColor( Quantity_Color( aFontColor.redF(), aFontColor.greenF(), aFontColor.blueF(), Quantity_TOC_RGB ) );
841   thePrs->SetLineColor( Quantity_Color( aLineColor.redF(), aLineColor.greenF(), aLineColor.blueF(), Quantity_TOC_RGB ) );
842   thePrs->SetLineWidth( aLineWidth );
843   thePrs->SetLineStyle( static_cast<Aspect_TypeOfLine>( aLineStyle ) );
844   thePrs->SetAutoHide( isAutoHide ? Standard_True : Standard_False );
845   thePrs->SetDepthCulling( Standard_True );
846
847   SOCC_Viewer* aView = viewOrActiveView( theView );
848   if ( aView && !theEntry.isEmpty() ) {
849
850     SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>( getApplication()->activeStudy() );
851     int aMgrId = dynamic_cast< SUIT_ViewModel* >( aView )->getViewManager()->getGlobalId();
852     QVariant aVal = aStudy->getObjectProperty( aMgrId, theEntry, GEOM::propertyName( GEOM::TopLevel ), QVariant() );
853     bool isBringToFront = aVal.isValid() ? aVal.toBool() : false;
854     if( isBringToFront ) {
855       thePrs->SetZLayer( Graphic3d_ZLayerId_Topmost );
856     }
857     else {
858       thePrs->SetDefaultZLayer();
859     }
860   }
861   else {
862     thePrs->SetDefaultZLayer();
863   }
864 }
865
866 //=======================================================================
867 // function : GEOMGUI_AnnotationMgr::getAISPresentation
868 // purpose  : 
869 //=======================================================================
870 Handle(GEOM_Annotation) GEOMGUI_AnnotationMgr::getAISPresentation ( const QString& theEntry,
871                                                                     const int theIndex,
872                                                                     SOCC_Viewer* theView )
873 {
874   if ( !myVisualized.contains( theView ) ) {
875     return Handle(GEOM_Annotation)();
876   }
877
878   EntryToAnnotations& aEntryToAnnotation = myVisualized[theView];
879   if ( !aEntryToAnnotation.contains( theEntry ) ) {
880     return Handle(GEOM_Annotation)();
881   }
882
883   AnnotationToPrs& aAnnotationToPrs = aEntryToAnnotation[theEntry];
884   if ( !aAnnotationToPrs.contains( theIndex ) ) {
885     return Handle(GEOM_Annotation)();
886   }
887
888   SALOME_Prs* aPrs = aAnnotationToPrs[theIndex];
889
890   // set or unset preview style for the presentation
891   AIS_ListOfInteractive aIObjects;
892   ((SOCC_Prs*)aPrs)->GetObjects( aIObjects );
893   AIS_ListOfInteractive::Iterator aIOIt( aIObjects );
894   for ( ; aIOIt.More(); aIOIt.Next() ) {
895     return Handle(GEOM_Annotation)::DownCast( aIOIt.Value() );
896   }
897
898   return Handle(GEOM_Annotation)();
899 }