radiobutton.cpp 3.85 KB
Newer Older
1 2 3
/*
 *  radiobutton.cpp  -  radio button with read-only option
 *  Program:  kalarm
4
 *  Copyright © 2002,2003,2005 by David Jarvie <djarvie@kde.org>
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.
19 20
 */

Laurent Montel's avatar
Laurent Montel committed
21
#include "radiobutton.h"
Laurent Montel's avatar
Qt3to4  
Laurent Montel committed
22 23
#include <QMouseEvent>
#include <QKeyEvent>
24 25


David Jarvie's avatar
David Jarvie committed
26
RadioButton::RadioButton(QWidget* parent)
27 28 29 30
    : QRadioButton(parent),
      mFocusPolicy(focusPolicy()),
      mFocusWidget(0),
      mReadOnly(false)
31 32
{ }

David Jarvie's avatar
David Jarvie committed
33
RadioButton::RadioButton(const QString& text, QWidget* parent)
34 35 36 37
    : QRadioButton(text, parent),
      mFocusPolicy(focusPolicy()),
      mFocusWidget(0),
      mReadOnly(false)
38 39
{ }

David Jarvie's avatar
David Jarvie committed
40 41 42 43
/******************************************************************************
*  Set the read-only status. If read-only, the button can be toggled by the
*  application, but not by the user.
*/
44 45
void RadioButton::setReadOnly(bool ro)
{
46 47 48 49 50 51 52
    if ((int)ro != (int)mReadOnly)
    {
        mReadOnly = ro;
        setFocusPolicy(ro ? Qt::NoFocus : mFocusPolicy);
        if (ro)
            clearFocus();
    }
53 54
}

David Jarvie's avatar
David Jarvie committed
55 56 57 58 59
/******************************************************************************
*  Specify a widget to receive focus when the button is clicked on.
*/
void RadioButton::setFocusWidget(QWidget* w, bool enable)
{
60 61 62
    mFocusWidget = w;
    mFocusWidgetEnable = enable;
    if (w)
Laurent Montel's avatar
Laurent Montel committed
63
        connect(this, &RadioButton::clicked, this, &RadioButton::slotClicked);
64
    else
Laurent Montel's avatar
Laurent Montel committed
65
        disconnect(this, &RadioButton::clicked, this, &RadioButton::slotClicked);
David Jarvie's avatar
David Jarvie committed
66 67 68 69 70 71 72 73
}

/******************************************************************************
*  Called when the button is clicked.
*  If it is now checked, focus is transferred to any specified focus widget.
*/
void RadioButton::slotClicked()
{
74 75 76 77 78 79
    if (mFocusWidget  &&  isChecked())
    {
        if (mFocusWidgetEnable)
            mFocusWidget->setEnabled(true);
        mFocusWidget->setFocus();
    }
David Jarvie's avatar
David Jarvie committed
80 81 82 83 84 85
}

/******************************************************************************
*  Event handlers to intercept events if in read-only mode.
*  Any events which could change the button state are discarded.
*/
86 87
void RadioButton::mousePressEvent(QMouseEvent* e)
{
88 89 90 91 92 93 94
    if (mReadOnly)
    {
        // Swallow up the event if it's the left button
        if (e->button() == Qt::LeftButton)
            return;
    }
    QRadioButton::mousePressEvent(e);
95 96 97 98
}

void RadioButton::mouseReleaseEvent(QMouseEvent* e)
{
99 100 101 102 103 104 105
    if (mReadOnly)
    {
        // Swallow up the event if it's the left button
        if (e->button() == Qt::LeftButton)
            return;
    }
    QRadioButton::mouseReleaseEvent(e);
106 107 108 109
}

void RadioButton::mouseMoveEvent(QMouseEvent* e)
{
110 111
    if (!mReadOnly)
        QRadioButton::mouseMoveEvent(e);
112 113 114 115
}

void RadioButton::keyPressEvent(QKeyEvent* e)
{
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
    if (mReadOnly)
    {
        switch (e->key())
        {
            case Qt::Key_Up:
            case Qt::Key_Left:
            case Qt::Key_Right:
            case Qt::Key_Down:
                // Process keys which shift the focus
            case Qt::Key_Escape:
                break;
            default:
                return;
        }
    }
    QRadioButton::keyPressEvent(e);
132 133 134 135
}

void RadioButton::keyReleaseEvent(QKeyEvent* e)
{
136 137
    if (!mReadOnly)
        QRadioButton::keyReleaseEvent(e);
138
}
139

140
// vim: et sw=4: