Salome HOME
340d64d7a1f328bb19fc24e887d9e0350cf6cde5
[modules/geom.git] / src / GEOMGUI / GEOMGUI_TextTreeSelector.cxx
1 // Copyright (C) 2015-2023  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 // File   : GEOMGUI_TextTreeSelector.cxx
21 // Author :
22 //
23 #include "GEOMGUI_TextTreeSelector.h"
24 #include "GEOMGUI_TextTreeWdg.h"
25 #include "GEOMGUI_AnnotationMgr.h"
26
27 #include "LightApp_DataOwner.h"
28 #include "LightApp_DataObject.h"
29 #include "LightApp_Application.h"
30 #include <SUIT_Session.h>
31 #include <SUIT_DataObjectIterator.h>
32
33 /*!
34   \class GEOMGUI_TextTreeSelector
35   \brief Text tree selection handler class. It provides selection synchronization between
36   application and text tree widget. This selector listens item selection changed signal of
37   text tree widget to emit common selection changed signal of SUIT selector to start selection
38   synchronization. In get/setSelection selector processes annotation items. These items have
39   specific entry generated in annotation manager, having the "object entry:annotation_id" structure.
40 */
41
42 /*!
43   \brief Constructor.
44   \param widget text tree widget
45   \param theManager selection manager
46 */
47 GEOMGUI_TextTreeSelector::GEOMGUI_TextTreeSelector( GEOMGUI_TextTreeWdg* theWidget,
48                                                     GEOMGUI_AnnotationMgr* theAnnotationMgr,
49                                                     SUIT_SelectionMgr* theManager )
50 : SUIT_Selector( theManager, theWidget ),
51   myTextTree( theWidget ), myAnnotationMgr( theAnnotationMgr )
52 {
53   if ( myTextTree ) {
54     connect( myTextTree, SIGNAL( itemSelectionChanged() ), this, SLOT( onSelectionChanged() ) );
55   }
56 }
57
58 /*!
59   \brief Destructor.
60 */
61 GEOMGUI_TextTreeSelector::~GEOMGUI_TextTreeSelector()
62 {
63 }
64
65 /*!
66   \brief Get object browser.
67   \return a pointer to the text tree widget
68 */
69 GEOMGUI_TextTreeWdg* GEOMGUI_TextTreeSelector::textTree() const
70 {
71   return myTextTree;
72 }
73
74 /*!
75   \brief Get selector unique type.
76   \return selector type
77 */
78 QString GEOMGUI_TextTreeSelector::type() const
79
80   return "TextTree";
81 }
82
83 /*!
84   \brief Called when the Object browser selection is changed. It emits signal to synchronize
85   selection in application.
86 */
87 void GEOMGUI_TextTreeSelector::onSelectionChanged()
88 {
89   mySelectedList.clear();
90   selectionChanged();
91 }
92
93 /*!
94   \brief Get list of currently selected annotation objects.
95   \param theList list to be filled with the selected objects owners
96   The list contains owners for interactive objects of annotations
97 */
98 void GEOMGUI_TextTreeSelector::getSelection( SUIT_DataOwnerPtrList& theList ) const
99 {
100   if ( mySelectedList.count() == 0 ) {
101
102     GEOMGUI_TextTreeSelector* aThisSelector = (GEOMGUI_TextTreeSelector*)this;
103     QMap<QString, QList<int> > aSelectedAnnotations;
104     myTextTree->getSelected(aSelectedAnnotations);
105
106     QMap<QString, QList<int> >::const_iterator anIt = aSelectedAnnotations.begin(),
107                                                aLast = aSelectedAnnotations.end();
108     for ( ; anIt != aLast; anIt++ ) {
109       QString anEntry = anIt.key();
110       QList<int> anIndices = anIt.value();
111       QList<int>::const_iterator anIdIt = anIndices.begin(), anIdLast = anIndices.end();
112       for ( ; anIdIt != anIdLast; anIdIt++ ) {
113         int anIndex = *anIdIt;
114         Handle(SALOME_InteractiveObject) anIO = myAnnotationMgr->FindInteractiveObject( anEntry, anIndex );
115         if ( anIO.IsNull() )
116           continue;
117
118         LightApp_DataOwner* owner = new LightApp_DataOwner( anIO );
119         aThisSelector->mySelectedList.append( SUIT_DataOwnerPtr( owner ) );
120       }
121     }
122   }
123   theList = mySelectedList;
124 }
125
126 /*!
127   \brief Set selection.
128   \param theList list of the object owners to be set selected
129 */
130 void GEOMGUI_TextTreeSelector::setSelection( const SUIT_DataOwnerPtrList& theList )
131 {
132   if ( !myTextTree )
133     return;
134
135   QMap<QString, QList<int> > aSelectedAnnotations;
136   DataObjectList objList;
137   for ( SUIT_DataOwnerPtrList::const_iterator it = theList.begin(); 
138         it != theList.end(); ++it ) {
139     const LightApp_DataOwner* anOwner = dynamic_cast<const LightApp_DataOwner*>( (*it).operator->() );
140     if ( !anOwner )
141       continue;
142     Handle(SALOME_InteractiveObject) anIO = anOwner->IO();
143     if ( anIO.IsNull() )
144       continue;
145
146     QString anIOEntry = anIO->getEntry();
147     QStringList anAnnotationInfo = anIOEntry.split(GEOMGUI_AnnotationMgr::GetEntrySeparator());
148     if (anAnnotationInfo.size() != 2 )
149       continue;
150
151     QString anEntry = anAnnotationInfo[0];
152     int anAnnotationId = anAnnotationInfo[1].toInt();
153     if ( anAnnotationId <  0)
154       continue;
155
156     QList<int> anIndices;
157     if ( aSelectedAnnotations.contains( anEntry ) )
158       anIndices = aSelectedAnnotations[anEntry];
159
160     if ( !anIndices.contains( anAnnotationId ) )
161       anIndices.append( anAnnotationId );
162
163     aSelectedAnnotations[anEntry] = anIndices;
164   }
165   myTextTree->setSelected(aSelectedAnnotations);
166   mySelectedList.clear();
167 }
168