]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphincolumnview.cpp
restore zooming functionality
[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 "selectionmanager.h"
34 #include "tooltips/tooltipmanager.h"
35 #include "versioncontrolobserver.h"
36 #include "zoomlevelinfo.h"
37
38 #include <kcolorscheme.h>
39 #include <kdirlister.h>
40 #include <kfileitem.h>
41 #include <kfilepreviewgenerator.h>
42 #include <kio/previewjob.h>
43 #include <kiconeffect.h>
44 #include <kjob.h>
45 #include <konqmimedata.h>
46
47 #include <QApplication>
48 #include <QClipboard>
49 #include <QPainter>
50 #include <QPoint>
51 #include <QScrollBar>
52
53 DolphinColumnView::DolphinColumnView(QWidget* parent,
54 DolphinColumnViewContainer* container,
55 const KUrl& url) :
56 QListView(parent),
57 m_active(false),
58 m_container(container),
59 m_selectionManager(0),
60 m_autoScroller(0),
61 m_url(url),
62 m_childUrl(),
63 m_font(),
64 m_decorationSize(),
65 m_dirLister(0),
66 m_dolphinModel(0),
67 m_proxyModel(0),
68 m_previewGenerator(0),
69 m_toolTipManager(0),
70 m_dropRect()
71 {
72 setMouseTracking(true);
73 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
74 setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
75 setSelectionBehavior(SelectItems);
76 setSelectionMode(QAbstractItemView::ExtendedSelection);
77 setDragDropMode(QAbstractItemView::DragDrop);
78 setDropIndicatorShown(false);
79 setSelectionRectVisible(true);
80 setEditTriggers(QAbstractItemView::NoEditTriggers);
81
82 setVerticalScrollMode(QListView::ScrollPerPixel);
83 setHorizontalScrollMode(QListView::ScrollPerPixel);
84
85 m_autoScroller = new DolphinViewAutoScroller(this);
86
87 // apply the column mode settings to the widget
88 const ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
89 Q_ASSERT(settings != 0);
90
91 if (settings->useSystemFont()) {
92 m_font = KGlobalSettings::generalFont();
93 } else {
94 m_font = QFont(settings->fontFamily(),
95 settings->fontSize(),
96 settings->fontWeight(),
97 settings->italicFont());
98 }
99
100 // KFileItemDelegate* delegate = new KFileItemDelegate(this);
101 // delegate->setShowToolTipWhenElided(false);
102 // setItemDelegate(delegate);
103
104 activate();
105
106 connect(this, SIGNAL(viewportEntered()),
107 m_container->m_controller, SLOT(emitViewportEntered()));
108 connect(this, SIGNAL(entered(const QModelIndex&)),
109 this, SLOT(slotEntered(const QModelIndex&)));
110
111 const DolphinView* dolphinView = m_container->m_controller->dolphinView();
112 connect(dolphinView, SIGNAL(sortingChanged(DolphinView::Sorting)),
113 this, SLOT(slotSortingChanged(DolphinView::Sorting)));
114 connect(dolphinView, SIGNAL(sortOrderChanged(Qt::SortOrder)),
115 this, SLOT(slotSortOrderChanged(Qt::SortOrder)));
116 connect(dolphinView, SIGNAL(sortFoldersFirstChanged(bool)),
117 this, SLOT(slotSortFoldersFirstChanged(bool)));
118 connect(dolphinView, SIGNAL(showHiddenFilesChanged()),
119 this, SLOT(slotShowHiddenFilesChanged()));
120 connect(dolphinView, SIGNAL(showPreviewChanged()),
121 this, SLOT(slotShowPreviewChanged()));
122
123 m_dirLister = new DolphinDirLister();
124 m_dirLister->setAutoUpdate(true);
125 m_dirLister->setMainWindow(window());
126 m_dirLister->setDelayedMimeTypes(true);
127 const bool showHiddenFiles = m_container->m_controller->dolphinView()->showHiddenFiles();
128 m_dirLister->setShowingDotFiles(showHiddenFiles);
129
130 m_dolphinModel = new DolphinModel(this);
131 m_dolphinModel->setDirLister(m_dirLister);
132 m_dolphinModel->setDropsAllowed(DolphinModel::DropOnDirectory);
133
134 m_proxyModel = new DolphinSortFilterProxyModel(this);
135 m_proxyModel->setSourceModel(m_dolphinModel);
136 m_proxyModel->setFilterCaseSensitivity(Qt::CaseInsensitive);
137
138 m_proxyModel->setSorting(dolphinView->sorting());
139 m_proxyModel->setSortOrder(dolphinView->sortOrder());
140 m_proxyModel->setSortFoldersFirst(dolphinView->sortFoldersFirst());
141
142 setModel(m_proxyModel);
143
144 if (DolphinSettings::instance().generalSettings()->showSelectionToggle()) {
145 m_selectionManager = new SelectionManager(this);
146 connect(m_selectionManager, SIGNAL(selectionChanged()),
147 this, SLOT(requestActivation()));
148 connect(m_container->m_controller, SIGNAL(urlChanged(const KUrl&)),
149 m_selectionManager, SLOT(reset()));
150 }
151
152 //m_previewGenerator = new KFilePreviewGenerator(this);
153 //m_previewGenerator->setPreviewShown(m_container->m_controller->dolphinView()->showPreview());
154
155 //if (DolphinSettings::instance().generalSettings()->showToolTips()) {
156 // m_toolTipManager = new ToolTipManager(this, m_proxyModel);
157 //}
158
159 //m_dirLister->openUrl(url, KDirLister::NoFlags);
160
161 connect(KGlobalSettings::self(), SIGNAL(kdisplayFontChanged()),
162 this, SLOT(updateFont()));
163
164 /*FolderExpander* folderExpander = new FolderExpander(this, m_proxyModel);
165 folderExpander->setEnabled(DolphinSettings::instance().generalSettings()->autoExpandFolders());
166 connect (folderExpander, SIGNAL(enterDir(const QModelIndex&)),
167 m_container->m_controller, SLOT(triggerItem(const QModelIndex&)));
168
169 new VersionControlObserver(this);*/
170
171 DolphinController* controller = m_container->m_controller;
172 connect(controller, SIGNAL(zoomLevelChanged(int)),
173 this, SLOT(setZoomLevel(int)));
174
175 updateDecorationSize(dolphinView->showPreview());
176 }
177
178 DolphinColumnView::~DolphinColumnView()
179 {
180 delete m_proxyModel;
181 m_proxyModel = 0;
182 delete m_dolphinModel;
183 m_dolphinModel = 0;
184 m_dirLister = 0; // deleted by m_dolphinModel
185 }
186
187 void DolphinColumnView::setActive(bool active)
188 {
189 if (active && (m_container->focusProxy() != this)) {
190 m_container->setFocusProxy(this);
191 }
192
193 if (m_active != active) {
194 m_active = active;
195
196 if (active) {
197 activate();
198 } else {
199 deactivate();
200 }
201 }
202 }
203
204 /*void DolphinColumnView::setSorting(DolphinView::Sorting sorting)
205 {
206 m_proxyModel->setSorting(sorting);
207 }
208
209 void DolphinColumnView::setSortOrder(Qt::SortOrder order)
210 {
211 m_proxyModel->setSortOrder(order);
212 }
213
214 void DolphinColumnView::setSortFoldersFirst(bool foldersFirst)
215 {
216 m_proxyModel->setSortFoldersFirst(foldersFirst);
217 }
218
219 void DolphinColumnView::setShowHiddenFiles(bool show)
220 {
221 if (show != m_dirLister->showingDotFiles()) {
222 m_dirLister->setShowingDotFiles(show);
223 m_dirLister->stop();
224 m_dirLister->openUrl(m_url, KDirLister::Reload);
225 }
226 }
227
228 void DolphinColumnView::setShowPreview(bool show)
229 {
230 m_previewGenerator->setPreviewShown(show);
231
232 m_dirLister->stop();
233 m_dirLister->openUrl(m_url, KDirLister::Reload);
234 }*/
235
236 void DolphinColumnView::updateBackground()
237 {
238 // TODO: The alpha-value 150 is copied from DolphinView::setActive(). When
239 // cleaning up the cut-indication of DolphinColumnView with the code from
240 // DolphinView a common helper-class should be available which can be shared
241 // by all view implementations -> no hardcoded value anymore
242 const QPalette::ColorRole role = viewport()->backgroundRole();
243 QColor color = viewport()->palette().color(role);
244 color.setAlpha((m_active && m_container->m_active) ? 255 : 150);
245
246 QPalette palette = viewport()->palette();
247 palette.setColor(role, color);
248 viewport()->setPalette(palette);
249
250 update();
251 }
252
253 KFileItem DolphinColumnView::itemAt(const QPoint& pos) const
254 {
255 KFileItem item;
256 const QModelIndex index = indexAt(pos);
257 if (index.isValid() && (index.column() == DolphinModel::Name)) {
258 const QModelIndex dolphinModelIndex = m_proxyModel->mapToSource(index);
259 item = m_dolphinModel->itemForIndex(dolphinModelIndex);
260 }
261 return item;
262 }
263
264 QStyleOptionViewItem DolphinColumnView::viewOptions() const
265 {
266 QStyleOptionViewItem viewOptions = QListView::viewOptions();
267 viewOptions.font = m_font;
268 viewOptions.decorationSize = m_decorationSize;
269 viewOptions.showDecorationSelected = true;
270 return viewOptions;
271 }
272
273 void DolphinColumnView::startDrag(Qt::DropActions supportedActions)
274 {
275 DragAndDropHelper::instance().startDrag(this, supportedActions, m_container->m_controller);
276 }
277
278 void DolphinColumnView::dragEnterEvent(QDragEnterEvent* event)
279 {
280 if (DragAndDropHelper::instance().isMimeDataSupported(event->mimeData())) {
281 event->acceptProposedAction();
282 requestActivation();
283 }
284 }
285
286 void DolphinColumnView::dragLeaveEvent(QDragLeaveEvent* event)
287 {
288 QListView::dragLeaveEvent(event);
289 setDirtyRegion(m_dropRect);
290 }
291
292 void DolphinColumnView::dragMoveEvent(QDragMoveEvent* event)
293 {
294 QListView::dragMoveEvent(event);
295
296 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
297 const QModelIndex index = indexAt(event->pos());
298 setDirtyRegion(m_dropRect);
299
300 m_dropRect.setSize(QSize()); // set as invalid
301 if (index.isValid()) {
302 m_container->m_controller->setItemView(this);
303 const KFileItem item = m_container->m_controller->itemForIndex(index);
304 if (!item.isNull() && item.isDir()) {
305 m_dropRect = visualRect(index);
306 }
307 }
308 setDirtyRegion(m_dropRect);
309
310 if (DragAndDropHelper::instance().isMimeDataSupported(event->mimeData())) {
311 // accept url drops, independently from the destination item
312 event->acceptProposedAction();
313 }
314 }
315
316 void DolphinColumnView::dropEvent(QDropEvent* event)
317 {
318 const QModelIndex index = indexAt(event->pos());
319 m_container->m_controller->setItemView(this);
320 const KFileItem item = m_container->m_controller->itemForIndex(index);
321 m_container->m_controller->indicateDroppedUrls(item, url(), event);
322 QListView::dropEvent(event);
323 }
324
325 void DolphinColumnView::paintEvent(QPaintEvent* event)
326 {
327 if (!m_childUrl.isEmpty()) {
328 // indicate the shown URL of the next column by highlighting the shown folder item
329 const QModelIndex dirIndex = m_dolphinModel->indexForUrl(m_childUrl);
330 const QModelIndex proxyIndex = m_proxyModel->mapFromSource(dirIndex);
331 if (proxyIndex.isValid() && !selectionModel()->isSelected(proxyIndex)) {
332 const QRect itemRect = visualRect(proxyIndex);
333 QPainter painter(viewport());
334 QColor color = KColorScheme(QPalette::Active, KColorScheme::View).foreground().color();
335 color.setAlpha(32);
336 painter.setPen(Qt::NoPen);
337 painter.setBrush(color);
338 painter.drawRect(itemRect);
339 }
340 }
341
342 QListView::paintEvent(event);
343 }
344
345 void DolphinColumnView::mousePressEvent(QMouseEvent* event)
346 {
347 requestActivation();
348 if (!indexAt(event->pos()).isValid()) {
349 if (QApplication::mouseButtons() & Qt::MidButton) {
350 m_container->m_controller->replaceUrlByClipboard();
351 }
352 } else if (event->button() == Qt::LeftButton) {
353 // TODO: see comment in DolphinIconsView::mousePressEvent()
354 setState(QAbstractItemView::DraggingState);
355 }
356 QListView::mousePressEvent(event);
357 }
358
359 void DolphinColumnView::keyPressEvent(QKeyEvent* event)
360 {
361 QListView::keyPressEvent(event);
362 requestActivation();
363
364 DolphinController* controller = m_container->m_controller;
365 controller->handleKeyPressEvent(event);
366 switch (event->key()) {
367 case Qt::Key_Right: {
368 // Special key handling for the column: A Key_Right should
369 // open a new column for the currently selected folder.
370 const QModelIndex index = currentIndex();
371 const KFileItem item = controller->itemForIndex(index);
372 if (!item.isNull() && item.isDir()) {
373 controller->emitItemTriggered(item);
374 }
375 break;
376 }
377
378 case Qt::Key_Escape:
379 selectionModel()->setCurrentIndex(selectionModel()->currentIndex(),
380 QItemSelectionModel::Current |
381 QItemSelectionModel::Clear);
382 break;
383
384 default:
385 break;
386 }
387
388 if (m_toolTipManager != 0) {
389 m_toolTipManager->hideTip();
390 }
391 }
392
393 void DolphinColumnView::contextMenuEvent(QContextMenuEvent* event)
394 {
395 if (!m_active) {
396 m_container->requestActivation(this);
397 Q_ASSERT(m_container->m_controller->itemView() == this);
398 m_container->m_controller->triggerUrlChangeRequest(m_url);
399 }
400 Q_ASSERT(m_active);
401
402 QListView::contextMenuEvent(event);
403
404 const QModelIndex index = indexAt(event->pos());
405 if (!index.isValid()) {
406 clearSelection();
407 }
408
409 if (m_toolTipManager != 0) {
410 m_toolTipManager->hideTip();
411 }
412
413 const QPoint pos = m_container->viewport()->mapFromGlobal(event->globalPos());
414 Q_ASSERT(m_container->m_controller->itemView() == this);
415 m_container->m_controller->triggerContextMenuRequest(pos);
416 }
417
418 void DolphinColumnView::wheelEvent(QWheelEvent* event)
419 {
420 if (m_selectionManager != 0) {
421 m_selectionManager->reset();
422 }
423
424 // let Ctrl+wheel events propagate to the DolphinView for icon zooming
425 if (event->modifiers() & Qt::ControlModifier) {
426 event->ignore();
427 return;
428 }
429
430 const int height = m_decorationSize.height();
431 const int step = (height >= KIconLoader::SizeHuge) ? height / 10 : (KIconLoader::SizeHuge - height) / 2;
432 verticalScrollBar()->setSingleStep(step);
433
434 QListView::wheelEvent(event);
435 }
436
437 void DolphinColumnView::leaveEvent(QEvent* event)
438 {
439 QListView::leaveEvent(event);
440 // if the mouse is above an item and moved very fast outside the widget,
441 // no viewportEntered() signal might be emitted although the mouse has been moved
442 // above the viewport
443 m_container->m_controller->emitViewportEntered();
444 }
445
446 void DolphinColumnView::selectionChanged(const QItemSelection& selected, const QItemSelection& deselected)
447 {
448 QListView::selectionChanged(selected, deselected);
449
450 //QItemSelectionModel* selModel = m_container->selectionModel();
451 //selModel->select(selected, QItemSelectionModel::Select);
452 //selModel->select(deselected, QItemSelectionModel::Deselect);
453 }
454
455 void DolphinColumnView::currentChanged(const QModelIndex& current, const QModelIndex& previous)
456 {
457 QListView::currentChanged(current, previous);
458 m_autoScroller->handleCurrentIndexChange(current, previous);
459 }
460
461
462 void DolphinColumnView::setZoomLevel(int level)
463 {
464 const int size = ZoomLevelInfo::iconSizeForZoomLevel(level);
465 ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
466
467 const bool showPreview = m_container->m_controller->dolphinView()->showPreview();
468 if (showPreview) {
469 settings->setPreviewSize(size);
470 } else {
471 settings->setIconSize(size);
472 }
473
474 updateDecorationSize(showPreview);
475 }
476
477 void DolphinColumnView::slotEntered(const QModelIndex& index)
478 {
479 m_container->m_controller->setItemView(this);
480 m_container->m_controller->emitItemEntered(index);
481 }
482
483 void DolphinColumnView::requestActivation()
484 {
485 m_container->m_controller->setItemView(this);
486 m_container->m_controller->requestActivation();
487 if (!m_active) {
488 m_container->requestActivation(this);
489 m_container->m_controller->triggerUrlChangeRequest(m_url);
490 selectionModel()->clear();
491 }
492 }
493
494 void DolphinColumnView::updateFont()
495 {
496 const ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
497 Q_ASSERT(settings != 0);
498
499 if (settings->useSystemFont()) {
500 m_font = KGlobalSettings::generalFont();
501 }
502 }
503
504 void DolphinColumnView::slotShowPreviewChanged()
505 {
506 const DolphinView* view = m_container->m_controller->dolphinView();
507 updateDecorationSize(view->showPreview());
508 }
509
510 void DolphinColumnView::activate()
511 {
512 setFocus(Qt::OtherFocusReason);
513
514 if (KGlobalSettings::singleClick()) {
515 connect(this, SIGNAL(clicked(const QModelIndex&)),
516 m_container->m_controller, SLOT(triggerItem(const QModelIndex&)));
517 } else {
518 connect(this, SIGNAL(doubleClicked(const QModelIndex&)),
519 m_container->m_controller, SLOT(triggerItem(const QModelIndex&)));
520 }
521
522 if (selectionModel() && selectionModel()->currentIndex().isValid()) {
523 selectionModel()->setCurrentIndex(selectionModel()->currentIndex(), QItemSelectionModel::SelectCurrent);
524 }
525
526 updateBackground();
527 }
528
529 void DolphinColumnView::deactivate()
530 {
531 clearFocus();
532 if (KGlobalSettings::singleClick()) {
533 disconnect(this, SIGNAL(clicked(const QModelIndex&)),
534 m_container->m_controller, SLOT(triggerItem(const QModelIndex&)));
535 } else {
536 disconnect(this, SIGNAL(doubleClicked(const QModelIndex&)),
537 m_container->m_controller, SLOT(triggerItem(const QModelIndex&)));
538 }
539
540 const QModelIndex current = selectionModel()->currentIndex();
541 selectionModel()->clear();
542 selectionModel()->setCurrentIndex(current, QItemSelectionModel::NoUpdate);
543 updateBackground();
544 }
545
546 void DolphinColumnView::updateDecorationSize(bool showPreview)
547 {
548 ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
549 const int iconSize = showPreview ? settings->previewSize() : settings->iconSize();
550 const QSize size(iconSize, iconSize);
551 setIconSize(size);
552
553 m_decorationSize = size;
554
555 if (m_selectionManager != 0) {
556 m_selectionManager->reset();
557 }
558
559 doItemsLayout();
560 }
561
562 #include "dolphincolumnview.moc"