KDE3Support
k3listbox.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
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
00079
00080
00081
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
00093
00094
00095
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
00108 if( index( m_pCurrentItem ) == -1 )
00109 return;
00110
00111
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
00122 if( (keybstate & Qt::ShiftModifier) ) {
00123 bool block = signalsBlocked();
00124 blockSignals( true );
00125
00126
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
00182 if( !( m_bUseSingle && ((keybstate & Qt::ShiftModifier) || (keybstate & Qt::ControlModifier)) ) ) {
00183 emit executed( item );
00184 emit executed( item, pos );
00185 }
00186 }
00187
00188
00189
00190
00191
00192
00193
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"