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