kalarm/lib

pushbutton.cpp

00001 /*
00002  *  pushbutton.cpp  -  push button with read-only option
00003  *  Program:  kalarm
00004  *  Copyright (C) 2002 by David Jarvie  software@astrojar.org.uk
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; either version 2 of the License, or
00009  *  (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License along
00017  *  with this program; if not, write to the Free Software Foundation, Inc.,
00018  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00019  */
00020 
00021 #include "pushbutton.moc"
00022 
00023 
00024 PushButton::PushButton(QWidget* parent, const char* name)
00025     : QPushButton(parent, name),
00026       mFocusPolicy(focusPolicy()),
00027       mReadOnly(false)
00028 { }
00029 
00030 PushButton::PushButton(const QString& text, QWidget* parent, const char* name)
00031     : QPushButton(text, parent, name),
00032       mFocusPolicy(focusPolicy()),
00033       mReadOnly(false)
00034 { }
00035 
00036 PushButton::PushButton(const QIconSet& icon, const QString& text, QWidget* parent, const char* name)
00037     : QPushButton(icon, text, parent, name),
00038       mFocusPolicy(focusPolicy()),
00039       mReadOnly(false)
00040 { }
00041 
00042 void PushButton::setReadOnly(bool ro)
00043 {
00044     if ((int)ro != (int)mReadOnly)
00045     {
00046         mReadOnly = ro;
00047         setFocusPolicy(ro ? QWidget::NoFocus : mFocusPolicy);
00048         if (ro)
00049             clearFocus();
00050     }
00051 }
00052 
00053 void PushButton::mousePressEvent(QMouseEvent* e)
00054 {
00055     if (mReadOnly)
00056     {
00057         // Swallow up the event if it's the left button
00058         if (e->button() == LeftButton)
00059             return;
00060     }
00061     QPushButton::mousePressEvent(e);
00062 }
00063 
00064 void PushButton::mouseReleaseEvent(QMouseEvent* e)
00065 {
00066     if (mReadOnly)
00067     {
00068         // Swallow up the event if it's the left button
00069         if (e->button() == LeftButton)
00070             return;
00071     }
00072     QPushButton::mouseReleaseEvent(e);
00073 }
00074 
00075 void PushButton::mouseMoveEvent(QMouseEvent* e)
00076 {
00077     if (!mReadOnly)
00078         QPushButton::mouseMoveEvent(e);
00079 }
00080 
00081 void PushButton::keyPressEvent(QKeyEvent* e)
00082 {
00083     if (mReadOnly)
00084         switch (e->key())
00085         {
00086             case Key_Up:
00087             case Key_Left:
00088             case Key_Right:
00089             case Key_Down:
00090                 // Process keys which shift the focus
00091                 break;
00092             default:
00093                 return;
00094         }
00095     QPushButton::keyPressEvent(e);
00096 }
00097 
00098 void PushButton::keyReleaseEvent(QKeyEvent* e)
00099 {
00100     if (!mReadOnly)
00101         QPushButton::keyReleaseEvent(e);
00102 }
KDE Home | KDE Accessibility Home | Description of Access Keys