]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphincontroller.cpp
fix crash when opening a lot of columns and going back very fast by clicking on each...
[dolphin.git] / src / dolphincontroller.cpp
1 /***************************************************************************
2 * Copyright (C) 2006 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 "dolphincontroller.h"
21
22 #include <kdirmodel.h>
23 #include <QAbstractProxyModel>
24 #include <QApplication>
25 #include <QClipboard>
26 #include <QDir>
27
28 DolphinController::DolphinController(DolphinView* dolphinView) :
29 QObject(dolphinView),
30 m_zoomInPossible(false),
31 m_zoomOutPossible(false),
32 m_openTab(false),
33 m_url(),
34 m_dolphinView(dolphinView),
35 m_itemView(0)
36 {
37 }
38
39 DolphinController::~DolphinController()
40 {
41 }
42
43 void DolphinController::setUrl(const KUrl& url)
44 {
45 if (m_url != url) {
46 m_url = url;
47 emit urlChanged(url);
48 }
49 }
50
51 void DolphinController::setItemView(QAbstractItemView* view)
52 {
53 if (m_itemView != 0) {
54 disconnect(m_itemView, SIGNAL(pressed(const QModelIndex&)),
55 this, SLOT(updateOpenTabState()));
56 }
57
58 m_itemView = view;
59
60 if (m_itemView != 0) {
61 // TODO: this is a workaround until Qt-issue 176832 has been fixed
62 connect(m_itemView, SIGNAL(pressed(const QModelIndex&)),
63 this, SLOT(updateOpenTabState()));
64 }
65 }
66
67 void DolphinController::triggerUrlChangeRequest(const KUrl& url)
68 {
69 if (m_url != url) {
70 emit requestUrlChange(url);
71 }
72 }
73
74 void DolphinController::triggerContextMenuRequest(const QPoint& pos)
75 {
76 emit activated();
77 emit requestContextMenu(pos);
78 }
79
80 void DolphinController::requestActivation()
81 {
82 emit activated();
83 }
84
85 void DolphinController::indicateDroppedUrls(const KUrl::List& urls,
86 const KUrl& destPath,
87 const KFileItem& destItem)
88 {
89 emit urlsDropped(urls, destPath, destItem);
90 }
91
92
93 void DolphinController::indicateSortingChange(DolphinView::Sorting sorting)
94 {
95 emit sortingChanged(sorting);
96 }
97
98 void DolphinController::indicateSortOrderChange(Qt::SortOrder order)
99 {
100 emit sortOrderChanged(order);
101 }
102
103 void DolphinController::indicateAdditionalInfoChange(const KFileItemDelegate::InformationList& info)
104 {
105 emit additionalInfoChanged(info);
106 }
107
108 void DolphinController::indicateActivationChange(bool active)
109 {
110 emit activationChanged(active);
111 }
112
113 void DolphinController::triggerZoomIn()
114 {
115 emit zoomIn();
116 }
117
118 void DolphinController::triggerZoomOut()
119 {
120 emit zoomOut();
121 }
122
123 void DolphinController::handleKeyPressEvent(QKeyEvent* event)
124 {
125 Q_ASSERT(m_itemView != 0);
126
127 const QItemSelectionModel* selModel = m_itemView->selectionModel();
128 const QModelIndex currentIndex = selModel->currentIndex();
129 const bool trigger = currentIndex.isValid()
130 && (event->key() == Qt::Key_Return)
131 && (selModel->selectedIndexes().count() > 0);
132 if (trigger) {
133 const QModelIndexList indexList = selModel->selectedIndexes();
134 foreach (const QModelIndex& index, indexList) {
135 triggerItem(index);
136 }
137 }
138 }
139
140 void DolphinController::replaceUrlByClipboard()
141 {
142 const QClipboard* clipboard = QApplication::clipboard();
143 QString text;
144 if (clipboard->mimeData(QClipboard::Selection)->hasText()) {
145 text = clipboard->mimeData(QClipboard::Selection)->text();
146 } else if (clipboard->mimeData(QClipboard::Clipboard)->hasText()) {
147 text = clipboard->mimeData(QClipboard::Clipboard)->text();
148 }
149 if (!text.isEmpty() && QDir::isAbsolutePath(text)) {
150 m_dolphinView->setUrl(KUrl(text));
151 }
152 }
153
154 KFileItem DolphinController::itemForIndex(const QModelIndex& index) const
155 {
156 Q_ASSERT(m_itemView != 0);
157
158 QAbstractProxyModel* proxyModel = static_cast<QAbstractProxyModel*>(m_itemView->model());
159 KDirModel* dirModel = static_cast<KDirModel*>(proxyModel->sourceModel());
160 const QModelIndex dirIndex = proxyModel->mapToSource(index);
161 return dirModel->itemForIndex(dirIndex);
162 }
163
164 void DolphinController::triggerItem(const QModelIndex& index)
165 {
166 const bool openTab = m_openTab;
167 m_openTab = false;
168
169 const KFileItem item = itemForIndex(index);
170 if (index.isValid() && (index.column() == KDirModel::Name)) {
171 if (openTab && (item.isDir() || m_dolphinView->isTabsForFilesEnabled())) {
172 emit tabRequested(item.url());
173 } else {
174 emit itemTriggered(item);
175 }
176 } else {
177 m_itemView->clearSelection();
178 if (!openTab) {
179 emit itemEntered(KFileItem());
180 }
181 }
182 }
183
184 void DolphinController::emitItemEntered(const QModelIndex& index)
185 {
186 KFileItem item = itemForIndex(index);
187 if (!item.isNull()) {
188 emit itemEntered(item);
189 }
190 }
191
192 void DolphinController::emitViewportEntered()
193 {
194 emit viewportEntered();
195 }
196
197 void DolphinController::updateOpenTabState()
198 {
199 m_openTab = QApplication::mouseButtons() & Qt::MidButton;
200 }
201
202 #include "dolphincontroller.moc"