]> cloud.milkyroute.net Git - dolphin.git/blob - src/views/selectionmanager.cpp
Add a small invisible margin to the selection toggle in case the item-height is nearl...
[dolphin.git] / src / views / selectionmanager.cpp
1 /***************************************************************************
2 * Copyright (C) 2008 by Peter Penz <peter.penz@gmx.at> *
3 * *
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. *
8 * *
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. *
13 * *
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 ***************************************************************************/
19
20 #include "selectionmanager.h"
21
22 #include "dolphinmodel.h"
23 #include "selectiontoggle.h"
24 #include <kdirmodel.h>
25 #include <kiconeffect.h>
26
27 #include <QAbstractButton>
28 #include <QAbstractItemView>
29 #include <QAbstractProxyModel>
30 #include <QApplication>
31 #include <QModelIndex>
32 #include <QPainter>
33 #include <QPaintEvent>
34 #include <QRect>
35 #include <QTimeLine>
36
37 SelectionManager::SelectionManager(QAbstractItemView* parent) :
38 QObject(parent),
39 m_view(parent),
40 m_toggle(0),
41 m_connected(false)
42 {
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);
49 m_toggle->hide();
50 connect(m_toggle, SIGNAL(clicked(bool)),
51 this, SLOT(setItemSelected(bool)));
52
53 m_view->viewport()->installEventFilter(this);
54 }
55
56 SelectionManager::~SelectionManager()
57 {
58 }
59
60 bool SelectionManager::eventFilter(QObject* watched, QEvent* event)
61 {
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()));
69 }
70 return QObject::eventFilter(watched, event);
71 }
72
73 void SelectionManager::reset()
74 {
75 m_toggle->reset();
76 }
77
78 void SelectionManager::slotEntered(const QModelIndex& index)
79 {
80 m_toggle->hide();
81 const bool showToggle = index.isValid() &&
82 (index.column() == DolphinModel::Name) &&
83 (QApplication::mouseButtons() == Qt::NoButton);
84 if (showToggle) {
85 m_toggle->setUrl(urlForIndex(index));
86
87 if (!m_connected) {
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&)),
92 this,
93 SLOT(slotSelectionChanged(const QItemSelection&, const QItemSelection&)));
94 m_connected = true;
95 }
96
97 // Increase the size of the toggle for large items
98 const int iconHeight = m_view->iconSize().height();
99
100 int toggleSize = KIconLoader::SizeSmall;
101 if (iconHeight >= KIconLoader::SizeEnormous) {
102 toggleSize = KIconLoader::SizeMedium;
103 } else if (iconHeight >= KIconLoader::SizeLarge) {
104 toggleSize = KIconLoader::SizeSmallMedium;
105 }
106
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;
111 if (margin > 4) {
112 margin = 0;
113 }
114 toggleSize += 2 * margin;
115 m_toggle->setMargin(margin);
116 m_toggle->resize(toggleSize, toggleSize);
117 m_toggle->move(rect.topLeft());
118
119 QItemSelectionModel* selModel = m_view->selectionModel();
120 m_toggle->setChecked(selModel->isSelected(index));
121 m_toggle->show();
122 } else {
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&)),
128 this,
129 SLOT(slotSelectionChanged(const QItemSelection&, const QItemSelection&)));
130 m_connected = false;
131 }
132 }
133
134 void SelectionManager::slotViewportEntered()
135 {
136 m_toggle->hide();
137 }
138
139 void SelectionManager::setItemSelected(bool selected)
140 {
141 emit selectionChanged();
142
143 if (!m_toggle->url().isEmpty()) {
144 const QModelIndex index = indexForUrl(m_toggle->url());
145 if (index.isValid()) {
146 QItemSelectionModel* selModel = m_view->selectionModel();
147 if (selected) {
148 selModel->select(index, QItemSelectionModel::Select);
149 } else {
150 selModel->select(index, QItemSelectionModel::Deselect);
151 }
152 selModel->setCurrentIndex(index, QItemSelectionModel::Current);
153 }
154 }
155 }
156
157 void SelectionManager::slotRowsRemoved(const QModelIndex& parent, int start, int end)
158 {
159 Q_UNUSED(parent);
160 Q_UNUSED(start);
161 Q_UNUSED(end);
162 m_toggle->hide();
163 }
164
165 void SelectionManager::slotSelectionChanged(const QItemSelection& selected,
166 const QItemSelection& deselected)
167 {
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);
176 }
177
178 if (deselected.contains(index)) {
179 m_toggle->setChecked(false);
180 }
181 }
182 }
183 }
184
185 KUrl SelectionManager::urlForIndex(const QModelIndex& index) const
186 {
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();
191 }
192
193 const QModelIndex SelectionManager::indexForUrl(const KUrl& url) const
194 {
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);
199 }
200
201 #include "selectionmanager.moc"