1 /***************************************************************************
2 * Copyright (C) 2006 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 "dolphincontroller.h"
21 #include "zoomlevelinfo.h"
23 #include <kdirmodel.h>
24 #include <QAbstractProxyModel>
25 #include <QApplication>
29 Qt::MouseButtons
DolphinController::m_mouseButtons
= Qt::NoButton
;
31 DolphinController::DolphinController(DolphinView
* dolphinView
) :
36 m_dolphinView(dolphinView
),
38 m_versionControlActions()
42 DolphinController::~DolphinController()
46 void DolphinController::setUrl(const KUrl
& url
)
50 emit
cancelPreviews();
55 void DolphinController::redirectToUrl(const KUrl
& url
)
60 void DolphinController::setItemView(QAbstractItemView
* view
)
62 if (m_itemView
!= 0) {
63 disconnect(m_itemView
, SIGNAL(pressed(const QModelIndex
&)),
64 this, SLOT(updateMouseButtonState()));
69 if (m_itemView
!= 0) {
70 m_zoomLevel
= ZoomLevelInfo::zoomLevelForIconSize(m_itemView
->iconSize());
72 // TODO: this is a workaround until Qt-issue 176832 has been fixed
73 connect(m_itemView
, SIGNAL(pressed(const QModelIndex
&)),
74 this, SLOT(updateMouseButtonState()));
78 void DolphinController::triggerContextMenuRequest(const QPoint
& pos
,
79 const QList
<QAction
*>& customActions
)
82 emit
requestContextMenu(pos
, customActions
);
85 void DolphinController::requestActivation()
90 void DolphinController::indicateDroppedUrls(const KFileItem
& destItem
,
94 emit
urlsDropped(destItem
, destPath
, event
);
98 void DolphinController::indicateSortingChange(DolphinView::Sorting sorting
)
100 emit
sortingChanged(sorting
);
103 void DolphinController::indicateSortOrderChange(Qt::SortOrder order
)
105 emit
sortOrderChanged(order
);
108 void DolphinController::indicateSortFoldersFirstChange(bool foldersFirst
)
110 emit
sortFoldersFirstChanged(foldersFirst
);
113 void DolphinController::indicateAdditionalInfoChange(const KFileItemDelegate::InformationList
& info
)
115 emit
additionalInfoChanged(info
);
118 void DolphinController::indicateActivationChange(bool active
)
120 emit
activationChanged(active
);
123 void DolphinController::setNameFilter(const QString
& nameFilter
)
125 if (nameFilter
!= m_nameFilter
) {
126 m_nameFilter
= nameFilter
;
127 emit
nameFilterChanged(nameFilter
);
131 QString
DolphinController::nameFilter() const
136 void DolphinController::setZoomLevel(int level
)
138 Q_ASSERT(level
>= ZoomLevelInfo::minimumLevel());
139 Q_ASSERT(level
<= ZoomLevelInfo::maximumLevel());
140 if (level
!= m_zoomLevel
) {
142 emit
zoomLevelChanged(m_zoomLevel
);
146 void DolphinController::setVersionControlActions(QList
<QAction
*> actions
)
148 m_versionControlActions
= actions
;
151 QList
<QAction
*> DolphinController::versionControlActions(const KFileItemList
& items
)
153 emit
requestVersionControlActions(items
);
154 // All view implementations are connected with the signal requestVersionControlActions()
155 // (see ViewExtensionFactory) and will invoke DolphinController::setVersionControlActions(),
156 // so that the context dependent actions can be returned.
157 return m_versionControlActions
;
160 void DolphinController::handleKeyPressEvent(QKeyEvent
* event
)
162 Q_ASSERT(m_itemView
!= 0);
164 const QItemSelectionModel
* selModel
= m_itemView
->selectionModel();
165 const QModelIndex currentIndex
= selModel
->currentIndex();
166 const bool trigger
= currentIndex
.isValid()
167 && ((event
->key() == Qt::Key_Return
) || (event
->key() == Qt::Key_Enter
))
168 && !selModel
->selectedIndexes().isEmpty();
173 // Emit the signal itemTriggered() for all selected files.
174 // Several selected directories are opened in separate tabs,
175 // one selected directory will get opened in the view.
176 QModelIndexList dirQueue
;
177 const QModelIndexList indexList
= selModel
->selectedIndexes();
178 foreach (const QModelIndex
& index
, indexList
) {
179 if (itemForIndex(index
).isDir()) {
182 emit
itemTriggered(itemForIndex(index
));
186 if (dirQueue
.length() == 1) {
187 // open directory in the view
188 emit
itemTriggered(itemForIndex(dirQueue
[0]));
190 // open directories in separate tabs
191 foreach(const QModelIndex
& dir
, dirQueue
) {
192 emit
tabRequested(itemForIndex(dir
).url());
197 void DolphinController::replaceUrlByClipboard()
199 const QClipboard
* clipboard
= QApplication::clipboard();
201 if (clipboard
->mimeData(QClipboard::Selection
)->hasText()) {
202 text
= clipboard
->mimeData(QClipboard::Selection
)->text();
203 } else if (clipboard
->mimeData(QClipboard::Clipboard
)->hasText()) {
204 text
= clipboard
->mimeData(QClipboard::Clipboard
)->text();
206 if (!text
.isEmpty() && QDir::isAbsolutePath(text
)) {
207 m_dolphinView
->setUrl(KUrl(text
));
211 void DolphinController::emitHideToolTip()
216 void DolphinController::emitItemTriggered(const KFileItem
& item
)
218 emit
itemTriggered(item
);
221 KFileItem
DolphinController::itemForIndex(const QModelIndex
& index
) const
223 Q_ASSERT(m_itemView
!= 0);
225 QAbstractProxyModel
* proxyModel
= static_cast<QAbstractProxyModel
*>(m_itemView
->model());
226 KDirModel
* dirModel
= static_cast<KDirModel
*>(proxyModel
->sourceModel());
227 const QModelIndex dirIndex
= proxyModel
->mapToSource(index
);
228 return dirModel
->itemForIndex(dirIndex
);
231 void DolphinController::triggerItem(const QModelIndex
& index
)
233 if (m_mouseButtons
& Qt::LeftButton
) {
234 const KFileItem item
= itemForIndex(index
);
235 if (index
.isValid() && (index
.column() == KDirModel::Name
)) {
236 emit
itemTriggered(item
);
238 m_itemView
->clearSelection();
239 emit
itemEntered(KFileItem());
244 void DolphinController::requestTab(const QModelIndex
& index
)
246 if (m_mouseButtons
& Qt::MidButton
) {
247 const KFileItem item
= itemForIndex(index
);
248 const bool validRequest
= index
.isValid() &&
249 (index
.column() == KDirModel::Name
) &&
250 (item
.isDir() || m_dolphinView
->isTabsForFilesEnabled());
252 emit
tabRequested(item
.url());
257 void DolphinController::emitItemEntered(const QModelIndex
& index
)
259 KFileItem item
= itemForIndex(index
);
260 if (!item
.isNull()) {
261 emit
itemEntered(item
);
265 void DolphinController::emitViewportEntered()
267 emit
viewportEntered();
270 void DolphinController::updateMouseButtonState()
272 m_mouseButtons
= QApplication::mouseButtons();
275 #include "dolphincontroller.moc"