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