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