]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphincontroller.cpp
fixed krazy string warning
[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 Qt::MouseButtons DolphinController::m_mouseButtons = Qt::NoButton;
30
31 DolphinController::DolphinController(DolphinView* dolphinView) :
32 QObject(dolphinView),
33 m_zoomLevel(0),
34 m_url(),
35 m_dolphinView(dolphinView),
36 m_itemView(0)
37 {
38 }
39
40 DolphinController::~DolphinController()
41 {
42 }
43
44 void DolphinController::setUrl(const KUrl& url)
45 {
46 if (m_url != url) {
47 m_url = url;
48 emit urlChanged(url);
49 }
50 }
51
52 void DolphinController::setItemView(QAbstractItemView* view)
53 {
54 if (m_itemView != 0) {
55 disconnect(m_itemView, SIGNAL(pressed(const QModelIndex&)),
56 this, SLOT(updateMouseButtonState()));
57 }
58
59 m_itemView = view;
60
61 if (m_itemView != 0) {
62 m_zoomLevel = ZoomLevelInfo::zoomLevelForIconSize(m_itemView->iconSize());
63
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()));
67 }
68 }
69
70 void DolphinController::triggerUrlChangeRequest(const KUrl& url)
71 {
72 if (m_url != url) {
73 emit requestUrlChange(url);
74 }
75 }
76
77 void DolphinController::triggerContextMenuRequest(const QPoint& pos,
78 const QList<QAction*>& customActions)
79 {
80 emit activated();
81 emit requestContextMenu(pos, customActions);
82 }
83
84 void DolphinController::requestActivation()
85 {
86 emit activated();
87 }
88
89 void DolphinController::indicateDroppedUrls(const KFileItem& destItem,
90 const KUrl& destPath,
91 QDropEvent* event)
92 {
93 emit urlsDropped(destItem, destPath, event);
94 }
95
96
97 void DolphinController::indicateSortingChange(DolphinView::Sorting sorting)
98 {
99 emit sortingChanged(sorting);
100 }
101
102 void DolphinController::indicateSortOrderChange(Qt::SortOrder order)
103 {
104 emit sortOrderChanged(order);
105 }
106
107 void DolphinController::indicateSortFoldersFirstChange(bool foldersFirst)
108 {
109 emit sortFoldersFirstChanged(foldersFirst);
110 }
111
112 void DolphinController::indicateAdditionalInfoChange(const KFileItemDelegate::InformationList& info)
113 {
114 emit additionalInfoChanged(info);
115 }
116
117 void DolphinController::indicateActivationChange(bool active)
118 {
119 emit activationChanged(active);
120 }
121
122 void DolphinController::setZoomLevel(int level)
123 {
124 Q_ASSERT(level >= ZoomLevelInfo::minimumLevel());
125 Q_ASSERT(level <= ZoomLevelInfo::maximumLevel());
126 if (level != m_zoomLevel) {
127 m_zoomLevel = level;
128 emit zoomLevelChanged(m_zoomLevel);
129 }
130 }
131
132 void DolphinController::handleKeyPressEvent(QKeyEvent* event)
133 {
134 Q_ASSERT(m_itemView != 0);
135
136 const QItemSelectionModel* selModel = m_itemView->selectionModel();
137 const QModelIndex currentIndex = selModel->currentIndex();
138 const bool trigger = currentIndex.isValid()
139 && ((event->key() == Qt::Key_Return)
140 || (event->key() == Qt::Key_Enter))
141 && (selModel->selectedIndexes().count() > 0);
142 if (trigger) {
143 const QModelIndexList indexList = selModel->selectedIndexes();
144 foreach (const QModelIndex& index, indexList) {
145 emit itemTriggered(itemForIndex(index));
146 }
147 }
148 }
149
150 void DolphinController::replaceUrlByClipboard()
151 {
152 const QClipboard* clipboard = QApplication::clipboard();
153 QString text;
154 if (clipboard->mimeData(QClipboard::Selection)->hasText()) {
155 text = clipboard->mimeData(QClipboard::Selection)->text();
156 } else if (clipboard->mimeData(QClipboard::Clipboard)->hasText()) {
157 text = clipboard->mimeData(QClipboard::Clipboard)->text();
158 }
159 if (!text.isEmpty() && QDir::isAbsolutePath(text)) {
160 m_dolphinView->setUrl(KUrl(text));
161 }
162 }
163
164 void DolphinController::emitHideToolTip()
165 {
166 emit hideToolTip();
167 }
168
169 void DolphinController::emitItemTriggered(const KFileItem& item)
170 {
171 emit itemTriggered(item);
172 }
173
174 KFileItem DolphinController::itemForIndex(const QModelIndex& index) const
175 {
176 Q_ASSERT(m_itemView != 0);
177
178 QAbstractProxyModel* proxyModel = static_cast<QAbstractProxyModel*>(m_itemView->model());
179 KDirModel* dirModel = static_cast<KDirModel*>(proxyModel->sourceModel());
180 const QModelIndex dirIndex = proxyModel->mapToSource(index);
181 return dirModel->itemForIndex(dirIndex);
182 }
183
184 void DolphinController::triggerItem(const QModelIndex& index)
185 {
186 if (m_mouseButtons & Qt::LeftButton) {
187 const KFileItem item = itemForIndex(index);
188 if (index.isValid() && (index.column() == KDirModel::Name)) {
189 emit itemTriggered(item);
190 } else {
191 m_itemView->clearSelection();
192 emit itemEntered(KFileItem());
193 }
194 }
195 }
196
197 void DolphinController::requestTab(const QModelIndex& index)
198 {
199 if (m_mouseButtons & Qt::MidButton) {
200 const KFileItem item = itemForIndex(index);
201 const bool validRequest = index.isValid() &&
202 (index.column() == KDirModel::Name) &&
203 (item.isDir() || m_dolphinView->isTabsForFilesEnabled());
204 if (validRequest) {
205 emit tabRequested(item.url());
206 }
207 }
208 }
209
210 void DolphinController::emitItemEntered(const QModelIndex& index)
211 {
212 KFileItem item = itemForIndex(index);
213 if (!item.isNull()) {
214 emit itemEntered(item);
215 }
216 }
217
218 void DolphinController::emitViewportEntered()
219 {
220 emit viewportEntered();
221 }
222
223 void DolphinController::updateMouseButtonState()
224 {
225 m_mouseButtons = QApplication::mouseButtons();
226 }
227
228 #include "dolphincontroller.moc"