Salome HOME
Join modifications from branch BR_DEBUG_3_2_0b1
[modules/gui.git] / src / QDS / QDS_RadioBox.cxx
1 // Copyright (C) 2005  CEA/DEN, EDF R&D, OPEN CASCADE, PRINCIPIA R&D
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.
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 #include "QDS_RadioBox.h"
20
21 #include <DDS_Dictionary.h>
22
23 #include <TCollection_AsciiString.hxx>
24 #include <TColStd_HArray1OfInteger.hxx>
25 #include <TColStd_HArray1OfExtendedString.hxx>
26
27 #include <qobjectlist.h>
28 #include <qbuttongroup.h>
29 #include <qradiobutton.h>
30
31 /*!
32   Constructor.
33 */
34 QDS_RadioBox::QDS_RadioBox( const QString& id, QWidget* parent, const int flags, const QString& comp )
35 : QDS_Datum( id, parent, flags & ~( Label | Units ), comp )
36 {
37 }
38
39 /*!
40   Destructor.
41 */
42 QDS_RadioBox::~QDS_RadioBox()
43 {
44 }
45
46 /*!
47   Returns number of items in ComboBox. If total is 'false' then only
48   visible items are taken into account otherwise all items.
49 */
50 int QDS_RadioBox::count( bool total ) const
51 {
52   if ( total )
53     return myValue.count();
54   else
55   {
56     QPtrList<QRadioButton> bList;
57     buttons( bList );
58     return bList.count();
59   }
60 }
61
62 /*!
63   Returns list of ids. If total is 'false' then only visible items
64   are taken into account otherwise all items.
65 */
66 void QDS_RadioBox::values( QValueList<int>& ids, bool total ) const
67 {
68   ids.clear();
69   for ( QIntList::const_iterator it = myDataIds.begin(); it != myDataIds.end(); ++it )
70     if ( total || ( myState.contains( *it ) && myState[*it] ) )
71       ids.append( *it );
72 }
73
74 /*!
75   Returns visible state of identificator.
76 */
77 bool QDS_RadioBox::state( const int id ) const
78 {
79   bool state = false;
80   if ( myState.contains( id ) )
81     state = myState[id];
82   return state;
83 }
84
85 /*!
86   Sets the visible state of identificator. If 'id' is -1 then specified
87   state will be set to all ids.
88 */
89 void QDS_RadioBox::setState( const bool on, const int id, const bool append )
90 {
91   QValueList<int> lst;
92   if ( id < 0 )
93   {
94     for ( IdStateMap::Iterator it = myState.begin(); it != myState.end(); ++it )
95       lst.append( it.key() );
96   }
97   else
98     lst.append( id );
99
100   setState( on, lst, append );
101 }
102
103 /*!
104   Sets the visible state of identificator from the specified list.
105 */
106 void QDS_RadioBox::setState( const bool on, const QValueList<int>& ids, const bool append )
107 {
108   if ( ids.isEmpty() && append )
109     return;
110
111   bool changed = false;
112
113   QMap<int, int> aMap;
114   for ( uint i = 0; i < ids.count(); i++ )
115     aMap.insert( *ids.at( i ), 0 );
116
117   for ( IdStateMap::Iterator it = myState.begin(); it != myState.end(); ++it )
118   {
119     if ( aMap.contains( it.key() ) )
120     {
121       if ( it.data() != on )
122       {
123         it.data() = on;
124         changed = true;
125       }
126     }
127     else if ( !append && it.data() == on )
128     {
129       it.data() = !on;
130       changed = true;
131     }
132   }
133   if ( changed )
134     updateRadioBox();
135 }
136
137 /*!
138   Sets the user items into the combo box.
139 */
140 void QDS_RadioBox::setValues( const QValueList<int>& ids, const QStringList& names )
141 {
142   if ( ids.count() != names.count() )
143     return;
144
145   myUserIds = ids;
146   myUserNames = names;
147 }
148
149 /*!
150   This is an overloaded member function, provided for convenience.
151   It behaves essentially like the above function. It creates
152   QValueList (0, 1, 2 ... ) and call previous method
153 */
154 void QDS_RadioBox::setValues( const QStringList& names )
155 {
156   QValueList< int > ids;
157   for ( int i = 0, n = names.count(); i < n; i++ )
158     ids.append( i );
159   setValues( ids, names );
160 }
161
162 /*!
163   Returns string from control.
164 */
165 QString QDS_RadioBox::getString() const
166 {
167   QString res;
168   QButtonGroup* bg = buttonGroup();
169   if ( bg )
170   {
171     int id = bg->selectedId();
172     if ( id != -1 )
173       res = QString::number( id );
174   }
175   return res;
176 }
177
178 /*!
179   Sets the string into control.
180 */
181 void QDS_RadioBox::setString( const QString& txt )
182 {
183   QButtonGroup* bg = buttonGroup();
184   if ( !bg )
185     return;
186
187   int oldId = bg->selectedId();
188
189   if ( txt.isEmpty() )
190   {
191     QPtrList<QRadioButton> bList;
192     buttons( bList );
193     for ( QPtrListIterator<QRadioButton> it( bList ); it.current(); ++it )
194       it.current()->setChecked( false );
195   }
196   else
197   {
198     bool ok;
199     int id = txt.toInt( &ok );
200     if ( !ok )
201       id = -1;
202
203     bool block = signalsBlocked();
204     blockSignals( true );
205     bg->setButton( id );
206     blockSignals( block );
207   }
208
209   int newId = bg->selectedId();
210
211   if ( oldId != newId )
212   {
213     onParamChanged();
214     QString str = getString();
215     emit activated( newId );
216     emit paramChanged();
217     emit paramChanged( str );
218   }
219 }
220
221 /*!
222   Returns pointer to QButtonGroup widget.
223 */
224 QButtonGroup* QDS_RadioBox::buttonGroup() const
225 {
226   return ::qt_cast<QButtonGroup*>( controlWidget() );
227 }
228
229 /*!
230   Create QComboBox widget as control subwidget.
231 */
232 QWidget* QDS_RadioBox::createControl( QWidget* parent )
233 {
234   QButtonGroup* bg = new QButtonGroup( 1, Qt::Vertical, "", parent );
235   bg->setExclusive( true );
236   bg->setRadioButtonExclusive( true );
237   return bg;
238 }
239
240 void QDS_RadioBox::unitSystemChanged( const QString& system )
241 {
242   QDS_Datum::unitSystemChanged( system );
243
244   Handle(TColStd_HArray1OfInteger) anIds;
245   Handle(TColStd_HArray1OfExtendedString) aValues, anIcons;
246
247   Handle(DDS_DicItem) aDicItem = dicItem();
248   if ( !aDicItem.IsNull() )
249     aDicItem->GetListOfValues( aValues, anIds, anIcons );
250
251   myValue.clear();
252   myDataIds.clear();
253
254   QMap<int, QString> userMap;
255   QIntList::iterator iIt = myUserIds.begin();
256   QStringList::iterator sIt = myUserNames.begin();
257   for ( ; iIt != myUserIds.end() && sIt != myUserNames.end(); ++iIt, ++sIt )
258     userMap.insert( *iIt, *sIt );
259
260   if ( !anIds.IsNull() && !aValues.IsNull() &&
261        anIds->Length() == aValues->Length() )
262   {
263     for ( int i = anIds->Lower(); i <= anIds->Upper(); i++ )
264     {
265       QString aValue;
266       int id = anIds->Value( i );
267       if ( userMap.contains( id  ) )
268         aValue = userMap[id];
269       else
270         aValue = toQString( aValues->Value( i ) );
271
272       myDataIds.append( id );
273       myValue.insert( id, aValue );
274       myState.insert( id, true );
275     }
276   }
277
278   for ( iIt = myUserIds.begin(); iIt != myUserIds.end(); ++iIt )
279   {
280     int id = *iIt;
281     if ( !myValue.contains( id  ) )
282     {
283       myDataIds.append( id );
284       myValue.insert( id, userMap[id] );
285     }
286   }
287
288   QIntList del, add;
289   for ( IdStateMap::Iterator it1 = myState.begin(); it1 != myState.end(); ++it1 )
290     if ( !myValue.contains( it1.key() ) )
291       del.append( it1.key() );
292
293   for ( IdValueMap::Iterator it2 = myValue.begin(); it2 != myValue.end(); ++it2 )
294     if ( !myState.contains( it2.key() ) )
295       add.append( it2.key() );
296
297   for ( QIntList::iterator iter1 = del.begin(); iter1 != del.end(); ++iter1 )
298     myState.remove( *iter1 );
299
300   for ( QIntList::iterator iter2 = add.begin(); iter2 != add.end(); ++iter2 )
301     myState.insert( *iter2, true );
302
303   QButtonGroup* bg = buttonGroup();
304   if ( bg )
305     bg->setTitle( label() );
306
307   updateRadioBox();
308 }
309
310 /*!
311   Notify about state changed in line edit of RadioBox.
312 */
313 void QDS_RadioBox::onToggled( bool on )
314 {
315   if ( !on )
316     return;
317
318   onParamChanged();
319   emit paramChanged();
320   QString str = getString();
321   emit paramChanged( str );
322 }
323
324 /*!
325   Updates RadioBox after have change of visible state or items have been inserted / removed.
326 */
327 void QDS_RadioBox::updateRadioBox()
328 {
329   QButtonGroup* bg = buttonGroup();
330   if ( !bg )
331     return;
332
333   int curId = bg->selectedId();
334
335   QPtrList<QRadioButton> bList;
336   buttons( bList );
337   for ( QPtrListIterator<QRadioButton> itr( bList ); itr.current(); ++itr )
338     delete itr.current();
339
340   for ( QIntList::const_iterator it = myDataIds.begin(); it != myDataIds.end(); ++it )
341   {
342     int id = *it;
343     if ( !myValue.contains( id ) || !myState.contains( id ) || !myState[id] )
344       continue;
345
346     QRadioButton* rb = new QRadioButton( myValue[id], bg );
347     bg->insert( rb, id );
348
349     connect( rb, SIGNAL( toggled( bool ) ), this, SLOT( onToggled( bool ) ) );
350   }
351
352   if ( curId != -1 )
353   {
354     int id = curId;
355     if ( !bg->find( id ) )
356     {
357       QPtrList<QRadioButton> bList;
358       buttons( bList );
359       if ( !bList.isEmpty() )
360         id = bg->id( bList.getFirst() );
361     }
362
363     bool block = signalsBlocked();
364     blockSignals( true );
365     bg->setButton( id );
366     blockSignals( block );
367   }
368
369   if ( curId != bg->selectedId() )
370   {
371     onParamChanged();
372     emit paramChanged();
373     emit paramChanged( getString() );
374   }
375 }
376
377 void QDS_RadioBox::buttons( QPtrList<QRadioButton>& lst ) const
378 {
379   lst.setAutoDelete( false );
380   lst.clear();
381
382   QButtonGroup* bg = buttonGroup();
383   if ( !bg )
384     return;
385
386   QObjectList* objs = bg->queryList( "QRadioButton" );
387   if ( objs )
388   {
389     for ( QObjectListIt it( *objs ); it.current(); ++it )
390     {
391       QRadioButton* rb = ::qt_cast<QRadioButton*>( it.current() );
392       if ( rb )
393         lst.append( rb );
394     }
395   }
396   delete objs;
397 }