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
) :
35 m_dolphinView(dolphinView
),
40 DolphinController::~DolphinController()
44 void DolphinController::setUrl(const KUrl
& url
)
52 void DolphinController::setItemView(QAbstractItemView
* view
)
54 if (m_itemView
!= 0) {
55 disconnect(m_itemView
, SIGNAL(pressed(const QModelIndex
&)),
56 this, SLOT(updateMouseButtonState()));
61 if (m_itemView
!= 0) {
62 m_zoomLevel
= ZoomLevelInfo::zoomLevelForIconSize(m_itemView
->iconSize());
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 void DolphinController::triggerUrlChangeRequest(const KUrl
& url
)
73 emit
requestUrlChange(url
);
77 void DolphinController::triggerContextMenuRequest(const QPoint
& pos
)
80 emit
requestContextMenu(pos
);
83 void DolphinController::requestActivation()
88 void DolphinController::indicateDroppedUrls(const KFileItem
& destItem
,
92 emit
urlsDropped(destItem
, destPath
, event
);
96 void DolphinController::indicateSortingChange(DolphinView::Sorting sorting
)
98 emit
sortingChanged(sorting
);
101 void DolphinController::indicateSortOrderChange(Qt::SortOrder order
)
103 emit
sortOrderChanged(order
);
106 void DolphinController::indicateAdditionalInfoChange(const KFileItemDelegate::InformationList
& info
)
108 emit
additionalInfoChanged(info
);
111 void DolphinController::indicateActivationChange(bool active
)
113 emit
activationChanged(active
);
116 void DolphinController::setZoomLevel(int level
)
118 Q_ASSERT(level
>= ZoomLevelInfo::minimumLevel());
119 Q_ASSERT(level
<= ZoomLevelInfo::maximumLevel());
120 if (level
!= m_zoomLevel
) {
122 emit
zoomLevelChanged(m_zoomLevel
);
126 void DolphinController::handleKeyPressEvent(QKeyEvent
* event
)
128 Q_ASSERT(m_itemView
!= 0);
130 const QItemSelectionModel
* selModel
= m_itemView
->selectionModel();
131 const QModelIndex currentIndex
= selModel
->currentIndex();
133 if (currentIndex
.isValid() && selModel
->selectedIndexes().count() > 0) {
134 const int key
= event
->key();
136 if ((key
== Qt::Key_Return
) || (key
== Qt::Key_Enter
) || (key
== Qt::Key_Right
)) {
138 const QModelIndexList indexList
= selModel
->selectedIndexes();
139 const bool isColumnView
= m_dolphinView
->mode() == m_dolphinView
->ColumnView
;
141 if (key
== Qt::Key_Right
) {
143 // If it is the right arrow key and in the column view-only.
144 KFileItem curFileItem
;
145 foreach(const QModelIndex
& index
, indexList
) {
146 curFileItem
= itemForIndex(index
);
147 if (!curFileItem
.isFile()) {
149 * to make sure that the selected item
150 * is only a folder. If we did not have this check, it would be possible to use
151 * the right arrow to open a file when in the column view */
152 emit
itemTriggered(curFileItem
);
157 //Else it is Return or Enter keypress, so it is okay to perform the action of triggering, on files also.
158 foreach(const QModelIndex
& index
, indexList
) {
159 emit
itemTriggered(itemForIndex(index
));
166 void DolphinController::replaceUrlByClipboard()
168 const QClipboard
* clipboard
= QApplication::clipboard();
170 if (clipboard
->mimeData(QClipboard::Selection
)->hasText()) {
171 text
= clipboard
->mimeData(QClipboard::Selection
)->text();
172 } else if (clipboard
->mimeData(QClipboard::Clipboard
)->hasText()) {
173 text
= clipboard
->mimeData(QClipboard::Clipboard
)->text();
175 if (!text
.isEmpty() && QDir::isAbsolutePath(text
)) {
176 m_dolphinView
->setUrl(KUrl(text
));
180 void DolphinController::emitHideToolTip()
185 KFileItem
DolphinController::itemForIndex(const QModelIndex
& index
) const
187 Q_ASSERT(m_itemView
!= 0);
189 QAbstractProxyModel
* proxyModel
= static_cast<QAbstractProxyModel
*>(m_itemView
->model());
190 KDirModel
* dirModel
= static_cast<KDirModel
*>(proxyModel
->sourceModel());
191 const QModelIndex dirIndex
= proxyModel
->mapToSource(index
);
192 return dirModel
->itemForIndex(dirIndex
);
195 void DolphinController::triggerItem(const QModelIndex
& index
)
197 if (m_mouseButtons
& Qt::LeftButton
) {
198 const KFileItem item
= itemForIndex(index
);
199 if (index
.isValid() && (index
.column() == KDirModel::Name
)) {
200 emit
itemTriggered(item
);
202 m_itemView
->clearSelection();
203 emit
itemEntered(KFileItem());
208 void DolphinController::requestTab(const QModelIndex
& index
)
210 if (m_mouseButtons
& Qt::MidButton
) {
211 const KFileItem item
= itemForIndex(index
);
212 const bool validRequest
= index
.isValid() &&
213 (index
.column() == KDirModel::Name
) &&
214 (item
.isDir() || m_dolphinView
->isTabsForFilesEnabled());
216 emit
tabRequested(item
.url());
221 void DolphinController::emitItemEntered(const QModelIndex
& index
)
223 KFileItem item
= itemForIndex(index
);
224 if (!item
.isNull()) {
225 emit
itemEntered(item
);
229 void DolphinController::emitViewportEntered()
231 emit
viewportEntered();
234 void DolphinController::updateMouseButtonState()
236 m_mouseButtons
= QApplication::mouseButtons();
239 #include "dolphincontroller.moc"