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
),
41 DolphinController::~DolphinController()
45 void DolphinController::setUrl(const KUrl
& url
)
49 emit
cancelPreviews();
54 void DolphinController::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 m_zoomLevel
= ZoomLevelInfo::zoomLevelForIconSize(m_itemView
->iconSize());
66 // TODO: this is a workaround until Qt-issue 176832 has been fixed
67 connect(m_itemView
, SIGNAL(pressed(const QModelIndex
&)),
68 this, SLOT(updateMouseButtonState()));
72 void DolphinController::triggerUrlChangeRequest(const KUrl
& url
)
75 emit
requestUrlChange(url
);
79 void DolphinController::triggerContextMenuRequest(const QPoint
& pos
,
80 const QList
<QAction
*>& customActions
)
83 emit
requestContextMenu(pos
, customActions
);
86 void DolphinController::requestActivation()
91 void DolphinController::indicateDroppedUrls(const KFileItem
& destItem
,
95 emit
urlsDropped(destItem
, destPath
, event
);
99 void DolphinController::indicateSortingChange(DolphinView::Sorting sorting
)
101 emit
sortingChanged(sorting
);
104 void DolphinController::indicateSortOrderChange(Qt::SortOrder order
)
106 emit
sortOrderChanged(order
);
109 void DolphinController::indicateSortFoldersFirstChange(bool foldersFirst
)
111 emit
sortFoldersFirstChanged(foldersFirst
);
114 void DolphinController::indicateAdditionalInfoChange(const KFileItemDelegate::InformationList
& info
)
116 emit
additionalInfoChanged(info
);
119 void DolphinController::indicateActivationChange(bool active
)
121 emit
activationChanged(active
);
124 void DolphinController::setNameFilter(const QString
& nameFilter
)
126 if (nameFilter
!= m_nameFilter
) {
127 m_nameFilter
= nameFilter
;
128 emit
nameFilterChanged(nameFilter
);
132 QString
DolphinController::nameFilter() const
137 void DolphinController::setZoomLevel(int level
)
139 Q_ASSERT(level
>= ZoomLevelInfo::minimumLevel());
140 Q_ASSERT(level
<= ZoomLevelInfo::maximumLevel());
141 if (level
!= m_zoomLevel
) {
143 emit
zoomLevelChanged(m_zoomLevel
);
147 void DolphinController::handleKeyPressEvent(QKeyEvent
* event
)
149 Q_ASSERT(m_itemView
!= 0);
151 const QItemSelectionModel
* selModel
= m_itemView
->selectionModel();
152 const QModelIndex currentIndex
= selModel
->currentIndex();
153 const bool trigger
= currentIndex
.isValid()
154 && ((event
->key() == Qt::Key_Return
)
155 || (event
->key() == Qt::Key_Enter
))
156 && !selModel
->selectedIndexes().isEmpty();
158 const QModelIndexList indexList
= selModel
->selectedIndexes();
159 foreach (const QModelIndex
& index
, indexList
) {
160 emit
itemTriggered(itemForIndex(index
));
165 void DolphinController::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 DolphinController::emitHideToolTip()
184 void DolphinController::emitItemTriggered(const KFileItem
& item
)
186 emit
itemTriggered(item
);
189 KFileItem
DolphinController::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 DolphinController::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 DolphinController::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 DolphinController::emitItemEntered(const QModelIndex
& index
)
227 KFileItem item
= itemForIndex(index
);
228 if (!item
.isNull()) {
229 emit
itemEntered(item
);
233 void DolphinController::emitViewportEntered()
235 emit
viewportEntered();
238 void DolphinController::updateMouseButtonState()
240 m_mouseButtons
= QApplication::mouseButtons();
243 #include "dolphincontroller.moc"