KDELibs4Support

klistwidget.cpp
1 /* This file is part of the KDE libraries
2  Copyright (C) 2000 Reginald Stadlbauer <[email protected]>
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 <QTimer>
25 #include <QKeyEvent>
26 #include <QApplication>
27 
28 class Q_DECL_HIDDEN KListWidget::KListWidgetPrivate
29 {
30 public:
31  KListWidgetPrivate(KListWidget *q)
32  : q(q),
33  m_pCurrentItem(nullptr),
34  m_eventPos()
35  {
36  }
37 
38  void _k_slotItemEntered(QListWidgetItem *);
39  void _k_slotOnViewport();
40  void _k_slotSettingsChanged(int);
41  void _k_slotAutoSelect();
42  void _k_slotEmitExecute(QListWidgetItem *item);
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  QPoint m_eventPos;
52 };
53 
54 KListWidget::KListWidget(QWidget *parent)
55  : QListWidget(parent), d(new KListWidgetPrivate(this))
56 {
57  connect(this, SIGNAL(viewportEntered()),
58  this, SLOT(_k_slotOnViewport()));
59  connect(this, SIGNAL(itemEntered(QListWidgetItem*)),
60  this, SLOT(_k_slotItemEntered(QListWidgetItem*)));
61  d->_k_slotSettingsChanged(KGlobalSettings::SETTINGS_MOUSE);
62  connect(KGlobalSettings::self(), SIGNAL(settingsChanged(int)), SLOT(_k_slotSettingsChanged(int)));
63 
64  d->m_pAutoSelect = new QTimer(this);
65  connect(d->m_pAutoSelect, SIGNAL(timeout()),
66  this, SLOT(_k_slotAutoSelect()));
67 }
68 
69 KListWidget::~KListWidget()
70 {
71  delete d;
72 }
73 
74 void KListWidget::KListWidgetPrivate::_k_slotItemEntered(QListWidgetItem *item)
75 {
76  if (item && m_bChangeCursorOverItem && m_bUseSingle) {
77  q->viewport()->setCursor(QCursor(Qt::OpenHandCursor));
78  }
79 
80  if (item && (m_autoSelectDelay > -1) && m_bUseSingle) {
81  m_pAutoSelect->setSingleShot(true);
82  m_pAutoSelect->start(m_autoSelectDelay);
83  m_pCurrentItem = item;
84  }
85 }
86 
87 void KListWidget::KListWidgetPrivate::_k_slotOnViewport()
88 {
89  if (m_bChangeCursorOverItem) {
90  q->viewport()->unsetCursor();
91  }
92 
93  m_pAutoSelect->stop();
94  m_pCurrentItem = nullptr;
95 }
96 
97 void KListWidget::KListWidgetPrivate::_k_slotSettingsChanged(int category)
98 {
99  if (category != KGlobalSettings::SETTINGS_MOUSE) {
100  return;
101  }
102  m_bUseSingle = q->style()->styleHint(QStyle::SH_ItemView_ActivateItemOnSingleClick);
103 
104  q->disconnect(q, SIGNAL(itemClicked(QListWidgetItem*)));
105  q->disconnect(q, SIGNAL(itemDoubleClicked(QListWidgetItem*)));
106 
107  if (m_bUseSingle) {
108  q->connect(q, SIGNAL(itemClicked(QListWidgetItem*)),
109  SLOT(_k_slotEmitExecute(QListWidgetItem*)));
110  } else {
111  q->connect(q, SIGNAL(itemDoubleClicked(QListWidgetItem*)),
112  SLOT(_k_slotEmitExecute(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 
123 void KListWidget::KListWidgetPrivate::_k_slotAutoSelect()
124 {
125  // check that the item still exists
126  if (q->row(m_pCurrentItem) == -1) {
127  return;
128  }
129 
130  //Give this widget the keyboard focus.
131  if (!q->hasFocus()) {
132  q->setFocus();
133  }
134 
136 
137  QListWidgetItem *previousItem = q->currentItem();
138  q->setCurrentItem(m_pCurrentItem);
139 
140  if (m_pCurrentItem) {
141  //Shift pressed?
142  if ((keybstate & Qt::ShiftModifier)) {
143  bool block = q->signalsBlocked();
144  q->blockSignals(true);
145 
146  //No Ctrl? Then clear before!
147  if (!(keybstate & Qt::ControlModifier)) {
148  q->clearSelection();
149  }
150 
151  bool select = !m_pCurrentItem->isSelected();
152  bool update = q->viewport()->updatesEnabled();
153  q->viewport()->setUpdatesEnabled(false);
154 
155  bool down = q->row(previousItem) < q->row(m_pCurrentItem);
156  QListWidgetItem *it = down ? previousItem : m_pCurrentItem;
157 
158  for (int i = q->row(it); i < q->count(); i++) {
159  if (down && q->item(i) == m_pCurrentItem) {
160  m_pCurrentItem->setSelected(select);
161  break;
162  }
163 
164  if (!down && q->item(i) == previousItem) {
165  previousItem->setSelected(select);
166  break;
167  }
168  it->setSelected(select);
169  }
170 
171  q->blockSignals(block);
172  q->viewport()->setUpdatesEnabled(update);
173 
174  emit q->itemSelectionChanged();
175 
176  if (q->selectionMode() == QAbstractItemView::SingleSelection) {
177  q->emit itemSelectionChanged();
178  }
179  } else if ((keybstate & Qt::ControlModifier)) {
180  m_pCurrentItem->setSelected(!m_pCurrentItem->isSelected());
181  } else {
182  bool block = q->signalsBlocked();
183  q->blockSignals(true);
184 
185  if (!m_pCurrentItem->isSelected()) {
186  q->clearSelection();
187  }
188 
189  q->blockSignals(block);
190 
191  m_pCurrentItem->setSelected(true);
192  }
193  } else {
194  kDebug() << "That's not supposed to happen!!!!";
195  }
196 }
197 
198 void KListWidget::KListWidgetPrivate::_k_slotEmitExecute(QListWidgetItem *item)
199 {
201 
202  m_pAutoSelect->stop();
203 
204  //Don't emit executed if in SC mode and Shift or Ctrl are pressed
205  if (!(m_bUseSingle && ((keybstate & Qt::ShiftModifier) || (keybstate & Qt::ControlModifier)))) {
206  emit q->executed(item);
207  if (!m_eventPos.isNull()) {
208  emit q->executed(item, m_eventPos);
209  }
210  }
211 }
212 
213 //
214 // 2000-16-01 Espen Sand
215 // This widget is used in dialogs. It should ignore
216 // F1 (and combinations) and Escape since these are used
217 // to start help or close the dialog. This functionality
218 // should be done in QListView but it is not (at least now)
219 //
220 void KListWidget::keyPressEvent(QKeyEvent *e)
221 {
222  if (e->key() == Qt::Key_Escape) {
223  e->ignore();
224  } else if (e->key() == Qt::Key_F1) {
225  e->ignore();
226  } else {
228  }
229 }
230 
231 void KListWidget::focusOutEvent(QFocusEvent *fe)
232 {
233  d->m_pAutoSelect->stop();
234 
236 }
237 
238 void KListWidget::leaveEvent(QEvent *e)
239 {
240  d->m_pAutoSelect->stop();
241 
243 }
244 
245 void KListWidget::mousePressEvent(QMouseEvent *e)
246 {
247  if ((selectionMode() == QAbstractItemView::ExtendedSelection) && (e->modifiers() & Qt::ShiftModifier) && !(e->modifiers() & Qt::ControlModifier)) {
248  bool block = signalsBlocked();
249  blockSignals(true);
250 
251  clearSelection();
252 
253  blockSignals(block);
254  }
255 
257 }
258 
259 void KListWidget::mouseDoubleClickEvent(QMouseEvent *e)
260 {
261  QPoint oldPos = d->m_eventPos;
262  d->m_eventPos = e->globalPos();
264  d->m_eventPos = oldPos;
265 }
266 
267 void KListWidget::mouseReleaseEvent(QMouseEvent *e)
268 {
269  QPoint oldPos = d->m_eventPos;
270  d->m_eventPos = e->globalPos();
272  d->m_eventPos = oldPos;
273 }
274 
275 #include "moc_klistwidget.cpp"
typedef KeyboardModifiers
bool isSelected() const const
virtual void focusOutEvent(QFocusEvent *event) override
virtual void mouseReleaseEvent(QMouseEvent *e) override
static KGlobalSettings * self()
Return the KGlobalSettings singleton.
Qt::KeyboardModifiers keyboardModifiers()
void update()
QPoint globalPos() const const
void ignore()
bool signalsBlocked() const const
Qt::KeyboardModifiers modifiers() const const
int key() const const
bool blockSignals(bool block)
static bool changeCursorOverIcon()
Checks whether the cursor changes over icons.
virtual void mousePressEvent(QMouseEvent *event) override
static int autoSelectDelay()
Returns the KDE setting for the auto-select option.
virtual void keyPressEvent(QKeyEvent *event) override
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
A variant of QListWidget that honors KDE&#39;s system-wide settings.
Definition: klistwidget.h:41
virtual void leaveEvent(QEvent *event)
void setSelected(bool select)
virtual void mouseDoubleClickEvent(QMouseEvent *event) override
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Wed May 27 2020 22:55:36 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.