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