]>
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)));
54 SelectionManager::~SelectionManager()
58 void SelectionManager::reset()
63 void SelectionManager::slotEntered(const QModelIndex
& index
)
66 const bool showToggle
= index
.isValid() &&
67 (index
.column() == DolphinModel::Name
) &&
68 (QApplication::mouseButtons() == Qt::NoButton
);
70 m_toggle
->setUrl(urlForIndex(index
));
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
&)),
78 SLOT(slotSelectionChanged(const QItemSelection
&, const QItemSelection
&)));
82 // increase the size of the toggle for large items
83 const int height
= m_view
->iconSize().height();
84 if (height
>= KIconLoader::SizeEnormous
) {
85 m_toggle
->resize(KIconLoader::SizeMedium
, KIconLoader::SizeMedium
);
86 } else if (height
>= KIconLoader::SizeLarge
) {
87 m_toggle
->resize(KIconLoader::SizeSmallMedium
, KIconLoader::SizeSmallMedium
);
89 m_toggle
->resize(KIconLoader::SizeSmall
, KIconLoader::SizeSmall
);
92 const QRect rect
= m_view
->visualRect(index
);
95 if (height
< KIconLoader::SizeSmallMedium
) {
96 // The height is nearly equal to the smallest toggle height.
97 // Assure that the toggle is vertically centered instead
98 // of aligned on the top and gets more horizontal gap.
100 y
+= (rect
.height() - m_toggle
->height()) / 2;
102 m_toggle
->move(QPoint(x
, y
));
104 QItemSelectionModel
* selModel
= m_view
->selectionModel();
105 m_toggle
->setChecked(selModel
->isSelected(index
));
108 m_toggle
->setUrl(KUrl());
109 disconnect(m_view
->model(), SIGNAL(rowsRemoved(const QModelIndex
&, int, int)),
110 this, SLOT(slotRowsRemoved(const QModelIndex
&, int, int)));
111 disconnect(m_view
->selectionModel(),
112 SIGNAL(selectionChanged(const QItemSelection
&, const QItemSelection
&)),
114 SLOT(slotSelectionChanged(const QItemSelection
&, const QItemSelection
&)));
119 void SelectionManager::slotViewportEntered()
124 void SelectionManager::setItemSelected(bool selected
)
126 emit
selectionChanged();
128 if (!m_toggle
->url().isEmpty()) {
129 const QModelIndex index
= indexForUrl(m_toggle
->url());
130 if (index
.isValid()) {
131 QItemSelectionModel
* selModel
= m_view
->selectionModel();
133 selModel
->select(index
, QItemSelectionModel::Select
);
135 selModel
->select(index
, QItemSelectionModel::Deselect
);
137 selModel
->setCurrentIndex(index
, QItemSelectionModel::Current
);
142 void SelectionManager::slotRowsRemoved(const QModelIndex
& parent
, int start
, int end
)
150 void SelectionManager::slotSelectionChanged(const QItemSelection
& selected
,
151 const QItemSelection
& deselected
)
153 // The selection has been changed outside the scope of the selection manager
154 // (e. g. by the rubberband or the "Select All" action). Take care updating
155 // the state of the toggle button.
156 if (!m_toggle
->url().isEmpty()) {
157 const QModelIndex index
= indexForUrl(m_toggle
->url());
158 if (index
.isValid()) {
159 if (selected
.contains(index
)) {
160 m_toggle
->setChecked(true);
163 if (deselected
.contains(index
)) {
164 m_toggle
->setChecked(false);
170 KUrl
SelectionManager::urlForIndex(const QModelIndex
& index
) const
172 QAbstractProxyModel
* proxyModel
= static_cast<QAbstractProxyModel
*>(m_view
->model());
173 KDirModel
* dirModel
= static_cast<KDirModel
*>(proxyModel
->sourceModel());
174 const QModelIndex dirIndex
= proxyModel
->mapToSource(index
);
175 return dirModel
->itemForIndex(dirIndex
).url();
178 const QModelIndex
SelectionManager::indexForUrl(const KUrl
& url
) const
180 QAbstractProxyModel
* proxyModel
= static_cast<QAbstractProxyModel
*>(m_view
->model());
181 KDirModel
* dirModel
= static_cast<KDirModel
*>(proxyModel
->sourceModel());
182 const QModelIndex dirIndex
= dirModel
->indexForUrl(url
);
183 return proxyModel
->mapFromSource(dirIndex
);
186 #include "selectionmanager.moc"