1 // Copyright (C) 2005 CEA/DEN, EDF R&D, OPEN CASCADE, PRINCIPIA R&D
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.
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.
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
17 // See http://www.salome-platform.org/
19 #include "QDS_RadioBox.h"
21 #include <DDS_Dictionary.h>
23 #include <TCollection_AsciiString.hxx>
24 #include <TColStd_HArray1OfInteger.hxx>
25 #include <TColStd_HArray1OfExtendedString.hxx>
27 #include <qobjectlist.h>
28 #include <qbuttongroup.h>
29 #include <qradiobutton.h>
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 )
42 QDS_RadioBox::~QDS_RadioBox()
47 Returns number of items in ComboBox. If total is 'false' then only
48 visible items are taken into account otherwise all items.
50 int QDS_RadioBox::count( bool total ) const
53 return myValue.count();
56 QPtrList<QRadioButton> bList;
63 Returns list of ids. If total is 'false' then only visible items
64 are taken into account otherwise all items.
66 void QDS_RadioBox::values( QValueList<int>& ids, bool total ) const
69 for ( QIntList::const_iterator it = myDataIds.begin(); it != myDataIds.end(); ++it )
70 if ( total || ( myState.contains( *it ) && myState[*it] ) )
75 Returns visible state of identificator.
77 bool QDS_RadioBox::state( const int id ) const
80 if ( myState.contains( id ) )
86 Sets the visible state of identificator. If 'id' is -1 then specified
87 state will be set to all ids.
89 void QDS_RadioBox::setState( const bool on, const int id, const bool append )
94 for ( IdStateMap::Iterator it = myState.begin(); it != myState.end(); ++it )
95 lst.append( it.key() );
100 setState( on, lst, append );
104 Sets the visible state of identificator from the specified list.
106 void QDS_RadioBox::setState( const bool on, const QValueList<int>& ids, const bool append )
108 if ( ids.isEmpty() && append )
111 bool changed = false;
114 for ( uint i = 0; i < ids.count(); i++ )
115 aMap.insert( *ids.at( i ), 0 );
117 for ( IdStateMap::Iterator it = myState.begin(); it != myState.end(); ++it )
119 if ( aMap.contains( it.key() ) )
121 if ( it.data() != on )
127 else if ( !append && it.data() == on )
138 Sets the user items into the combo box.
140 void QDS_RadioBox::setValues( const QValueList<int>& ids, const QStringList& names )
142 if ( ids.count() != names.count() )
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
154 void QDS_RadioBox::setValues( const QStringList& names )
156 QValueList< int > ids;
157 for ( int i = 0, n = names.count(); i < n; i++ )
159 setValues( ids, names );
163 Returns string from control.
165 QString QDS_RadioBox::getString() const
168 QButtonGroup* bg = buttonGroup();
171 int id = bg->selectedId();
173 res = QString::number( id );
179 Sets the string into control.
181 void QDS_RadioBox::setString( const QString& txt )
183 QButtonGroup* bg = buttonGroup();
187 int oldId = bg->selectedId();
191 QPtrList<QRadioButton> bList;
193 for ( QPtrListIterator<QRadioButton> it( bList ); it.current(); ++it )
194 it.current()->setChecked( false );
199 int id = txt.toInt( &ok );
203 bool block = signalsBlocked();
204 blockSignals( true );
206 blockSignals( block );
209 int newId = bg->selectedId();
211 if ( oldId != newId )
214 QString str = getString();
215 emit activated( newId );
217 emit paramChanged( str );
222 Returns pointer to QButtonGroup widget.
224 QButtonGroup* QDS_RadioBox::buttonGroup() const
226 return ::qt_cast<QButtonGroup*>( controlWidget() );
230 Create QComboBox widget as control subwidget.
232 QWidget* QDS_RadioBox::createControl( QWidget* parent )
234 QButtonGroup* bg = new QButtonGroup( 1, Qt::Vertical, "", parent );
235 bg->setExclusive( true );
236 bg->setRadioButtonExclusive( true );
240 void QDS_RadioBox::unitSystemChanged( const QString& system )
242 QDS_Datum::unitSystemChanged( system );
244 Handle(TColStd_HArray1OfInteger) anIds;
245 Handle(TColStd_HArray1OfExtendedString) aValues, anIcons;
247 Handle(DDS_DicItem) aDicItem = dicItem();
248 if ( !aDicItem.IsNull() )
249 aDicItem->GetListOfValues( aValues, anIds, anIcons );
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 );
260 if ( !anIds.IsNull() && !aValues.IsNull() &&
261 anIds->Length() == aValues->Length() )
263 for ( int i = anIds->Lower(); i <= anIds->Upper(); i++ )
266 int id = anIds->Value( i );
267 if ( userMap.contains( id ) )
268 aValue = userMap[id];
270 aValue = toQString( aValues->Value( i ) );
272 myDataIds.append( id );
273 myValue.insert( id, aValue );
274 myState.insert( id, true );
278 for ( iIt = myUserIds.begin(); iIt != myUserIds.end(); ++iIt )
281 if ( !myValue.contains( id ) )
283 myDataIds.append( id );
284 myValue.insert( id, userMap[id] );
289 for ( IdStateMap::Iterator it1 = myState.begin(); it1 != myState.end(); ++it1 )
290 if ( !myValue.contains( it1.key() ) )
291 del.append( it1.key() );
293 for ( IdValueMap::Iterator it2 = myValue.begin(); it2 != myValue.end(); ++it2 )
294 if ( !myState.contains( it2.key() ) )
295 add.append( it2.key() );
297 for ( QIntList::iterator iter1 = del.begin(); iter1 != del.end(); ++iter1 )
298 myState.remove( *iter1 );
300 for ( QIntList::iterator iter2 = add.begin(); iter2 != add.end(); ++iter2 )
301 myState.insert( *iter2, true );
303 QButtonGroup* bg = buttonGroup();
305 bg->setTitle( label() );
311 Notify about state changed in line edit of RadioBox.
313 void QDS_RadioBox::onToggled( bool on )
320 QString str = getString();
321 emit paramChanged( str );
325 Updates RadioBox after have change of visible state or items have been inserted / removed.
327 void QDS_RadioBox::updateRadioBox()
329 QButtonGroup* bg = buttonGroup();
333 int curId = bg->selectedId();
335 QPtrList<QRadioButton> bList;
337 for ( QPtrListIterator<QRadioButton> itr( bList ); itr.current(); ++itr )
338 delete itr.current();
340 for ( QIntList::const_iterator it = myDataIds.begin(); it != myDataIds.end(); ++it )
343 if ( !myValue.contains( id ) || !myState.contains( id ) || !myState[id] )
346 QRadioButton* rb = new QRadioButton( myValue[id], bg );
347 bg->insert( rb, id );
349 connect( rb, SIGNAL( toggled( bool ) ), this, SLOT( onToggled( bool ) ) );
355 if ( !bg->find( id ) )
357 QPtrList<QRadioButton> bList;
359 if ( !bList.isEmpty() )
360 id = bg->id( bList.getFirst() );
363 bool block = signalsBlocked();
364 blockSignals( true );
366 blockSignals( block );
369 if ( curId != bg->selectedId() )
373 emit paramChanged( getString() );
377 void QDS_RadioBox::buttons( QPtrList<QRadioButton>& lst ) const
379 lst.setAutoDelete( false );
382 QButtonGroup* bg = buttonGroup();
386 QObjectList* objs = bg->queryList( "QRadioButton" );
389 for ( QObjectListIt it( *objs ); it.current(); ++it )
391 QRadioButton* rb = ::qt_cast<QRadioButton*>( it.current() );