kdeui Library API Documentation

klistbox.cpp

00001 /* This file is part of the KDE libraries
00002    Copyright (C) 2000 Reginald Stadlbauer <reggie@kde.org>
00003 
00004    This library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License version 2 as published by the Free Software Foundation.
00007 
00008    This library is distributed in the hope that it will be useful,
00009    but WITHOUT ANY WARRANTY; without even the implied warranty of
00010    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011    Library General Public License for more details.
00012 
00013    You should have received a copy of the GNU Library General Public License
00014    along with this library; see the file COPYING.LIB.  If not, write to
00015    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00016    Boston, MA 02111-1307, USA.
00017 */
00018 #include "config.h"
00019 
00020 #include <qtimer.h>
00021 
00022 #include <kglobalsettings.h>
00023 #include <kcursor.h>
00024 #include <kapplication.h>
00025 
00026 #if defined Q_WS_X11 && ! defined K_WS_QTONLY
00027 #include <kipc.h> 
00028 #endif
00029 
00030 #include <kdebug.h>
00031 
00032 #include "klistbox.h"
00033 
00034 KListBox::KListBox( QWidget *parent, const char *name, WFlags f )
00035     : QListBox( parent, name, f )
00036 {
00037     connect( this, SIGNAL( onViewport() ),
00038          this, SLOT( slotOnViewport() ) );
00039     connect( this, SIGNAL( onItem( QListBoxItem * ) ),
00040          this, SLOT( slotOnItem( QListBoxItem * ) ) );
00041     slotSettingsChanged(KApplication::SETTINGS_MOUSE);
00042     if (kapp)
00043     {
00044         connect( kapp, SIGNAL( settingsChanged(int) ), SLOT( slotSettingsChanged(int) ) );
00045 #if defined Q_WS_X11 && ! defined K_WS_QTONLY
00046         kapp->addKipcEventMask( KIPC::SettingsChanged );
00047 #endif
00048     }
00049 
00050     m_pCurrentItem = 0L;
00051 
00052     m_pAutoSelect = new QTimer( this );
00053     connect( m_pAutoSelect, SIGNAL( timeout() ),
00054              this, SLOT( slotAutoSelect() ) );
00055 }
00056 
00057 void KListBox::slotOnItem( QListBoxItem *item )
00058 {
00059     if ( item && m_bChangeCursorOverItem && m_bUseSingle )
00060         viewport()->setCursor( KCursor().handCursor() );
00061 
00062     if ( item && (m_autoSelectDelay > -1) && m_bUseSingle ) {
00063       m_pAutoSelect->start( m_autoSelectDelay, true );
00064       m_pCurrentItem = item;
00065     }
00066 }
00067 
00068 void KListBox::slotOnViewport()
00069 {
00070     if ( m_bChangeCursorOverItem )
00071         viewport()->unsetCursor();
00072 
00073     m_pAutoSelect->stop();
00074     m_pCurrentItem = 0L;
00075 }
00076 
00077 
00078 void KListBox::slotSettingsChanged(int category)
00079 {
00080     if (category != KApplication::SETTINGS_MOUSE)
00081         return;
00082     m_bUseSingle = KGlobalSettings::singleClick();
00083 
00084     disconnect( this, SIGNAL( mouseButtonClicked( int, QListBoxItem *,
00085                           const QPoint & ) ),
00086         this, SLOT( slotMouseButtonClicked( int, QListBoxItem *,
00087                             const QPoint & ) ) );
00088 //         disconnect( this, SIGNAL( doubleClicked( QListBoxItem *, 
00089 //                       const QPoint & ) ),
00090 //          this, SLOT( slotExecute( QListBoxItem *, 
00091 //                       const QPoint & ) ) );
00092 
00093     if( m_bUseSingle )
00094     {
00095       connect( this, SIGNAL( mouseButtonClicked( int, QListBoxItem *, 
00096                          const QPoint & ) ),
00097            this, SLOT( slotMouseButtonClicked( int, QListBoxItem *,
00098                            const QPoint & ) ) );
00099     }
00100     else
00101     {
00102 //         connect( this, SIGNAL( doubleClicked( QListBoxItem *, 
00103 //                        const QPoint & ) ),
00104 //                  this, SLOT( slotExecute( QListBoxItem *, 
00105 //                    const QPoint & ) ) );
00106     }
00107 
00108     m_bChangeCursorOverItem = KGlobalSettings::changeCursorOverIcon();
00109     m_autoSelectDelay = KGlobalSettings::autoSelectDelay();
00110 
00111     if( !m_bUseSingle || !m_bChangeCursorOverItem )
00112         viewport()->unsetCursor();
00113 }
00114 
00115 void KListBox::slotAutoSelect()
00116 {
00117   // check that the item still exists
00118   if( index( m_pCurrentItem ) == -1 )
00119     return;
00120 
00121   //Give this widget the keyboard focus.
00122   if( !hasFocus() )
00123     setFocus();
00124 
00125 #if defined Q_WS_X11 && ! defined K_WS_QTONLY //FIXME
00126   uint keybstate = KApplication::keyboardModifiers();
00127 #endif
00128 
00129   QListBoxItem* previousItem = item( currentItem() ); 
00130   setCurrentItem( m_pCurrentItem );
00131 
00132   if( m_pCurrentItem ) {
00133 #if defined Q_WS_X11 && ! defined K_WS_QTONLY //FIXME
00134     //Shift pressed?
00135     if( (keybstate & KApplication::ShiftModifier) ) {
00136 #endif
00137       bool block = signalsBlocked();
00138       blockSignals( true );
00139 
00140 #if defined Q_WS_X11 && ! defined K_WS_QTONLY //FIXME
00141       //No Ctrl? Then clear before!
00142       if( !(keybstate & KApplication::ControlModifier) )  
00143     clearSelection(); 
00144 #endif
00145 
00146       bool select = !m_pCurrentItem->isSelected();
00147       bool update = viewport()->isUpdatesEnabled();
00148       viewport()->setUpdatesEnabled( false );
00149 
00150       bool down = index( previousItem ) < index( m_pCurrentItem );
00151       QListBoxItem* it = down ? previousItem : m_pCurrentItem;
00152       for (;it ; it = it->next() ) {
00153     if ( down && it == m_pCurrentItem ) {
00154       setSelected( m_pCurrentItem, select );
00155       break;
00156     }
00157     if ( !down && it == previousItem ) {
00158       setSelected( previousItem, select );
00159       break;
00160     }
00161     setSelected( it, select );
00162       }
00163       
00164       blockSignals( block );
00165       viewport()->setUpdatesEnabled( update );
00166       triggerUpdate( false );
00167 
00168       emit selectionChanged();
00169 
00170       if( selectionMode() == QListBox::Single )
00171     emit selectionChanged( m_pCurrentItem );
00172     }
00173 #if defined Q_WS_X11 && ! defined K_WS_QTONLY //FIXME
00174     else if( (keybstate & KApplication::ControlModifier) )
00175       setSelected( m_pCurrentItem, !m_pCurrentItem->isSelected() );
00176 #endif
00177     else {
00178       bool block = signalsBlocked();
00179       blockSignals( true );
00180 
00181       if( !m_pCurrentItem->isSelected() )
00182     clearSelection(); 
00183 
00184       blockSignals( block );
00185 
00186       setSelected( m_pCurrentItem, true );
00187     }
00188 #if defined Q_WS_X11 && ! defined K_WS_QTONLY //FIXME
00189   }
00190   else
00191     kdDebug() << "Thatīs not supposed to happen!!!!" << endl;
00192 #endif
00193 }
00194 
00195 void KListBox::emitExecute( QListBoxItem *item, const QPoint &pos )
00196 {
00197 #if defined Q_WS_X11 && ! defined K_WS_QTONLY //FIXME
00198   uint keybstate = KApplication::keyboardModifiers();
00199 #endif
00200     
00201   m_pAutoSelect->stop();
00202   
00203 #if defined Q_WS_X11 && ! defined K_WS_QTONLY //FIXME
00204   //Donīt emit executed if in SC mode and Shift or Ctrl are pressed
00205   if( !( m_bUseSingle && ((keybstate & KApplication::ShiftModifier) || (keybstate & KApplication::ControlModifier)) ) ) {
00206 #endif
00207     emit executed( item );
00208     emit executed( item, pos );
00209 #if defined Q_WS_X11 && ! defined K_WS_QTONLY //FIXME
00210   }
00211 #endif
00212 }
00213 
00214 //
00215 // 2000-16-01 Espen Sand
00216 // This widget is used in dialogs. It should ignore
00217 // F1 (and combinations) and Escape since these are used
00218 // to start help or close the dialog. This functionality
00219 // should be done in QListView but it is not (at least now)
00220 //
00221 void KListBox::keyPressEvent(QKeyEvent *e)
00222 {
00223   if( e->key() == Key_Escape )
00224   {
00225     e->ignore();
00226   }
00227   else if( e->key() == Key_F1 )
00228   {
00229     e->ignore();
00230   }
00231   else
00232   {
00233     QListBox::keyPressEvent(e);
00234   }
00235 }
00236 
00237 void KListBox::focusOutEvent( QFocusEvent *fe )
00238 {
00239   m_pAutoSelect->stop();
00240 
00241   QListBox::focusOutEvent( fe );
00242 }
00243 
00244 void KListBox::leaveEvent( QEvent *e ) 
00245 {
00246   m_pAutoSelect->stop();
00247 
00248   QListBox::leaveEvent( e );
00249 }
00250 
00251 void KListBox::contentsMousePressEvent( QMouseEvent *e )
00252 {
00253   if( (selectionMode() == Extended) && (e->state() & ShiftButton) && !(e->state() & ControlButton) ) {
00254     bool block = signalsBlocked();
00255     blockSignals( true );
00256 
00257     clearSelection();
00258 
00259     blockSignals( block );
00260   }
00261 
00262   QListBox::contentsMousePressEvent( e );
00263 }
00264 
00265 void KListBox::contentsMouseDoubleClickEvent ( QMouseEvent * e )
00266 {
00267   QListBox::contentsMouseDoubleClickEvent( e );
00268 
00269   QListBoxItem* item = itemAt( e->pos() );
00270 
00271   if( item ) {
00272     emit doubleClicked( item, e->globalPos() );
00273 
00274     if( (e->button() == LeftButton) && !m_bUseSingle )
00275       emitExecute( item, e->globalPos() );
00276   }
00277 }
00278 
00279 void KListBox::slotMouseButtonClicked( int btn, QListBoxItem *item, const QPoint &pos )
00280 {
00281   if( (btn == LeftButton) && item )
00282     emitExecute( item, pos );
00283 }
00284 
00285 void KListBox::virtual_hook( int, void* )
00286 { /*BASE::virtual_hook( id, data );*/ }
00287 
00288 #include "klistbox.moc"
KDE Logo
This file is part of the documentation for kdeui Library Version 3.3.0.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Thu Sep 23 17:11:54 2004 by doxygen 1.3.8-20040913 written by Dimitri van Heesch, © 1997-2003