00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
#include "kkeybutton.h"
00022
#include "kshortcutdialog.h"
00023
00024
#include <qcursor.h>
00025
#include <qdrawutil.h>
00026
#include <qpainter.h>
00027
#include <kapplication.h>
00028
#include <kdebug.h>
00029
#include <kglobalaccel.h>
00030
#include <klocale.h>
00031
00032
#include "config.h"
00033
#ifdef Q_WS_X11
00034
#define XK_XKB_KEYS
00035
#define XK_MISCELLANY
00036
#include <X11/Xlib.h>
00037
#include <X11/keysymdef.h>
00038
00039
#ifdef KeyPress
00040
const int XFocusOut = FocusOut;
00041
const int XFocusIn = FocusIn;
00042
const int XKeyPress = KeyPress;
00043
const int XKeyRelease = KeyRelease;
00044
#undef KeyRelease
00045
#undef KeyPress
00046
#undef FocusOut
00047
#undef FocusIn
00048
#endif // KeyPress
00049
#endif // Q_WS_X11
00050
00051
00052
00053
00054
00055
class KKeyButtonPrivate
00056 {
00057
public:
00058
bool bQtShortcut;
00059 };
00060
00061
00062
00063
00064
00065
00066
00067
00068 KKeyButton::KKeyButton(
QWidget *parent,
const char *name)
00069 :
QPushButton( parent, name )
00070 {
00071 d =
new KKeyButtonPrivate;
00072 setFocusPolicy( QWidget::StrongFocus );
00073 m_bEditing =
false;
00074 connect(
this, SIGNAL(
clicked()),
this, SLOT(
captureShortcut()) );
00075
setShortcut(
KShortcut(),
true );
00076 }
00077
00078 KKeyButton::~KKeyButton ()
00079 {
00080
delete d;
00081 }
00082
00083 void KKeyButton::setShortcut(
const KShortcut& cut,
bool bQtShortcut )
00084 {
00085 d->bQtShortcut = bQtShortcut;
00086 m_cut = cut;
00087
QString keyStr = m_cut.
toString();
00088 keyStr.
replace(
'&', QString::fromLatin1(
"&&"));
00089
setText( keyStr.
isEmpty() ? i18n(
"None") : keyStr );
00090 }
00091
00092
00093 void KKeyButton::setShortcut(
const KShortcut& cut )
00094 {
00095
setShortcut( cut,
false );
00096 }
00097
00098 void KKeyButton::setText(
const QString& text )
00099 {
00100 QPushButton::setText( text );
00101 setFixedSize( sizeHint().width()+12, sizeHint().height()+8 );
00102 }
00103
00104 void KKeyButton::captureShortcut()
00105 {
00106
KShortcut cut;
00107
00108 m_bEditing =
true;
00109 repaint();
00110
00111 {
00112
KShortcutDialog dlg( m_cut, d->bQtShortcut,
this );
00113
if( dlg.
exec() == KDialog::Accepted )
00114 cut = dlg.
shortcut();
00115 }
00116
if( !cut.
isNull())
00117 emit capturedShortcut( cut );
00118
00119 m_bEditing =
false;
00120 repaint();
00121 }
00122
00123 void KKeyButton::drawButton(
QPainter *painter )
00124 {
00125
QPointArray a( 4 );
00126 a.
setPoint( 0, 0, 0) ;
00127 a.
setPoint( 1, width(), 0 );
00128 a.
setPoint( 2, 0, height() );
00129 a.
setPoint( 3, 0, 0 );
00130
00131
QRegion r1( a );
00132 painter->
setClipRegion( r1 );
00133 painter->
setBrush( backgroundColor().light() );
00134 painter->
drawRoundRect( 0, 0, width(), height(), 20, 20);
00135
00136 a.
setPoint( 0, width(), height() );
00137 a.
setPoint( 1, width(), 0 );
00138 a.
setPoint( 2, 0, height() );
00139 a.
setPoint( 3, width(), height() );
00140
00141
QRegion r2( a );
00142 painter->
setClipRegion( r2 );
00143 painter->
setBrush( backgroundColor().dark() );
00144 painter->
drawRoundRect( 0, 0, width(), height(), 20, 20 );
00145
00146 painter->
setClipping(
false );
00147
if( width() > 12 && height() > 8 )
00148 qDrawShadePanel( painter, 6, 4, width() - 12, height() - 8,
00149 colorGroup(),
true, 1, 0L );
00150
if ( m_bEditing )
00151 {
00152 painter->
setPen( colorGroup().base() );
00153 painter->
setBrush( colorGroup().base() );
00154 }
00155
else
00156 {
00157 painter->
setPen( backgroundColor() );
00158 painter->
setBrush( backgroundColor() );
00159 }
00160
if( width() > 14 && height() > 10 )
00161 painter->
drawRect( 7, 5, width() - 14, height() - 10 );
00162
00163 drawButtonLabel( painter );
00164
00165 painter->
setPen( colorGroup().
text() );
00166 painter->
setBrush( NoBrush );
00167
if( hasFocus() || m_bEditing )
00168 {
00169
if( width() > 16 && height() > 12 )
00170 painter->
drawRect( 8, 6, width() - 16, height() - 12 );
00171 }
00172
00173 }
00174
00175
void KKeyButton::virtual_hook(
int,
void* )
00176 { }
00177
00178
#include "kkeybutton.moc"