]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphincolumnview.cpp
Restore filtering of items. The DolphinView just tells the controller about the filte...
[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 const QString nameFilter = controller->nameFilter();
176 if (!nameFilter.isEmpty()) {
177 m_proxyModel->setFilterRegExp(nameFilter);
178 }
179 connect(controller, SIGNAL(nameFilterChanged(const QString&)),
180 this, SLOT(setNameFilter(const QString&)));
181
182 updateDecorationSize(dolphinView->showPreview());
183 }
184
185 DolphinColumnView::~DolphinColumnView()
186 {
187 delete m_proxyModel;
188 m_proxyModel = 0;
189 delete m_dolphinModel;
190 m_dolphinModel = 0;
191 m_dirLister = 0; // deleted by m_dolphinModel
192 }
193
194 void DolphinColumnView::setActive(bool active)
195 {
196 if (active && (m_container->focusProxy() != this)) {
197 m_container->setFocusProxy(this);
198 }
199
200 if (m_active != active) {
201 m_active = active;
202
203 if (active) {
204 activate();
205 } else {
206 deactivate();
207 }
208 }
209 }
210
211 /*void DolphinColumnView::setSorting(DolphinView::Sorting sorting)
212 {
213 m_proxyModel->setSorting(sorting);
214 }
215
216 void DolphinColumnView::setSortOrder(Qt::SortOrder order)
217 {
218 m_proxyModel->setSortOrder(order);
219 }
220
221 void DolphinColumnView::setSortFoldersFirst(bool foldersFirst)
222 {
223 m_proxyModel->setSortFoldersFirst(foldersFirst);
224 }
225
226 void DolphinColumnView::setShowHiddenFiles(bool show)
227 {
228 if (show != m_dirLister->showingDotFiles()) {
229 m_dirLister->setShowingDotFiles(show);
230 m_dirLister->stop();
231 m_dirLister->openUrl(m_url, KDirLister::Reload);
232 }
233 }
234
235 void DolphinColumnView::setShowPreview(bool show)
236 {
237 m_previewGenerator->setPreviewShown(show);
238
239 m_dirLister->stop();
240 m_dirLister->openUrl(m_url, KDirLister::Reload);
241 }*/
242
243 void DolphinColumnView::updateBackground()
244 {
245 // TODO: The alpha-value 150 is copied from DolphinView::setActive(). When
246 // cleaning up the cut-indication of DolphinColumnView with the code from
247 // DolphinView a common helper-class should be available which can be shared
248 // by all view implementations -> no hardcoded value anymore
249 const QPalette::ColorRole role = viewport()->backgroundRole();
250 QColor color = viewport()->palette().color(role);
251 color.setAlpha((m_active && m_container->m_active) ? 255 : 150);
252
253 QPalette palette = viewport()->palette();
254 palette.setColor(role, color);
255 viewport()->setPalette(palette);
256
257 update();
258 }
259
260 KFileItem DolphinColumnView::itemAt(const QPoint& pos) const
261 {
262 KFileItem item;
263 const QModelIndex index = indexAt(pos);
264 if (index.isValid() && (index.column() == DolphinModel::Name)) {
265 const QModelIndex dolphinModelIndex = m_proxyModel->mapToSource(index);
266 item = m_dolphinModel->itemForIndex(dolphinModelIndex);
267 }
268 return item;
269 }
270
271 QStyleOptionViewItem DolphinColumnView::viewOptions() const
272 {
273 QStyleOptionViewItem viewOptions = QListView::viewOptions();
274 viewOptions.font = m_font;
275 viewOptions.decorationSize = m_decorationSize;
276 viewOptions.showDecorationSelected = true;
277 return viewOptions;
278 }
279
280 void DolphinColumnView::startDrag(Qt::DropActions supportedActions)
281 {
282 DragAndDropHelper::instance().startDrag(this, supportedActions, m_container->m_controller);
283 }
284
285 void DolphinColumnView::dragEnterEvent(QDragEnterEvent* event)
286 {
287 if (DragAndDropHelper::instance().isMimeDataSupported(event->mimeData())) {
288 event->acceptProposedAction();
289 requestActivation();
290 }
291 }
292
293 void DolphinColumnView::dragLeaveEvent(QDragLeaveEvent* event)
294 {
295 QListView::dragLeaveEvent(event);
296 setDirtyRegion(m_dropRect);
297 }
298
299 void DolphinColumnView::dragMoveEvent(QDragMoveEvent* event)
300 {
301 QListView::dragMoveEvent(event);
302
303 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
304 const QModelIndex index = indexAt(event->pos());
305 setDirtyRegion(m_dropRect);
306
307 m_dropRect.setSize(QSize()); // set as invalid
308 if (index.isValid()) {
309 m_container->m_controller->setItemView(this);
310 const KFileItem item = m_container->m_controller->itemForIndex(index);
311 if (!item.isNull() && item.isDir()) {
312 m_dropRect = visualRect(index);
313 }
314 }
315 setDirtyRegion(m_dropRect);
316
317 if (DragAndDropHelper::instance().isMimeDataSupported(event->mimeData())) {
318 // accept url drops, independently from the destination item
319 event->acceptProposedAction();
320 }
321 }
322
323 void DolphinColumnView::dropEvent(QDropEvent* event)
324 {
325 const QModelIndex index = indexAt(event->pos());
326 m_container->m_controller->setItemView(this);
327 const KFileItem item = m_container->m_controller->itemForIndex(index);
328 m_container->m_controller->indicateDroppedUrls(item, url(), event);
329 QListView::dropEvent(event);
330 }
331
332 void DolphinColumnView::paintEvent(QPaintEvent* event)
333 {
334 if (!m_childUrl.isEmpty()) {
335 // indicate the shown URL of the next column by highlighting the shown folder item
336 const QModelIndex dirIndex = m_dolphinModel->indexForUrl(m_childUrl);
337 const QModelIndex proxyIndex = m_proxyModel->mapFromSource(dirIndex);
338 if (proxyIndex.isValid() && !selectionModel()->isSelected(proxyIndex)) {
339 const QRect itemRect = visualRect(proxyIndex);
340 QPainter painter(viewport());
341 QColor color = KColorScheme(QPalette::Active, KColorScheme::View).foreground().color();
342 color.setAlpha(32);
343 painter.setPen(Qt::NoPen);
344 painter.setBrush(color);
345 painter.drawRect(itemRect);
346 }
347 }
348
349 QListView::paintEvent(event);
350 }
351
352 void DolphinColumnView::mousePressEvent(QMouseEvent* event)
353 {
354 requestActivation();
355 if (!indexAt(event->pos()).isValid()) {
356 if (QApplication::mouseButtons() & Qt::MidButton) {
357 m_container->m_controller->replaceUrlByClipboard();
358 }
359 } else if (event->button() == Qt::LeftButton) {
360 // TODO: see comment in DolphinIconsView::mousePressEvent()
361 setState(QAbstractItemView::DraggingState);
362 }
363 QListView::mousePressEvent(event);
364 }
365
366 void DolphinColumnView::keyPressEvent(QKeyEvent* event)
367 {
368 QListView::keyPressEvent(event);
369 requestActivation();
370
371 DolphinController* controller = m_container->m_controller;
372 controller->handleKeyPressEvent(event);
373 switch (event->key()) {
374 case Qt::Key_Right: {
375 // Special key handling for the column: A Key_Right should
376 // open a new column for the currently selected folder.
377 const QModelIndex index = currentIndex();
378 const KFileItem item = controller->itemForIndex(index);
379 if (!item.isNull() && item.isDir()) {
380 controller->emitItemTriggered(item);
381 }
382 break;
383 }
384
385 case Qt::Key_Escape:
386 selectionModel()->setCurrentIndex(selectionModel()->currentIndex(),
387 QItemSelectionModel::Current |
388 QItemSelectionModel::Clear);
389 break;
390
391 default:
392 break;
393 }
394
395 if (m_toolTipManager != 0) {
396 m_toolTipManager->hideTip();
397 }
398 }
399
400 void DolphinColumnView::contextMenuEvent(QContextMenuEvent* event)
401 {
402 if (!m_active) {
403 m_container->requestActivation(this);
404 Q_ASSERT(m_container->m_controller->itemView() == this);
405 m_container->m_controller->triggerUrlChangeRequest(m_url);
406 }
407 Q_ASSERT(m_active);
408
409 QListView::contextMenuEvent(event);
410
411 const QModelIndex index = indexAt(event->pos());
412 if (!index.isValid()) {
413 clearSelection();
414 }
415
416 if (m_toolTipManager != 0) {
417 m_toolTipManager->hideTip();
418 }
419
420 const QPoint pos = m_container->viewport()->mapFromGlobal(event->globalPos());
421 Q_ASSERT(m_container->m_controller->itemView() == this);
422 m_container->m_controller->triggerContextMenuRequest(pos);
423 }
424
425 void DolphinColumnView::wheelEvent(QWheelEvent* event)
426 {
427 if (m_selectionManager != 0) {
428 m_selectionManager->reset();
429 }
430
431 // let Ctrl+wheel events propagate to the DolphinView for icon zooming
432 if (event->modifiers() & Qt::ControlModifier) {
433 event->ignore();
434 return;
435 }
436
437 const int height = m_decorationSize.height();
438 const int step = (height >= KIconLoader::SizeHuge) ? height / 10 : (KIconLoader::SizeHuge - height) / 2;
439 verticalScrollBar()->setSingleStep(step);
440
441 QListView::wheelEvent(event);
442 }
443
444 void DolphinColumnView::leaveEvent(QEvent* event)
445 {
446 QListView::leaveEvent(event);
447 // if the mouse is above an item and moved very fast outside the widget,
448 // no viewportEntered() signal might be emitted although the mouse has been moved
449 // above the viewport
450 m_container->m_controller->emitViewportEntered();
451 }
452
453 void DolphinColumnView::selectionChanged(const QItemSelection& selected, const QItemSelection& deselected)
454 {
455 QListView::selectionChanged(selected, deselected);
456
457 //QItemSelectionModel* selModel = m_container->selectionModel();
458 //selModel->select(selected, QItemSelectionModel::Select);
459 //selModel->select(deselected, QItemSelectionModel::Deselect);
460 }
461
462 void DolphinColumnView::currentChanged(const QModelIndex& current, const QModelIndex& previous)
463 {
464 QListView::currentChanged(current, previous);
465 m_autoScroller->handleCurrentIndexChange(current, previous);
466 }
467
468 void DolphinColumnView::setNameFilter(const QString& nameFilter)
469 {
470 m_proxyModel->setFilterRegExp(nameFilter);
471 }
472
473 void DolphinColumnView::setZoomLevel(int level)
474 {
475 const int size = ZoomLevelInfo::iconSizeForZoomLevel(level);
476 ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
477
478 const bool showPreview = m_container->m_controller->dolphinView()->showPreview();
479 if (showPreview) {
480 settings->setPreviewSize(size);
481 } else {
482 settings->setIconSize(size);
483 }
484
485 updateDecorationSize(showPreview);
486 }
487
488 void DolphinColumnView::slotEntered(const QModelIndex& index)
489 {
490 m_container->m_controller->setItemView(this);
491 m_container->m_controller->emitItemEntered(index);
492 }
493
494 void DolphinColumnView::requestActivation()
495 {
496 m_container->m_controller->setItemView(this);
497 m_container->m_controller->requestActivation();
498 if (!m_active) {
499 m_container->requestActivation(this);
500 m_container->m_controller->triggerUrlChangeRequest(m_url);
501 selectionModel()->clear();
502 }
503 }
504
505 void DolphinColumnView::updateFont()
506 {
507 const ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
508 Q_ASSERT(settings != 0);
509
510 if (settings->useSystemFont()) {
511 m_font = KGlobalSettings::generalFont();
512 }
513 }
514
515 void DolphinColumnView::slotShowPreviewChanged()
516 {
517 const DolphinView* view = m_container->m_controller->dolphinView();
518 updateDecorationSize(view->showPreview());
519 }
520
521 void DolphinColumnView::activate()
522 {
523 setFocus(Qt::OtherFocusReason);
524
525 if (KGlobalSettings::singleClick()) {
526 connect(this, SIGNAL(clicked(const QModelIndex&)),
527 m_container->m_controller, SLOT(triggerItem(const QModelIndex&)));
528 } else {
529 connect(this, SIGNAL(doubleClicked(const QModelIndex&)),
530 m_container->m_controller, SLOT(triggerItem(const QModelIndex&)));
531 }
532
533 if (selectionModel() && selectionModel()->currentIndex().isValid()) {
534 selectionModel()->setCurrentIndex(selectionModel()->currentIndex(), QItemSelectionModel::SelectCurrent);
535 }
536
537 updateBackground();
538 }
539
540 void DolphinColumnView::deactivate()
541 {
542 clearFocus();
543 if (KGlobalSettings::singleClick()) {
544 disconnect(this, SIGNAL(clicked(const QModelIndex&)),
545 m_container->m_controller, SLOT(triggerItem(const QModelIndex&)));
546 } else {
547 disconnect(this, SIGNAL(doubleClicked(const QModelIndex&)),
548 m_container->m_controller, SLOT(triggerItem(const QModelIndex&)));
549 }
550
551 const QModelIndex current = selectionModel()->currentIndex();
552 selectionModel()->clear();
553 selectionModel()->setCurrentIndex(current, QItemSelectionModel::NoUpdate);
554 updateBackground();
555 }
556
557 void DolphinColumnView::updateDecorationSize(bool showPreview)
558 {
559 ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
560 const int iconSize = showPreview ? settings->previewSize() : settings->iconSize();
561 const QSize size(iconSize, iconSize);
562 setIconSize(size);
563
564 m_decorationSize = size;
565
566 if (m_selectionManager != 0) {
567 m_selectionManager->reset();
568 }
569
570 doItemsLayout();
571 }
572
573 #include "dolphincolumnview.moc"