]> cloud.milkyroute.net Git - dolphin.git/blob - src/selectionmanager.cpp
There are some extractable strings in subdirs too.
[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 <QModelIndex>
31 #include <QPainter>
32 #include <QPaintEvent>
33 #include <QRect>
34 #include <QTimeLine>
35
36 SelectionManager::SelectionManager(QAbstractItemView* parent) :
37 QObject(parent),
38 m_view(parent),
39 m_toggle(0)
40 {
41 connect(parent, SIGNAL(entered(const QModelIndex&)),
42 this, SLOT(slotEntered(const QModelIndex&)));
43 connect(parent, SIGNAL(viewportEntered()),
44 this, SLOT(slotViewportEntered()));
45 m_toggle = new SelectionToggle(m_view->viewport());
46 m_toggle->setCheckable(true);
47 m_toggle->hide();
48 connect(m_toggle, SIGNAL(clicked(bool)),
49 this, SLOT(setItemSelected(bool)));
50 }
51
52 SelectionManager::~SelectionManager()
53 {
54 }
55
56 void SelectionManager::reset()
57 {
58 m_toggle->reset();
59 }
60
61 void SelectionManager::slotEntered(const QModelIndex& index)
62 {
63 m_toggle->hide();
64 if (index.isValid() && (index.column() == DolphinModel::Name)) {
65 m_toggle->setUrl(urlForIndex(index));
66
67 connect(m_view->model(), SIGNAL(rowsRemoved(const QModelIndex&, int, int)),
68 this, SLOT(slotRowsRemoved(const QModelIndex&, int, int)));
69 connect(m_view->selectionModel(),
70 SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
71 this,
72 SLOT(slotSelectionChanged(const QItemSelection&, const QItemSelection&)));
73
74 const QRect rect = m_view->visualRect(index);
75
76 const int gap = 2;
77 const int x = rect.left() + gap;
78 const int y = rect.top() + gap;
79 m_toggle->move(QPoint(x, y));
80
81 QItemSelectionModel* selModel = m_view->selectionModel();
82 m_toggle->setChecked(selModel->isSelected(index));
83 m_toggle->show();
84 } else {
85 m_toggle->setUrl(KUrl());
86 disconnect(m_view->model(), SIGNAL(rowsRemoved(const QModelIndex&, int, int)),
87 this, SLOT(slotRowsRemoved(const QModelIndex&, int, int)));
88 disconnect(m_view->selectionModel(),
89 SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
90 this,
91 SLOT(slotSelectionChanged(const QItemSelection&, const QItemSelection&)));
92 }
93 }
94
95 void SelectionManager::slotViewportEntered()
96 {
97 m_toggle->hide();
98 }
99
100 void SelectionManager::setItemSelected(bool selected)
101 {
102 emit selectionChanged();
103
104 const QModelIndex index = indexForUrl(m_toggle->url());
105 if (index.isValid()) {
106 QItemSelectionModel* selModel = m_view->selectionModel();
107 if (selected) {
108 selModel->select(index, QItemSelectionModel::Select);
109 } else {
110 selModel->select(index, QItemSelectionModel::Deselect);
111 }
112 }
113 }
114
115 void SelectionManager::slotRowsRemoved(const QModelIndex& parent, int start, int end)
116 {
117 Q_UNUSED(parent);
118 Q_UNUSED(start);
119 Q_UNUSED(end);
120 m_toggle->hide();
121 }
122
123 void SelectionManager::slotSelectionChanged(const QItemSelection& selected,
124 const QItemSelection& deselected)
125 {
126 // The selection has been changed outside the scope of the selection manager
127 // (e. g. by the rubberband or the "Select All" action). Take care updating
128 // the state of the toggle button.
129 const QModelIndex index = indexForUrl(m_toggle->url());
130 if (index.isValid()) {
131 if (selected.contains(index)) {
132 m_toggle->setChecked(true);
133 }
134
135 if (deselected.contains(index)) {
136 m_toggle->setChecked(false);
137 }
138 }
139 }
140
141 KUrl SelectionManager::urlForIndex(const QModelIndex& index) const
142 {
143 QAbstractProxyModel* proxyModel = static_cast<QAbstractProxyModel*>(m_view->model());
144 KDirModel* dirModel = static_cast<KDirModel*>(proxyModel->sourceModel());
145 const QModelIndex dirIndex = proxyModel->mapToSource(index);
146 return dirModel->itemForIndex(dirIndex).url();
147 }
148
149 const QModelIndex SelectionManager::indexForUrl(const KUrl& url) const
150 {
151 QAbstractProxyModel* proxyModel = static_cast<QAbstractProxyModel*>(m_view->model());
152 KDirModel* dirModel = static_cast<KDirModel*>(proxyModel->sourceModel());
153 const QModelIndex dirIndex = dirModel->indexForUrl(url);
154 return proxyModel->mapFromSource(dirIndex);
155 }
156
157 #include "selectionmanager.moc"