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 DolphinController::DolphinController(DolphinView
* dolphinView
) :
32 m_mouseButtons(Qt::NoButton
),
34 m_dolphinView(dolphinView
),
39 DolphinController::~DolphinController()
43 void DolphinController::setUrl(const KUrl
& url
)
51 void DolphinController::setItemView(QAbstractItemView
* view
)
53 if (m_itemView
!= 0) {
54 disconnect(m_itemView
, SIGNAL(pressed(const QModelIndex
&)),
55 this, SLOT(updateMouseButtonState()));
60 if (m_itemView
!= 0) {
61 m_zoomLevel
= ZoomLevelInfo::zoomLevelForIconSize(m_itemView
->iconSize());
63 // TODO: this is a workaround until Qt-issue 176832 has been fixed
64 connect(m_itemView
, SIGNAL(pressed(const QModelIndex
&)),
65 this, SLOT(updateMouseButtonState()));
69 void DolphinController::triggerUrlChangeRequest(const KUrl
& url
)
72 emit
requestUrlChange(url
);
76 void DolphinController::triggerContextMenuRequest(const QPoint
& pos
)
79 emit
requestContextMenu(pos
);
82 void DolphinController::requestActivation()
87 void DolphinController::indicateDroppedUrls(const KFileItem
& destItem
,
91 emit
urlsDropped(destItem
, destPath
, event
);
95 void DolphinController::indicateSortingChange(DolphinView::Sorting sorting
)
97 emit
sortingChanged(sorting
);
100 void DolphinController::indicateSortOrderChange(Qt::SortOrder order
)
102 emit
sortOrderChanged(order
);
105 void DolphinController::indicateAdditionalInfoChange(const KFileItemDelegate::InformationList
& info
)
107 emit
additionalInfoChanged(info
);
110 void DolphinController::indicateActivationChange(bool active
)
112 emit
activationChanged(active
);
115 void DolphinController::setZoomLevel(int level
)
117 Q_ASSERT(level
>= ZoomLevelInfo::minimumLevel());
118 Q_ASSERT(level
<= ZoomLevelInfo::maximumLevel());
119 if (level
!= m_zoomLevel
) {
121 emit
zoomLevelChanged(m_zoomLevel
);
125 void DolphinController::handleKeyPressEvent(QKeyEvent
* event
)
127 Q_ASSERT(m_itemView
!= 0);
129 const QItemSelectionModel
* selModel
= m_itemView
->selectionModel();
130 const QModelIndex currentIndex
= selModel
->currentIndex();
131 const bool trigger
= currentIndex
.isValid()
132 && ((event
->key() == Qt::Key_Return
)
133 || (event
->key() == Qt::Key_Enter
))
134 && (selModel
->selectedIndexes().count() > 0);
136 const QModelIndexList indexList
= selModel
->selectedIndexes();
137 foreach (const QModelIndex
& index
, indexList
) {
138 emit
itemTriggered(itemForIndex(index
));
143 void DolphinController::replaceUrlByClipboard()
145 const QClipboard
* clipboard
= QApplication::clipboard();
147 if (clipboard
->mimeData(QClipboard::Selection
)->hasText()) {
148 text
= clipboard
->mimeData(QClipboard::Selection
)->text();
149 } else if (clipboard
->mimeData(QClipboard::Clipboard
)->hasText()) {
150 text
= clipboard
->mimeData(QClipboard::Clipboard
)->text();
152 if (!text
.isEmpty() && QDir::isAbsolutePath(text
)) {
153 m_dolphinView
->setUrl(KUrl(text
));
157 void DolphinController::emitHideToolTip()
162 KFileItem
DolphinController::itemForIndex(const QModelIndex
& index
) const
164 Q_ASSERT(m_itemView
!= 0);
166 QAbstractProxyModel
* proxyModel
= static_cast<QAbstractProxyModel
*>(m_itemView
->model());
167 KDirModel
* dirModel
= static_cast<KDirModel
*>(proxyModel
->sourceModel());
168 const QModelIndex dirIndex
= proxyModel
->mapToSource(index
);
169 return dirModel
->itemForIndex(dirIndex
);
172 void DolphinController::triggerItem(const QModelIndex
& index
)
174 if (m_mouseButtons
& Qt::LeftButton
) {
175 const KFileItem item
= itemForIndex(index
);
176 if (index
.isValid() && (index
.column() == KDirModel::Name
)) {
177 emit
itemTriggered(item
);
179 m_itemView
->clearSelection();
180 emit
itemEntered(KFileItem());
185 void DolphinController::requestTab(const QModelIndex
& index
)
187 if (m_mouseButtons
& Qt::MidButton
) {
188 const KFileItem item
= itemForIndex(index
);
189 const bool validRequest
= index
.isValid() &&
190 (index
.column() == KDirModel::Name
) &&
191 (item
.isDir() || m_dolphinView
->isTabsForFilesEnabled());
193 emit
tabRequested(item
.url());
198 void DolphinController::emitItemEntered(const QModelIndex
& index
)
200 KFileItem item
= itemForIndex(index
);
201 if (!item
.isNull()) {
202 emit
itemEntered(item
);
206 void DolphinController::emitViewportEntered()
208 emit
viewportEntered();
211 void DolphinController::updateMouseButtonState()
213 m_mouseButtons
= QApplication::mouseButtons();
216 #include "dolphincontroller.moc"