File indexing completed on 2024-09-15 03:28:21
0001 /* 0002 This file is part of Kiten, a KDE Japanese Reference Tool 0003 SPDX-FileCopyrightText: 2006 Joseph Kerian <jkerian@gmail.com> 0004 0005 SPDX-License-Identifier: LGPL-2.0-or-later 0006 */ 0007 0008 #include "radicalbutton.h" 0009 0010 #include <QMouseEvent> 0011 #include <QStyle> 0012 #include <QStyleOptionButton> 0013 #include <QWidget> 0014 0015 RadicalButton::RadicalButton(const QString &text, QWidget *parent) 0016 : QPushButton(text, parent) 0017 , m_status(Normal) 0018 { 0019 setCheckable(true); 0020 } 0021 0022 bool RadicalButton::event(QEvent *event) 0023 { 0024 // This button does not handle wheel events, and unlike 0025 // the superclass, we don't care if we're enabled or disabled 0026 //(the superclass eats wheel events when disabled) 0027 if (event->type() == QEvent::Wheel) { 0028 return false; 0029 } 0030 return QPushButton::event(event); 0031 } 0032 0033 QSize RadicalButton::minimumSizeHint() const 0034 { 0035 int width = fontMetrics().size(Qt::TextShowMnemonic, text()).width(); 0036 int height = QPushButton::sizeHint().height(); 0037 // TODO: RadicalButton size calculation right, one of these days 0038 // QSize sz = fontMetrics().size(Qt::TextShowMnemonic, *it); 0039 // setMinimumSize(sz); 0040 0041 // QStyleOptionButton opt; 0042 // opt.initFrom(this); 0043 // QRect rect = kapp->style()->subElementRect(QStyle::SE_PushButtonContents,&opt,this); 0044 return {width, height}; 0045 } 0046 0047 void RadicalButton::mousePressEvent(QMouseEvent *e) 0048 { 0049 QPushButton::mousePressEvent(e); 0050 if (e->button() == Qt::RightButton) { 0051 // setStatus( Related ); 0052 Q_EMIT userClicked(text(), Related); 0053 } 0054 } 0055 0056 void RadicalButton::mouseReleaseEvent(QMouseEvent *e) 0057 { 0058 QPushButton::mouseReleaseEvent(e); 0059 if (e->button() == Qt::LeftButton) { 0060 switch (m_status) { 0061 case Selected: 0062 setStatus(Normal); 0063 Q_EMIT userClicked(text(), Normal); 0064 break; 0065 default: 0066 setStatus(Selected); 0067 Q_EMIT userClicked(text(), Selected); 0068 } 0069 } 0070 } 0071 0072 void RadicalButton::resetButton() 0073 { 0074 setStatus(Normal); 0075 } 0076 0077 void RadicalButton::setStatus(RadicalButton::ButtonStatus newStatus) 0078 { 0079 if (m_status == newStatus) { 0080 return; 0081 } 0082 0083 // Because it's more work to check everything rather than just set it, 0084 // we'll just set everything every time 0085 bool checked = false, hidden = false, disabled = false; 0086 switch (newStatus) { 0087 case Normal: 0088 break; 0089 case Selected: 0090 checked = true; 0091 break; 0092 case NotAppropriate: 0093 disabled = true; 0094 break; 0095 case Related: 0096 break; 0097 case Hidden: 0098 hidden = true; 0099 } 0100 0101 setVisible(!hidden); 0102 setEnabled(!disabled); 0103 setChecked(checked); 0104 m_status = newStatus; 0105 } 0106 0107 QSize RadicalButton::sizeHint() const 0108 { 0109 return minimumSizeHint(); 0110 } 0111 0112 #include "moc_radicalbutton.cpp"