]> cloud.milkyroute.net Git - dolphin.git/blob - src/selectionmanager.cpp
Select copied/moved items automatically if no item is already selected. This gives...
[dolphin.git] / src / 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
54 SelectionManager::~SelectionManager()
55 {
56 }
57
58 void SelectionManager::reset()
59 {
60 m_toggle->reset();
61 }
62
63 void SelectionManager::slotEntered(const QModelIndex& index)
64 {
65 m_toggle->hide();
66 const bool showToggle = index.isValid() &&
67 (index.column() == DolphinModel::Name) &&
68 (QApplication::mouseButtons() == Qt::NoButton);
69 if (showToggle) {
70 m_toggle->setUrl(urlForIndex(index));
71
72 if (!m_connected) {
73 connect(m_view->model(), SIGNAL(rowsRemoved(const QModelIndex&, int, int)),
74 this, SLOT(slotRowsRemoved(const QModelIndex&, int, int)));
75 connect(m_view->selectionModel(),
76 SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
77 this,
78 SLOT(slotSelectionChanged(const QItemSelection&, const QItemSelection&)));
79 m_connected = true;
80 }
81
82 const QRect rect = m_view->visualRect(index);
83 m_toggle->move(QPoint(rect.left(), rect.top()));
84
85 // increase the size of the toggle for large items
86 const int height = m_view->iconSize().height();
87 if (height >= KIconLoader::SizeEnormous) {
88 m_toggle->resize(KIconLoader::SizeMedium, KIconLoader::SizeMedium);
89 } else if (height >= KIconLoader::SizeLarge) {
90 m_toggle->resize(KIconLoader::SizeSmallMedium, KIconLoader::SizeSmallMedium);
91 } else {
92 m_toggle->resize(KIconLoader::SizeSmall, KIconLoader::SizeSmall);
93 }
94
95 QItemSelectionModel* selModel = m_view->selectionModel();
96 m_toggle->setChecked(selModel->isSelected(index));
97 m_toggle->show();
98 } else {
99 m_toggle->setUrl(KUrl());
100 disconnect(m_view->model(), SIGNAL(rowsRemoved(const QModelIndex&, int, int)),
101 this, SLOT(slotRowsRemoved(const QModelIndex&, int, int)));
102 disconnect(m_view->selectionModel(),
103 SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
104 this,
105 SLOT(slotSelectionChanged(const QItemSelection&, const QItemSelection&)));
106 m_connected = false;
107 }
108 }
109
110 void SelectionManager::slotViewportEntered()
111 {
112 m_toggle->hide();
113 }
114
115 void SelectionManager::setItemSelected(bool selected)
116 {
117 emit selectionChanged();
118
119 if (!m_toggle->url().isEmpty()) {
120 const QModelIndex index = indexForUrl(m_toggle->url());
121 if (index.isValid()) {
122 QItemSelectionModel* selModel = m_view->selectionModel();
123 if (selected) {
124 selModel->select(index, QItemSelectionModel::Select);
125 } else {
126 selModel->select(index, QItemSelectionModel::Deselect);
127 }
128 selModel->setCurrentIndex(index, QItemSelectionModel::Current);
129 }
130 }
131 }
132
133 void SelectionManager::slotRowsRemoved(const QModelIndex& parent, int start, int end)
134 {
135 Q_UNUSED(parent);
136 Q_UNUSED(start);
137 Q_UNUSED(end);
138 m_toggle->hide();
139 }
140
141 void SelectionManager::slotSelectionChanged(const QItemSelection& selected,
142 const QItemSelection& deselected)
143 {
144 // The selection has been changed outside the scope of the selection manager
145 // (e. g. by the rubberband or the "Select All" action). Take care updating
146 // the state of the toggle button.
147 if (!m_toggle->url().isEmpty()) {
148 const QModelIndex index = indexForUrl(m_toggle->url());
149 if (index.isValid()) {
150 if (selected.contains(index)) {
151 m_toggle->setChecked(true);
152 }
153
154 if (deselected.contains(index)) {
155 m_toggle->setChecked(false);
156 }
157 }
158 }
159 }
160
161 KUrl SelectionManager::urlForIndex(const QModelIndex& index) const
162 {
163 QAbstractProxyModel* proxyModel = static_cast<QAbstractProxyModel*>(m_view->model());
164 KDirModel* dirModel = static_cast<KDirModel*>(proxyModel->sourceModel());
165 const QModelIndex dirIndex = proxyModel->mapToSource(index);
166 return dirModel->itemForIndex(dirIndex).url();
167 }
168
169 const QModelIndex SelectionManager::indexForUrl(const KUrl& url) const
170 {
171 QAbstractProxyModel* proxyModel = static_cast<QAbstractProxyModel*>(m_view->model());
172 KDirModel* dirModel = static_cast<KDirModel*>(proxyModel->sourceModel());
173 const QModelIndex dirIndex = dirModel->indexForUrl(url);
174 return proxyModel->mapFromSource(dirIndex);
175 }
176
177 #include "selectionmanager.moc"