]>
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
->setFileItem(itemForIndex(index
));
67 connect(m_view
->model(), SIGNAL(rowsRemoved(const QModelIndex
&, int, int)),
68 this, SLOT(slotRowsRemoved(const QModelIndex
&, int, int)));
70 const QRect rect
= m_view
->visualRect(index
);
73 const int x
= rect
.left() + gap
;
74 const int y
= rect
.top() + gap
;
75 m_toggle
->move(QPoint(x
, y
));
77 QItemSelectionModel
* selModel
= m_view
->selectionModel();
78 m_toggle
->setChecked(selModel
->isSelected(index
));
81 m_toggle
->setFileItem(KFileItem());
82 disconnect(m_view
->model(), SIGNAL(rowsRemoved(const QModelIndex
&, int, int)),
83 this, SLOT(slotRowsRemoved(const QModelIndex
&, int, int)));
87 void SelectionManager::slotViewportEntered()
92 void SelectionManager::setItemSelected(bool selected
)
94 emit
selectionChanged();
95 Q_ASSERT(!m_toggle
->fileItem().isNull());
97 const QModelIndex index
= indexForItem(m_toggle
->fileItem());
98 if (index
.isValid()) {
99 QItemSelectionModel
* selModel
= m_view
->selectionModel();
101 selModel
->select(index
, QItemSelectionModel::Select
);
103 selModel
->select(index
, QItemSelectionModel::Deselect
);
108 void SelectionManager::slotRowsRemoved(const QModelIndex
& parent
, int start
, int end
)
116 KFileItem
SelectionManager::itemForIndex(const QModelIndex
& index
) const
118 QAbstractProxyModel
* proxyModel
= static_cast<QAbstractProxyModel
*>(m_view
->model());
119 KDirModel
* dirModel
= static_cast<KDirModel
*>(proxyModel
->sourceModel());
120 const QModelIndex dirIndex
= proxyModel
->mapToSource(index
);
121 return dirModel
->itemForIndex(dirIndex
);
124 const QModelIndex
SelectionManager::indexForItem(const KFileItem
& item
) const
126 QAbstractProxyModel
* proxyModel
= static_cast<QAbstractProxyModel
*>(m_view
->model());
127 KDirModel
* dirModel
= static_cast<KDirModel
*>(proxyModel
->sourceModel());
128 const QModelIndex dirIndex
= dirModel
->indexForItem(item
);
129 return proxyModel
->mapFromSource(dirIndex
);
132 #include "selectionmanager.moc"