• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.8.5 API Reference
  • KDE Home
  • Contact Us
 

KDEUI

  • kdeui
  • itemviews
klistwidget.cpp
Go to the documentation of this file.
1 /* This file is part of the KDE libraries
2  Copyright (C) 2000 Reginald Stadlbauer <reggie@kde.org>
3 
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Library General Public
6  License version 2 as published by the Free Software Foundation.
7 
8  This library is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  Library General Public License for more details.
12 
13  You should have received a copy of the GNU Library General Public License
14  along with this library; see the file COPYING.LIB. If not, write to
15  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16  Boston, MA 02110-1301, USA.
17 */
18 
19 #include "klistwidget.h"
20 
21 #include <kglobalsettings.h>
22 #include <kdebug.h>
23 
24 #include <QtCore/QTimer>
25 #include <QKeyEvent>
26 #include <QApplication>
27 
28 class KListWidget::KListWidgetPrivate
29 {
30  public:
31  KListWidgetPrivate(KListWidget *q)
32  : q(q),
33  m_pCurrentItem(0)
34  {
35  }
36 
37  void emitExecute( QListWidgetItem *item, const QPoint &pos );
38 
39  void _k_slotItemEntered(QListWidgetItem*);
40  void _k_slotOnViewport();
41  void _k_slotSettingsChanged(int);
42  void _k_slotAutoSelect();
43 
44  KListWidget *q;
45  bool m_bUseSingle : 1;
46  bool m_bChangeCursorOverItem : 1;
47 
48  QListWidgetItem* m_pCurrentItem;
49  QTimer* m_pAutoSelect;
50  int m_autoSelectDelay;
51 };
52 
53 KListWidget::KListWidget( QWidget *parent )
54  : QListWidget(parent), d(new KListWidgetPrivate(this))
55 {
56  connect( this, SIGNAL(viewportEntered()),
57  this, SLOT(_k_slotOnViewport()) );
58  connect( this, SIGNAL(itemEntered(QListWidgetItem*)),
59  this, SLOT(_k_slotItemEntered(QListWidgetItem*)) );
60  d->_k_slotSettingsChanged(KGlobalSettings::SETTINGS_MOUSE);
61  connect( KGlobalSettings::self(), SIGNAL(settingsChanged(int)), SLOT(_k_slotSettingsChanged(int)) );
62 
63  d->m_pAutoSelect = new QTimer( this );
64  connect( d->m_pAutoSelect, SIGNAL(timeout()),
65  this, SLOT(_k_slotAutoSelect()) );
66 }
67 
68 KListWidget::~KListWidget()
69 {
70  delete d;
71 }
72 
73 void KListWidget::KListWidgetPrivate::_k_slotItemEntered( QListWidgetItem *item )
74 {
75  if ( item && m_bChangeCursorOverItem && m_bUseSingle )
76  q->viewport()->setCursor( QCursor( Qt::OpenHandCursor ) );
77 
78  if ( item && (m_autoSelectDelay > -1) && m_bUseSingle ) {
79  m_pAutoSelect->setSingleShot( true );
80  m_pAutoSelect->start( m_autoSelectDelay );
81  m_pCurrentItem = item;
82  }
83 }
84 
85 void KListWidget::KListWidgetPrivate::_k_slotOnViewport()
86 {
87  if ( m_bChangeCursorOverItem )
88  q->viewport()->unsetCursor();
89 
90  m_pAutoSelect->stop();
91  m_pCurrentItem = 0;
92 }
93 
94 
95 void KListWidget::KListWidgetPrivate::_k_slotSettingsChanged(int category)
96 {
97  if (category != KGlobalSettings::SETTINGS_MOUSE)
98  return;
99  m_bUseSingle = KGlobalSettings::singleClick();
100 
101  q->disconnect(q, SIGNAL(itemClicked(QListWidgetItem*)));
102  q->disconnect(q, SIGNAL(itemDoubleClicked(QListWidgetItem*)));
103 
104  if( m_bUseSingle )
105  {
106  q->connect(q, SIGNAL(itemClicked(QListWidgetItem*)),
107  SIGNAL(executed(QListWidgetItem*)));
108  }
109  else
110  {
111  q->connect(q, SIGNAL(itemDoubleClicked(QListWidgetItem*)),
112  SIGNAL(executed(QListWidgetItem*)));
113  }
114 
115  m_bChangeCursorOverItem = KGlobalSettings::changeCursorOverIcon();
116  m_autoSelectDelay = KGlobalSettings::autoSelectDelay();
117 
118  if( !m_bUseSingle || !m_bChangeCursorOverItem )
119  q->viewport()->unsetCursor();
120 }
121 
122 void KListWidget::KListWidgetPrivate::_k_slotAutoSelect()
123 {
124  // check that the item still exists
125  if( q->row( m_pCurrentItem ) == -1 )
126  return;
127 
128  //Give this widget the keyboard focus.
129  if( !q->hasFocus() )
130  q->setFocus();
131 
132  Qt::KeyboardModifiers keybstate = QApplication::keyboardModifiers();
133 
134  QListWidgetItem* previousItem = q->currentItem();
135  q->setCurrentItem( m_pCurrentItem );
136 
137  if( m_pCurrentItem ) {
138  //Shift pressed?
139  if( (keybstate & Qt::ShiftModifier) ) {
140  bool block = q->signalsBlocked();
141  q->blockSignals( true );
142 
143  //No Ctrl? Then clear before!
144  if( !(keybstate & Qt::ControlModifier) )
145  q->clearSelection();
146 
147  bool select = !m_pCurrentItem->isSelected();
148  bool update = q->viewport()->updatesEnabled();
149  q->viewport()->setUpdatesEnabled( false );
150 
151  bool down = q->row( previousItem ) < q->row( m_pCurrentItem );
152  QListWidgetItem* it = down ? previousItem : m_pCurrentItem;
153 
154  for (int i = q->row(it) ; i < q->count() ; i++ ) {
155  if ( down && q->item(i) == m_pCurrentItem ) {
156  m_pCurrentItem->setSelected(select);
157  break;
158  }
159 
160  if ( !down && q->item(i) == previousItem ) {
161  previousItem->setSelected(select);
162  break;
163  }
164  it->setSelected(select);
165  }
166 
167  q->blockSignals( block );
168  q->viewport()->setUpdatesEnabled( update );
169 
170  emit q->itemSelectionChanged();
171 
172  if( q->selectionMode() == QAbstractItemView::SingleSelection )
173  q->emit itemSelectionChanged();
174  }
175  else if( (keybstate & Qt::ControlModifier) )
176  m_pCurrentItem->setSelected(!m_pCurrentItem->isSelected());
177  else {
178  bool block = q->signalsBlocked();
179  q->blockSignals( true );
180 
181  if( !m_pCurrentItem->isSelected() )
182  q->clearSelection();
183 
184  q->blockSignals( block );
185 
186  m_pCurrentItem->setSelected(true);
187  }
188  }
189  else
190  kDebug() << "That's not supposed to happen!!!!";
191 }
192 
193 void KListWidget::KListWidgetPrivate::emitExecute( QListWidgetItem *item, const QPoint &pos )
194 {
195  Qt::KeyboardModifiers keybstate = QApplication::keyboardModifiers();
196 
197  m_pAutoSelect->stop();
198 
199  //Don't emit executed if in SC mode and Shift or Ctrl are pressed
200  if( !( m_bUseSingle && ((keybstate & Qt::ShiftModifier) || (keybstate & Qt::ControlModifier)) ) ) {
201  emit q->executed( item );
202  emit q->executed( item, pos );
203  }
204 }
205 
206 //
207 // 2000-16-01 Espen Sand
208 // This widget is used in dialogs. It should ignore
209 // F1 (and combinations) and Escape since these are used
210 // to start help or close the dialog. This functionality
211 // should be done in QListView but it is not (at least now)
212 //
213 void KListWidget::keyPressEvent(QKeyEvent *e)
214 {
215  if( e->key() == Qt::Key_Escape )
216  {
217  e->ignore();
218  }
219  else if( e->key() == Qt::Key_F1 )
220  {
221  e->ignore();
222  }
223  else
224  {
225  QListWidget::keyPressEvent(e);
226  }
227 }
228 
229 void KListWidget::focusOutEvent( QFocusEvent *fe )
230 {
231  d->m_pAutoSelect->stop();
232 
233  QListWidget::focusOutEvent( fe );
234 }
235 
236 void KListWidget::leaveEvent( QEvent *e )
237 {
238  d->m_pAutoSelect->stop();
239 
240  QListWidget::leaveEvent( e );
241 }
242 
243 void KListWidget::mousePressEvent( QMouseEvent *e )
244 {
245  if( (selectionMode() == QAbstractItemView::ExtendedSelection) && (e->modifiers() & Qt::ShiftModifier) && !(e->modifiers() & Qt::ControlModifier) ) {
246  bool block = signalsBlocked();
247  blockSignals( true );
248 
249  clearSelection();
250 
251  blockSignals( block );
252  }
253 
254  QListWidget::mousePressEvent( e );
255 }
256 
257 void KListWidget::mouseDoubleClickEvent ( QMouseEvent * e )
258 {
259  QListWidget::mouseDoubleClickEvent( e );
260 
261  QListWidgetItem* item = itemAt( e->pos() );
262 
263  if( item ) {
264  emit doubleClicked( item, e->globalPos() );
265 
266  if( (e->button() == Qt::LeftButton) && !d->m_bUseSingle )
267  d->emitExecute( item, e->globalPos() );
268  }
269 }
270 
271 #include "klistwidget.moc"
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Fri Dec 7 2012 16:04:41 by doxygen 1.8.1 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KDEUI

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

kdelibs-4.8.5 API Reference

Skip menu "kdelibs-4.8.5 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal