1 // Copyright (C) 2007-2015 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, or (at your option) any later version.
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
23 #include "QDS_RadioBox.h"
25 #include <TColStd_HArray1OfInteger.hxx>
26 #include <TColStd_HArray1OfExtendedString.hxx>
28 #include <QButtonGroup>
30 #include <QVBoxLayout>
31 #include <QRadioButton>
35 \brief Datum with control corresponding to button group with set of exclusive radio buttons.
37 This control used for datum with enumerable values. It can be used for datum which has
38 type of value 'List'. Each radio button of group box is defined by two properties:
39 integer identifier and string name. All operations on radio buttons are performed via identifier.
41 If datum label text is specified, then it is displayed in the group box title.
47 Create radio button box datum object with datum identifier \a id and parent widget \a parent.
49 Parameter \a flags defines behaviour of datum and set of created
50 subwidgets. Default value of this parameter is QDS::All.
52 Parameter \a comp specifies the component name which will be used
53 when searching the dictionary item.
55 \param id datum identifier
56 \param parent parent widget
57 \param flags datum flags
60 QDS_RadioBox::QDS_RadioBox( const QString& id, QWidget* parent, const int flags, const QString& comp )
61 : QDS_Datum( id, parent, flags & ~( Label | Units ), comp ),
69 QDS_RadioBox::~QDS_RadioBox()
74 \brief Get number of buttons in radio box.
76 If \a total is \c false, only visible buttons are taken into account;
77 otherwise total number of buttons is returned
79 \param total get number of visible buttons if \c true and total number of buttons if \c false
80 \return requested number of items
82 int QDS_RadioBox::count( bool total ) const
85 return myValue.count();
88 QList<QRadioButton*> bList;
95 \brief Get buttons identifiers.
96 \param ids returned list of buttons IDs
97 \param total take into account only visible buttons if \c true and all buttons if \c false
99 void QDS_RadioBox::values( QList<int>& ids, bool total ) const
102 for ( QIntList::const_iterator it = myDataIds.begin(); it != myDataIds.end(); ++it )
103 if ( total || ( myState.contains( *it ) && myState[*it] ) )
108 \brief Get visibility state of the button specified by \a id.
110 \return item visibility state
112 bool QDS_RadioBox::state( const int id ) const
115 if ( myState.contains( id ) )
121 \brief Set the visibility state of the button specified by \a id.
123 If \a id is -1 then specified state will be set to all buttons.
125 If \a append is set to \c true, keep current status for other buttons,
126 otherwise status of other buttons is cleared.
128 \param on new visibility state
130 \param append if \c true, keep original status for other buttons
132 void QDS_RadioBox::setState( const bool on, const int id, const bool append )
137 for ( IdStateMap::Iterator it = myState.begin(); it != myState.end(); ++it )
138 lst.append( it.key() );
143 setState( on, lst, append );
147 \brief Set the visibility state of buttons specified by \a ids.
149 If \a append is set to \c true, keep the current status for other buttons,
150 otherwise status of other buttons is cleared.
152 \param on new visibility state
153 \param ids buttons IDs list
154 \param append if \c true, keep original status for other buttons
156 void QDS_RadioBox::setState( const bool on, const QList<int>& ids, const bool append )
158 if ( ids.isEmpty() && append )
161 bool changed = false;
164 for ( uint i = 0; i < ids.count(); i++ )
165 aMap.insert( ids.at( i ), 0 );
167 for ( IdStateMap::Iterator it = myState.begin(); it != myState.end(); ++it )
169 if ( aMap.contains( it.key() ) )
171 if ( it.value() != on )
177 else if ( !append && it.value() == on )
188 \brief Set the custom user buttons into the radio box.
190 User items like standard dictionary buttons will be added
191 into the radio box. This function allows user to customize
194 \param ids buttons IDs
195 \param names buttons names
197 void QDS_RadioBox::setValues( const QList<int>& ids, const QStringList& names )
199 if ( ids.count() != names.count() )
207 \brief Set the custom user buttons into the radio box.
210 User buttons like standard dictionary buttons will be added
211 into the radio box. This function allows user to customize
214 Uses (0, 1, 2 ... ) as buttons IDs.
216 \param names buttons names
218 void QDS_RadioBox::setValues( const QStringList& names )
221 for ( int i = 0, n = names.count(); i < n; i++ )
223 setValues( ids, names );
227 \brief Get string from the radio box.
229 String which contains identifier of the currently selected button is returned.
230 \return identifier of the current button converted to string
232 QString QDS_RadioBox::getString() const
235 QButtonGroup* bg = buttonGroup();
238 int id = bg->checkedId();
240 res = QString::number( id );
246 \brief Set the string value to the radio box widget.
248 Button with the identifier from specified string \a txt becomes selected in the radio box.
250 \param txt string value
252 void QDS_RadioBox::setString( const QString& txt )
254 QButtonGroup* bg = buttonGroup();
258 int oldId = bg->checkedId();
262 QList<QRadioButton*> bList;
264 QListIterator<QRadioButton*> it( bList );
265 while ( it.hasNext() )
266 it.next()->setChecked( false );
271 int id = txt.toInt( &ok );
275 bool block = signalsBlocked();
276 blockSignals( true );
277 bg->button(id)->setChecked(true);
278 blockSignals( block );
281 int newId = bg->checkedId();
283 if ( oldId != newId )
286 QString str = getString();
287 emit activated( newId );
289 emit paramChanged( str );
294 \brief Get internal button group.
295 \return pointer to the QButtonGroup object
297 QButtonGroup* QDS_RadioBox::buttonGroup() const
299 return myButtonGroup;
303 \brief Get internal group box widget.
304 \return pointer to the QGroupBox widget
306 QGroupBox* QDS_RadioBox::groupBox() const
308 return ::qobject_cast<QGroupBox*>( controlWidget() );
312 \brief Get radio button group box widget.
313 \return internal group box widget
315 QWidget* QDS_RadioBox::createControl( QWidget* parent )
317 myButtonGroup = new QButtonGroup( parent );
318 myButtonGroup->setExclusive( true );
320 QGroupBox *gb = new QGroupBox( "", parent );
321 QVBoxLayout *vbox = new QVBoxLayout;
328 \brief Process notification about active units system changing.
330 Update radio box contents.
332 \param system new active units system
334 void QDS_RadioBox::unitSystemChanged( const QString& system )
336 QDS_Datum::unitSystemChanged( system );
338 Handle(TColStd_HArray1OfInteger) anIds;
339 Handle(TColStd_HArray1OfExtendedString) aValues, anIcons;
341 Handle(DDS_DicItem) aDicItem = dicItem();
342 if ( !aDicItem.IsNull() )
343 aDicItem->GetListOfValues( aValues, anIds, anIcons );
348 QMap<int, QString> userMap;
349 QIntList::iterator iIt = myUserIds.begin();
350 QStringList::iterator sIt = myUserNames.begin();
351 for ( ; iIt != myUserIds.end() && sIt != myUserNames.end(); ++iIt, ++sIt )
352 userMap.insert( *iIt, *sIt );
354 if ( !anIds.IsNull() && !aValues.IsNull() &&
355 anIds->Length() == aValues->Length() )
357 for ( int i = anIds->Lower(); i <= anIds->Upper(); i++ )
360 int id = anIds->Value( i );
361 if ( userMap.contains( id ) )
362 aValue = userMap[id];
364 aValue = toQString( aValues->Value( i ) );
366 myDataIds.append( id );
367 myValue.insert( id, aValue );
368 myState.insert( id, true );
372 for ( iIt = myUserIds.begin(); iIt != myUserIds.end(); ++iIt )
375 if ( !myValue.contains( id ) )
377 myDataIds.append( id );
378 myValue.insert( id, userMap[id] );
383 for ( IdStateMap::Iterator it1 = myState.begin(); it1 != myState.end(); ++it1 )
384 if ( !myValue.contains( it1.key() ) )
385 del.append( it1.key() );
387 for ( IdValueMap::Iterator it2 = myValue.begin(); it2 != myValue.end(); ++it2 )
388 if ( !myState.contains( it2.key() ) )
389 add.append( it2.key() );
391 for ( QIntList::iterator iter1 = del.begin(); iter1 != del.end(); ++iter1 )
392 myState.remove( *iter1 );
394 for ( QIntList::iterator iter2 = add.begin(); iter2 != add.end(); ++iter2 )
395 myState.insert( *iter2, true );
397 QGroupBox* gb = groupBox();
398 if ( gb ) gb->setTitle( label() );
404 \brief Called when user toggles any radio button.
405 \param on new radio button state
407 void QDS_RadioBox::onToggled( bool on )
414 QString str = getString();
415 emit paramChanged( str );
419 \brief Update radio box.
421 void QDS_RadioBox::updateRadioBox()
423 QButtonGroup* bg = buttonGroup();
427 QGroupBox* gb = groupBox();
431 int curId = bg->checkedId();
433 QList<QRadioButton*> bList;
435 QListIterator<QRadioButton*> itr( bList );
436 while ( itr.hasNext() ) {
437 QRadioButton* aButton = itr.next();
438 if ( gb->layout() ) gb->layout()->removeWidget(aButton);
442 for ( QIntList::const_iterator it = myDataIds.begin(); it != myDataIds.end(); ++it )
445 if ( !myValue.contains( id ) || !myState.contains( id ) || !myState[id] )
448 QRadioButton* rb = new QRadioButton( myValue[id] );
449 ((QObject*)rb)->setParent( bg );
450 bg->addButton( rb, id );
451 if ( gb->layout() ) gb->layout()->addWidget(rb);
453 connect( rb, SIGNAL( toggled( bool ) ), this, SLOT( onToggled( bool ) ) );
459 if ( !bg->button( id ) )
461 QList<QRadioButton*> bList;
463 if ( !bList.isEmpty() )
464 id = bg->id( bList.empty() ? 0 : bList.first() );
467 bool block = signalsBlocked();
468 blockSignals( true );
469 bg->button(id)->setChecked(true);
470 blockSignals( block );
473 if ( curId != bg->checkedId() )
476 QString str = getString();
478 emit paramChanged( str );
483 \brief Get all the radio buttons from the radio box.
484 \param lst returned list of radio buttons
486 void QDS_RadioBox::buttons( QList<QRadioButton*>& lst ) const
490 QButtonGroup* bg = buttonGroup();
494 QList<QRadioButton*> objs = bg->findChildren<QRadioButton*>();
495 QListIterator<QRadioButton*> it( objs );
496 while ( it.hasNext() )
497 lst.append( it.next() );
501 \fn void QDS_RadioBox::activated( int id );
502 \brief The signal is emitted when any radio button is toggled.