pushbutton.cpp 3.32 KB
Newer Older
1 2 3
/*
 *  pushbutton.cpp  -  push button with read-only option
 *  Program:  kalarm
4
 *  Copyright © 2002,2005,2009 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 "pushbutton.h"
22

David Jarvie's avatar
David Jarvie committed
23
#include <KGuiItem>
Laurent Montel's avatar
Laurent Montel committed
24
#include <QIcon>
Laurent Montel's avatar
Qt3to4  
Laurent Montel committed
25 26
#include <QMouseEvent>
#include <QKeyEvent>
27

David Jarvie's avatar
David Jarvie committed
28
PushButton::PushButton(QWidget* parent)
Laurent Montel's avatar
Laurent Montel committed
29
    : QPushButton(parent),
30 31 32
      mFocusPolicy(focusPolicy()),
      mReadOnly(false),
      mNoHighlight(false)
33 34
{ }

David Jarvie's avatar
David Jarvie committed
35
PushButton::PushButton(const KGuiItem& gui, QWidget* parent)
Laurent Montel's avatar
Laurent Montel committed
36
    : QPushButton(parent),
37 38 39
      mFocusPolicy(focusPolicy()),
      mReadOnly(false),
      mNoHighlight(false)
Laurent Montel's avatar
Laurent Montel committed
40
{
David Jarvie's avatar
David Jarvie committed
41
     KGuiItem::assign(this, gui);
Laurent Montel's avatar
Laurent Montel committed
42
}
43

David Jarvie's avatar
David Jarvie committed
44
PushButton::PushButton(const QString& text, QWidget* parent)
Laurent Montel's avatar
Laurent Montel committed
45
    : QPushButton(text, parent),
46 47 48
      mFocusPolicy(focusPolicy()),
      mReadOnly(false),
      mNoHighlight(false)
49 50
{ }

Laurent Montel's avatar
Laurent Montel committed
51
PushButton::PushButton(const QIcon& icon, const QString& text, QWidget* parent)
Laurent Montel's avatar
Laurent Montel committed
52
    : QPushButton(icon, text, parent),
53 54 55
      mFocusPolicy(focusPolicy()),
      mReadOnly(false),
      mNoHighlight(false)
56 57
{ }

58
void PushButton::setReadOnly(bool ro, bool noHighlight)
59
{
60 61 62 63 64 65 66 67
    mNoHighlight = noHighlight;
    if ((int)ro != (int)mReadOnly)
    {
        mReadOnly = ro;
        setFocusPolicy(ro ? Qt::NoFocus : mFocusPolicy);
        if (ro)
            clearFocus();
    }
68 69 70 71
}

void PushButton::mousePressEvent(QMouseEvent* e)
{
72 73 74 75 76 77
    if (mReadOnly)
    {
        // Swallow up the event if it's the left button
        if (e->button() == Qt::LeftButton)
            return;
    }
Laurent Montel's avatar
Laurent Montel committed
78
    QPushButton::mousePressEvent(e);
79 80 81 82
}

void PushButton::mouseReleaseEvent(QMouseEvent* e)
{
83 84 85 86 87 88
    if (mReadOnly)
    {
        // Swallow up the event if it's the left button
        if (e->button() == Qt::LeftButton)
            return;
    }
Laurent Montel's avatar
Laurent Montel committed
89
    QPushButton::mouseReleaseEvent(e);
90 91 92 93
}

void PushButton::mouseMoveEvent(QMouseEvent* e)
{
94
    if (!mReadOnly)
Laurent Montel's avatar
Laurent Montel committed
95
        QPushButton::mouseMoveEvent(e);
96 97 98 99
}

void PushButton::keyPressEvent(QKeyEvent* e)
{
100 101 102 103 104 105 106 107 108 109 110 111 112 113
    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;
        }
    }
Laurent Montel's avatar
Laurent Montel committed
114
    QPushButton::keyPressEvent(e);
115 116 117 118
}

void PushButton::keyReleaseEvent(QKeyEvent* e)
{
119
    if (!mReadOnly)
Laurent Montel's avatar
Laurent Montel committed
120
        QPushButton::keyReleaseEvent(e);
121 122 123 124
}

bool PushButton::event(QEvent* e)
{
125 126 127 128 129 130
    if (mReadOnly  &&  mNoHighlight)
    {
        // Don't highlight the button on mouse hover
        if (e->type() == QEvent::HoverEnter)
            return true;
    }
Laurent Montel's avatar
Laurent Montel committed
131
    return QPushButton::event(e);
132
}
133

134
// vim: et sw=4: