1 /***************************************************************************
2 * Copyright (C) 2010 by Peter Penz <peter.penz@gmx.at> *
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. *
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. *
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 ***************************************************************************/
20 #include "dolphinviewcontroller.h"
21 #include "zoomlevelinfo.h"
23 #include <kdirmodel.h>
24 #include <QAbstractProxyModel>
25 #include <QApplication>
29 Qt::MouseButtons
DolphinViewController::m_mouseButtons
= Qt::NoButton
;
31 DolphinViewController::DolphinViewController(DolphinView
* dolphinView
) :
33 m_dolphinView(dolphinView
),
35 m_versionControlActions()
39 DolphinViewController::~DolphinViewController()
43 const DolphinView
* DolphinViewController::view() const
48 void DolphinViewController::requestUrlChange(const KUrl
& url
)
50 emit
urlChangeRequested(url
);
53 void DolphinViewController::setItemView(QAbstractItemView
* view
)
55 if (m_itemView
!= 0) {
56 disconnect(m_itemView
, SIGNAL(pressed(const QModelIndex
&)),
57 this, SLOT(updateMouseButtonState()));
62 if (m_itemView
!= 0) {
63 // TODO: this is a workaround until Qt-issue 176832 has been fixed
64 connect(m_itemView
, SIGNAL(pressed(const QModelIndex
&)),
65 this, SLOT(updateMouseButtonState()));
69 QAbstractItemView
* DolphinViewController::itemView() const
74 void DolphinViewController::triggerContextMenuRequest(const QPoint
& pos
,
75 const QList
<QAction
*>& customActions
)
78 emit
requestContextMenu(pos
, customActions
);
81 void DolphinViewController::requestActivation()
86 void DolphinViewController::indicateDroppedUrls(const KFileItem
& destItem
,
90 emit
urlsDropped(destItem
, destPath
, event
);
94 void DolphinViewController::indicateSortingChange(DolphinView::Sorting sorting
)
96 emit
sortingChanged(sorting
);
99 void DolphinViewController::indicateSortOrderChange(Qt::SortOrder order
)
101 emit
sortOrderChanged(order
);
104 void DolphinViewController::indicateSortFoldersFirstChange(bool foldersFirst
)
106 emit
sortFoldersFirstChanged(foldersFirst
);
109 void DolphinViewController::indicateAdditionalInfoChange(const KFileItemDelegate::InformationList
& info
)
111 emit
additionalInfoChanged(info
);
114 void DolphinViewController::setVersionControlActions(QList
<QAction
*> actions
)
116 m_versionControlActions
= actions
;
119 QList
<QAction
*> DolphinViewController::versionControlActions(const KFileItemList
& items
)
121 emit
requestVersionControlActions(items
);
122 // All view implementations are connected with the signal requestVersionControlActions()
123 // (see ViewExtensionFactory) and will invoke DolphinViewController::setVersionControlActions(),
124 // so that the context dependent actions can be returned.
125 return m_versionControlActions
;
128 void DolphinViewController::handleKeyPressEvent(QKeyEvent
* event
)
130 Q_ASSERT(m_itemView
!= 0);
132 const QItemSelectionModel
* selModel
= m_itemView
->selectionModel();
133 const QModelIndex currentIndex
= selModel
->currentIndex();
134 const bool trigger
= currentIndex
.isValid()
135 && ((event
->key() == Qt::Key_Return
) || (event
->key() == Qt::Key_Enter
))
136 && !selModel
->selectedIndexes().isEmpty();
141 // Emit the signal itemTriggered() for all selected files.
142 // Several selected directories are opened in separate tabs,
143 // one selected directory will get opened in the view.
144 QModelIndexList dirQueue
;
145 const QModelIndexList indexList
= selModel
->selectedIndexes();
146 foreach (const QModelIndex
& index
, indexList
) {
147 if (itemForIndex(index
).isDir()) {
150 emit
itemTriggered(itemForIndex(index
));
154 if (dirQueue
.length() == 1) {
155 // open directory in the view
156 emit
itemTriggered(itemForIndex(dirQueue
[0]));
158 // open directories in separate tabs
159 foreach(const QModelIndex
& dir
, dirQueue
) {
160 emit
tabRequested(itemForIndex(dir
).url());
165 void DolphinViewController::replaceUrlByClipboard()
167 const QClipboard
* clipboard
= QApplication::clipboard();
169 if (clipboard
->mimeData(QClipboard::Selection
)->hasText()) {
170 text
= clipboard
->mimeData(QClipboard::Selection
)->text();
171 } else if (clipboard
->mimeData(QClipboard::Clipboard
)->hasText()) {
172 text
= clipboard
->mimeData(QClipboard::Clipboard
)->text();
174 if (!text
.isEmpty() && QDir::isAbsolutePath(text
)) {
175 m_dolphinView
->setUrl(KUrl(text
));
179 void DolphinViewController::requestToolTipHiding()
184 void DolphinViewController::emitItemTriggered(const KFileItem
& item
)
186 emit
itemTriggered(item
);
189 KFileItem
DolphinViewController::itemForIndex(const QModelIndex
& index
) const
191 Q_ASSERT(m_itemView
!= 0);
193 QAbstractProxyModel
* proxyModel
= static_cast<QAbstractProxyModel
*>(m_itemView
->model());
194 KDirModel
* dirModel
= static_cast<KDirModel
*>(proxyModel
->sourceModel());
195 const QModelIndex dirIndex
= proxyModel
->mapToSource(index
);
196 return dirModel
->itemForIndex(dirIndex
);
199 void DolphinViewController::triggerItem(const QModelIndex
& index
)
201 if (m_mouseButtons
& Qt::LeftButton
) {
202 const KFileItem item
= itemForIndex(index
);
203 if (index
.isValid() && (index
.column() == KDirModel::Name
)) {
204 emit
itemTriggered(item
);
206 m_itemView
->clearSelection();
207 emit
itemEntered(KFileItem());
212 void DolphinViewController::requestTab(const QModelIndex
& index
)
214 if (m_mouseButtons
& Qt::MidButton
) {
215 const KFileItem item
= itemForIndex(index
);
216 const bool validRequest
= index
.isValid() &&
217 (index
.column() == KDirModel::Name
) &&
218 (item
.isDir() || m_dolphinView
->isTabsForFilesEnabled());
220 emit
tabRequested(item
.url());
225 void DolphinViewController::emitItemEntered(const QModelIndex
& index
)
227 KFileItem item
= itemForIndex(index
);
228 if (!item
.isNull()) {
229 emit
itemEntered(item
);
233 void DolphinViewController::emitViewportEntered()
235 emit
viewportEntered();
238 void DolphinViewController::updateMouseButtonState()
240 m_mouseButtons
= QApplication::mouseButtons();
243 #include "dolphinviewcontroller.moc"