~ubuntu-branches/ubuntu/quantal/kiten/quantal-proposed

« back to all changes in this revision

Viewing changes to radselect/radicalbutton.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Harald Sitter
  • Date: 2011-07-10 11:23:47 UTC
  • Revision ID: james.westby@ubuntu.com-20110710112347-ykfhtvam3kgssspo
Tags: upstream-4.6.90+repack
ImportĀ upstreamĀ versionĀ 4.6.90+repack

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*****************************************************************************
 
2
 * This file is part of Kiten, a KDE Japanese Reference Tool                 *
 
3
 * Copyright (C) 2006 Joseph Kerian <jkerian@gmail.com>                      *
 
4
 *                                                                           *
 
5
 * This library is free software; you can redistribute it and/or             *
 
6
 * modify it under the terms of the GNU Library General Public               *
 
7
 * License as published by the Free Software Foundation; either              *
 
8
 * version 2 of the License, or (at your option) any later version.          *
 
9
 *                                                                           *
 
10
 * This library is distributed in the hope that it will be useful,           *
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of            *
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU         *
 
13
 * Library General Public License for more details.                          *
 
14
 *                                                                           *
 
15
 * You should have received a copy of the GNU Library General Public License *
 
16
 * along with this library; see the file COPYING.LIB.  If not, write to      *
 
17
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,      *
 
18
 * Boston, MA 02110-1301, USA.                                               *
 
19
 *****************************************************************************/
 
20
 
 
21
#include "radicalbutton.h"
 
22
 
 
23
#include <KApplication>
 
24
#include <KStyle>
 
25
 
 
26
#include <QMouseEvent>
 
27
#include <QPushButton>
 
28
#include <QString>
 
29
#include <QStyle>
 
30
#include <QStyleOptionButton>
 
31
#include <QWidget>
 
32
 
 
33
RadicalButton::RadicalButton( const QString &text, QWidget *parent )
 
34
: QPushButton( text, parent )
 
35
{
 
36
  setCheckable( true );
 
37
}
 
38
 
 
39
RadicalButton::~RadicalButton()
 
40
{
 
41
}
 
42
 
 
43
bool RadicalButton::event( QEvent *event )
 
44
{
 
45
  //This button does not handle wheel events, and unlike
 
46
  //the superclass, we don't care if we're enabled or disabled
 
47
  //(the superclass eats wheel events when disabled)
 
48
  if( event->type() == QEvent::Wheel )
 
49
  {
 
50
    return false;
 
51
  }
 
52
  return QPushButton::event( event );
 
53
}
 
54
 
 
55
QSize RadicalButton::minimumSizeHint() const
 
56
{
 
57
  int width = fontMetrics().size( Qt::TextShowMnemonic, text() ).width();
 
58
  int height = QPushButton::sizeHint().height();
 
59
//TODO: RadicalButton size calculation right, one of these days
 
60
//      QSize sz = fontMetrics().size(Qt::TextShowMnemonic, *it);
 
61
//      setMinimumSize(sz);
 
62
 
 
63
//      QStyleOptionButton opt;
 
64
//      opt.initFrom(this);
 
65
//      QRect rect = kapp->style()->subElementRect(QStyle::SE_PushButtonContents,&opt,this);
 
66
  return QSize( width, height );
 
67
}
 
68
 
 
69
void RadicalButton::mousePressEvent( QMouseEvent *e )
 
70
{
 
71
  QPushButton::mousePressEvent( e );
 
72
  if( e->button() == Qt::RightButton )
 
73
  {
 
74
    setStatus( kRelated );
 
75
    emit userClicked( text(), kRelated );
 
76
  }
 
77
}
 
78
 
 
79
void RadicalButton::mouseReleaseEvent( QMouseEvent *e )
 
80
{
 
81
  QPushButton::mouseReleaseEvent( e );
 
82
  if( e->button() == Qt::LeftButton )
 
83
  {
 
84
    switch( m_status )
 
85
    {
 
86
      case kSelected:
 
87
        setStatus( kNormal );
 
88
        emit userClicked( text(), kNormal );
 
89
        break;
 
90
      default:
 
91
        setStatus( kSelected );
 
92
        emit userClicked( text(), kSelected );
 
93
    }
 
94
  }
 
95
}
 
96
 
 
97
void RadicalButton::resetButton()
 
98
{
 
99
  setStatus( kNormal );
 
100
}
 
101
 
 
102
void RadicalButton::setStatus( RadicalButton::ButtonStatus newStatus )
 
103
{
 
104
  if( m_status == newStatus )
 
105
  {
 
106
    return;
 
107
  }
 
108
 
 
109
  //Because it's more work to check everything rather than just set it,
 
110
  //we'll just set everything every time
 
111
  bool checked   = false,
 
112
       underline = false,
 
113
       italic    = false,
 
114
       hidden    = false,
 
115
       disabled  = false;
 
116
  switch( newStatus )
 
117
  {
 
118
    case kNormal:
 
119
      break;
 
120
    case kSelected:
 
121
      checked = true;
 
122
      break;
 
123
    case kNotAppropriate:
 
124
      disabled = true;
 
125
      break;
 
126
    case kRelated:
 
127
      italic = true;
 
128
      break;
 
129
    case kHidden:
 
130
      hidden = true;
 
131
  }
 
132
 
 
133
  QFont theFont = font();
 
134
  theFont.setUnderline( underline );
 
135
  theFont.setItalic( italic );
 
136
  setFont( theFont );
 
137
  setVisible( ! hidden );
 
138
  setEnabled( ! disabled );
 
139
  setChecked( checked );
 
140
  m_status = newStatus;
 
141
}
 
142
 
 
143
QSize RadicalButton::sizeHint() const
 
144
{
 
145
  return minimumSizeHint();
 
146
}
 
147
 
 
148
#include "radicalbutton.moc"