File indexing completed on 2022-11-29 18:12:55

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 RadicalButton::~RadicalButton()
0023 {
0024 }
0025 
0026 bool RadicalButton::event( QEvent *event )
0027 {
0028   //This button does not handle wheel events, and unlike
0029   //the superclass, we don't care if we're enabled or disabled
0030   //(the superclass eats wheel events when disabled)
0031   if( event->type() == QEvent::Wheel )
0032   {
0033     return false;
0034   }
0035   return QPushButton::event( event );
0036 }
0037 
0038 QSize RadicalButton::minimumSizeHint() const
0039 {
0040   int width = fontMetrics().size( Qt::TextShowMnemonic, text() ).width();
0041   int height = QPushButton::sizeHint().height();
0042 //TODO: RadicalButton size calculation right, one of these days
0043 //  QSize sz = fontMetrics().size(Qt::TextShowMnemonic, *it);
0044 //  setMinimumSize(sz);
0045 
0046 //  QStyleOptionButton opt;
0047 //  opt.initFrom(this);
0048 //  QRect rect = kapp->style()->subElementRect(QStyle::SE_PushButtonContents,&opt,this);
0049   return QSize( width, height );
0050 }
0051 
0052 void RadicalButton::mousePressEvent( QMouseEvent *e )
0053 {
0054   QPushButton::mousePressEvent( e );
0055   if( e->button() == Qt::RightButton )
0056   {
0057     //setStatus( Related );
0058     Q_EMIT userClicked( text(), Related );
0059   }
0060 }
0061 
0062 void RadicalButton::mouseReleaseEvent( QMouseEvent *e )
0063 {
0064   QPushButton::mouseReleaseEvent( e );
0065   if( e->button() == Qt::LeftButton )
0066   {
0067     switch( m_status )
0068     {
0069       case Selected:
0070         setStatus( Normal );
0071         Q_EMIT userClicked( text(), Normal );
0072         break;
0073       default:
0074         setStatus( Selected );
0075         Q_EMIT userClicked( text(), Selected );
0076     }
0077   }
0078 }
0079 
0080 void RadicalButton::resetButton()
0081 {
0082   setStatus( Normal );
0083 }
0084 
0085 void RadicalButton::setStatus( RadicalButton::ButtonStatus newStatus )
0086 {
0087   if( m_status == newStatus )
0088   {
0089     return;
0090   }
0091 
0092   //Because it's more work to check everything rather than just set it,
0093   //we'll just set everything every time
0094   bool checked   = false,
0095        hidden    = false,
0096        disabled  = false;
0097   switch( newStatus )
0098   {
0099     case Normal:
0100       break;
0101     case Selected:
0102       checked = true;
0103       break;
0104     case NotAppropriate:
0105       disabled = true;
0106       break;
0107     case Related:
0108       break;
0109     case Hidden:
0110       hidden = true;
0111   }
0112 
0113   setVisible( ! hidden );
0114   setEnabled( ! disabled );
0115   setChecked( checked );
0116   m_status = newStatus;
0117 }
0118 
0119 QSize RadicalButton::sizeHint() const
0120 {
0121   return minimumSizeHint();
0122 }
0123 
0124