• Skip to content
  • Skip to link menu
KDE 4.3 API Reference
  • KDE API Reference
  • kdelibs
  • Sitemap
  • Contact Us
 

KDE3Support

k3listbox.cpp

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

KDE3Support

Skip menu "KDE3Support"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.6.1
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal