1 // Copyright (C) 2007-2008 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 #include "QDS_RadioBox.h"
24 #include <TColStd_HArray1OfInteger.hxx>
25 #include <TColStd_HArray1OfExtendedString.hxx>
27 #include <QButtonGroup>
29 #include <QVBoxLayout>
30 #include <QRadioButton>
34 \brief Datum with control corresponding to button group with set of exclusive radio buttons.
36 This control used for datum with enumerable values. It can be used for datum which has
37 type of value 'List'. Each radio button of group box is defined by two properties:
38 integer identifier and string name. All operations on radio buttons are performed via identifier.
40 If datum label text is specified, then it is displayed in the group box title.
46 Create radio button box datum object with datum identifier \a id and parent widget \a parent.
48 Parameter \a flags defines behaviour of datum and set of created
49 subwidgets. Default value of this parameter is QDS::All.
51 Parameter \a comp specifies the component name which will be used
52 when searching the dictionary item.
54 \param id datum identifier
55 \param parent parent widget
56 \param flags datum flags
59 QDS_RadioBox::QDS_RadioBox( const QString& id, QWidget* parent, const int flags, const QString& comp )
60 : QDS_Datum( id, parent, flags & ~( Label | Units ), comp ),
68 QDS_RadioBox::~QDS_RadioBox()
73 \brief Get number of buttons in radio box.
75 If \a total is \c false, only visible buttons are taken into account;
76 otherwise total number of buttons is returned
78 \param total get number of visible buttons if \c true and total number of buttons if \c false
79 \return requested number of items
81 int QDS_RadioBox::count( bool total ) const
84 return myValue.count();
87 QList<QRadioButton*> bList;
94 \brief Get buttons identifiers.
95 \param ids returned list of buttons IDs
96 \param total take into account only visible buttons if \c true and all buttons if \c false
98 void QDS_RadioBox::values( QList<int>& ids, bool total ) const
101 for ( QIntList::const_iterator it = myDataIds.begin(); it != myDataIds.end(); ++it )
102 if ( total || ( myState.contains( *it ) && myState[*it] ) )
107 \brief Get visibility state of the button specified by \a id.
109 \return item visibility state
111 bool QDS_RadioBox::state( const int id ) const
114 if ( myState.contains( id ) )
120 \brief Set the visibility state of the button specified by \a id.
122 If \a id is -1 then specified state will be set to all buttons.
124 If \a append is set to \c true, keep current status for other buttons,
125 otherwise status of other buttons is cleared.
127 \param on new visibility state
129 \param append if \c true, keep original status for other buttons
131 void QDS_RadioBox::setState( const bool on, const int id, const bool append )
136 for ( IdStateMap::Iterator it = myState.begin(); it != myState.end(); ++it )
137 lst.append( it.key() );
142 setState( on, lst, append );
146 \brief Set the visibility state of buttons specified by \a ids.
148 If \a append is set to \c true, keep the current status for other buttons,
149 otherwise status of other buttons is cleared.
151 \param on new visibility state
152 \param ids buttons IDs list
153 \param append if \c true, keep original status for other buttons
155 void QDS_RadioBox::setState( const bool on, const QList<int>& ids, const bool append )
157 if ( ids.isEmpty() && append )
160 bool changed = false;
163 for ( uint i = 0; i < ids.count(); i++ )
164 aMap.insert( ids.at( i ), 0 );
166 for ( IdStateMap::Iterator it = myState.begin(); it != myState.end(); ++it )
168 if ( aMap.contains( it.key() ) )
170 if ( it.value() != on )
176 else if ( !append && it.value() == on )
187 \brief Set the custom user buttons into the radio box.
189 User items like standard dictionary buttons will be added
190 into the radio box. This function allows user to customize
193 \param ids buttons IDs
194 \param names buttons names
196 void QDS_RadioBox::setValues( const QList<int>& ids, const QStringList& names )
198 if ( ids.count() != names.count() )
206 \brief Set the custom user buttons into the radio box.
209 User buttons like standard dictionary buttons will be added
210 into the radio box. This function allows user to customize
213 Uses (0, 1, 2 ... ) as buttons IDs.
215 \param names buttons names
217 void QDS_RadioBox::setValues( const QStringList& names )
220 for ( int i = 0, n = names.count(); i < n; i++ )
222 setValues( ids, names );
226 \brief Get string from the radio box.
228 String which contains identifier of the currently selected button is returned.
229 \return identifier of the current button converted to string
231 QString QDS_RadioBox::getString() const
234 QButtonGroup* bg = buttonGroup();
237 int id = bg->checkedId();
239 res = QString::number( id );
245 \brief Set the string value to the radio box widget.
247 Button with the identifier from specified string \a txt becomes selected in the radio box.
249 \param txt string value
251 void QDS_RadioBox::setString( const QString& txt )
253 QButtonGroup* bg = buttonGroup();
257 int oldId = bg->checkedId();
261 QList<QRadioButton*> bList;
263 QListIterator<QRadioButton*> it( bList );
264 while ( it.hasNext() )
265 it.next()->setChecked( false );
270 int id = txt.toInt( &ok );
274 bool block = signalsBlocked();
275 blockSignals( true );
276 bg->button(id)->setChecked(true);
277 blockSignals( block );
280 int newId = bg->checkedId();
282 if ( oldId != newId )
285 QString str = getString();
286 emit activated( newId );
288 emit paramChanged( str );
293 \brief Get internal button group.
294 \return pointer to the QButtonGroup object
296 QButtonGroup* QDS_RadioBox::buttonGroup() const
298 return myButtonGroup;
302 \brief Get internal group box widget.
303 \return pointer to the QGroupBox widget
305 QGroupBox* QDS_RadioBox::groupBox() const
307 return ::qobject_cast<QGroupBox*>( controlWidget() );
311 \brief Get radio button group box widget.
312 \return internal group box widget
314 QWidget* QDS_RadioBox::createControl( QWidget* parent )
316 myButtonGroup = new QButtonGroup( parent );
317 myButtonGroup->setExclusive( true );
319 QGroupBox *gb = new QGroupBox( "", parent );
320 QVBoxLayout *vbox = new QVBoxLayout;
327 \brief Process notification about active units system changing.
329 Update radio box contents.
331 \param system new active units system
333 void QDS_RadioBox::unitSystemChanged( const QString& system )
335 QDS_Datum::unitSystemChanged( system );
337 Handle(TColStd_HArray1OfInteger) anIds;
338 Handle(TColStd_HArray1OfExtendedString) aValues, anIcons;
340 Handle(DDS_DicItem) aDicItem = dicItem();
341 if ( !aDicItem.IsNull() )
342 aDicItem->GetListOfValues( aValues, anIds, anIcons );
347 QMap<int, QString> userMap;
348 QIntList::iterator iIt = myUserIds.begin();
349 QStringList::iterator sIt = myUserNames.begin();
350 for ( ; iIt != myUserIds.end() && sIt != myUserNames.end(); ++iIt, ++sIt )
351 userMap.insert( *iIt, *sIt );
353 if ( !anIds.IsNull() && !aValues.IsNull() &&
354 anIds->Length() == aValues->Length() )
356 for ( int i = anIds->Lower(); i <= anIds->Upper(); i++ )
359 int id = anIds->Value( i );
360 if ( userMap.contains( id ) )
361 aValue = userMap[id];
363 aValue = toQString( aValues->Value( i ) );
365 myDataIds.append( id );
366 myValue.insert( id, aValue );
367 myState.insert( id, true );
371 for ( iIt = myUserIds.begin(); iIt != myUserIds.end(); ++iIt )
374 if ( !myValue.contains( id ) )
376 myDataIds.append( id );
377 myValue.insert( id, userMap[id] );
382 for ( IdStateMap::Iterator it1 = myState.begin(); it1 != myState.end(); ++it1 )
383 if ( !myValue.contains( it1.key() ) )
384 del.append( it1.key() );
386 for ( IdValueMap::Iterator it2 = myValue.begin(); it2 != myValue.end(); ++it2 )
387 if ( !myState.contains( it2.key() ) )
388 add.append( it2.key() );
390 for ( QIntList::iterator iter1 = del.begin(); iter1 != del.end(); ++iter1 )
391 myState.remove( *iter1 );
393 for ( QIntList::iterator iter2 = add.begin(); iter2 != add.end(); ++iter2 )
394 myState.insert( *iter2, true );
396 QGroupBox* gb = groupBox();
397 if ( gb ) gb->setTitle( label() );
403 \brief Called when user toggles any radio button.
404 \param on new radio button state
406 void QDS_RadioBox::onToggled( bool on )
413 QString str = getString();
414 emit paramChanged( str );
418 \brief Update radio box.
420 void QDS_RadioBox::updateRadioBox()
422 QButtonGroup* bg = buttonGroup();
426 QGroupBox* gb = groupBox();
430 int curId = bg->checkedId();
432 QList<QRadioButton*> bList;
434 QListIterator<QRadioButton*> itr( bList );
435 while ( itr.hasNext() ) {
436 QRadioButton* aButton = itr.next();
437 if ( gb->layout() ) gb->layout()->removeWidget(aButton);
441 for ( QIntList::const_iterator it = myDataIds.begin(); it != myDataIds.end(); ++it )
444 if ( !myValue.contains( id ) || !myState.contains( id ) || !myState[id] )
447 QRadioButton* rb = new QRadioButton( myValue[id] );
448 ((QObject*)rb)->setParent( bg );
449 bg->addButton( rb, id );
450 if ( gb->layout() ) gb->layout()->addWidget(rb);
452 connect( rb, SIGNAL( toggled( bool ) ), this, SLOT( onToggled( bool ) ) );
458 if ( !bg->button( id ) )
460 QList<QRadioButton*> bList;
462 if ( !bList.isEmpty() )
463 id = bg->id( bList.empty() ? 0 : bList.first() );
466 bool block = signalsBlocked();
467 blockSignals( true );
468 bg->button(id)->setChecked(true);
469 blockSignals( block );
472 if ( curId != bg->checkedId() )
475 QString str = getString();
477 emit paramChanged( str );
482 \brief Get all the radio buttons from the radio box.
483 \param lst returned list of radio buttons
485 void QDS_RadioBox::buttons( QList<QRadioButton*>& lst ) const
489 QButtonGroup* bg = buttonGroup();
493 QList<QRadioButton*> objs = bg->findChildren<QRadioButton*>();
494 QListIterator<QRadioButton*> it( objs );
495 while ( it.hasNext() )
496 lst.append( it.next() );
500 \fn void QDS_RadioBox::activated( int id );
501 \brief The signal is emitted when any radio button is toggled.