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 #if defined Q_WS_X11 && ! defined K_WS_QTONLY
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 && ! K_WS_QTONLY
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     setText( keyStr.isEmpty() ? i18n("None") : keyStr );
00089 }
00090 
00091 
00092 void KKeyButton::setShortcut( const KShortcut& cut )
00093 {
00094     setShortcut( cut, false );
00095 }
00096 
00097 void KKeyButton::setText( const QString& text )
00098 {
00099     QPushButton::setText( text );
00100     setFixedSize( sizeHint().width()+12, sizeHint().height()+8 );
00101 }
00102 
00103 void KKeyButton::captureShortcut()
00104 {
00105     KShortcut cut;
00106 
00107     m_bEditing = true;
00108     repaint();
00109 
00110         {
00111     KShortcutDialog dlg( m_cut, d->bQtShortcut, this );
00112     if( dlg.exec() == KDialog::Accepted )
00113                 cut = dlg.shortcut();
00114         } 
00115         if( !cut.isNull())
00116         emit capturedShortcut( cut );
00117 
00118     m_bEditing = false;
00119     repaint();
00120 }
00121 
00122 void KKeyButton::drawButton( QPainter *painter )
00123 {
00124   QPointArray a( 4 );
00125   a.setPoint( 0, 0, 0) ;
00126   a.setPoint( 1, width(), 0 );
00127   a.setPoint( 2, 0, height() );
00128   a.setPoint( 3, 0, 0 );
00129 
00130   QRegion r1( a );
00131   painter->setClipRegion( r1 );
00132   painter->setBrush( backgroundColor().light() );
00133   painter->drawRoundRect( 0, 0, width(), height(), 20, 20);
00134 
00135   a.setPoint( 0, width(), height() );
00136   a.setPoint( 1, width(), 0 );
00137   a.setPoint( 2, 0, height() );
00138   a.setPoint( 3, width(), height() );
00139 
00140   QRegion r2( a );
00141   painter->setClipRegion( r2 );
00142   painter->setBrush( backgroundColor().dark() );
00143   painter->drawRoundRect( 0, 0, width(), height(), 20, 20 );
00144 
00145   painter->setClipping( false );
00146   if( width() > 12 && height() > 8 )
00147     qDrawShadePanel( painter, 6, 4, width() - 12, height() - 8,
00148                      colorGroup(), true, 1, 0L );
00149   if ( m_bEditing )
00150   {
00151     painter->setPen( colorGroup().base() );
00152     painter->setBrush( colorGroup().base() );
00153   }
00154   else
00155   {
00156     painter->setPen( backgroundColor() );
00157     painter->setBrush( backgroundColor() );
00158   }
00159   if( width() > 14 && height() > 10 )
00160     painter->drawRect( 7, 5, width() - 14, height() - 10 );
00161 
00162   drawButtonLabel( painter );
00163 
00164   painter->setPen( colorGroup().text() );
00165   painter->setBrush( NoBrush );
00166   if( hasFocus() || m_bEditing )
00167   {
00168     if( width() > 16 && height() > 12 )
00169       painter->drawRect( 8, 6, width() - 16, height() - 12 );
00170   }
00171 
00172 }
00173 
00174 void KKeyButton::virtual_hook( int, void* )
00175 {  }
00176 
00177 #include "kkeybutton.moc"