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"
22 #include <kdirmodel.h>
23 #include <QAbstractProxyModel>
24 #include <QApplication>
28 DolphinController::DolphinController(DolphinView
* dolphinView
) :
33 m_dolphinView(dolphinView
),
38 DolphinController::~DolphinController()
42 void DolphinController::setUrl(const KUrl
& url
)
50 void DolphinController::setItemView(QAbstractItemView
* view
)
52 if (m_itemView
!= 0) {
53 disconnect(m_itemView
, SIGNAL(pressed(const QModelIndex
&)),
54 this, SLOT(updateOpenTabState()));
59 if (m_itemView
!= 0) {
60 switch (m_itemView
->iconSize().height()) {
61 case KIconLoader::SizeSmallMedium
: m_zoomLevel
= 0; break;
62 case KIconLoader::SizeMedium
: m_zoomLevel
= 1; break;
63 case KIconLoader::SizeLarge
: m_zoomLevel
= 2; break;
64 case KIconLoader::SizeHuge
: m_zoomLevel
= 3; break;
65 case KIconLoader::SizeEnormous
: m_zoomLevel
= 4; break;
66 case KIconLoader::SizeEnormous
* 3 / 2: m_zoomLevel
= 5; break;
67 case KIconLoader::SizeEnormous
* 2: m_zoomLevel
= 6; break;
68 default: Q_ASSERT(false); m_zoomLevel
= 2; break;
71 // TODO: this is a workaround until Qt-issue 176832 has been fixed
72 connect(m_itemView
, SIGNAL(pressed(const QModelIndex
&)),
73 this, SLOT(updateOpenTabState()));
77 void DolphinController::triggerUrlChangeRequest(const KUrl
& url
)
80 emit
requestUrlChange(url
);
84 void DolphinController::triggerContextMenuRequest(const QPoint
& pos
)
87 emit
requestContextMenu(pos
);
90 void DolphinController::requestActivation()
95 void DolphinController::indicateDroppedUrls(const KUrl::List
& urls
,
97 const KFileItem
& destItem
)
99 emit
urlsDropped(urls
, destPath
, destItem
);
103 void DolphinController::indicateSortingChange(DolphinView::Sorting sorting
)
105 emit
sortingChanged(sorting
);
108 void DolphinController::indicateSortOrderChange(Qt::SortOrder order
)
110 emit
sortOrderChanged(order
);
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::setZoomLevel(int level
)
125 Q_ASSERT(level
>= zoomLevelMinimum());
126 Q_ASSERT(level
<= zoomLevelMaximum());
127 if (level
!= m_zoomLevel
) {
129 emit
zoomLevelChanged(m_zoomLevel
);
133 int DolphinController::iconSizeForZoomLevel(int level
)
135 int size
= KIconLoader::SizeMedium
;
137 case 0: size
= KIconLoader::SizeSmallMedium
; break;
138 case 1: size
= KIconLoader::SizeMedium
; break;
139 case 2: size
= KIconLoader::SizeLarge
; break;
140 case 3: size
= KIconLoader::SizeHuge
; break;
141 case 4: size
= KIconLoader::SizeEnormous
; break;
142 case 5: size
= KIconLoader::SizeEnormous
* 3 / 2; break;
143 case 6: size
= KIconLoader::SizeEnormous
* 2; break;
144 default: Q_ASSERT(false); break;
149 void DolphinController::handleKeyPressEvent(QKeyEvent
* event
)
151 Q_ASSERT(m_itemView
!= 0);
153 const QItemSelectionModel
* selModel
= m_itemView
->selectionModel();
154 const QModelIndex currentIndex
= selModel
->currentIndex();
155 const bool trigger
= currentIndex
.isValid()
156 && (event
->key() == Qt::Key_Return
)
157 && (selModel
->selectedIndexes().count() > 0);
159 const QModelIndexList indexList
= selModel
->selectedIndexes();
160 foreach (const QModelIndex
& index
, indexList
) {
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 KFileItem
DolphinController::itemForIndex(const QModelIndex
& index
) const
182 Q_ASSERT(m_itemView
!= 0);
184 QAbstractProxyModel
* proxyModel
= static_cast<QAbstractProxyModel
*>(m_itemView
->model());
185 KDirModel
* dirModel
= static_cast<KDirModel
*>(proxyModel
->sourceModel());
186 const QModelIndex dirIndex
= proxyModel
->mapToSource(index
);
187 return dirModel
->itemForIndex(dirIndex
);
190 void DolphinController::triggerItem(const QModelIndex
& index
)
192 const bool openTab
= m_openTab
;
195 const KFileItem item
= itemForIndex(index
);
196 if (index
.isValid() && (index
.column() == KDirModel::Name
)) {
197 if (openTab
&& (item
.isDir() || m_dolphinView
->isTabsForFilesEnabled())) {
198 emit
tabRequested(item
.url());
200 emit
itemTriggered(item
);
203 m_itemView
->clearSelection();
205 emit
itemEntered(KFileItem());
210 void DolphinController::emitItemEntered(const QModelIndex
& index
)
212 KFileItem item
= itemForIndex(index
);
213 if (!item
.isNull()) {
214 emit
itemEntered(item
);
218 void DolphinController::emitViewportEntered()
220 emit
viewportEntered();
223 void DolphinController::updateOpenTabState()
225 m_openTab
= QApplication::mouseButtons() & Qt::MidButton
;
228 #include "dolphincontroller.moc"