]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphincolumnview.cpp
- The view implementations don't need to keep the Extensions Factory as member.
[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_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_dropRect()
69 {
70 setMouseTracking(true);
71 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
72 setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
73 setSelectionBehavior(SelectItems);
74 setSelectionMode(QAbstractItemView::ExtendedSelection);
75 setDragDropMode(QAbstractItemView::DragDrop);
76 setDropIndicatorShown(false);
77 setSelectionRectVisible(true);
78 setEditTriggers(QAbstractItemView::NoEditTriggers);
79
80 setVerticalScrollMode(QListView::ScrollPerPixel);
81 setHorizontalScrollMode(QListView::ScrollPerPixel);
82
83 m_autoScroller = new DolphinViewAutoScroller(this);
84
85 // apply the column mode settings to the widget
86 const ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
87 Q_ASSERT(settings != 0);
88
89 if (settings->useSystemFont()) {
90 m_font = KGlobalSettings::generalFont();
91 } else {
92 m_font = QFont(settings->fontFamily(),
93 settings->fontSize(),
94 settings->fontWeight(),
95 settings->italicFont());
96 }
97
98 // KFileItemDelegate* delegate = new KFileItemDelegate(this);
99 // delegate->setShowToolTipWhenElided(false);
100 // setItemDelegate(delegate);
101
102 activate();
103
104 connect(this, SIGNAL(viewportEntered()),
105 m_container->m_controller, SLOT(emitViewportEntered()));
106 connect(this, SIGNAL(entered(const QModelIndex&)),
107 this, SLOT(slotEntered(const QModelIndex&)));
108
109 const DolphinView* dolphinView = m_container->m_controller->dolphinView();
110 connect(dolphinView, SIGNAL(sortingChanged(DolphinView::Sorting)),
111 this, SLOT(slotSortingChanged(DolphinView::Sorting)));
112 connect(dolphinView, SIGNAL(sortOrderChanged(Qt::SortOrder)),
113 this, SLOT(slotSortOrderChanged(Qt::SortOrder)));
114 connect(dolphinView, SIGNAL(sortFoldersFirstChanged(bool)),
115 this, SLOT(slotSortFoldersFirstChanged(bool)));
116 connect(dolphinView, SIGNAL(showHiddenFilesChanged()),
117 this, SLOT(slotShowHiddenFilesChanged()));
118 connect(dolphinView, SIGNAL(showPreviewChanged()),
119 this, SLOT(slotShowPreviewChanged()));
120
121 m_dirLister = new DolphinDirLister();
122 m_dirLister->setAutoUpdate(true);
123 m_dirLister->setMainWindow(window());
124 m_dirLister->setDelayedMimeTypes(true);
125 const bool showHiddenFiles = m_container->m_controller->dolphinView()->showHiddenFiles();
126 m_dirLister->setShowingDotFiles(showHiddenFiles);
127
128 m_dolphinModel = new DolphinModel(this);
129 m_dolphinModel->setDirLister(m_dirLister);
130 m_dolphinModel->setDropsAllowed(DolphinModel::DropOnDirectory);
131
132 m_proxyModel = new DolphinSortFilterProxyModel(this);
133 m_proxyModel->setSourceModel(m_dolphinModel);
134 m_proxyModel->setFilterCaseSensitivity(Qt::CaseInsensitive);
135
136 m_proxyModel->setSorting(dolphinView->sorting());
137 m_proxyModel->setSortOrder(dolphinView->sortOrder());
138 m_proxyModel->setSortFoldersFirst(dolphinView->sortFoldersFirst());
139
140 setModel(m_proxyModel);
141
142 if (DolphinSettings::instance().generalSettings()->showSelectionToggle()) {
143 m_selectionManager = new SelectionManager(this);
144 connect(m_selectionManager, SIGNAL(selectionChanged()),
145 this, SLOT(requestActivation()));
146 connect(m_container->m_controller, SIGNAL(urlChanged(const KUrl&)),
147 m_selectionManager, SLOT(reset()));
148 }
149
150 //m_dirLister->openUrl(url, KDirLister::NoFlags);
151
152 connect(KGlobalSettings::self(), SIGNAL(kdisplayFontChanged()),
153 this, SLOT(updateFont()));
154
155 /*FolderExpander* folderExpander = new FolderExpander(this, m_proxyModel);
156 folderExpander->setEnabled(DolphinSettings::instance().generalSettings()->autoExpandFolders());
157 connect (folderExpander, SIGNAL(enterDir(const QModelIndex&)),
158 m_container->m_controller, SLOT(triggerItem(const QModelIndex&)));
159
160 new VersionControlObserver(this);*/
161
162 DolphinController* controller = m_container->m_controller;
163 connect(controller, SIGNAL(zoomLevelChanged(int)),
164 this, SLOT(setZoomLevel(int)));
165
166 const QString nameFilter = controller->nameFilter();
167 if (!nameFilter.isEmpty()) {
168 m_proxyModel->setFilterRegExp(nameFilter);
169 }
170 connect(controller, SIGNAL(nameFilterChanged(const QString&)),
171 this, SLOT(setNameFilter(const QString&)));
172
173 updateDecorationSize(dolphinView->showPreview());
174
175 new ViewExtensionsFactory(this, controller);
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
389 void DolphinColumnView::contextMenuEvent(QContextMenuEvent* event)
390 {
391 if (!m_active) {
392 m_container->requestActivation(this);
393 Q_ASSERT(m_container->m_controller->itemView() == this);
394 m_container->m_controller->triggerUrlChangeRequest(m_url);
395 }
396 Q_ASSERT(m_active);
397
398 QListView::contextMenuEvent(event);
399
400 const QModelIndex index = indexAt(event->pos());
401 if (!index.isValid()) {
402 clearSelection();
403 }
404
405 const QPoint pos = m_container->viewport()->mapFromGlobal(event->globalPos());
406 Q_ASSERT(m_container->m_controller->itemView() == this);
407 m_container->m_controller->triggerContextMenuRequest(pos);
408 }
409
410 void DolphinColumnView::wheelEvent(QWheelEvent* event)
411 {
412 if (m_selectionManager != 0) {
413 m_selectionManager->reset();
414 }
415
416 // let Ctrl+wheel events propagate to the DolphinView for icon zooming
417 if (event->modifiers() & Qt::ControlModifier) {
418 event->ignore();
419 return;
420 }
421
422 const int height = m_decorationSize.height();
423 const int step = (height >= KIconLoader::SizeHuge) ? height / 10 : (KIconLoader::SizeHuge - height) / 2;
424 verticalScrollBar()->setSingleStep(step);
425
426 QListView::wheelEvent(event);
427 }
428
429 void DolphinColumnView::leaveEvent(QEvent* event)
430 {
431 QListView::leaveEvent(event);
432 // if the mouse is above an item and moved very fast outside the widget,
433 // no viewportEntered() signal might be emitted although the mouse has been moved
434 // above the viewport
435 m_container->m_controller->emitViewportEntered();
436 }
437
438 void DolphinColumnView::selectionChanged(const QItemSelection& selected, const QItemSelection& deselected)
439 {
440 QListView::selectionChanged(selected, deselected);
441
442 //QItemSelectionModel* selModel = m_container->selectionModel();
443 //selModel->select(selected, QItemSelectionModel::Select);
444 //selModel->select(deselected, QItemSelectionModel::Deselect);
445 }
446
447 void DolphinColumnView::currentChanged(const QModelIndex& current, const QModelIndex& previous)
448 {
449 QListView::currentChanged(current, previous);
450 m_autoScroller->handleCurrentIndexChange(current, previous);
451 }
452
453 void DolphinColumnView::setNameFilter(const QString& nameFilter)
454 {
455 m_proxyModel->setFilterRegExp(nameFilter);
456 }
457
458 void DolphinColumnView::setZoomLevel(int level)
459 {
460 const int size = ZoomLevelInfo::iconSizeForZoomLevel(level);
461 ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
462
463 const bool showPreview = m_container->m_controller->dolphinView()->showPreview();
464 if (showPreview) {
465 settings->setPreviewSize(size);
466 } else {
467 settings->setIconSize(size);
468 }
469
470 updateDecorationSize(showPreview);
471 }
472
473 void DolphinColumnView::slotEntered(const QModelIndex& index)
474 {
475 m_container->m_controller->setItemView(this);
476 m_container->m_controller->emitItemEntered(index);
477 }
478
479 void DolphinColumnView::requestActivation()
480 {
481 m_container->m_controller->setItemView(this);
482 m_container->m_controller->requestActivation();
483 if (!m_active) {
484 m_container->requestActivation(this);
485 m_container->m_controller->triggerUrlChangeRequest(m_url);
486 selectionModel()->clear();
487 }
488 }
489
490 void DolphinColumnView::updateFont()
491 {
492 const ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
493 Q_ASSERT(settings != 0);
494
495 if (settings->useSystemFont()) {
496 m_font = KGlobalSettings::generalFont();
497 }
498 }
499
500 void DolphinColumnView::slotShowPreviewChanged()
501 {
502 const DolphinView* view = m_container->m_controller->dolphinView();
503 updateDecorationSize(view->showPreview());
504 }
505
506 void DolphinColumnView::activate()
507 {
508 setFocus(Qt::OtherFocusReason);
509
510 if (KGlobalSettings::singleClick()) {
511 connect(this, SIGNAL(clicked(const QModelIndex&)),
512 m_container->m_controller, SLOT(triggerItem(const QModelIndex&)));
513 } else {
514 connect(this, SIGNAL(doubleClicked(const QModelIndex&)),
515 m_container->m_controller, SLOT(triggerItem(const QModelIndex&)));
516 }
517
518 if (selectionModel() && selectionModel()->currentIndex().isValid()) {
519 selectionModel()->setCurrentIndex(selectionModel()->currentIndex(), QItemSelectionModel::SelectCurrent);
520 }
521
522 updateBackground();
523 }
524
525 void DolphinColumnView::deactivate()
526 {
527 clearFocus();
528 if (KGlobalSettings::singleClick()) {
529 disconnect(this, SIGNAL(clicked(const QModelIndex&)),
530 m_container->m_controller, SLOT(triggerItem(const QModelIndex&)));
531 } else {
532 disconnect(this, SIGNAL(doubleClicked(const QModelIndex&)),
533 m_container->m_controller, SLOT(triggerItem(const QModelIndex&)));
534 }
535
536 const QModelIndex current = selectionModel()->currentIndex();
537 selectionModel()->clear();
538 selectionModel()->setCurrentIndex(current, QItemSelectionModel::NoUpdate);
539 updateBackground();
540 }
541
542 void DolphinColumnView::updateDecorationSize(bool showPreview)
543 {
544 ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
545 const int iconSize = showPreview ? settings->previewSize() : settings->iconSize();
546 const QSize size(iconSize, iconSize);
547 setIconSize(size);
548
549 m_decorationSize = size;
550
551 if (m_selectionManager != 0) {
552 m_selectionManager->reset();
553 }
554
555 doItemsLayout();
556 }
557
558 #include "dolphincolumnview.moc"