]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphincolumnview.cpp
ups: I forgot to set the and-term for the file query in the last commit
[dolphin.git] / src / dolphincolumnview.cpp
1 /***************************************************************************
2 * Copyright (C) 2007-2009 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 "dolphincolumnview.h"
21
22 #include "dolphinmodel.h"
23 #include "dolphincolumnviewcontainer.h"
24 #include "dolphincontroller.h"
25 #include "dolphindirlister.h"
26 #include "dolphinsortfilterproxymodel.h"
27 #include "settings/dolphinsettings.h"
28 #include "dolphinviewautoscroller.h"
29 #include "dolphin_columnmodesettings.h"
30 #include "dolphin_generalsettings.h"
31 #include "draganddrophelper.h"
32 #include "folderexpander.h"
33 #include "tooltips/tooltipmanager.h"
34 #include "viewextensionsfactory.h"
35 #include "zoomlevelinfo.h"
36
37 #include <kcolorscheme.h>
38 #include <kdirlister.h>
39 #include <kfileitem.h>
40 #include <kio/previewjob.h>
41 #include <kiconeffect.h>
42 #include <kjob.h>
43 #include <konqmimedata.h>
44
45 #include <QApplication>
46 #include <QClipboard>
47 #include <QPainter>
48 #include <QPoint>
49 #include <QScrollBar>
50
51 DolphinColumnView::DolphinColumnView(QWidget* parent,
52 DolphinColumnViewContainer* container,
53 const KUrl& url) :
54 QListView(parent),
55 m_active(false),
56 m_container(container),
57 m_extensionsFactory(0),
58 m_url(url),
59 m_childUrl(),
60 m_font(),
61 m_decorationSize(),
62 m_dirLister(0),
63 m_dolphinModel(0),
64 m_proxyModel(0),
65 m_dropRect()
66 {
67 setMouseTracking(true);
68 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
69 setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
70 setSelectionBehavior(SelectItems);
71 setSelectionMode(QAbstractItemView::ExtendedSelection);
72 setDragDropMode(QAbstractItemView::DragDrop);
73 setDropIndicatorShown(false);
74 setSelectionRectVisible(true);
75 setEditTriggers(QAbstractItemView::NoEditTriggers);
76
77 setVerticalScrollMode(QListView::ScrollPerPixel);
78 setHorizontalScrollMode(QListView::ScrollPerPixel);
79
80 // apply the column mode settings to the widget
81 const ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
82 Q_ASSERT(settings != 0);
83
84 if (settings->useSystemFont()) {
85 m_font = KGlobalSettings::generalFont();
86 } else {
87 m_font = QFont(settings->fontFamily(),
88 qRound(settings->fontSize()),
89 settings->fontWeight(),
90 settings->italicFont());
91 m_font.setPointSizeF(settings->fontSize());
92 }
93
94 connect(this, SIGNAL(viewportEntered()),
95 m_container->m_controller, SLOT(emitViewportEntered()));
96 connect(this, SIGNAL(entered(const QModelIndex&)),
97 this, SLOT(slotEntered(const QModelIndex&)));
98
99 const DolphinView* dolphinView = m_container->m_controller->dolphinView();
100 connect(dolphinView, SIGNAL(showPreviewChanged()),
101 this, SLOT(slotShowPreviewChanged()));
102
103 m_dirLister = new DolphinDirLister();
104 m_dirLister->setAutoUpdate(true);
105 m_dirLister->setMainWindow(window());
106 m_dirLister->setDelayedMimeTypes(true);
107 const bool showHiddenFiles = m_container->m_controller->dolphinView()->showHiddenFiles();
108 m_dirLister->setShowingDotFiles(showHiddenFiles);
109
110 m_dolphinModel = new DolphinModel(this);
111 m_dolphinModel->setDirLister(m_dirLister);
112 m_dolphinModel->setDropsAllowed(DolphinModel::DropOnDirectory);
113
114 m_proxyModel = new DolphinSortFilterProxyModel(this);
115 m_proxyModel->setSourceModel(m_dolphinModel);
116 m_proxyModel->setFilterCaseSensitivity(Qt::CaseInsensitive);
117
118 m_proxyModel->setSorting(dolphinView->sorting());
119 m_proxyModel->setSortOrder(dolphinView->sortOrder());
120 m_proxyModel->setSortFoldersFirst(dolphinView->sortFoldersFirst());
121
122 setModel(m_proxyModel);
123
124 connect(KGlobalSettings::self(), SIGNAL(kdisplayFontChanged()),
125 this, SLOT(updateFont()));
126
127 DolphinController* controller = m_container->m_controller;
128 connect(controller, SIGNAL(zoomLevelChanged(int)),
129 this, SLOT(setZoomLevel(int)));
130
131 const QString nameFilter = controller->nameFilter();
132 if (!nameFilter.isEmpty()) {
133 m_proxyModel->setFilterFixedString(nameFilter);
134 }
135
136 updateDecorationSize(dolphinView->showPreview());
137 m_extensionsFactory = new ViewExtensionsFactory(this, controller);
138 }
139
140 DolphinColumnView::~DolphinColumnView()
141 {
142 delete m_proxyModel;
143 m_proxyModel = 0;
144 delete m_dolphinModel;
145 m_dolphinModel = 0;
146 m_dirLister = 0; // deleted by m_dolphinModel
147 }
148
149 void DolphinColumnView::setActive(bool active)
150 {
151 if (active && (m_container->focusProxy() != this)) {
152 m_container->setFocusProxy(this);
153 }
154
155 if (m_active != active) {
156 m_active = active;
157
158 if (active) {
159 activate();
160 } else {
161 deactivate();
162 }
163 }
164 }
165
166 void DolphinColumnView::updateBackground()
167 {
168 // TODO: The alpha-value 150 is copied from DolphinView::setActive(). When
169 // cleaning up the cut-indication of DolphinColumnView with the code from
170 // DolphinView a common helper-class should be available which can be shared
171 // by all view implementations -> no hardcoded value anymore
172 const QPalette::ColorRole role = viewport()->backgroundRole();
173 QColor color = viewport()->palette().color(role);
174 color.setAlpha((m_active && m_container->m_active) ? 255 : 150);
175
176 QPalette palette = viewport()->palette();
177 palette.setColor(role, color);
178 viewport()->setPalette(palette);
179
180 update();
181 }
182
183 KFileItem DolphinColumnView::itemAt(const QPoint& pos) const
184 {
185 KFileItem item;
186 const QModelIndex index = indexAt(pos);
187 if (index.isValid() && (index.column() == DolphinModel::Name)) {
188 const QModelIndex dolphinModelIndex = m_proxyModel->mapToSource(index);
189 item = m_dolphinModel->itemForIndex(dolphinModelIndex);
190 }
191 return item;
192 }
193
194 void DolphinColumnView::setSelectionModel(QItemSelectionModel* model)
195 {
196 // If a change of the selection is done although the view is not active
197 // (e. g. by the selection markers), the column must be activated. This
198 // is done by listening to the current selectionChanged() signal.
199 if (selectionModel() != 0) {
200 disconnect(selectionModel(), SIGNAL(selectionChanged(QItemSelection, QItemSelection)),
201 this, SLOT(requestActivation()));
202 }
203
204 QListView::setSelectionModel(model);
205
206 connect(selectionModel(), SIGNAL(selectionChanged(QItemSelection, QItemSelection)),
207 this, SLOT(requestActivation()));
208 }
209
210 QStyleOptionViewItem DolphinColumnView::viewOptions() const
211 {
212 QStyleOptionViewItem viewOptions = QListView::viewOptions();
213 viewOptions.font = m_font;
214 viewOptions.fontMetrics = QFontMetrics(m_font);
215 viewOptions.decorationSize = m_decorationSize;
216 viewOptions.showDecorationSelected = true;
217 return viewOptions;
218 }
219
220 void DolphinColumnView::startDrag(Qt::DropActions supportedActions)
221 {
222 DragAndDropHelper::instance().startDrag(this, supportedActions, m_container->m_controller);
223 }
224
225 void DolphinColumnView::dragEnterEvent(QDragEnterEvent* event)
226 {
227 if (DragAndDropHelper::instance().isMimeDataSupported(event->mimeData())) {
228 event->acceptProposedAction();
229 requestActivation();
230 }
231 }
232
233 void DolphinColumnView::dragLeaveEvent(QDragLeaveEvent* event)
234 {
235 QListView::dragLeaveEvent(event);
236 setDirtyRegion(m_dropRect);
237 }
238
239 void DolphinColumnView::dragMoveEvent(QDragMoveEvent* event)
240 {
241 QListView::dragMoveEvent(event);
242
243 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
244 const QModelIndex index = indexAt(event->pos());
245 setDirtyRegion(m_dropRect);
246
247 m_dropRect.setSize(QSize()); // set as invalid
248 if (index.isValid()) {
249 m_container->m_controller->setItemView(this);
250 const KFileItem item = m_container->m_controller->itemForIndex(index);
251 if (!item.isNull() && item.isDir()) {
252 m_dropRect = visualRect(index);
253 }
254 }
255 setDirtyRegion(m_dropRect);
256
257 if (DragAndDropHelper::instance().isMimeDataSupported(event->mimeData())) {
258 // accept url drops, independently from the destination item
259 event->acceptProposedAction();
260 }
261 }
262
263 void DolphinColumnView::dropEvent(QDropEvent* event)
264 {
265 const QModelIndex index = indexAt(event->pos());
266 m_container->m_controller->setItemView(this);
267 const KFileItem item = m_container->m_controller->itemForIndex(index);
268 m_container->m_controller->indicateDroppedUrls(item, url(), event);
269 QListView::dropEvent(event);
270 }
271
272 void DolphinColumnView::paintEvent(QPaintEvent* event)
273 {
274 if (!m_childUrl.isEmpty()) {
275 // indicate the shown URL of the next column by highlighting the shown folder item
276 const QModelIndex dirIndex = m_dolphinModel->indexForUrl(m_childUrl);
277 const QModelIndex proxyIndex = m_proxyModel->mapFromSource(dirIndex);
278 if (proxyIndex.isValid() && !selectionModel()->isSelected(proxyIndex)) {
279 const QRect itemRect = visualRect(proxyIndex);
280 QPainter painter(viewport());
281 QColor color = KColorScheme(QPalette::Active, KColorScheme::View).foreground().color();
282 color.setAlpha(32);
283 painter.setPen(Qt::NoPen);
284 painter.setBrush(color);
285 painter.drawRect(itemRect);
286 }
287 }
288
289 QListView::paintEvent(event);
290 }
291
292 void DolphinColumnView::mousePressEvent(QMouseEvent* event)
293 {
294 requestActivation();
295 if (!indexAt(event->pos()).isValid()) {
296 if (QApplication::mouseButtons() & Qt::MidButton) {
297 m_container->m_controller->replaceUrlByClipboard();
298 }
299 } else if (event->button() == Qt::LeftButton) {
300 // TODO: see comment in DolphinIconsView::mousePressEvent()
301 setState(QAbstractItemView::DraggingState);
302 }
303 QListView::mousePressEvent(event);
304 }
305
306 void DolphinColumnView::keyPressEvent(QKeyEvent* event)
307 {
308 QListView::keyPressEvent(event);
309 requestActivation();
310
311 DolphinController* controller = m_container->m_controller;
312 controller->handleKeyPressEvent(event);
313 switch (event->key()) {
314 case Qt::Key_Right: {
315 // Special key handling for the column: A Key_Right should
316 // open a new column for the currently selected folder.
317 const QModelIndex index = currentIndex();
318 const KFileItem item = controller->itemForIndex(index);
319 if (!item.isNull() && item.isDir()) {
320 controller->emitItemTriggered(item);
321 }
322 break;
323 }
324
325 case Qt::Key_Escape:
326 selectionModel()->setCurrentIndex(selectionModel()->currentIndex(),
327 QItemSelectionModel::Current |
328 QItemSelectionModel::Clear);
329 break;
330
331 default:
332 break;
333 }
334 }
335
336 void DolphinColumnView::contextMenuEvent(QContextMenuEvent* event)
337 {
338 requestActivation();
339 QListView::contextMenuEvent(event);
340 m_container->m_controller->triggerContextMenuRequest(event->pos());
341 }
342
343 void DolphinColumnView::wheelEvent(QWheelEvent* event)
344 {
345 const int step = m_decorationSize.height();
346 verticalScrollBar()->setSingleStep(step);
347 QListView::wheelEvent(event);
348 }
349
350 void DolphinColumnView::leaveEvent(QEvent* event)
351 {
352 QListView::leaveEvent(event);
353 // if the mouse is above an item and moved very fast outside the widget,
354 // no viewportEntered() signal might be emitted although the mouse has been moved
355 // above the viewport
356 m_container->m_controller->emitViewportEntered();
357 }
358
359 void DolphinColumnView::currentChanged(const QModelIndex& current, const QModelIndex& previous)
360 {
361 QListView::currentChanged(current, previous);
362 m_extensionsFactory->handleCurrentIndexChange(current, previous);
363 }
364
365 void DolphinColumnView::setZoomLevel(int level)
366 {
367 const int size = ZoomLevelInfo::iconSizeForZoomLevel(level);
368 ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
369
370 const bool showPreview = m_container->m_controller->dolphinView()->showPreview();
371 if (showPreview) {
372 settings->setPreviewSize(size);
373 } else {
374 settings->setIconSize(size);
375 }
376
377 updateDecorationSize(showPreview);
378 }
379
380 void DolphinColumnView::slotEntered(const QModelIndex& index)
381 {
382 m_container->m_controller->setItemView(this);
383 m_container->m_controller->emitItemEntered(index);
384 }
385
386 void DolphinColumnView::requestActivation()
387 {
388 if (!m_active) {
389 m_container->requestActivation(this);
390 selectionModel()->clear();
391 }
392 }
393
394 void DolphinColumnView::updateFont()
395 {
396 const ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
397 Q_ASSERT(settings != 0);
398
399 if (settings->useSystemFont()) {
400 m_font = KGlobalSettings::generalFont();
401 }
402 }
403
404 void DolphinColumnView::slotShowPreviewChanged()
405 {
406 const DolphinView* view = m_container->m_controller->dolphinView();
407 updateDecorationSize(view->showPreview());
408 }
409
410 void DolphinColumnView::activate()
411 {
412 setFocus(Qt::OtherFocusReason);
413
414 if (KGlobalSettings::singleClick()) {
415 connect(this, SIGNAL(clicked(const QModelIndex&)),
416 m_container->m_controller, SLOT(triggerItem(const QModelIndex&)));
417 } else {
418 connect(this, SIGNAL(doubleClicked(const QModelIndex&)),
419 m_container->m_controller, SLOT(triggerItem(const QModelIndex&)));
420 }
421
422 if (selectionModel() && selectionModel()->currentIndex().isValid()) {
423 selectionModel()->setCurrentIndex(selectionModel()->currentIndex(), QItemSelectionModel::SelectCurrent);
424 }
425
426 updateBackground();
427 }
428
429 void DolphinColumnView::deactivate()
430 {
431 clearFocus();
432 if (KGlobalSettings::singleClick()) {
433 disconnect(this, SIGNAL(clicked(const QModelIndex&)),
434 m_container->m_controller, SLOT(triggerItem(const QModelIndex&)));
435 } else {
436 disconnect(this, SIGNAL(doubleClicked(const QModelIndex&)),
437 m_container->m_controller, SLOT(triggerItem(const QModelIndex&)));
438 }
439
440 const QModelIndex current = selectionModel()->currentIndex();
441 selectionModel()->clear();
442 selectionModel()->setCurrentIndex(current, QItemSelectionModel::NoUpdate);
443 updateBackground();
444 }
445
446 void DolphinColumnView::updateDecorationSize(bool showPreview)
447 {
448 ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
449 const int iconSize = showPreview ? settings->previewSize() : settings->iconSize();
450 const QSize size(iconSize, iconSize);
451 setIconSize(size);
452
453 m_decorationSize = size;
454
455 doItemsLayout();
456 }
457
458 #include "dolphincolumnview.moc"