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