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

KDEUI

  • sources
  • kde-4.12
  • kdelibs
  • 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),
34  clickedMapper(),
35  pressedMapper(),
36  releasedMapper(),
37  currentId(-1),
38  nextId(0),
39  wantToBeId(-1)
40  {
41  connect(&clickedMapper, SIGNAL(mapped(int)), q, SLOT(slotClicked(int)));
42  connect(&pressedMapper, SIGNAL(mapped(int)), q, SIGNAL(pressed(int)));
43  connect(&releasedMapper, SIGNAL(mapped(int)), q, SIGNAL(released(int)));
44  }
45 
46  void slotClicked(int id);
47 
48  KButtonGroup *q;
49  QSignalMapper clickedMapper;
50  QSignalMapper pressedMapper;
51  QSignalMapper releasedMapper;
52 
53  QHash<QObject*, int> btnMap;
54  int currentId;
55  int nextId;
56  int wantToBeId;
57 };
58 
59 KButtonGroup::KButtonGroup(QWidget* parent) : QGroupBox(parent), d(new Private(this))
60 {
61 }
62 
63 KButtonGroup::~KButtonGroup()
64 {
65  delete d;
66 }
67 
68 void KButtonGroup::setSelected(int id)
69 {
70  if (!testAttribute(Qt::WA_WState_Polished)) {
71  d->wantToBeId = id;
72  ensurePolished();
73  return;
74  }
75 
76  QHash<QObject*, int>::Iterator it = d->btnMap.begin();
77  QHash<QObject*, int>::Iterator itEnd = d->btnMap.end();
78  QAbstractButton* button = 0;
79 
80  for (; it != itEnd; ++it) {
81  if ((it.value() == id) && (button = qobject_cast<QAbstractButton*>(it.key()))) {
82  button->setChecked(true);
83  d->currentId = id;
84  emit changed(id);
85  d->wantToBeId = -1;
86  return;
87  }
88  }
89  // button not found, it might still show up though, eg. because of premature polishing above
90  d->wantToBeId = id;
91 }
92 
93 int KButtonGroup::selected() const
94 {
95  return d->currentId;
96 }
97 
98 void KButtonGroup::childEvent(QChildEvent* event)
99 {
100  if (event->polished()) {
101  QAbstractButton* button = qobject_cast<QAbstractButton*>(event->child());
102  if (!d->btnMap.contains( event->child()) && button) {
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  d->currentId = d->wantToBeId;
116  d->wantToBeId = -1;
117  button->setChecked(true);
118  emit changed( d->currentId );
119  }
120 
121  ++d->nextId;
122  }
123  } else if (event->removed()) {
124  QObject* obj = event->child();
125  QHash<QObject*, int>::ConstIterator it = d->btnMap.constFind(obj);
126 
127  if (it != d->btnMap.constEnd()) {
128  d->clickedMapper.removeMappings(obj);
129  d->pressedMapper.removeMappings(obj);
130  d->releasedMapper.removeMappings(obj);
131 
132  if (it.value() == d->currentId) {
133  d->currentId = -1;
134  }
135 
136  d->btnMap.remove(obj);
137  }
138  }
139 
140  // be transparent
141  QGroupBox::childEvent(event);
142 }
143 
144 int KButtonGroup::id(QAbstractButton* button) const
145 {
146  QHash<QObject*, int>::ConstIterator it = d->btnMap.constFind(button);
147 
148  if (it != d->btnMap.constEnd()) {
149  return it.value();
150  }
151 
152  return -1;
153 }
154 
155 void KButtonGroup::Private::slotClicked(int id)
156 {
157  currentId = id;
158  emit q->clicked(id);
159  emit q->changed(id);
160 }
161 
162 #include "kbuttongroup.moc"
163 
KButtonGroup::KButtonGroup
KButtonGroup(QWidget *parent=0)
Construct a new empty KGroupBox.
Definition: kbuttongroup.cpp:59
kbuttongroup.h
QWidget
QHash< QObject *, int >
QObject
KButtonGroup::selected
int selected() const
Return the index of the selected QAbstractButton, among the QAbstractButton's added to the widget...
Definition: kbuttongroup.cpp:93
KButtonGroup::pressed
void pressed(int id)
The button with index id was pressed.
KButtonGroup
Group box with index of the selected button KButtonGroup is a simple group box that can keep track of...
Definition: kbuttongroup.h:41
KButtonGroup::changed
void changed(int id)
Emitted when anything (a click on a button, or calling setSelected()) change the id of the current se...
KButtonGroup::setSelected
void setSelected(int id)
Select the id -th button.
Definition: kbuttongroup.cpp:68
QGroupBox
KButtonGroup::childEvent
virtual void childEvent(QChildEvent *event)
Reimplemented from QGroupBox.
Definition: kbuttongroup.cpp:98
KButtonGroup::Private
friend class Private
Definition: kbuttongroup.h:106
KButtonGroup::~KButtonGroup
~KButtonGroup()
Destroys the widget.
Definition: kbuttongroup.cpp:63
KButtonGroup::released
void released(int id)
The button with index id was released.
KButtonGroup::clicked
void clicked(int id)
The button with index id was clicked.
KButtonGroup::id
int id(QAbstractButton *button) const
Definition: kbuttongroup.cpp:144
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:49:13 by doxygen 1.8.7 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 API Reference

Skip menu "kdelibs 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
  • kjsembed
  •   WTF
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Nepomuk-Core
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

Search



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