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