]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphincontroller.cpp
Provide a common iconsize-widget for the settings dialog of the icons-, details-...
[dolphin.git] / src / dolphincontroller.cpp
1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz (peter.penz@gmx.at) *
3 * *
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. *
8 * *
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. *
13 * *
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 ***************************************************************************/
19
20 #include "dolphincontroller.h"
21 #include "zoomlevelinfo.h"
22
23 #include <kdirmodel.h>
24 #include <QAbstractProxyModel>
25 #include <QApplication>
26 #include <QClipboard>
27 #include <QDir>
28
29 DolphinController::DolphinController(DolphinView* dolphinView) :
30 QObject(dolphinView),
31 m_zoomLevel(0),
32 m_openTab(false),
33 m_url(),
34 m_dolphinView(dolphinView),
35 m_itemView(0)
36 {
37 }
38
39 DolphinController::~DolphinController()
40 {
41 }
42
43 void DolphinController::setUrl(const KUrl& url)
44 {
45 if (m_url != url) {
46 m_url = url;
47 emit urlChanged(url);
48 }
49 }
50
51 void DolphinController::setItemView(QAbstractItemView* view)
52 {
53 if (m_itemView != 0) {
54 disconnect(m_itemView, SIGNAL(pressed(const QModelIndex&)),
55 this, SLOT(updateOpenTabState()));
56 }
57
58 m_itemView = view;
59
60 if (m_itemView != 0) {
61 m_zoomLevel = ZoomLevelInfo::zoomLevelForIconSize(m_itemView->iconSize());
62
63 // TODO: this is a workaround until Qt-issue 176832 has been fixed
64 connect(m_itemView, SIGNAL(pressed(const QModelIndex&)),
65 this, SLOT(updateOpenTabState()));
66 }
67 }
68
69 void DolphinController::triggerUrlChangeRequest(const KUrl& url)
70 {
71 if (m_url != url) {
72 emit requestUrlChange(url);
73 }
74 }
75
76 void DolphinController::triggerContextMenuRequest(const QPoint& pos)
77 {
78 emit activated();
79 emit requestContextMenu(pos);
80 }
81
82 void DolphinController::requestActivation()
83 {
84 emit activated();
85 }
86
87 void DolphinController::indicateDroppedUrls(const KUrl::List& urls,
88 const KUrl& destPath,
89 const KFileItem& destItem)
90 {
91 emit urlsDropped(urls, destPath, destItem);
92 }
93
94
95 void DolphinController::indicateSortingChange(DolphinView::Sorting sorting)
96 {
97 emit sortingChanged(sorting);
98 }
99
100 void DolphinController::indicateSortOrderChange(Qt::SortOrder order)
101 {
102 emit sortOrderChanged(order);
103 }
104
105 void DolphinController::indicateAdditionalInfoChange(const KFileItemDelegate::InformationList& info)
106 {
107 emit additionalInfoChanged(info);
108 }
109
110 void DolphinController::indicateActivationChange(bool active)
111 {
112 emit activationChanged(active);
113 }
114
115 void DolphinController::setZoomLevel(int level)
116 {
117 Q_ASSERT(level >= ZoomLevelInfo::minimumLevel());
118 Q_ASSERT(level <= ZoomLevelInfo::maximumLevel());
119 if (level != m_zoomLevel) {
120 m_zoomLevel = level;
121 emit zoomLevelChanged(m_zoomLevel);
122 }
123 }
124
125 void DolphinController::handleKeyPressEvent(QKeyEvent* event)
126 {
127 Q_ASSERT(m_itemView != 0);
128
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 && (selModel->selectedIndexes().count() > 0);
134 if (trigger) {
135 const QModelIndexList indexList = selModel->selectedIndexes();
136 foreach (const QModelIndex& index, indexList) {
137 triggerItem(index);
138 }
139 }
140 }
141
142 void DolphinController::replaceUrlByClipboard()
143 {
144 const QClipboard* clipboard = QApplication::clipboard();
145 QString text;
146 if (clipboard->mimeData(QClipboard::Selection)->hasText()) {
147 text = clipboard->mimeData(QClipboard::Selection)->text();
148 } else if (clipboard->mimeData(QClipboard::Clipboard)->hasText()) {
149 text = clipboard->mimeData(QClipboard::Clipboard)->text();
150 }
151 if (!text.isEmpty() && QDir::isAbsolutePath(text)) {
152 m_dolphinView->setUrl(KUrl(text));
153 }
154 }
155
156 KFileItem DolphinController::itemForIndex(const QModelIndex& index) const
157 {
158 Q_ASSERT(m_itemView != 0);
159
160 QAbstractProxyModel* proxyModel = static_cast<QAbstractProxyModel*>(m_itemView->model());
161 KDirModel* dirModel = static_cast<KDirModel*>(proxyModel->sourceModel());
162 const QModelIndex dirIndex = proxyModel->mapToSource(index);
163 return dirModel->itemForIndex(dirIndex);
164 }
165
166 void DolphinController::triggerItem(const QModelIndex& index)
167 {
168 const bool openTab = m_openTab;
169 m_openTab = false;
170
171 const KFileItem item = itemForIndex(index);
172 if (index.isValid() && (index.column() == KDirModel::Name)) {
173 if (openTab && (item.isDir() || m_dolphinView->isTabsForFilesEnabled())) {
174 emit tabRequested(item.url());
175 } else {
176 emit itemTriggered(item);
177 }
178 } else {
179 m_itemView->clearSelection();
180 if (!openTab) {
181 emit itemEntered(KFileItem());
182 }
183 }
184 }
185
186 void DolphinController::emitItemEntered(const QModelIndex& index)
187 {
188 KFileItem item = itemForIndex(index);
189 if (!item.isNull()) {
190 emit itemEntered(item);
191 }
192 }
193
194 void DolphinController::emitViewportEntered()
195 {
196 emit viewportEntered();
197 }
198
199 void DolphinController::updateOpenTabState()
200 {
201 m_openTab = QApplication::mouseButtons() & Qt::MidButton;
202 }
203
204 #include "dolphincontroller.moc"