• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.11.5 API Reference
  • KDE Home
  • Contact Us
 

KDEUI

  • kdeui
  • widgets
kbuttongroup.cpp
Go to the documentation of this file.
1 /*
2  This file is part of the KDE Libraries
3 
4  Copyright (C) 2006 Pino Toscano <toscano.pino@tiscali.it>
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Library General Public
8  License as published by the Free Software Foundation; either
9  version 2 of the License, or (at your option) any later version.
10 
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  Library General Public License for more details.
15 
16  You should have received a copy of the GNU Library General Public License
17  along with this library; see the file COPYING.LIB. If not, write to
18  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  Boston, MA 02110-1301, USA.
20 */
21 
22 #include "kbuttongroup.h"
23 
24 #include <QChildEvent>
25 #include <QHash>
26 #include <QAbstractButton>
27 #include <QSignalMapper>
28 
29 class KButtonGroup::Private
30 {
31  public:
32  Private( KButtonGroup* q )
33  : q(q), clickedMapper(), pressedMapper(), releasedMapper(),
34  currentId( -1 ), nextId( 0 ), wantToBeId( -1 )
35  {
36  connect( &clickedMapper, SIGNAL(mapped(int)), q, SLOT(slotClicked(int)) );
37  connect( &pressedMapper, SIGNAL(mapped(int)), q, SIGNAL(pressed(int)) );
38  connect( &releasedMapper, SIGNAL(mapped(int)), q, SIGNAL(released(int)) );
39  }
40 
41  void slotClicked( int id );
42 
43  KButtonGroup *q;
44  QSignalMapper clickedMapper;
45  QSignalMapper pressedMapper;
46  QSignalMapper releasedMapper;
47 
48  QHash<QObject*, int> btnMap;
49  int currentId;
50  int nextId;
51  int wantToBeId;
52 };
53 
54 KButtonGroup::KButtonGroup( QWidget* parent )
55  : QGroupBox( parent ), d( new Private( this ) )
56 {
57 }
58 
59 KButtonGroup::~KButtonGroup()
60 {
61  delete d;
62 }
63 
64 void KButtonGroup::setSelected( int id )
65 {
66  if ( !testAttribute( Qt::WA_WState_Polished ) )
67  {
68  d->wantToBeId = id;
69  ensurePolished();
70  return;
71  }
72 
73  QHash<QObject*, int>::Iterator it = d->btnMap.begin();
74  QHash<QObject*, int>::Iterator itEnd = d->btnMap.end();
75  QAbstractButton* button = 0;
76  for ( ; it != itEnd; ++it )
77  {
78  if ( ( it.value() == id ) && ( button = qobject_cast<QAbstractButton*>( it.key() ) ) )
79  {
80  button->setChecked( true );
81  d->currentId = id;
82  emit changed( id );
83  d->wantToBeId = -1;
84  return;
85  }
86  }
87  // button not found, it might still show up though, eg. because of premature polishing above
88  d->wantToBeId = id;
89 }
90 
91 int KButtonGroup::selected() const
92 {
93  return d->currentId;
94 }
95 
96 void KButtonGroup::childEvent( QChildEvent* event )
97 {
98  if ( event->polished() )
99  {
100  QAbstractButton* button = qobject_cast<QAbstractButton*>( event->child() );
101  if ( !d->btnMap.contains( event->child() ) && button )
102  {
103  connect( button, SIGNAL(clicked()), &d->clickedMapper, SLOT(map()) );
104  d->clickedMapper.setMapping( button, d->nextId );
105 
106  connect( button, SIGNAL(pressed()), &d->pressedMapper, SLOT(map()) );
107  d->pressedMapper.setMapping( button, d->nextId );
108 
109  connect( button, SIGNAL(released()), &d->releasedMapper, SLOT(map()) );
110  d->releasedMapper.setMapping( button, d->nextId );
111 
112  d->btnMap[ button ] = d->nextId;
113 
114  if ( d->nextId == d->wantToBeId )
115  {
116  d->currentId = d->wantToBeId;
117  d->wantToBeId = -1;
118  button->setChecked( true );
119  emit changed( d->currentId );
120  }
121 
122  ++d->nextId;
123  }
124  }
125  else if ( event->removed() )
126  {
127  QObject* obj = event->child();
128  QHash<QObject*, int>::ConstIterator it = d->btnMap.constFind( obj );
129  if ( it != d->btnMap.constEnd() )
130  {
131  d->clickedMapper.removeMappings( obj );
132  d->pressedMapper.removeMappings( obj );
133  d->releasedMapper.removeMappings( obj );
134 
135  if ( it.value() == d->currentId )
136  d->currentId = -1;
137 
138  d->btnMap.remove( obj );
139  }
140  }
141 
142  // be transparent
143  QGroupBox::childEvent( event );
144 }
145 
146 int KButtonGroup::id( QAbstractButton* button ) const
147 {
148  QHash<QObject*, int>::ConstIterator it = d->btnMap.constFind( button );
149  if ( it != d->btnMap.constEnd() )
150  {
151  return it.value();
152  }
153  return -1;
154 }
155 
156 void KButtonGroup::Private::slotClicked( int id )
157 {
158  currentId = id;
159  emit q->clicked( id );
160  emit q->changed( id );
161 }
162 
163 #include "kbuttongroup.moc"
164 
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Sep 23 2014 09:57:44 by doxygen 1.8.3.1 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KDEUI

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

kdelibs-4.11.5 API Reference

Skip menu "kdelibs-4.11.5 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal