| # kdeui Library API Documentation
Overview | Class Hierarchy | Classes | Classes (annotated) | Members | Namespaces | Source Files

# 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 #include <kipc.h>
    00026 #include <kdebug.h>
    00027
    00028 #include "klistbox.h"
    00029
    00030 KListBox::KListBox( QWidget *parent, const char *name, WFlags f )
    00031     : QListBox( parent, name, f ), d(0)
    00032 {
    00033     connect( this, SIGNAL( onViewport() ),
    00034          this, SLOT( slotOnViewport() ) );
    00035     connect( this, SIGNAL( onItem( QListBoxItem * ) ),
    00036          this, SLOT( slotOnItem( QListBoxItem * ) ) );
    00037     slotSettingsChanged(KApplication::SETTINGS_MOUSE);
    00038     if (kapp)
    00039     {
    00040         connect( kapp, SIGNAL( settingsChanged(int) ), SLOT( slotSettingsChanged(int) ) );
    00041         kapp->addKipcEventMask( KIPC::SettingsChanged );
    00042     }
    00043
    00044     m_pCurrentItem = 0L;
    00045
    00046     m_pAutoSelect = new QTimer( this );
    00047     connect( m_pAutoSelect, SIGNAL( timeout() ),
    00048              this, SLOT( slotAutoSelect() ) );
    00049 }
    00050
    00051 void KListBox::slotOnItem( QListBoxItem *item )
    00052 {
    00053     if ( item && m_bChangeCursorOverItem && m_bUseSingle )
    00054         viewport()->setCursor( KCursor().handCursor() );
    00055
    00056     if ( item && (m_autoSelectDelay > -1) && m_bUseSingle ) {
    00057       m_pAutoSelect->start( m_autoSelectDelay, true );
    00058       m_pCurrentItem = item;
    00059     }
    00060 }
    00061
    00062 void KListBox::slotOnViewport()
    00063 {
    00064     if ( m_bChangeCursorOverItem )
    00065         viewport()->unsetCursor();
    00066
    00067     m_pAutoSelect->stop();
    00068     m_pCurrentItem = 0L;
    00069 }
    00070
    00071
    00072 void KListBox::slotSettingsChanged(int category)
    00073 {
    00074     if (category != KApplication::SETTINGS_MOUSE)
    00075         return;
    00076     m_bUseSingle = KGlobalSettings::singleClick();
    00077
    00078     disconnect( this, SIGNAL( mouseButtonClicked( int, QListBoxItem *,
    00079                           const QPoint & ) ),
    00080         this, SLOT( slotMouseButtonClicked( int, QListBoxItem *,
    00081                             const QPoint & ) ) );
    00082 //         disconnect( this, SIGNAL( doubleClicked( QListBoxItem *,
    00083 //                       const QPoint & ) ),
    00084 //          this, SLOT( slotExecute( QListBoxItem *,
    00085 //                       const QPoint & ) ) );
    00086
    00087     if( m_bUseSingle )
    00088     {
    00089       connect( this, SIGNAL( mouseButtonClicked( int, QListBoxItem *,
    00090                          const QPoint & ) ),
    00091            this, SLOT( slotMouseButtonClicked( int, QListBoxItem *,
    00092                            const QPoint & ) ) );
    00093     }
    00094     else
    00095     {
    00096 //         connect( this, SIGNAL( doubleClicked( QListBoxItem *,
    00097 //                        const QPoint & ) ),
    00098 //                  this, SLOT( slotExecute( QListBoxItem *,
    00099 //                    const QPoint & ) ) );
    00100     }
    00101
    00102     m_bChangeCursorOverItem = KGlobalSettings::changeCursorOverIcon();
    00103     m_autoSelectDelay = KGlobalSettings::autoSelectDelay();
    00104
    00105     if( !m_bUseSingle || !m_bChangeCursorOverItem )
    00106         viewport()->unsetCursor();
    00107 }
    00108
    00109 void KListBox::slotAutoSelect()
    00110 {
    00111   // check that the item still exists
    00112   if( index( m_pCurrentItem ) == -1 )
    00113     return;
    00114
    00115   //Give this widget the keyboard focus.
    00116   if( !hasFocus() )
    00117     setFocus();
    00118
    00119   ButtonState keybstate = KApplication::keyboardMouseState();
    00120
    00121   QListBoxItem* previousItem = item( currentItem() );
    00122   setCurrentItem( m_pCurrentItem );
    00123
    00124   if( m_pCurrentItem ) {
    00125     //Shift pressed?
    00126     if( (keybstate & ShiftButton) ) {
    00127       bool block = signalsBlocked();
    00128       blockSignals( true );
    00129
    00130       //No Ctrl? Then clear before!
    00131       if( !(keybstate & ControlButton) )
    00132     clearSelection();
    00133
    00134       bool select = !m_pCurrentItem->isSelected();
    00135       bool update = viewport()->isUpdatesEnabled();
    00136       viewport()->setUpdatesEnabled( false );
    00137
    00138       bool down = index( previousItem ) < index( m_pCurrentItem );
    00139       QListBoxItem* it = down ? previousItem : m_pCurrentItem;
    00140       for (;it ; it = it->next() ) {
    00141     if ( down && it == m_pCurrentItem ) {
    00142       setSelected( m_pCurrentItem, select );
    00143       break;
    00144     }
    00145     if ( !down && it == previousItem ) {
    00146       setSelected( previousItem, select );
    00147       break;
    00148     }
    00149     setSelected( it, select );
    00150       }
    00151
    00152       blockSignals( block );
    00153       viewport()->setUpdatesEnabled( update );
    00154       triggerUpdate( false );
    00155
    00156       emit selectionChanged();
    00157
    00158       if( selectionMode() == QListBox::Single )
    00159     emit selectionChanged( m_pCurrentItem );
    00160     }
    00161     else if( (keybstate & ControlButton) )
    00162       setSelected( m_pCurrentItem, !m_pCurrentItem->isSelected() );
    00163     else {
    00164       bool block = signalsBlocked();
    00165       blockSignals( true );
    00166
    00167       if( !m_pCurrentItem->isSelected() )
    00168     clearSelection();
    00169
    00170       blockSignals( block );
    00171
    00172       setSelected( m_pCurrentItem, true );
    00173     }
    00174   }
    00175   else
    00176     kdDebug() << "Thatīs not supposed to happen!!!!" << endl;
    00177 }
    00178
    00179 void KListBox::emitExecute( QListBoxItem *item, const QPoint &pos )
    00180 {
    00181   ButtonState keybstate = KApplication::keyboardMouseState();
    00182
    00183   m_pAutoSelect->stop();
    00184
    00185   //Donīt emit executed if in SC mode and Shift or Ctrl are pressed
    00186   if( !( m_bUseSingle && ((keybstate & ShiftButton) || (keybstate & ControlButton)) ) ) {
    00187     emit executed( item );
    00188     emit executed( item, pos );
    00189   }
    00190 }
    00191
    00192 //
    00193 // 2000-16-01 Espen Sand
    00194 // This widget is used in dialogs. It should ignore
    00195 // F1 (and combinations) and Escape since these are used
    00196 // to start help or close the dialog. This functionality
    00197 // should be done in QListView but it is not (at least now)
    00198 //
    00199 void KListBox::keyPressEvent(QKeyEvent *e)
    00200 {
    00201   if( e->key() == Key_Escape )
    00202   {
    00203     e->ignore();
    00204   }
    00205   else if( e->key() == Key_F1 )
    00206   {
    00207     e->ignore();
    00208   }
    00209   else
    00210   {
    00211     QListBox::keyPressEvent(e);
    00212   }
    00213 }
    00214
    00215 void KListBox::focusOutEvent( QFocusEvent *fe )
    00216 {
    00217   m_pAutoSelect->stop();
    00218
    00219   QListBox::focusOutEvent( fe );
    00220 }
    00221
    00222 void KListBox::leaveEvent( QEvent *e )
    00223 {
    00224   m_pAutoSelect->stop();
    00225
    00226   QListBox::leaveEvent( e );
    00227 }
    00228
    00229 void KListBox::contentsMousePressEvent( QMouseEvent *e )
    00230 {
    00231   if( (selectionMode() == Extended) && (e->state() & ShiftButton) && !(e->state() & ControlButton) ) {
    00232     bool block = signalsBlocked();
    00233     blockSignals( true );
    00234
    00235     clearSelection();
    00236
    00237     blockSignals( block );
    00238   }
    00239
    00240   QListBox::contentsMousePressEvent( e );
    00241 }
    00242
    00243 void KListBox::contentsMouseDoubleClickEvent ( QMouseEvent * e )
    00244 {
    00245   QListBox::contentsMouseDoubleClickEvent( e );
    00246
    00247   QListBoxItem* item = itemAt( contentsToViewport( e->pos() ) );
    00248
    00249   if( item ) {
    00250     emit doubleClicked( item, e->globalPos() );
    00251
    00252     if( (e->button() == LeftButton) && !m_bUseSingle )
    00253       emitExecute( item, e->globalPos() );
    00254   }
    00255 }
    00256
    00257 void KListBox::slotMouseButtonClicked( int btn, QListBoxItem *item, const QPoint &pos )
    00258 {
    00259   if( (btn == LeftButton) && item )
    00260     emitExecute( item, pos );
    00261 }
    00262
    00263 void KListBox::virtual_hook( int, void* )
    00264 { /*BASE::virtual_hook( id, data );*/ }
    00265
    00266 #include "klistbox.moc"

This file is part of the documentation for kdeui Library Version 3.4.1.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sat Jun 11 20:20:18 2005 by doxygen 1.4.1 written by Dimitri van Heesch, © 1997-2003