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