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 m_zoomLevel
= zoomLevelForIconSize(m_itemView
->iconSize());
62 // TODO: this is a workaround until Qt-issue 176832 has been fixed
63 connect(m_itemView
, SIGNAL(pressed(const QModelIndex
&)),
64 this, SLOT(updateOpenTabState()));
68 void DolphinController::triggerUrlChangeRequest(const KUrl
& url
)
71 emit
requestUrlChange(url
);
75 void DolphinController::triggerContextMenuRequest(const QPoint
& pos
)
78 emit
requestContextMenu(pos
);
81 void DolphinController::requestActivation()
86 void DolphinController::indicateDroppedUrls(const KUrl::List
& urls
,
88 const KFileItem
& destItem
)
90 emit
urlsDropped(urls
, destPath
, destItem
);
94 void DolphinController::indicateSortingChange(DolphinView::Sorting sorting
)
96 emit
sortingChanged(sorting
);
99 void DolphinController::indicateSortOrderChange(Qt::SortOrder order
)
101 emit
sortOrderChanged(order
);
104 void DolphinController::indicateAdditionalInfoChange(const KFileItemDelegate::InformationList
& info
)
106 emit
additionalInfoChanged(info
);
109 void DolphinController::indicateActivationChange(bool active
)
111 emit
activationChanged(active
);
114 void DolphinController::setZoomLevel(int level
)
116 Q_ASSERT(level
>= zoomLevelMinimum());
117 Q_ASSERT(level
<= zoomLevelMaximum());
118 if (level
!= m_zoomLevel
) {
120 emit
zoomLevelChanged(m_zoomLevel
);
124 int DolphinController::iconSizeForZoomLevel(int level
)
126 int size
= KIconLoader::SizeMedium
;
128 case 0: size
= KIconLoader::SizeSmall
; break;
129 case 1: size
= KIconLoader::SizeSmallMedium
; break;
130 case 2: size
= KIconLoader::SizeMedium
; break;
131 case 3: size
= KIconLoader::SizeLarge
; break;
132 case 4: size
= KIconLoader::SizeHuge
; break;
133 case 5: size
= KIconLoader::SizeEnormous
; break;
134 case 6: size
= KIconLoader::SizeEnormous
* 3 / 2; break;
135 case 7: size
= KIconLoader::SizeEnormous
* 2; break;
136 default: Q_ASSERT(false); break;
141 int DolphinController::zoomLevelForIconSize(const QSize
& size
)
144 switch (size
.height()) {
145 case KIconLoader::SizeSmall
: level
= 0; break;
146 case KIconLoader::SizeSmallMedium
: level
= 1; break;
147 case KIconLoader::SizeMedium
: level
= 2; break;
148 case KIconLoader::SizeLarge
: level
= 3; break;
149 case KIconLoader::SizeHuge
: level
= 4; break;
150 case KIconLoader::SizeEnormous
: level
= 5; break;
151 case KIconLoader::SizeEnormous
* 3 / 2: level
= 6; break;
152 case KIconLoader::SizeEnormous
* 2: level
= 7; break;
153 default: Q_ASSERT(false); level
= 3; break;
158 void DolphinController::handleKeyPressEvent(QKeyEvent
* event
)
160 Q_ASSERT(m_itemView
!= 0);
162 const QItemSelectionModel
* selModel
= m_itemView
->selectionModel();
163 const QModelIndex currentIndex
= selModel
->currentIndex();
164 const bool trigger
= currentIndex
.isValid()
165 && (event
->key() == Qt::Key_Return
)
166 && (selModel
->selectedIndexes().count() > 0);
168 const QModelIndexList indexList
= selModel
->selectedIndexes();
169 foreach (const QModelIndex
& index
, indexList
) {
175 void DolphinController::replaceUrlByClipboard()
177 const QClipboard
* clipboard
= QApplication::clipboard();
179 if (clipboard
->mimeData(QClipboard::Selection
)->hasText()) {
180 text
= clipboard
->mimeData(QClipboard::Selection
)->text();
181 } else if (clipboard
->mimeData(QClipboard::Clipboard
)->hasText()) {
182 text
= clipboard
->mimeData(QClipboard::Clipboard
)->text();
184 if (!text
.isEmpty() && QDir::isAbsolutePath(text
)) {
185 m_dolphinView
->setUrl(KUrl(text
));
189 KFileItem
DolphinController::itemForIndex(const QModelIndex
& index
) const
191 Q_ASSERT(m_itemView
!= 0);
193 QAbstractProxyModel
* proxyModel
= static_cast<QAbstractProxyModel
*>(m_itemView
->model());
194 KDirModel
* dirModel
= static_cast<KDirModel
*>(proxyModel
->sourceModel());
195 const QModelIndex dirIndex
= proxyModel
->mapToSource(index
);
196 return dirModel
->itemForIndex(dirIndex
);
199 void DolphinController::triggerItem(const QModelIndex
& index
)
201 const bool openTab
= m_openTab
;
204 const KFileItem item
= itemForIndex(index
);
205 if (index
.isValid() && (index
.column() == KDirModel::Name
)) {
206 if (openTab
&& (item
.isDir() || m_dolphinView
->isTabsForFilesEnabled())) {
207 emit
tabRequested(item
.url());
209 emit
itemTriggered(item
);
212 m_itemView
->clearSelection();
214 emit
itemEntered(KFileItem());
219 void DolphinController::emitItemEntered(const QModelIndex
& index
)
221 KFileItem item
= itemForIndex(index
);
222 if (!item
.isNull()) {
223 emit
itemEntered(item
);
227 void DolphinController::emitViewportEntered()
229 emit
viewportEntered();
232 void DolphinController::updateOpenTabState()
234 m_openTab
= QApplication::mouseButtons() & Qt::MidButton
;
237 #include "dolphincontroller.moc"