checkbox.cpp 3.75 KB
Newer Older
David Jarvie's avatar
David Jarvie committed
1 2 3
/*
 *  checkbox.cpp  -  check box with read-only option
 *  Program:  kalarm
4
 *  Copyright © 2002,2003,2005 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
 */

Laurent Montel's avatar
moc++  
Laurent Montel committed
21
#include "checkbox.h"
Laurent Montel's avatar
Qt3to4  
Laurent Montel committed
22 23
#include <QMouseEvent>
#include <QKeyEvent>
David Jarvie's avatar
David Jarvie committed
24 25


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

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

David Jarvie's avatar
David Jarvie committed
40 41 42 43
/******************************************************************************
*  Set the read-only status. If read-only, the checkbox can be toggled by the
*  application, but not by the user.
*/
David Jarvie's avatar
David Jarvie committed
44 45
void CheckBox::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();
    }
David Jarvie's avatar
David Jarvie committed
53 54
}

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

/******************************************************************************
*  Called when the checkbox is clicked.
*  If it is now checked, focus is transferred to any specified focus widget.
*/
void CheckBox::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 checkbox state are discarded.
*/
David Jarvie's avatar
David Jarvie committed
86 87
void CheckBox::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;
    }
    QCheckBox::mousePressEvent(e);
David Jarvie's avatar
David Jarvie committed
95 96 97 98
}

void CheckBox::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;
    }
    QCheckBox::mouseReleaseEvent(e);
David Jarvie's avatar
David Jarvie committed
106 107 108 109
}

void CheckBox::mouseMoveEvent(QMouseEvent* e)
{
110 111
    if (!mReadOnly)
        QCheckBox::mouseMoveEvent(e);
David Jarvie's avatar
David Jarvie committed
112 113 114 115
}

void CheckBox::keyPressEvent(QKeyEvent* e)
{
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
    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
                break;
            default:
                return;
        }
    }
    QCheckBox::keyPressEvent(e);
David Jarvie's avatar
David Jarvie committed
131 132 133 134
}

void CheckBox::keyReleaseEvent(QKeyEvent* e)
{
135 136
    if (!mReadOnly)
        QCheckBox::keyReleaseEvent(e);
David Jarvie's avatar
David Jarvie committed
137
}
138

139
// vim: et sw=4: