• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • kdelibs
  • Sitemap
  • Contact Us
 

KDEUI

kbuttongroup.cpp

Go to the documentation of this file.
00001 /*
00002     This file is part of the KDE Libraries
00003 
00004     Copyright (C) 2006 Pino Toscano <toscano.pino@tiscali.it>
00005 
00006     This library is free software; you can redistribute it and/or
00007     modify it under the terms of the GNU Library General Public
00008     License as published by the Free Software Foundation; either
00009     version 2 of the License, or (at your option) any later version.
00010 
00011     This library is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00014     Library General Public License for more details.
00015 
00016     You should have received a copy of the GNU Library General Public License
00017     along with this library; see the file COPYING.LIB. If not, write to
00018     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00019     Boston, MA 02110-1301, USA.
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.end() )
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   // be transparent
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.end() )
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 

KDEUI

Skip menu "KDEUI"
  • Main Page
  • Modules
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • Kross
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.7
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal