]>
cloud.milkyroute.net Git - dolphin.git/blob - src/views/selectionmanager.cpp
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 <kiconeffect.h>
27 #include <QAbstractButton>
28 #include <QAbstractItemView>
29 #include <QAbstractProxyModel>
30 #include <QApplication>
31 #include <QModelIndex>
33 #include <QPaintEvent>
37 SelectionManager::SelectionManager(QAbstractItemView
* parent
) :
43 connect(parent
, SIGNAL(entered(const QModelIndex
&)),
44 this, SLOT(slotEntered(const QModelIndex
&)));
45 connect(parent
, SIGNAL(viewportEntered()),
46 this, SLOT(slotViewportEntered()));
47 m_toggle
= new SelectionToggle(m_view
->viewport());
48 m_toggle
->setCheckable(true);
50 connect(m_toggle
, SIGNAL(clicked(bool)),
51 this, SLOT(setItemSelected(bool)));
53 m_view
->viewport()->installEventFilter(this);
56 SelectionManager::~SelectionManager()
60 bool SelectionManager::eventFilter(QObject
* watched
, QEvent
* event
)
62 Q_ASSERT(watched
== m_view
->viewport());
63 if (event
->type() == QEvent::MouseButtonPress
) {
64 // Set the toggle invisible, if a mouse button has been pressed
65 // outside the toggle boundaries. This e.g. assures, that the toggle
66 // gets invisible during dragging items.
67 const QRect
toggleBounds(m_toggle
->mapToGlobal(QPoint(0, 0)), m_toggle
->size());
68 m_toggle
->setVisible(toggleBounds
.contains(QCursor::pos()));
70 return QObject::eventFilter(watched
, event
);
73 void SelectionManager::reset()
78 void SelectionManager::slotEntered(const QModelIndex
& index
)
81 const bool showToggle
= index
.isValid() &&
82 (index
.column() == DolphinModel::Name
) &&
83 (QApplication::mouseButtons() == Qt::NoButton
);
85 m_toggle
->setUrl(urlForIndex(index
));
88 connect(m_view
->model(), SIGNAL(rowsRemoved(const QModelIndex
&, int, int)),
89 this, SLOT(slotRowsRemoved(const QModelIndex
&, int, int)));
90 connect(m_view
->selectionModel(),
91 SIGNAL(selectionChanged(const QItemSelection
&, const QItemSelection
&)),
93 SLOT(slotSelectionChanged(const QItemSelection
&, const QItemSelection
&)));
97 // Increase the size of the toggle for large items
98 const int iconHeight
= m_view
->iconSize().height();
100 int toggleSize
= KIconLoader::SizeSmall
;
101 if (iconHeight
>= KIconLoader::SizeEnormous
) {
102 toggleSize
= KIconLoader::SizeMedium
;
103 } else if (iconHeight
>= KIconLoader::SizeLarge
) {
104 toggleSize
= KIconLoader::SizeSmallMedium
;
107 // Add a small invisible margin, if the item-height is nearly
108 // equal to the toggleSize (#169494).
109 const QRect rect
= m_view
->visualRect(index
);
110 int margin
= (rect
.height() - toggleSize
) / 2;
114 toggleSize
+= 2 * margin
;
115 m_toggle
->setMargin(margin
);
116 m_toggle
->resize(toggleSize
, toggleSize
);
117 m_toggle
->move(rect
.topLeft());
119 QItemSelectionModel
* selModel
= m_view
->selectionModel();
120 m_toggle
->setChecked(selModel
->isSelected(index
));
123 m_toggle
->setUrl(KUrl());
124 disconnect(m_view
->model(), SIGNAL(rowsRemoved(const QModelIndex
&, int, int)),
125 this, SLOT(slotRowsRemoved(const QModelIndex
&, int, int)));
126 disconnect(m_view
->selectionModel(),
127 SIGNAL(selectionChanged(const QItemSelection
&, const QItemSelection
&)),
129 SLOT(slotSelectionChanged(const QItemSelection
&, const QItemSelection
&)));
134 void SelectionManager::slotViewportEntered()
139 void SelectionManager::setItemSelected(bool selected
)
141 emit
selectionChanged();
143 if (!m_toggle
->url().isEmpty()) {
144 const QModelIndex index
= indexForUrl(m_toggle
->url());
145 if (index
.isValid()) {
146 QItemSelectionModel
* selModel
= m_view
->selectionModel();
148 selModel
->select(index
, QItemSelectionModel::Select
);
150 selModel
->select(index
, QItemSelectionModel::Deselect
);
152 selModel
->setCurrentIndex(index
, QItemSelectionModel::Current
);
157 void SelectionManager::slotRowsRemoved(const QModelIndex
& parent
, int start
, int end
)
165 void SelectionManager::slotSelectionChanged(const QItemSelection
& selected
,
166 const QItemSelection
& deselected
)
168 // The selection has been changed outside the scope of the selection manager
169 // (e. g. by the rubberband or the "Select All" action). Take care updating
170 // the state of the toggle button.
171 if (!m_toggle
->url().isEmpty()) {
172 const QModelIndex index
= indexForUrl(m_toggle
->url());
173 if (index
.isValid()) {
174 if (selected
.contains(index
)) {
175 m_toggle
->setChecked(true);
178 if (deselected
.contains(index
)) {
179 m_toggle
->setChecked(false);
185 KUrl
SelectionManager::urlForIndex(const QModelIndex
& index
) const
187 QAbstractProxyModel
* proxyModel
= static_cast<QAbstractProxyModel
*>(m_view
->model());
188 KDirModel
* dirModel
= static_cast<KDirModel
*>(proxyModel
->sourceModel());
189 const QModelIndex dirIndex
= proxyModel
->mapToSource(index
);
190 return dirModel
->itemForIndex(dirIndex
).url();
193 const QModelIndex
SelectionManager::indexForUrl(const KUrl
& url
) const
195 QAbstractProxyModel
* proxyModel
= static_cast<QAbstractProxyModel
*>(m_view
->model());
196 KDirModel
* dirModel
= static_cast<KDirModel
*>(proxyModel
->sourceModel());
197 const QModelIndex dirIndex
= dirModel
->indexForUrl(url
);
198 return proxyModel
->mapFromSource(dirIndex
);
201 #include "selectionmanager.moc"