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 #ifdef Q_WS_X11
00033 #define XK_XKB_KEYS
00034 #define XK_MISCELLANY
00035 #include <X11/Xlib.h>   // For x11Event()
00036 #include <X11/keysymdef.h> // For XK_...
00037 
00038 #ifdef KeyPress
00039 const int XFocusOut = FocusOut;
00040 const int XFocusIn = FocusIn;
00041 const int XKeyPress = KeyPress;
00042 const int XKeyRelease = KeyRelease;
00043 #undef KeyRelease
00044 #undef KeyPress
00045 #undef FocusOut
00046 #undef FocusIn
00047 #endif
00048 #endif
00049 
00050 const char* psTemp[] = { I18N_NOOP("Primary"), I18N_NOOP("Alternate"), I18N_NOOP("Multi-Key") };
00051 
00052 class KKeyButtonPrivate
00053 {
00054  public:
00055     bool bQtShortcut;
00056 };
00057 
00058 /***********************************************************************/
00059 /* KKeyButton                                                          */
00060 /*                                                                     */
00061 /* Initially added by Mark Donohoe <donohoe@kde.org>                   */
00062 /*                                                                     */
00063 /***********************************************************************/
00064 
00065 KKeyButton::KKeyButton(QWidget *parent, const char *name)
00066 :   QPushButton( parent, name )
00067 {
00068     d = new KKeyButtonPrivate;
00069     setFocusPolicy( QWidget::StrongFocus );
00070     m_bEditing = false;
00071     connect( this, SIGNAL(clicked()), this, SLOT(captureShortcut()) );
00072     setShortcut( KShortcut(), true );
00073 }
00074 
00075 KKeyButton::~KKeyButton ()
00076 {
00077     delete d;
00078 }
00079 
00080 void KKeyButton::setShortcut( const KShortcut& cut, bool bQtShortcut )
00081 {
00082     d->bQtShortcut = bQtShortcut;
00083     m_cut = cut;
00084     QString keyStr = m_cut.toString();
00085     setText( keyStr.isEmpty() ? i18n("None") : keyStr );
00086 }
00087 
00088 // deprecated //
00089 void KKeyButton::setShortcut( const KShortcut& cut )
00090 {
00091     setShortcut( cut, false );
00092 }
00093 
00094 void KKeyButton::setText( const QString& text )
00095 {
00096     QPushButton::setText( text );
00097     setFixedSize( sizeHint().width()+12, sizeHint().height()+8 );
00098 }
00099 
00100 void KKeyButton::captureShortcut()
00101 {
00102     KShortcut cut;
00103 
00104     m_bEditing = true;
00105     repaint();
00106 
00107     KShortcutDialog dlg( m_cut, d->bQtShortcut, this );
00108     if( dlg.exec() == KDialog::Accepted )
00109         emit capturedShortcut( dlg.cut() );
00110 
00111     m_bEditing = false;
00112     repaint();
00113 }
00114 
00115 void KKeyButton::drawButton( QPainter *painter )
00116 {
00117   QPointArray a( 4 );
00118   a.setPoint( 0, 0, 0) ;
00119   a.setPoint( 1, width(), 0 );
00120   a.setPoint( 2, 0, height() );
00121   a.setPoint( 3, 0, 0 );
00122 
00123   QRegion r1( a );
00124   painter->setClipRegion( r1 );
00125   painter->setBrush( backgroundColor().light() );
00126   painter->drawRoundRect( 0, 0, width(), height(), 20, 20);
00127 
00128   a.setPoint( 0, width(), height() );
00129   a.setPoint( 1, width(), 0 );
00130   a.setPoint( 2, 0, height() );
00131   a.setPoint( 3, width(), height() );
00132 
00133   QRegion r2( a );
00134   painter->setClipRegion( r2 );
00135   painter->setBrush( backgroundColor().dark() );
00136   painter->drawRoundRect( 0, 0, width(), height(), 20, 20 );
00137 
00138   painter->setClipping( false );
00139   if( width() > 12 && height() > 8 )
00140     qDrawShadePanel( painter, 6, 4, width() - 12, height() - 8,
00141                      colorGroup(), true, 1, 0L );
00142   if ( m_bEditing )
00143   {
00144     painter->setPen( colorGroup().base() );
00145     painter->setBrush( colorGroup().base() );
00146   }
00147   else
00148   {
00149     painter->setPen( backgroundColor() );
00150     painter->setBrush( backgroundColor() );
00151   }
00152   if( width() > 14 && height() > 10 )
00153     painter->drawRect( 7, 5, width() - 14, height() - 10 );
00154 
00155   drawButtonLabel( painter );
00156 
00157   painter->setPen( colorGroup().text() );
00158   painter->setBrush( NoBrush );
00159   if( hasFocus() || m_bEditing )
00160   {
00161     if( width() > 16 && height() > 12 )
00162       painter->drawRect( 8, 6, width() - 16, height() - 12 );
00163   }
00164 
00165 }
00166 
00167 void KKeyButton::virtual_hook( int, void* )
00168 { /*BASE::virtual_hook( id, data );*/ }
00169 
00170 #include "kkeybutton.moc"
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.4.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Sun Feb 27 22:15:03 2005 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001