1 /***************************************************************************
2 * Copyright (C) 2008 by Peter Penz <peter.penz@gmx.at> *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18 ***************************************************************************/
20 #include "selectionmanager.h"
22 #include "dolphinmodel.h"
23 #include "selectiontoggle.h"
24 #include <kdirmodel.h>
25 #include <kglobalsettings.h>
26 #include <kiconeffect.h>
28 #include <QAbstractButton>
29 #include <QAbstractItemView>
30 #include <QAbstractProxyModel>
31 #include <QApplication>
32 #include <QModelIndex>
34 #include <QPaintEvent>
38 SelectionManager::SelectionManager(QAbstractItemView
* parent
) :
43 m_appliedPointingHandCursor(false)
45 connect(parent
, SIGNAL(entered(const QModelIndex
&)),
46 this, SLOT(slotEntered(const QModelIndex
&)));
47 connect(parent
, SIGNAL(viewportEntered()),
48 this, SLOT(slotViewportEntered()));
49 m_toggle
= new SelectionToggle(m_view
->viewport());
50 m_toggle
->setCheckable(true);
52 connect(m_toggle
, SIGNAL(clicked(bool)),
53 this, SLOT(setItemSelected(bool)));
54 m_toggle
->installEventFilter(this);
56 m_view
->viewport()->installEventFilter(this);
59 SelectionManager::~SelectionManager()
63 bool SelectionManager::eventFilter(QObject
* watched
, QEvent
* event
)
65 if (watched
== m_view
->viewport()) {
66 switch (event
->type()) {
71 case QEvent::MouseButtonPress
: {
72 // Set the toggle invisible, if a mouse button has been pressed
73 // outside the toggle boundaries. This e.g. assures, that the toggle
74 // gets invisible during dragging items.
75 const QRect
toggleBounds(m_toggle
->mapToGlobal(QPoint(0, 0)), m_toggle
->size());
76 m_toggle
->setVisible(toggleBounds
.contains(QCursor::pos()));
83 } else if (watched
== m_toggle
) {
84 switch (event
->type()) {
86 // If the toggle button gets hidden, the cursor is not above the item
87 // anymore and the shape must get restored
92 QApplication::changeOverrideCursor(Qt::ArrowCursor
);
96 QApplication::changeOverrideCursor(Qt::PointingHandCursor
);
104 return QObject::eventFilter(watched
, event
);
107 void SelectionManager::reset()
112 void SelectionManager::slotEntered(const QModelIndex
& index
)
115 const bool showToggle
= index
.isValid() &&
116 (index
.column() == DolphinModel::Name
) &&
117 (QApplication::mouseButtons() == Qt::NoButton
);
119 if (KGlobalSettings::singleClick()) {
120 applyPointingHandCursor();
123 m_toggle
->setUrl(urlForIndex(index
));
126 connect(m_view
->model(), SIGNAL(rowsRemoved(const QModelIndex
&, int, int)),
127 this, SLOT(slotRowsRemoved(const QModelIndex
&, int, int)));
128 connect(m_view
->selectionModel(),
129 SIGNAL(selectionChanged(const QItemSelection
&, const QItemSelection
&)),
131 SLOT(slotSelectionChanged(const QItemSelection
&, const QItemSelection
&)));
135 // Increase the size of the toggle for large items
136 const int iconHeight
= m_view
->iconSize().height();
138 int toggleSize
= KIconLoader::SizeSmall
;
139 if (iconHeight
>= KIconLoader::SizeEnormous
) {
140 toggleSize
= KIconLoader::SizeMedium
;
141 } else if (iconHeight
>= KIconLoader::SizeLarge
) {
142 toggleSize
= KIconLoader::SizeSmallMedium
;
145 // Add a small invisible margin, if the item-height is nearly
146 // equal to the toggleSize (#169494).
147 const QRect rect
= m_view
->visualRect(index
);
148 int margin
= (rect
.height() - toggleSize
) / 2;
152 toggleSize
+= 2 * margin
;
153 m_toggle
->setMargin(margin
);
154 m_toggle
->resize(toggleSize
, toggleSize
);
155 m_toggle
->move(rect
.topLeft());
157 QItemSelectionModel
* selModel
= m_view
->selectionModel();
158 m_toggle
->setChecked(selModel
->isSelected(index
));
161 m_toggle
->setUrl(KUrl());
162 disconnect(m_view
->model(), SIGNAL(rowsRemoved(const QModelIndex
&, int, int)),
163 this, SLOT(slotRowsRemoved(const QModelIndex
&, int, int)));
164 disconnect(m_view
->selectionModel(),
165 SIGNAL(selectionChanged(const QItemSelection
&, const QItemSelection
&)),
167 SLOT(slotSelectionChanged(const QItemSelection
&, const QItemSelection
&)));
172 void SelectionManager::slotViewportEntered()
177 void SelectionManager::setItemSelected(bool selected
)
179 emit
selectionChanged();
181 if (!m_toggle
->url().isEmpty()) {
182 const QModelIndex index
= indexForUrl(m_toggle
->url());
183 if (index
.isValid()) {
184 QItemSelectionModel
* selModel
= m_view
->selectionModel();
186 selModel
->select(index
, QItemSelectionModel::Select
);
188 selModel
->select(index
, QItemSelectionModel::Deselect
);
190 selModel
->setCurrentIndex(index
, QItemSelectionModel::Current
);
195 void SelectionManager::slotRowsRemoved(const QModelIndex
& parent
, int start
, int end
)
203 void SelectionManager::slotSelectionChanged(const QItemSelection
& selected
,
204 const QItemSelection
& deselected
)
206 // The selection has been changed outside the scope of the selection manager
207 // (e. g. by the rubberband or the "Select All" action). Take care updating
208 // the state of the toggle button.
209 if (!m_toggle
->url().isEmpty()) {
210 const QModelIndex index
= indexForUrl(m_toggle
->url());
211 if (index
.isValid()) {
212 if (selected
.contains(index
)) {
213 m_toggle
->setChecked(true);
216 if (deselected
.contains(index
)) {
217 m_toggle
->setChecked(false);
223 KUrl
SelectionManager::urlForIndex(const QModelIndex
& index
) const
225 QAbstractProxyModel
* proxyModel
= static_cast<QAbstractProxyModel
*>(m_view
->model());
226 KDirModel
* dirModel
= static_cast<KDirModel
*>(proxyModel
->sourceModel());
227 const QModelIndex dirIndex
= proxyModel
->mapToSource(index
);
228 return dirModel
->itemForIndex(dirIndex
).url();
231 const QModelIndex
SelectionManager::indexForUrl(const KUrl
& url
) const
233 QAbstractProxyModel
* proxyModel
= static_cast<QAbstractProxyModel
*>(m_view
->model());
234 KDirModel
* dirModel
= static_cast<KDirModel
*>(proxyModel
->sourceModel());
235 const QModelIndex dirIndex
= dirModel
->indexForUrl(url
);
236 return proxyModel
->mapFromSource(dirIndex
);
240 void SelectionManager::applyPointingHandCursor()
242 if (!m_appliedPointingHandCursor
) {
243 QApplication::setOverrideCursor(QCursor(Qt::PointingHandCursor
));
244 m_appliedPointingHandCursor
= true;
248 void SelectionManager::restoreCursor()
250 if (m_appliedPointingHandCursor
) {
251 QApplication::restoreOverrideCursor();
252 m_appliedPointingHandCursor
= false;
256 #include "selectionmanager.moc"