add more spacing
[personal-kdebase.git] / apps / dolphin / src / selectionmanager.cpp
blob1722bc3c5d20f7ba39a0f0899187686a29dce7fd
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 ***************************************************************************/
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 <QModelIndex>
31 #include <QPainter>
32 #include <QPaintEvent>
33 #include <QRect>
34 #include <QTimeLine>
36 SelectionManager::SelectionManager(QAbstractItemView* parent) :
37 QObject(parent),
38 m_view(parent),
39 m_toggle(0),
40 m_connected(false)
42 connect(parent, SIGNAL(entered(const QModelIndex&)),
43 this, SLOT(slotEntered(const QModelIndex&)));
44 connect(parent, SIGNAL(viewportEntered()),
45 this, SLOT(slotViewportEntered()));
46 m_toggle = new SelectionToggle(m_view->viewport());
47 m_toggle->setCheckable(true);
48 m_toggle->hide();
49 connect(m_toggle, SIGNAL(clicked(bool)),
50 this, SLOT(setItemSelected(bool)));
53 SelectionManager::~SelectionManager()
57 void SelectionManager::reset()
59 m_toggle->reset();
62 void SelectionManager::slotEntered(const QModelIndex& index)
64 m_toggle->hide();
65 if (index.isValid() && (index.column() == DolphinModel::Name)) {
66 m_toggle->setUrl(urlForIndex(index));
68 if (!m_connected) {
69 connect(m_view->model(), SIGNAL(rowsRemoved(const QModelIndex&, int, int)),
70 this, SLOT(slotRowsRemoved(const QModelIndex&, int, int)));
71 connect(m_view->selectionModel(),
72 SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
73 this,
74 SLOT(slotSelectionChanged(const QItemSelection&, const QItemSelection&)));
75 m_connected = true;
78 const QRect rect = m_view->visualRect(index);
80 const int gap = 2;
81 const int x = rect.left() + gap;
82 const int y = rect.top() + gap;
83 m_toggle->move(QPoint(x, y));
85 QItemSelectionModel* selModel = m_view->selectionModel();
86 m_toggle->setChecked(selModel->isSelected(index));
87 m_toggle->show();
88 } else {
89 m_toggle->setUrl(KUrl());
90 disconnect(m_view->model(), SIGNAL(rowsRemoved(const QModelIndex&, int, int)),
91 this, SLOT(slotRowsRemoved(const QModelIndex&, int, int)));
92 disconnect(m_view->selectionModel(),
93 SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
94 this,
95 SLOT(slotSelectionChanged(const QItemSelection&, const QItemSelection&)));
96 m_connected = false;
100 void SelectionManager::slotViewportEntered()
102 m_toggle->hide();
105 void SelectionManager::setItemSelected(bool selected)
107 emit selectionChanged();
109 if (!m_toggle->url().isEmpty()) {
110 const QModelIndex index = indexForUrl(m_toggle->url());
111 if (index.isValid()) {
112 QItemSelectionModel* selModel = m_view->selectionModel();
113 if (selected) {
114 selModel->select(index, QItemSelectionModel::Select);
115 } else {
116 selModel->select(index, QItemSelectionModel::Deselect);
118 selModel->setCurrentIndex(index, QItemSelectionModel::Current);
123 void SelectionManager::slotRowsRemoved(const QModelIndex& parent, int start, int end)
125 Q_UNUSED(parent);
126 Q_UNUSED(start);
127 Q_UNUSED(end);
128 m_toggle->hide();
131 void SelectionManager::slotSelectionChanged(const QItemSelection& selected,
132 const QItemSelection& deselected)
134 // The selection has been changed outside the scope of the selection manager
135 // (e. g. by the rubberband or the "Select All" action). Take care updating
136 // the state of the toggle button.
137 if (!m_toggle->url().isEmpty()) {
138 const QModelIndex index = indexForUrl(m_toggle->url());
139 if (index.isValid()) {
140 if (selected.contains(index)) {
141 m_toggle->setChecked(true);
144 if (deselected.contains(index)) {
145 m_toggle->setChecked(false);
151 KUrl SelectionManager::urlForIndex(const QModelIndex& index) const
153 QAbstractProxyModel* proxyModel = static_cast<QAbstractProxyModel*>(m_view->model());
154 KDirModel* dirModel = static_cast<KDirModel*>(proxyModel->sourceModel());
155 const QModelIndex dirIndex = proxyModel->mapToSource(index);
156 return dirModel->itemForIndex(dirIndex).url();
159 const QModelIndex SelectionManager::indexForUrl(const KUrl& url) const
161 QAbstractProxyModel* proxyModel = static_cast<QAbstractProxyModel*>(m_view->model());
162 KDirModel* dirModel = static_cast<KDirModel*>(proxyModel->sourceModel());
163 const QModelIndex dirIndex = dirModel->indexForUrl(url);
164 return proxyModel->mapFromSource(dirIndex);
167 #include "selectionmanager.moc"