]> cloud.milkyroute.net Git - dolphin.git/blob - src/selectionmanager.cpp
reset the selection manager when the URL has been changed (otherwise the selection...
[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
35 SelectionManager::SelectionManager(QAbstractItemView* parent) :
36 QObject(parent),
37 m_view(parent),
38 m_button(0),
39 m_item()
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_button = new SelectionToggle(m_view->viewport());
46 m_button->setCheckable(true);
47 m_button->hide();
48 connect(m_button, SIGNAL(clicked(bool)),
49 this, SLOT(setItemSelected(bool)));
50 }
51
52 SelectionManager::~SelectionManager()
53 {
54 }
55
56 void SelectionManager::reset()
57 {
58 m_button->hide();
59 m_item = KFileItem();
60 }
61
62 void SelectionManager::slotEntered(const QModelIndex& index)
63 {
64 m_button->hide();
65 if (index.isValid() && (index.column() == DolphinModel::Name)) {
66 m_item = itemForIndex(index);
67
68 const QRect rect = m_view->visualRect(index);
69 const int gap = 2;
70 const int x = rect.right() - m_button->width() - gap;
71 int y = rect.top();
72 if (rect.height() <= m_button->height() * 2) {
73 // center the button vertically
74 y += (rect.height() - m_button->height()) / 2;
75 } else {
76 y += gap;
77 }
78
79 m_button->move(QPoint(x, y));
80
81 QItemSelectionModel* selModel = m_view->selectionModel();
82 m_button->setChecked(selModel->isSelected(index));
83 m_button->show();
84 } else {
85 m_item = KFileItem();
86 }
87 }
88
89 void SelectionManager::slotViewportEntered()
90 {
91 m_button->hide();
92 m_item = KFileItem();
93 }
94
95 void SelectionManager::setItemSelected(bool selected)
96 {
97 emit selectionChanged();
98 Q_ASSERT(!m_item.isNull());
99
100 const QModelIndex index = indexForItem(m_item);
101 if (index.isValid()) {
102 QItemSelectionModel* selModel = m_view->selectionModel();
103 if (selected) {
104 selModel->select(index, QItemSelectionModel::Select);
105 } else {
106 selModel->select(index, QItemSelectionModel::Deselect);
107 }
108 }
109 }
110
111 KFileItem SelectionManager::itemForIndex(const QModelIndex& index) const
112 {
113 QAbstractProxyModel* proxyModel = static_cast<QAbstractProxyModel*>(m_view->model());
114 KDirModel* dirModel = static_cast<KDirModel*>(proxyModel->sourceModel());
115 const QModelIndex dirIndex = proxyModel->mapToSource(index);
116 return dirModel->itemForIndex(dirIndex);
117 }
118
119 const QModelIndex SelectionManager::indexForItem(const KFileItem& item) const
120 {
121 QAbstractProxyModel* proxyModel = static_cast<QAbstractProxyModel*>(m_view->model());
122 KDirModel* dirModel = static_cast<KDirModel*>(proxyModel->sourceModel());
123 const QModelIndex dirIndex = dirModel->indexForItem(item);
124 return proxyModel->mapFromSource(dirIndex);
125 }
126
127 #include "selectionmanager.moc"