]> cloud.milkyroute.net Git - dolphin.git/blob - src/selectionmanager.cpp
take care to remove the selection toggle when items have been deleted
[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 connect(m_view->model(), SIGNAL(rowsRemoved(const QModelIndex&, int, int)),
69 this, SLOT(slotRowsRemoved(const QModelIndex&, int, int)));
70
71 const QRect rect = m_view->visualRect(index);
72 const int gap = 2;
73 const int x = rect.right() - m_button->width() - gap;
74 int y = rect.top();
75 if (rect.height() <= m_button->height() * 2) {
76 // center the button vertically
77 y += (rect.height() - m_button->height()) / 2;
78 } else {
79 y += gap;
80 }
81
82 m_button->move(QPoint(x, y));
83
84 QItemSelectionModel* selModel = m_view->selectionModel();
85 m_button->setChecked(selModel->isSelected(index));
86 m_button->show();
87 } else {
88 m_item = KFileItem();
89 disconnect(m_view->model(), SIGNAL(rowsRemoved(const QModelIndex&, int, int)),
90 this, SLOT(slotRowsRemoved(const QModelIndex&, int, int)));
91 }
92 }
93
94 void SelectionManager::slotViewportEntered()
95 {
96 m_button->hide();
97 }
98
99 void SelectionManager::setItemSelected(bool selected)
100 {
101 emit selectionChanged();
102 Q_ASSERT(!m_item.isNull());
103
104 const QModelIndex index = indexForItem(m_item);
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_button->hide();
121 }
122
123 KFileItem SelectionManager::itemForIndex(const QModelIndex& index) const
124 {
125 QAbstractProxyModel* proxyModel = static_cast<QAbstractProxyModel*>(m_view->model());
126 KDirModel* dirModel = static_cast<KDirModel*>(proxyModel->sourceModel());
127 const QModelIndex dirIndex = proxyModel->mapToSource(index);
128 return dirModel->itemForIndex(dirIndex);
129 }
130
131 const QModelIndex SelectionManager::indexForItem(const KFileItem& item) const
132 {
133 QAbstractProxyModel* proxyModel = static_cast<QAbstractProxyModel*>(m_view->model());
134 KDirModel* dirModel = static_cast<KDirModel*>(proxyModel->sourceModel());
135 const QModelIndex dirIndex = dirModel->indexForItem(item);
136 return proxyModel->mapFromSource(dirIndex);
137 }
138
139 #include "selectionmanager.moc"