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/ or email : webmaster.salome@opencascade.com
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 Datum with control corresponding to button group with set of exclusive radio buttons.
35 This control used for datum with enumerable values. It can be used for datum which has
36 type of value 'List'. Each radio button of combobox defined two properties:
37 integer identifier and string name. All operations on radio buttons performed via identifier.
39 If datum label text is specified then it displayed in group box title.
43 Constructor. Create radio button box datum object with datum identifier \aid under widget \aparent.
44 Parameter \aflags define behaviour of datum and set of created subwidgets. Default value of this
45 parameter is QDS::Control. Parameter \acomp specify the component name which will be used during
46 search of dictionary item.
48 QDS_RadioBox::QDS_RadioBox( const QString& id, QWidget* parent, const int flags, const QString& comp )
49 : QDS_Datum( id, parent, flags & ~( Label | Units ), comp )
56 QDS_RadioBox::~QDS_RadioBox()
61 Returns number of buttons in radio box. If total is 'false' then only
62 visible buttons are taken into account otherwise all buttons.
64 int QDS_RadioBox::count( bool total ) const
67 return myValue.count();
70 QPtrList<QRadioButton> bList;
77 Returns list of button identifiers \aids. If \atotal is 'false' then only visible
78 buttons are taken into account otherwise all buttons.
80 void QDS_RadioBox::values( QValueList<int>& ids, bool total ) const
83 for ( QIntList::const_iterator it = myDataIds.begin(); it != myDataIds.end(); ++it )
84 if ( total || ( myState.contains( *it ) && myState[*it] ) )
89 Returns visible state of button specified by \aid.
91 bool QDS_RadioBox::state( const int id ) const
94 if ( myState.contains( id ) )
100 Sets the visible state of button specified by \aid. If \aid is -1 then specified
101 state will be set to all buttons. If \aappend is set then keep status for other
102 buttons otherwise status of other buttons will be cleared.
104 void QDS_RadioBox::setState( const bool on, const int id, const bool append )
109 for ( IdStateMap::Iterator it = myState.begin(); it != myState.end(); ++it )
110 lst.append( it.key() );
115 setState( on, lst, append );
119 Sets the visible state of buttons specified by list of identifiers \aids.
120 If \aappend is set then keep status for other buttons otherwise status of other
121 buttons will be cleared.
123 void QDS_RadioBox::setState( const bool on, const QValueList<int>& ids, const bool append )
125 if ( ids.isEmpty() && append )
128 bool changed = false;
131 for ( uint i = 0; i < ids.count(); i++ )
132 aMap.insert( *ids.at( i ), 0 );
134 for ( IdStateMap::Iterator it = myState.begin(); it != myState.end(); ++it )
136 if ( aMap.contains( it.key() ) )
138 if ( it.data() != on )
144 else if ( !append && it.data() == on )
155 Sets the custom user buttons into the radio box. User buttons like standard dictionary
156 button from list will be added into the radio box. This functionality allow to user override
159 void QDS_RadioBox::setValues( const QValueList<int>& ids, const QStringList& names )
161 if ( ids.count() != names.count() )
169 This is an overloaded member function, provided for convenience.
170 It behaves essentially like the above function. It creates
171 QValueList (0, 1, 2 ... ) and call previous method
173 void QDS_RadioBox::setValues( const QStringList& names )
175 QValueList< int > ids;
176 for ( int i = 0, n = names.count(); i < n; i++ )
178 setValues( ids, names );
182 Returns string from radio box. Reimplemented. String which contains identifier of
183 currently selected button returned.
185 QString QDS_RadioBox::getString() const
188 QButtonGroup* bg = buttonGroup();
191 int id = bg->selectedId();
193 res = QString::number( id );
199 Sets the string into radio box. Reimplemented. Button with identifier from specified
200 string \atxt became selected in radio box.
202 void QDS_RadioBox::setString( const QString& txt )
204 QButtonGroup* bg = buttonGroup();
208 int oldId = bg->selectedId();
212 QPtrList<QRadioButton> bList;
214 for ( QPtrListIterator<QRadioButton> it( bList ); it.current(); ++it )
215 it.current()->setChecked( false );
220 int id = txt.toInt( &ok );
224 bool block = signalsBlocked();
225 blockSignals( true );
227 blockSignals( block );
230 int newId = bg->selectedId();
232 if ( oldId != newId )
235 QString str = getString();
236 emit activated( newId );
238 emit paramChanged( str );
243 Returns pointer to QButtonGroup widget.
245 QButtonGroup* QDS_RadioBox::buttonGroup() const
247 return ::qt_cast<QButtonGroup*>( controlWidget() );
251 Create QButtonGroup widget as control subwidget.
253 QWidget* QDS_RadioBox::createControl( QWidget* parent )
255 QButtonGroup* bg = new QButtonGroup( 1, Qt::Vertical, "", parent );
256 bg->setExclusive( true );
257 bg->setRadioButtonExclusive( true );
262 Notification about active unit system changing. Reimplemented from QDS_Datum.
263 Update radio box content.
265 void QDS_RadioBox::unitSystemChanged( const QString& system )
267 QDS_Datum::unitSystemChanged( system );
269 Handle(TColStd_HArray1OfInteger) anIds;
270 Handle(TColStd_HArray1OfExtendedString) aValues, anIcons;
272 Handle(DDS_DicItem) aDicItem = dicItem();
273 if ( !aDicItem.IsNull() )
274 aDicItem->GetListOfValues( aValues, anIds, anIcons );
279 QMap<int, QString> userMap;
280 QIntList::iterator iIt = myUserIds.begin();
281 QStringList::iterator sIt = myUserNames.begin();
282 for ( ; iIt != myUserIds.end() && sIt != myUserNames.end(); ++iIt, ++sIt )
283 userMap.insert( *iIt, *sIt );
285 if ( !anIds.IsNull() && !aValues.IsNull() &&
286 anIds->Length() == aValues->Length() )
288 for ( int i = anIds->Lower(); i <= anIds->Upper(); i++ )
291 int id = anIds->Value( i );
292 if ( userMap.contains( id ) )
293 aValue = userMap[id];
295 aValue = toQString( aValues->Value( i ) );
297 myDataIds.append( id );
298 myValue.insert( id, aValue );
299 myState.insert( id, true );
303 for ( iIt = myUserIds.begin(); iIt != myUserIds.end(); ++iIt )
306 if ( !myValue.contains( id ) )
308 myDataIds.append( id );
309 myValue.insert( id, userMap[id] );
314 for ( IdStateMap::Iterator it1 = myState.begin(); it1 != myState.end(); ++it1 )
315 if ( !myValue.contains( it1.key() ) )
316 del.append( it1.key() );
318 for ( IdValueMap::Iterator it2 = myValue.begin(); it2 != myValue.end(); ++it2 )
319 if ( !myState.contains( it2.key() ) )
320 add.append( it2.key() );
322 for ( QIntList::iterator iter1 = del.begin(); iter1 != del.end(); ++iter1 )
323 myState.remove( *iter1 );
325 for ( QIntList::iterator iter2 = add.begin(); iter2 != add.end(); ++iter2 )
326 myState.insert( *iter2, true );
328 QButtonGroup* bg = buttonGroup();
330 bg->setTitle( label() );
336 Notify about activation radio button.
338 void QDS_RadioBox::onToggled( bool on )
345 QString str = getString();
346 emit paramChanged( str );
350 Updates RadioBox after have change of visible state or buttons have been inserted/removed.
352 void QDS_RadioBox::updateRadioBox()
354 QButtonGroup* bg = buttonGroup();
358 int curId = bg->selectedId();
360 QPtrList<QRadioButton> bList;
362 for ( QPtrListIterator<QRadioButton> itr( bList ); itr.current(); ++itr )
363 delete itr.current();
365 for ( QIntList::const_iterator it = myDataIds.begin(); it != myDataIds.end(); ++it )
368 if ( !myValue.contains( id ) || !myState.contains( id ) || !myState[id] )
371 QRadioButton* rb = new QRadioButton( myValue[id], bg );
372 bg->insert( rb, id );
374 connect( rb, SIGNAL( toggled( bool ) ), this, SLOT( onToggled( bool ) ) );
380 if ( !bg->find( id ) )
382 QPtrList<QRadioButton> bList;
384 if ( !bList.isEmpty() )
385 id = bg->id( bList.getFirst() );
388 bool block = signalsBlocked();
389 blockSignals( true );
391 blockSignals( block );
394 if ( curId != bg->selectedId() )
398 emit paramChanged( getString() );
403 Returns the list of the radio buttons from the button group.
405 void QDS_RadioBox::buttons( QPtrList<QRadioButton>& lst ) const
407 lst.setAutoDelete( false );
410 QButtonGroup* bg = buttonGroup();
414 QObjectList* objs = bg->queryList( "QRadioButton" );
417 for ( QObjectListIt it( *objs ); it.current(); ++it )
419 QRadioButton* rb = ::qt_cast<QRadioButton*>( it.current() );