kdeui Library API Documentation

kkeybutton.cpp

00001 /* This file is part of the KDE libraries
00002     Copyright (C) 1998 Mark Donohoe <donohoe@kde.org>
00003     Copyright (C) 2001 Ellis Whitehead <ellis@kde.org>
00004 
00005     This library is free software; you can redistribute it and/or
00006     modify it under the terms of the GNU Library General Public
00007     License as published by the Free Software Foundation; either
00008     version 2 of the License, or (at your option) any later version.
00009 
00010     This library is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013     Library General Public License for more details.
00014 
00015     You should have received a copy of the GNU Library General Public License
00016     along with this library; see the file COPYING.LIB.  If not, write to
00017     the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00018     Boston, MA 02111-1307, USA.
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>   // For x11Event() // schroder
00037 #include <X11/keysymdef.h> // For XK_... // schroder is this still needed ?
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 //static const char* psTemp[] = { 
00052 //  I18N_NOOP("Primary"), I18N_NOOP("Alternate"), I18N_NOOP("Multi-Key") 
00053 //};
00054 
00055 class KKeyButtonPrivate
00056 {
00057  public:
00058     bool bQtShortcut;
00059 };
00060 
00061 /***********************************************************************/
00062 /* KKeyButton                                                          */
00063 /*                                                                     */
00064 /* Initially added by Mark Donohoe <donohoe@kde.org>                   */
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 // deprecated //
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         } // emit the signal after the dialog is destroyed, otherwise it still has grab
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 { /*BASE::virtual_hook( id, data );*/ }
00176 
00177 #include "kkeybutton.moc"
KDE Logo
This file is part of the documentation for kdeui Library Version 3.2.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Thu Apr 22 14:23:26 2004 by doxygen 1.2.18 written by Dimitri van Heesch, © 1997-2003