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();
132 const bool trigger
= currentIndex
.isValid()
133 && ((event
->key() == Qt::Key_Return
)
134 || (event
->key() == Qt::Key_Enter
))
135 && (selModel
->selectedIndexes().count() > 0);
137 const QModelIndexList indexList
= selModel
->selectedIndexes();
138 foreach (const QModelIndex
& index
, indexList
) {
139 emit
itemTriggered(itemForIndex(index
));
144 void DolphinController::replaceUrlByClipboard()
146 const QClipboard
* clipboard
= QApplication::clipboard();
148 if (clipboard
->mimeData(QClipboard::Selection
)->hasText()) {
149 text
= clipboard
->mimeData(QClipboard::Selection
)->text();
150 } else if (clipboard
->mimeData(QClipboard::Clipboard
)->hasText()) {
151 text
= clipboard
->mimeData(QClipboard::Clipboard
)->text();
153 if (!text
.isEmpty() && QDir::isAbsolutePath(text
)) {
154 m_dolphinView
->setUrl(KUrl(text
));
158 void DolphinController::emitHideToolTip()
163 KFileItem
DolphinController::itemForIndex(const QModelIndex
& index
) const
165 Q_ASSERT(m_itemView
!= 0);
167 QAbstractProxyModel
* proxyModel
= static_cast<QAbstractProxyModel
*>(m_itemView
->model());
168 KDirModel
* dirModel
= static_cast<KDirModel
*>(proxyModel
->sourceModel());
169 const QModelIndex dirIndex
= proxyModel
->mapToSource(index
);
170 return dirModel
->itemForIndex(dirIndex
);
173 void DolphinController::triggerItem(const QModelIndex
& index
)
175 if (m_mouseButtons
& Qt::LeftButton
) {
176 const KFileItem item
= itemForIndex(index
);
177 if (index
.isValid() && (index
.column() == KDirModel::Name
)) {
178 emit
itemTriggered(item
);
180 m_itemView
->clearSelection();
181 emit
itemEntered(KFileItem());
186 void DolphinController::requestTab(const QModelIndex
& index
)
188 if (m_mouseButtons
& Qt::MidButton
) {
189 const KFileItem item
= itemForIndex(index
);
190 const bool validRequest
= index
.isValid() &&
191 (index
.column() == KDirModel::Name
) &&
192 (item
.isDir() || m_dolphinView
->isTabsForFilesEnabled());
194 emit
tabRequested(item
.url());
199 void DolphinController::emitItemEntered(const QModelIndex
& index
)
201 KFileItem item
= itemForIndex(index
);
202 if (!item
.isNull()) {
203 emit
itemEntered(item
);
207 void DolphinController::emitViewportEntered()
209 emit
viewportEntered();
212 void DolphinController::updateMouseButtonState()
214 m_mouseButtons
= QApplication::mouseButtons();
217 #include "dolphincontroller.moc"