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