]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphincontroller.cpp
Improve mouse middle click path paste behavior: first check selection clipboard,...
[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 // TODO: this is a workaround until Qt-issue 176832 has been fixed
61 connect(m_itemView, SIGNAL(pressed(const QModelIndex&)),
62 this, SLOT(updateOpenTabState()));
63 }
64
65 void DolphinController::triggerUrlChangeRequest(const KUrl& url)
66 {
67 if (m_url != url) {
68 emit requestUrlChange(url);
69 }
70 }
71
72 void DolphinController::triggerContextMenuRequest(const QPoint& pos)
73 {
74 emit activated();
75 emit requestContextMenu(pos);
76 }
77
78 void DolphinController::requestActivation()
79 {
80 emit activated();
81 }
82
83 void DolphinController::indicateDroppedUrls(const KUrl::List& urls,
84 const KUrl& destPath,
85 const KFileItem& destItem)
86 {
87 emit urlsDropped(urls, destPath, destItem);
88 }
89
90
91 void DolphinController::indicateSortingChange(DolphinView::Sorting sorting)
92 {
93 emit sortingChanged(sorting);
94 }
95
96 void DolphinController::indicateSortOrderChange(Qt::SortOrder order)
97 {
98 emit sortOrderChanged(order);
99 }
100
101 void DolphinController::indicateAdditionalInfoChange(const KFileItemDelegate::InformationList& info)
102 {
103 emit additionalInfoChanged(info);
104 }
105
106 void DolphinController::indicateActivationChange(bool active)
107 {
108 emit activationChanged(active);
109 }
110
111 void DolphinController::triggerZoomIn()
112 {
113 emit zoomIn();
114 }
115
116 void DolphinController::triggerZoomOut()
117 {
118 emit zoomOut();
119 }
120
121 void DolphinController::handleKeyPressEvent(QKeyEvent* event)
122 {
123 Q_ASSERT(m_itemView != 0);
124
125 const QItemSelectionModel* selModel = m_itemView->selectionModel();
126 const QModelIndex currentIndex = selModel->currentIndex();
127 const bool trigger = currentIndex.isValid()
128 && (event->key() == Qt::Key_Return)
129 && (selModel->selectedIndexes().count() > 0);
130 if (trigger) {
131 const QModelIndexList indexList = selModel->selectedIndexes();
132 foreach (const QModelIndex& index, indexList) {
133 triggerItem(index);
134 }
135 }
136 }
137
138 void DolphinController::replaceUrlByClipboard()
139 {
140 const QClipboard* clipboard = QApplication::clipboard();
141 QString text;
142 if (clipboard->mimeData(QClipboard::Selection)->hasText()) {
143 text = clipboard->mimeData(QClipboard::Selection)->text();
144 } else if (clipboard->mimeData(QClipboard::Clipboard)->hasText()) {
145 text = clipboard->mimeData(QClipboard::Clipboard)->text();
146 }
147 if (!text.isEmpty() && QDir::isAbsolutePath(text)) {
148 m_dolphinView->setUrl(KUrl(text));
149 }
150 }
151
152 KFileItem DolphinController::itemForIndex(const QModelIndex& index) const
153 {
154 Q_ASSERT(m_itemView != 0);
155
156 QAbstractProxyModel* proxyModel = static_cast<QAbstractProxyModel*>(m_itemView->model());
157 KDirModel* dirModel = static_cast<KDirModel*>(proxyModel->sourceModel());
158 const QModelIndex dirIndex = proxyModel->mapToSource(index);
159 return dirModel->itemForIndex(dirIndex);
160 }
161
162 void DolphinController::triggerItem(const QModelIndex& index)
163 {
164 const bool openTab = m_openTab;
165 m_openTab = false;
166
167 const KFileItem item = itemForIndex(index);
168 if (index.isValid() && (index.column() == KDirModel::Name)) {
169 if (openTab && (item.isDir() || m_dolphinView->isTabsForFilesEnabled())) {
170 emit tabRequested(item.url());
171 } else {
172 emit itemTriggered(item);
173 }
174 } else {
175 m_itemView->clearSelection();
176 if (!openTab) {
177 emit itemEntered(KFileItem());
178 }
179 }
180 }
181
182 void DolphinController::emitItemEntered(const QModelIndex& index)
183 {
184 KFileItem item = itemForIndex(index);
185 if (!item.isNull()) {
186 emit itemEntered(item);
187 }
188 }
189
190 void DolphinController::emitViewportEntered()
191 {
192 emit viewportEntered();
193 }
194
195 void DolphinController::updateOpenTabState()
196 {
197 m_openTab = QApplication::mouseButtons() & Qt::MidButton;
198 }
199
200 #include "dolphincontroller.moc"