File indexing completed on 2024-11-24 04:42:25
0001 /* 0002 * pushbutton.cpp - push button with read-only option 0003 * Program: kalarm 0004 * SPDX-FileCopyrightText: 2002-2019 David Jarvie <djarvie@kde.org> 0005 * 0006 * SPDX-License-Identifier: GPL-2.0-or-later 0007 */ 0008 0009 #include "pushbutton.h" 0010 0011 #include <KGuiItem> 0012 #include <QIcon> 0013 #include <QMouseEvent> 0014 #include <QKeyEvent> 0015 0016 PushButton::PushButton(QWidget* parent) 0017 : QPushButton(parent) 0018 , mFocusPolicy(focusPolicy()) 0019 { } 0020 0021 PushButton::PushButton(const KGuiItem& gui, QWidget* parent) 0022 : QPushButton(parent) 0023 , mFocusPolicy(focusPolicy()) 0024 { 0025 KGuiItem::assign(this, gui); 0026 } 0027 0028 PushButton::PushButton(const QString& text, QWidget* parent) 0029 : QPushButton(text, parent) 0030 , mFocusPolicy(focusPolicy()) 0031 { } 0032 0033 PushButton::PushButton(const QIcon& icon, const QString& text, QWidget* parent) 0034 : QPushButton(icon, text, parent) 0035 , mFocusPolicy(focusPolicy()) 0036 { } 0037 0038 void PushButton::setReadOnly(bool ro, bool noHighlight) 0039 { 0040 mNoHighlight = noHighlight; 0041 if (ro != mReadOnly) 0042 { 0043 mReadOnly = ro; 0044 setFocusPolicy(ro ? Qt::NoFocus : mFocusPolicy); 0045 if (ro) 0046 clearFocus(); 0047 } 0048 } 0049 0050 void PushButton::mousePressEvent(QMouseEvent* e) 0051 { 0052 if (mReadOnly) 0053 { 0054 // Swallow up the event if it's the left button 0055 if (e->button() == Qt::LeftButton) 0056 return; 0057 } 0058 QPushButton::mousePressEvent(e); 0059 } 0060 0061 void PushButton::mouseReleaseEvent(QMouseEvent* e) 0062 { 0063 if (mReadOnly) 0064 { 0065 // Swallow up the event if it's the left button 0066 if (e->button() == Qt::LeftButton) 0067 return; 0068 } 0069 QPushButton::mouseReleaseEvent(e); 0070 } 0071 0072 void PushButton::mouseMoveEvent(QMouseEvent* e) 0073 { 0074 if (!mReadOnly) 0075 QPushButton::mouseMoveEvent(e); 0076 } 0077 0078 void PushButton::keyPressEvent(QKeyEvent* e) 0079 { 0080 if (mReadOnly) 0081 { 0082 switch (e->key()) 0083 { 0084 case Qt::Key_Up: 0085 case Qt::Key_Left: 0086 case Qt::Key_Right: 0087 case Qt::Key_Down: 0088 // Process keys which shift the focus 0089 break; 0090 default: 0091 return; 0092 } 0093 } 0094 QPushButton::keyPressEvent(e); 0095 } 0096 0097 void PushButton::keyReleaseEvent(QKeyEvent* e) 0098 { 0099 if (!mReadOnly) 0100 QPushButton::keyReleaseEvent(e); 0101 } 0102 0103 bool PushButton::event(QEvent* e) 0104 { 0105 if (mReadOnly && mNoHighlight) 0106 { 0107 // Don't highlight the button on mouse hover 0108 if (e->type() == QEvent::HoverEnter) 0109 return true; 0110 } 0111 return QPushButton::event(e); 0112 } 0113 0114 #include "moc_pushbutton.cpp" 0115 0116 // vim: et sw=4: