KDEUI
kbuttongroup.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "kbuttongroup.h"
00023
00024 #include <QChildEvent>
00025 #include <QHash>
00026 #include <QAbstractButton>
00027 #include <QSignalMapper>
00028
00029 class KButtonGroup::Private
00030 {
00031 public:
00032 Private( KButtonGroup* q )
00033 : q(q), clickedMapper(), pressedMapper(), releasedMapper(),
00034 currentId( -1 ), nextId( 0 ), wantToBeId( -1 )
00035 {
00036 connect( &clickedMapper, SIGNAL( mapped( int ) ), q, SLOT( slotClicked( int ) ) );
00037 connect( &pressedMapper, SIGNAL( mapped( int ) ), q, SIGNAL( pressed( int ) ) );
00038 connect( &releasedMapper, SIGNAL( mapped( int ) ), q, SIGNAL( released( int ) ) );
00039 }
00040
00041 void slotClicked( int id );
00042
00043 KButtonGroup *q;
00044 QSignalMapper clickedMapper;
00045 QSignalMapper pressedMapper;
00046 QSignalMapper releasedMapper;
00047
00048 QHash<QObject*, int> btnMap;
00049 int currentId;
00050 int nextId;
00051 int wantToBeId;
00052 };
00053
00054 KButtonGroup::KButtonGroup( QWidget* parent )
00055 : QGroupBox( parent ), d( new Private( this ) )
00056 {
00057 }
00058
00059 KButtonGroup::~KButtonGroup()
00060 {
00061 delete d;
00062 }
00063
00064 void KButtonGroup::setSelected( int id )
00065 {
00066 if ( !testAttribute( Qt::WA_WState_Polished ) )
00067 {
00068 d->wantToBeId = id;
00069 ensurePolished();
00070 return;
00071 }
00072
00073 QHash<QObject*, int>::Iterator it = d->btnMap.begin();
00074 QHash<QObject*, int>::Iterator itEnd = d->btnMap.end();
00075 QAbstractButton* button = 0;
00076 for ( ; it != itEnd; ++it )
00077 {
00078 if ( ( it.value() == id ) && ( button = qobject_cast<QAbstractButton*>( it.key() ) ) )
00079 {
00080 button->setChecked( true );
00081 d->currentId = id;
00082 emit changed( id );
00083 d->wantToBeId = -1;
00084 return;
00085 }
00086 }
00087 }
00088
00089 int KButtonGroup::selected() const
00090 {
00091 return d->currentId;
00092 }
00093
00094 void KButtonGroup::childEvent( QChildEvent* event )
00095 {
00096 if ( event->polished() )
00097 {
00098 QAbstractButton* button = qobject_cast<QAbstractButton*>( event->child() );
00099 if ( !d->btnMap.contains( event->child() ) && button )
00100 {
00101 connect( button, SIGNAL( clicked() ), &d->clickedMapper, SLOT( map() ) );
00102 d->clickedMapper.setMapping( button, d->nextId );
00103
00104 connect( button, SIGNAL( pressed() ), &d->pressedMapper, SLOT( map() ) );
00105 d->pressedMapper.setMapping( button, d->nextId );
00106
00107 connect( button, SIGNAL( released() ), &d->releasedMapper, SLOT( map() ) );
00108 d->releasedMapper.setMapping( button, d->nextId );
00109
00110 d->btnMap[ button ] = d->nextId;
00111
00112 if ( d->nextId == d->wantToBeId )
00113 {
00114 d->currentId = d->wantToBeId;
00115 d->wantToBeId = -1;
00116 button->setChecked( true );
00117 emit changed( d->currentId );
00118 }
00119
00120 ++d->nextId;
00121 }
00122 }
00123 else if ( event->removed() )
00124 {
00125 QObject* obj = event->child();
00126 QHash<QObject*, int>::ConstIterator it = d->btnMap.constFind( obj );
00127 if ( it != d->btnMap.constEnd() )
00128 {
00129 d->clickedMapper.removeMappings( obj );
00130 d->pressedMapper.removeMappings( obj );
00131 d->releasedMapper.removeMappings( obj );
00132
00133 if ( it.value() == d->currentId )
00134 d->currentId = -1;
00135
00136 d->btnMap.remove( obj );
00137 }
00138 }
00139
00140
00141 QGroupBox::childEvent( event );
00142 }
00143
00144 int KButtonGroup::id( QAbstractButton* button ) const
00145 {
00146 QHash<QObject*, int>::ConstIterator it = d->btnMap.constFind( button );
00147 if ( it != d->btnMap.constEnd() )
00148 {
00149 return it.value();
00150 }
00151 return -1;
00152 }
00153
00154 void KButtonGroup::Private::slotClicked( int id )
00155 {
00156 currentId = id;
00157 emit q->clicked( id );
00158 emit q->changed( id );
00159 }
00160
00161 #include "kbuttongroup.moc"
00162