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 <kfileitemactions.h>
25 #include <QAbstractProxyModel>
26 #include <QApplication>
30 Qt::MouseButtons
DolphinViewController::m_mouseButtons
= Qt::NoButton
;
32 DolphinViewController::DolphinViewController(DolphinView
* dolphinView
) :
34 m_dolphinView(dolphinView
),
36 m_versionControlActions()
40 DolphinViewController::~DolphinViewController()
44 const DolphinView
* DolphinViewController::view() const
49 void DolphinViewController::requestUrlChange(const KUrl
& url
)
51 emit
urlChangeRequested(url
);
54 void DolphinViewController::setItemView(QAbstractItemView
* view
)
56 if (m_itemView
!= 0) {
57 disconnect(m_itemView
, SIGNAL(pressed(const QModelIndex
&)),
58 this, SLOT(updateMouseButtonState()));
63 if (m_itemView
!= 0) {
64 // TODO: this is a workaround until Qt-issue 176832 has been fixed
65 connect(m_itemView
, SIGNAL(pressed(const QModelIndex
&)),
66 this, SLOT(updateMouseButtonState()));
70 QAbstractItemView
* DolphinViewController::itemView() const
75 void DolphinViewController::triggerContextMenuRequest(const QPoint
& pos
,
76 const QList
<QAction
*>& customActions
)
79 emit
requestContextMenu(pos
, customActions
);
82 void DolphinViewController::requestActivation()
87 void DolphinViewController::indicateDroppedUrls(const KFileItem
& destItem
,
91 emit
urlsDropped(destItem
, destPath
, event
);
95 void DolphinViewController::indicateSortingChange(DolphinView::Sorting sorting
)
97 emit
sortingChanged(sorting
);
100 void DolphinViewController::indicateSortOrderChange(Qt::SortOrder order
)
102 emit
sortOrderChanged(order
);
105 void DolphinViewController::indicateSortFoldersFirstChange(bool foldersFirst
)
107 emit
sortFoldersFirstChanged(foldersFirst
);
110 void DolphinViewController::indicateAdditionalInfoChange(const KFileItemDelegate::InformationList
& info
)
112 emit
additionalInfoChanged(info
);
115 void DolphinViewController::setVersionControlActions(QList
<QAction
*> actions
)
117 m_versionControlActions
= actions
;
120 QList
<QAction
*> DolphinViewController::versionControlActions(const KFileItemList
& items
)
122 emit
requestVersionControlActions(items
);
123 // All view implementations are connected with the signal requestVersionControlActions()
124 // (see ViewExtensionFactory) and will invoke DolphinViewController::setVersionControlActions(),
125 // so that the context dependent actions can be returned.
126 return m_versionControlActions
;
129 void DolphinViewController::handleKeyPressEvent(QKeyEvent
* event
)
131 if (m_itemView
== 0) {
135 const QItemSelectionModel
* selModel
= m_itemView
->selectionModel();
136 const QModelIndex currentIndex
= selModel
->currentIndex();
137 const bool trigger
= currentIndex
.isValid()
138 && ((event
->key() == Qt::Key_Return
) || (event
->key() == Qt::Key_Enter
))
139 && !selModel
->selectedIndexes().isEmpty();
144 // Collect the non-directory files into a list and
145 // call runPreferredApplications for that list.
146 // Several selected directories are opened in separate tabs,
147 // one selected directory will get opened in the view.
148 QModelIndexList dirQueue
;
149 const QModelIndexList indexList
= selModel
->selectedIndexes();
150 KFileItemList fileOpenList
;
151 foreach (const QModelIndex
& index
, indexList
) {
152 if (itemForIndex(index
).isDir()) {
155 fileOpenList
<< itemForIndex(index
);
159 KFileItemActions fileItemActions
;
160 fileItemActions
.runPreferredApplications(fileOpenList
, "DesktopEntryName != 'dolphin'");
162 if (dirQueue
.length() == 1) {
163 // open directory in the view
164 emit
itemTriggered(itemForIndex(dirQueue
[0]));
166 // open directories in separate tabs
167 foreach(const QModelIndex
& dir
, dirQueue
) {
168 emit
tabRequested(itemForIndex(dir
).url());
173 void DolphinViewController::replaceUrlByClipboard()
175 const QClipboard
* clipboard
= QApplication::clipboard();
177 if (clipboard
->mimeData(QClipboard::Selection
)->hasText()) {
178 text
= clipboard
->mimeData(QClipboard::Selection
)->text();
179 } else if (clipboard
->mimeData(QClipboard::Clipboard
)->hasText()) {
180 text
= clipboard
->mimeData(QClipboard::Clipboard
)->text();
182 if (!text
.isEmpty() && QDir::isAbsolutePath(text
)) {
183 m_dolphinView
->setUrl(KUrl(text
));
187 void DolphinViewController::requestToolTipHiding()
192 void DolphinViewController::emitItemTriggered(const KFileItem
& item
)
194 emit
itemTriggered(item
);
197 KFileItem
DolphinViewController::itemForIndex(const QModelIndex
& index
) const
199 if (m_itemView
!= 0) {
200 QAbstractProxyModel
* proxyModel
= static_cast<QAbstractProxyModel
*>(m_itemView
->model());
201 if (proxyModel
!= 0) {
202 KDirModel
* dirModel
= static_cast<KDirModel
*>(proxyModel
->sourceModel());
203 const QModelIndex dirIndex
= proxyModel
->mapToSource(index
);
204 return dirModel
->itemForIndex(dirIndex
);
211 void DolphinViewController::triggerItem(const QModelIndex
& index
)
213 if (m_mouseButtons
& Qt::LeftButton
) {
214 const KFileItem item
= itemForIndex(index
);
215 if (index
.isValid() && (index
.column() == KDirModel::Name
)) {
216 emit
itemTriggered(item
);
217 } else if (m_itemView
!= 0) {
218 m_itemView
->clearSelection();
219 emit
itemEntered(KFileItem());
224 void DolphinViewController::requestTab(const QModelIndex
& index
)
226 if (m_mouseButtons
& Qt::MidButton
) {
227 const KFileItem item
= itemForIndex(index
);
228 const bool validRequest
= index
.isValid() &&
229 (index
.column() == KDirModel::Name
) &&
230 (item
.isDir() || m_dolphinView
->isTabsForFilesEnabled());
232 emit
tabRequested(item
.url());
237 void DolphinViewController::emitItemEntered(const QModelIndex
& index
)
239 KFileItem item
= itemForIndex(index
);
240 if (!item
.isNull()) {
241 emit
itemEntered(item
);
245 void DolphinViewController::emitViewportEntered()
247 emit
viewportEntered();
250 void DolphinViewController::updateMouseButtonState()
252 m_mouseButtons
= QApplication::mouseButtons();
255 #include "dolphinviewcontroller.moc"