]>
cloud.milkyroute.net Git - dolphin.git/blob - src/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 <QModelIndex>
32 #include <QPaintEvent>
36 SelectionManager::SelectionManager(QAbstractItemView
* parent
) :
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);
48 connect(m_toggle
, SIGNAL(clicked(bool)),
49 this, SLOT(setItemSelected(bool)));
52 SelectionManager::~SelectionManager()
56 void SelectionManager::reset()
61 void SelectionManager::slotEntered(const QModelIndex
& index
)
64 if (index
.isValid() && (index
.column() == DolphinModel::Name
)) {
65 m_toggle
->setUrl(urlForIndex(index
));
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
&)),
72 SLOT(slotSelectionChanged(const QItemSelection
&, const QItemSelection
&)));
74 const QRect rect
= m_view
->visualRect(index
);
77 const int x
= rect
.left() + gap
;
78 const int y
= rect
.top() + gap
;
79 m_toggle
->move(QPoint(x
, y
));
81 QItemSelectionModel
* selModel
= m_view
->selectionModel();
82 m_toggle
->setChecked(selModel
->isSelected(index
));
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
&)),
91 SLOT(slotSelectionChanged(const QItemSelection
&, const QItemSelection
&)));
95 void SelectionManager::slotViewportEntered()
100 void SelectionManager::setItemSelected(bool selected
)
102 emit
selectionChanged();
104 const QModelIndex index
= indexForUrl(m_toggle
->url());
105 if (index
.isValid()) {
106 QItemSelectionModel
* selModel
= m_view
->selectionModel();
108 selModel
->select(index
, QItemSelectionModel::Select
);
110 selModel
->select(index
, QItemSelectionModel::Deselect
);
115 void SelectionManager::slotRowsRemoved(const QModelIndex
& parent
, int start
, int end
)
123 void SelectionManager::slotSelectionChanged(const QItemSelection
& selected
,
124 const QItemSelection
& deselected
)
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);
135 if (deselected
.contains(index
)) {
136 m_toggle
->setChecked(false);
141 KUrl
SelectionManager::urlForIndex(const QModelIndex
& index
) const
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();
149 const QModelIndex
SelectionManager::indexForUrl(const KUrl
& url
) const
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
);
157 #include "selectionmanager.moc"