Salome HOME
updated copyright message
[modules/gui.git] / src / SUIT / SUIT_Selector.cxx
1 // Copyright (C) 2007-2023  CEA, EDF, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
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.
10 //
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.
15 //
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
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include "SUIT_Selector.h"
24
25 #include "SUIT_SelectionMgr.h"
26
27 #include <QObject>
28
29 /*!\class SUIT_Selector::Destroyer
30   Class provide the watching for qobject parent class of the selector.
31 */
32
33 class SUIT_Selector::Destroyer : public QObject
34 {
35 public:
36   Destroyer( SUIT_Selector*, QObject* = 0 );
37   virtual ~Destroyer();
38
39   SUIT_Selector* selector() const;
40   void           setSelector( SUIT_Selector* );
41
42 private:
43   SUIT_Selector* mySelector;
44 };
45
46 SUIT_Selector::Destroyer::Destroyer( SUIT_Selector* s, QObject* p )
47 : QObject( p ),
48   mySelector( s )
49 {
50 }
51
52 SUIT_Selector::Destroyer::~Destroyer()
53 {
54   SUIT_Selector* s = mySelector;
55   mySelector = 0;
56   if ( s )
57     delete s;
58 }
59
60 SUIT_Selector* SUIT_Selector::Destroyer::selector() const
61 {
62   return mySelector;
63 }
64
65 void SUIT_Selector::Destroyer::setSelector( SUIT_Selector* s )
66 {
67   mySelector = s;
68 }
69
70 /*!\class SUIT_Selector
71  * Class provide selector for data owners.
72  */
73
74 /*!
75   Constructor.
76 */
77 SUIT_Selector::SUIT_Selector( SUIT_SelectionMgr* selMgr, QObject* parent )
78 : mySelMgr( selMgr ),
79   myBlock( false ),
80   myEnabled( true ),
81   myAutoBlock( true ),
82   myDestroyer( 0 )
83 {
84   if ( selMgr )
85     selMgr->installSelector( this );
86
87   if ( parent )
88     myDestroyer = new Destroyer( this, parent );
89 }
90
91 /*!
92   Destructor.
93 */
94 SUIT_Selector::~SUIT_Selector()
95 {
96   if ( selectionMgr() )
97     selectionMgr()->removeSelector( this );
98
99   if ( myDestroyer && myDestroyer->selector() == this )
100   {
101     myDestroyer->setSelector( 0 );
102     delete myDestroyer;
103     myDestroyer = 0;
104   }
105 }
106
107 /*!
108   Gets selection manager.
109 */
110 SUIT_SelectionMgr* SUIT_Selector::selectionMgr() const
111 {
112   return mySelMgr;
113 }
114
115 /*!
116   Checks: Is selctor enabled?
117 */
118 bool SUIT_Selector::isEnabled() const
119 {
120   return myEnabled;
121 }
122
123 /*!
124   Sets selctor anbled to \a on.
125 */
126 void SUIT_Selector::setEnabled( const bool on )
127 {
128   myEnabled = on;
129 }
130
131 /*!
132   Checks: Is selector auto block?
133 */
134 bool SUIT_Selector::autoBlock() const
135 {
136   return myAutoBlock;
137 }
138
139 /*!
140   Sets selctor autoblock to \a on.
141 */
142 void SUIT_Selector::setAutoBlock( const bool on )
143 {
144   myAutoBlock = on;
145 }
146
147 /*!
148   Puts to \a lst selection list of data owners.
149 */
150 void SUIT_Selector::selected( SUIT_DataOwnerPtrList& lst ) const
151 {
152   lst.clear();
153   getSelection( lst );
154 }
155
156 /*!
157   Puts to selection list of data owners \a lst..
158 */
159 void SUIT_Selector::setSelected( const SUIT_DataOwnerPtrList& lst )
160 {
161   if ( !isEnabled() )
162     return;
163
164   bool block = myBlock;
165   myBlock = true;
166
167   setSelection( lst );
168
169   myBlock = block;
170 }
171
172 /*!
173   On selection changed.
174 */
175 void SUIT_Selector::selectionChanged()
176 {
177   if ( selectionMgr() && isEnabled() && ( !autoBlock() || !myBlock ) )
178     selectionMgr()->selectionChanged( this );
179 }
180
181 /*!
182   Checks: Is selection manager has selection mode \a mode?
183 */
184 bool SUIT_Selector::hasSelectionMode( const int mode ) const
185 {
186   if ( !selectionMgr() )
187     return false;
188
189   return selectionMgr()->hasSelectionMode( mode );
190 }
191
192 /*!
193   Puts to list \a lst selection modes from selection manager.
194 */
195 void SUIT_Selector::selectionModes( QList<int>& lst ) const
196 {
197   if ( selectionMgr() )
198     selectionMgr()->selectionModes( lst );
199 }
200
201 /*!
202   Get owner of this selector.
203  */
204 QObject* SUIT_Selector::owner() const
205 {
206   return myDestroyer ? myDestroyer->parent() : 0;
207 }