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::triggerUrlChangeRequest(const KUrl
& url
)
81 emit
requestUrlChange(url
);
85 void DolphinController::triggerContextMenuRequest(const QPoint
& pos
,
86 const QList
<QAction
*>& customActions
)
89 emit
requestContextMenu(pos
, customActions
);
92 void DolphinController::requestActivation()
97 void DolphinController::indicateDroppedUrls(const KFileItem
& destItem
,
101 emit
urlsDropped(destItem
, destPath
, event
);
105 void DolphinController::indicateSortingChange(DolphinView::Sorting sorting
)
107 emit
sortingChanged(sorting
);
110 void DolphinController::indicateSortOrderChange(Qt::SortOrder order
)
112 emit
sortOrderChanged(order
);
115 void DolphinController::indicateSortFoldersFirstChange(bool foldersFirst
)
117 emit
sortFoldersFirstChanged(foldersFirst
);
120 void DolphinController::indicateAdditionalInfoChange(const KFileItemDelegate::InformationList
& info
)
122 emit
additionalInfoChanged(info
);
125 void DolphinController::indicateActivationChange(bool active
)
127 emit
activationChanged(active
);
130 void DolphinController::setNameFilter(const QString
& nameFilter
)
132 if (nameFilter
!= m_nameFilter
) {
133 m_nameFilter
= nameFilter
;
134 emit
nameFilterChanged(nameFilter
);
138 QString
DolphinController::nameFilter() const
143 void DolphinController::setZoomLevel(int level
)
145 Q_ASSERT(level
>= ZoomLevelInfo::minimumLevel());
146 Q_ASSERT(level
<= ZoomLevelInfo::maximumLevel());
147 if (level
!= m_zoomLevel
) {
149 emit
zoomLevelChanged(m_zoomLevel
);
153 void DolphinController::setVersionControlActions(QList
<QAction
*> actions
)
155 m_versionControlActions
= actions
;
158 QList
<QAction
*> DolphinController::versionControlActions(const KFileItemList
& items
)
160 emit
requestVersionControlActions(items
);
161 // All view implementations are connected with the signal requestVersionControlActions()
162 // (see ViewExtensionFactory) and will invoke DolphinController::setVersionControlActions(),
163 // so that the context dependent actions can be returned.
164 return m_versionControlActions
;
167 void DolphinController::handleKeyPressEvent(QKeyEvent
* event
)
169 Q_ASSERT(m_itemView
!= 0);
171 const QItemSelectionModel
* selModel
= m_itemView
->selectionModel();
172 const QModelIndex currentIndex
= selModel
->currentIndex();
173 const bool trigger
= currentIndex
.isValid()
174 && ((event
->key() == Qt::Key_Return
)
175 || (event
->key() == Qt::Key_Enter
))
176 && !selModel
->selectedIndexes().isEmpty();
178 const QModelIndexList indexList
= selModel
->selectedIndexes();
179 foreach (const QModelIndex
& index
, indexList
) {
180 emit
itemTriggered(itemForIndex(index
));
185 void DolphinController::replaceUrlByClipboard()
187 const QClipboard
* clipboard
= QApplication::clipboard();
189 if (clipboard
->mimeData(QClipboard::Selection
)->hasText()) {
190 text
= clipboard
->mimeData(QClipboard::Selection
)->text();
191 } else if (clipboard
->mimeData(QClipboard::Clipboard
)->hasText()) {
192 text
= clipboard
->mimeData(QClipboard::Clipboard
)->text();
194 if (!text
.isEmpty() && QDir::isAbsolutePath(text
)) {
195 m_dolphinView
->setUrl(KUrl(text
));
199 void DolphinController::emitHideToolTip()
204 void DolphinController::emitItemTriggered(const KFileItem
& item
)
206 emit
itemTriggered(item
);
209 KFileItem
DolphinController::itemForIndex(const QModelIndex
& index
) const
211 Q_ASSERT(m_itemView
!= 0);
213 QAbstractProxyModel
* proxyModel
= static_cast<QAbstractProxyModel
*>(m_itemView
->model());
214 KDirModel
* dirModel
= static_cast<KDirModel
*>(proxyModel
->sourceModel());
215 const QModelIndex dirIndex
= proxyModel
->mapToSource(index
);
216 return dirModel
->itemForIndex(dirIndex
);
219 void DolphinController::triggerItem(const QModelIndex
& index
)
221 if (m_mouseButtons
& Qt::LeftButton
) {
222 const KFileItem item
= itemForIndex(index
);
223 if (index
.isValid() && (index
.column() == KDirModel::Name
)) {
224 emit
itemTriggered(item
);
226 m_itemView
->clearSelection();
227 emit
itemEntered(KFileItem());
232 void DolphinController::requestTab(const QModelIndex
& index
)
234 if (m_mouseButtons
& Qt::MidButton
) {
235 const KFileItem item
= itemForIndex(index
);
236 const bool validRequest
= index
.isValid() &&
237 (index
.column() == KDirModel::Name
) &&
238 (item
.isDir() || m_dolphinView
->isTabsForFilesEnabled());
240 emit
tabRequested(item
.url());
245 void DolphinController::emitItemEntered(const QModelIndex
& index
)
247 KFileItem item
= itemForIndex(index
);
248 if (!item
.isNull()) {
249 emit
itemEntered(item
);
253 void DolphinController::emitViewportEntered()
255 emit
viewportEntered();
258 void DolphinController::emitSelectionChanged()
260 emit
selectionChanged();
263 void DolphinController::updateMouseButtonState()
265 m_mouseButtons
= QApplication::mouseButtons();
268 #include "dolphincontroller.moc"