buttongroup.cpp 3.02 KB
Newer Older
David Jarvie's avatar
David Jarvie committed
1
/*
David Jarvie's avatar
David Jarvie committed
2
 *  buttongroup.cpp  -  QButtonGroup with an extra signal
David Jarvie's avatar
David Jarvie committed
3
 *  Program:  kalarm
4
 *  Copyright © 2002,2004,2005,2008 by David Jarvie <djarvie@kde.org>
David Jarvie's avatar
David Jarvie committed
5 6 7 8 9 10 11 12 13 14 15
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *  GNU General Public License for more details.
 *
David Jarvie's avatar
David Jarvie committed
16 17 18
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
David Jarvie's avatar
David Jarvie committed
19 20 21
 */
#include "kalarm.h"

Laurent Montel's avatar
moc++  
Laurent Montel committed
22
#include "buttongroup.h"
23
#include <QAbstractButton>
David Jarvie's avatar
David Jarvie committed
24 25


David Jarvie's avatar
David Jarvie committed
26
ButtonGroup::ButtonGroup(QObject* parent)
27
    : QButtonGroup(parent)
David Jarvie's avatar
David Jarvie committed
28
{
29
    connect(this, SIGNAL(buttonClicked(QAbstractButton*)), SIGNAL(buttonSet(QAbstractButton*)));
David Jarvie's avatar
David Jarvie committed
30 31
}

32
/******************************************************************************
David Jarvie's avatar
David Jarvie committed
33
 * Inserts a button into the group.
34
 */
David Jarvie's avatar
David Jarvie committed
35
void ButtonGroup::addButton(QAbstractButton* button)
36
{
37 38
    QButtonGroup::addButton(button);
    connect(button, SIGNAL(toggled(bool)), SLOT(slotButtonToggled(bool)));
39 40
}

David Jarvie's avatar
David Jarvie committed
41 42 43 44 45
/******************************************************************************
 * Inserts a button into the group.
 */
void ButtonGroup::addButton(QAbstractButton* button, int id)
{
46 47
    addButton(button);
    mIds[id] = button;
David Jarvie's avatar
David Jarvie committed
48 49 50 51 52 53 54 55
}

/******************************************************************************
 * Returns the ID of the specified button.
 * Reply = -1 if not found.
 */
int ButtonGroup::id(QAbstractButton* button) const
{
56 57 58 59
    for (QMap<int, QAbstractButton*>::ConstIterator it = mIds.constBegin();  it != mIds.constEnd();  ++it)
        if (it.value() == button)
            return it.key();
    return -1;
David Jarvie's avatar
David Jarvie committed
60 61 62 63 64 65 66
}

/******************************************************************************
 * Returns the button with the specified ID.
 */
QAbstractButton* ButtonGroup::find(int id) const
{
67 68 69 70
    QMap<int, QAbstractButton*>::ConstIterator it = mIds.find(id);
    if (it == mIds.constEnd())
        return 0;
    return it.value();
David Jarvie's avatar
David Jarvie committed
71 72 73 74 75 76 77
}

/******************************************************************************
 * Returns the ID of the currently selected button.
 */
int ButtonGroup::selectedId() const
{
78
    return id(checkedButton());
David Jarvie's avatar
David Jarvie committed
79 80 81 82 83 84 85
}

/******************************************************************************
 * Returns the ID of the currently selected button.
 */
void ButtonGroup::setButton(int id)
{
86 87 88
    QAbstractButton* button = find(id);
    if (button)
        button->setChecked(true);
David Jarvie's avatar
David Jarvie committed
89 90
}

91 92 93 94 95
/******************************************************************************
 * Called when one of the member buttons is toggled.
 */
void ButtonGroup::slotButtonToggled(bool)
{
96
    emit buttonSet(checkedButton());
97
}
98

99
// vim: et sw=4: