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
)
53 void DolphinController::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 m_zoomLevel
= ZoomLevelInfo::zoomLevelForIconSize(m_itemView
->iconSize());
65 // TODO: this is a workaround until Qt-issue 176832 has been fixed
66 connect(m_itemView
, SIGNAL(pressed(const QModelIndex
&)),
67 this, SLOT(updateMouseButtonState()));
71 void DolphinController::triggerUrlChangeRequest(const KUrl
& url
)
74 emit
requestUrlChange(url
);
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::handleKeyPressEvent(QKeyEvent
* event
)
148 Q_ASSERT(m_itemView
!= 0);
150 const QItemSelectionModel
* selModel
= m_itemView
->selectionModel();
151 const QModelIndex currentIndex
= selModel
->currentIndex();
152 const bool trigger
= currentIndex
.isValid()
153 && ((event
->key() == Qt::Key_Return
)
154 || (event
->key() == Qt::Key_Enter
))
155 && !selModel
->selectedIndexes().isEmpty();
157 const QModelIndexList indexList
= selModel
->selectedIndexes();
158 foreach (const QModelIndex
& index
, indexList
) {
159 emit
itemTriggered(itemForIndex(index
));
164 void DolphinController::replaceUrlByClipboard()
166 const QClipboard
* clipboard
= QApplication::clipboard();
168 if (clipboard
->mimeData(QClipboard::Selection
)->hasText()) {
169 text
= clipboard
->mimeData(QClipboard::Selection
)->text();
170 } else if (clipboard
->mimeData(QClipboard::Clipboard
)->hasText()) {
171 text
= clipboard
->mimeData(QClipboard::Clipboard
)->text();
173 if (!text
.isEmpty() && QDir::isAbsolutePath(text
)) {
174 m_dolphinView
->setUrl(KUrl(text
));
178 void DolphinController::emitHideToolTip()
183 void DolphinController::emitItemTriggered(const KFileItem
& item
)
185 emit
itemTriggered(item
);
188 KFileItem
DolphinController::itemForIndex(const QModelIndex
& index
) const
190 Q_ASSERT(m_itemView
!= 0);
192 QAbstractProxyModel
* proxyModel
= static_cast<QAbstractProxyModel
*>(m_itemView
->model());
193 KDirModel
* dirModel
= static_cast<KDirModel
*>(proxyModel
->sourceModel());
194 const QModelIndex dirIndex
= proxyModel
->mapToSource(index
);
195 return dirModel
->itemForIndex(dirIndex
);
198 void DolphinController::triggerItem(const QModelIndex
& index
)
200 if (m_mouseButtons
& Qt::LeftButton
) {
201 const KFileItem item
= itemForIndex(index
);
202 if (index
.isValid() && (index
.column() == KDirModel::Name
)) {
203 emit
itemTriggered(item
);
205 m_itemView
->clearSelection();
206 emit
itemEntered(KFileItem());
211 void DolphinController::requestTab(const QModelIndex
& index
)
213 if (m_mouseButtons
& Qt::MidButton
) {
214 const KFileItem item
= itemForIndex(index
);
215 const bool validRequest
= index
.isValid() &&
216 (index
.column() == KDirModel::Name
) &&
217 (item
.isDir() || m_dolphinView
->isTabsForFilesEnabled());
219 emit
tabRequested(item
.url());
224 void DolphinController::emitItemEntered(const QModelIndex
& index
)
226 KFileItem item
= itemForIndex(index
);
227 if (!item
.isNull()) {
228 emit
itemEntered(item
);
232 void DolphinController::emitViewportEntered()
234 emit
viewportEntered();
237 void DolphinController::updateMouseButtonState()
239 m_mouseButtons
= QApplication::mouseButtons();
242 #include "dolphincontroller.moc"