]> cloud.milkyroute.net Git - dolphin.git/blob - src/views/dolphincolumnview.cpp
Use DolphinTreeView as implementation for the columns instead of QListView. This...
[dolphin.git] / src / views / 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 "dolphinviewcontroller.h"
25 #include "dolphindirlister.h"
26 #include "dolphinfileitemdelegate.h"
27 #include "dolphinsortfilterproxymodel.h"
28 #include "settings/dolphinsettings.h"
29 #include "dolphinviewautoscroller.h"
30 #include "dolphin_columnmodesettings.h"
31 #include "dolphin_generalsettings.h"
32 #include "draganddrophelper.h"
33 #include "folderexpander.h"
34 #include "tooltips/tooltipmanager.h"
35 #include "viewextensionsfactory.h"
36 #include "viewmodecontroller.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 <QHeaderView>
50 #include <QPainter>
51 #include <QPoint>
52 #include <QScrollBar>
53
54 DolphinColumnView::DolphinColumnView(QWidget* parent,
55 DolphinColumnViewContainer* container,
56 const KUrl& url) :
57 DolphinTreeView(parent),
58 m_active(false),
59 m_container(container),
60 m_extensionsFactory(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 {
69 setMouseTracking(true);
70 setAcceptDrops(true);
71 setUniformRowHeights(true);
72 setSelectionBehavior(SelectItems);
73 setSelectionMode(QAbstractItemView::ExtendedSelection);
74 setDragDropMode(QAbstractItemView::DragDrop);
75 setDropIndicatorShown(false);
76 setRootIsDecorated(false);
77 setItemsExpandable(false);
78 setEditTriggers(QAbstractItemView::NoEditTriggers);
79
80 const ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
81 Q_ASSERT(settings != 0);
82
83 if (settings->useSystemFont()) {
84 m_font = KGlobalSettings::generalFont();
85 } else {
86 m_font = QFont(settings->fontFamily(),
87 qRound(settings->fontSize()),
88 settings->fontWeight(),
89 settings->italicFont());
90 m_font.setPointSizeF(settings->fontSize());
91 }
92
93 connect(this, SIGNAL(viewportEntered()),
94 m_container->m_dolphinViewController, SLOT(emitViewportEntered()));
95 connect(this, SIGNAL(entered(const QModelIndex&)),
96 this, SLOT(slotEntered(const QModelIndex&)));
97
98 const DolphinView* dolphinView = m_container->m_dolphinViewController->view();
99 connect(dolphinView, SIGNAL(showPreviewChanged()),
100 this, SLOT(slotShowPreviewChanged()));
101
102 m_dirLister = new DolphinDirLister();
103 m_dirLister->setAutoUpdate(true);
104 m_dirLister->setMainWindow(window());
105 m_dirLister->setDelayedMimeTypes(true);
106 const bool showHiddenFiles = m_container->m_dolphinViewController->view()->showHiddenFiles();
107 m_dirLister->setShowingDotFiles(showHiddenFiles);
108
109 m_dolphinModel = new DolphinModel(this);
110 m_dolphinModel->setDirLister(m_dirLister);
111 m_dolphinModel->setDropsAllowed(DolphinModel::DropOnDirectory);
112
113 m_proxyModel = new DolphinSortFilterProxyModel(this);
114 m_proxyModel->setSourceModel(m_dolphinModel);
115 m_proxyModel->setFilterCaseSensitivity(Qt::CaseInsensitive);
116
117 m_proxyModel->setSorting(dolphinView->sorting());
118 m_proxyModel->setSortOrder(dolphinView->sortOrder());
119 m_proxyModel->setSortFoldersFirst(dolphinView->sortFoldersFirst());
120
121 setModel(m_proxyModel);
122
123 connect(KGlobalSettings::self(), SIGNAL(kdisplayFontChanged()),
124 this, SLOT(updateFont()));
125
126 const ViewModeController* viewModeController = m_container->m_viewModeController;
127 connect(viewModeController, SIGNAL(zoomLevelChanged(int)),
128 this, SLOT(setZoomLevel(int)));
129 const QString nameFilter = viewModeController->nameFilter();
130 if (!nameFilter.isEmpty()) {
131 m_proxyModel->setFilterFixedString(nameFilter);
132 }
133
134 updateDecorationSize(dolphinView->showPreview());
135 updateBackground();
136
137 DolphinViewController* dolphinViewController = m_container->m_dolphinViewController;
138 m_extensionsFactory = new ViewExtensionsFactory(this, dolphinViewController, viewModeController);
139 m_extensionsFactory->fileItemDelegate()->setMinimizedNameColumn(true);
140
141 m_dirLister->openUrl(url, KDirLister::NoFlags);
142 }
143
144 DolphinColumnView::~DolphinColumnView()
145 {
146 delete m_proxyModel;
147 m_proxyModel = 0;
148 delete m_dolphinModel;
149 m_dolphinModel = 0;
150 m_dirLister = 0; // deleted by m_dolphinModel
151 }
152
153 void DolphinColumnView::setActive(bool active)
154 {
155 if (m_active != active) {
156 m_active = active;
157
158 if (active) {
159 activate();
160 } else {
161 deactivate();
162 }
163 }
164 }
165
166 void DolphinColumnView::updateBackground()
167 {
168 // TODO: The alpha-value 150 is copied from DolphinView::setActive(). When
169 // cleaning up the cut-indication of DolphinColumnView with the code from
170 // DolphinView a common helper-class should be available which can be shared
171 // by all view implementations -> no hardcoded value anymore
172 const QPalette::ColorRole role = viewport()->backgroundRole();
173 QColor color = viewport()->palette().color(role);
174 color.setAlpha((m_active && m_container->m_active) ? 255 : 150);
175
176 QPalette palette = viewport()->palette();
177 palette.setColor(role, color);
178 viewport()->setPalette(palette);
179
180 update();
181 }
182
183 KFileItem DolphinColumnView::itemAt(const QPoint& pos) const
184 {
185 KFileItem item;
186 const QModelIndex index = indexAt(pos);
187 if (index.isValid() && (index.column() == DolphinModel::Name)) {
188 const QModelIndex dolphinModelIndex = m_proxyModel->mapToSource(index);
189 item = m_dolphinModel->itemForIndex(dolphinModelIndex);
190 }
191 return item;
192 }
193
194 void DolphinColumnView::setSelectionModel(QItemSelectionModel* model)
195 {
196 // If a change of the selection is done although the view is not active
197 // (e. g. by the selection markers), the column must be activated. This
198 // is done by listening to the current selectionChanged() signal.
199 if (selectionModel() != 0) {
200 disconnect(selectionModel(), SIGNAL(selectionChanged(QItemSelection, QItemSelection)),
201 this, SLOT(requestActivation()));
202 }
203
204 DolphinTreeView::setSelectionModel(model);
205
206 connect(selectionModel(), SIGNAL(selectionChanged(QItemSelection, QItemSelection)),
207 this, SLOT(requestActivation()));
208 }
209
210 QStyleOptionViewItem DolphinColumnView::viewOptions() const
211 {
212 QStyleOptionViewItem viewOptions = DolphinTreeView::viewOptions();
213 viewOptions.font = m_font;
214 viewOptions.fontMetrics = QFontMetrics(m_font);
215 viewOptions.decorationSize = m_decorationSize;
216 viewOptions.showDecorationSelected = true;
217 return viewOptions;
218 }
219
220 bool DolphinColumnView::event(QEvent* event)
221 {
222 if (event->type() == QEvent::Polish) {
223 // Hide all columns except of the 'Name' column
224 for (int i = DolphinModel::Name + 1; i < DolphinModel::ExtraColumnCount; ++i) {
225 hideColumn(i);
226 }
227 header()->hide();
228 }
229
230 return DolphinTreeView::event(event);
231 }
232
233 void DolphinColumnView::startDrag(Qt::DropActions supportedActions)
234 {
235 DragAndDropHelper::instance().startDrag(this, supportedActions, m_container->m_dolphinViewController);
236 DolphinTreeView::startDrag(supportedActions);
237 }
238
239 void DolphinColumnView::dragEnterEvent(QDragEnterEvent* event)
240 {
241 if (DragAndDropHelper::instance().isMimeDataSupported(event->mimeData())) {
242 event->acceptProposedAction();
243 requestActivation();
244 }
245 DolphinTreeView::dragEnterEvent(event);
246 }
247
248 void DolphinColumnView::dragMoveEvent(QDragMoveEvent* event)
249 {
250 DolphinTreeView::dragMoveEvent(event);
251
252 if (DragAndDropHelper::instance().isMimeDataSupported(event->mimeData())) {
253 // accept url drops, independently from the destination item
254 event->acceptProposedAction();
255 }
256 }
257
258 void DolphinColumnView::dropEvent(QDropEvent* event)
259 {
260 const QModelIndex index = indexAt(event->pos());
261 m_container->m_dolphinViewController->setItemView(this);
262 const QModelIndex dolphinModelIndex = m_proxyModel->mapToSource(index);
263 const KFileItem item = m_dolphinModel->itemForIndex(dolphinModelIndex);
264 m_container->m_dolphinViewController->indicateDroppedUrls(item, url(), event);
265 DolphinTreeView::dropEvent(event);
266 }
267
268 void DolphinColumnView::paintEvent(QPaintEvent* event)
269 {
270 if (!m_childUrl.isEmpty()) {
271 // indicate the shown URL of the next column by highlighting the shown folder item
272 const QModelIndex dirIndex = m_dolphinModel->indexForUrl(m_childUrl);
273 const QModelIndex proxyIndex = m_proxyModel->mapFromSource(dirIndex);
274 if (proxyIndex.isValid() && !selectionModel()->isSelected(proxyIndex)) {
275 const QRect itemRect = visualRect(proxyIndex);
276 QPainter painter(viewport());
277 QColor color = KColorScheme(QPalette::Active, KColorScheme::View).foreground().color();
278 color.setAlpha(32);
279 painter.setPen(Qt::NoPen);
280 painter.setBrush(color);
281 painter.drawRect(itemRect);
282 }
283 }
284
285 DolphinTreeView::paintEvent(event);
286 }
287
288 void DolphinColumnView::mousePressEvent(QMouseEvent* event)
289 {
290 requestActivation();
291 if (!indexAt(event->pos()).isValid() && (QApplication::mouseButtons() & Qt::MidButton)) {
292 m_container->m_dolphinViewController->replaceUrlByClipboard();
293 }
294
295 DolphinTreeView::mousePressEvent(event);
296 }
297
298 void DolphinColumnView::keyPressEvent(QKeyEvent* event)
299 {
300 DolphinTreeView::keyPressEvent(event);
301
302 DolphinViewController* controller = m_container->m_dolphinViewController;
303 controller->handleKeyPressEvent(event);
304 switch (event->key()) {
305 case Qt::Key_Right: {
306 // Special key handling for the column: A Key_Right should
307 // open a new column for the currently selected folder.
308 const QModelIndex dolphinModelIndex = m_proxyModel->mapToSource(currentIndex());
309 const KFileItem item = m_dolphinModel->itemForIndex(dolphinModelIndex);
310 if (!item.isNull() && item.isDir()) {
311 controller->emitItemTriggered(item);
312 }
313 break;
314 }
315
316 case Qt::Key_Escape:
317 selectionModel()->setCurrentIndex(selectionModel()->currentIndex(),
318 QItemSelectionModel::Current |
319 QItemSelectionModel::Clear);
320 break;
321
322 default:
323 break;
324 }
325 }
326
327 void DolphinColumnView::contextMenuEvent(QContextMenuEvent* event)
328 {
329 requestActivation();
330 DolphinTreeView::contextMenuEvent(event);
331 m_container->m_dolphinViewController->triggerContextMenuRequest(event->pos());
332 }
333
334 void DolphinColumnView::wheelEvent(QWheelEvent* event)
335 {
336 const int step = m_decorationSize.height();
337 verticalScrollBar()->setSingleStep(step);
338 DolphinTreeView::wheelEvent(event);
339 }
340
341 void DolphinColumnView::leaveEvent(QEvent* event)
342 {
343 DolphinTreeView::leaveEvent(event);
344 // if the mouse is above an item and moved very fast outside the widget,
345 // no viewportEntered() signal might be emitted although the mouse has been moved
346 // above the viewport
347 m_container->m_dolphinViewController->emitViewportEntered();
348 }
349
350 void DolphinColumnView::currentChanged(const QModelIndex& current, const QModelIndex& previous)
351 {
352 DolphinTreeView::currentChanged(current, previous);
353 m_extensionsFactory->handleCurrentIndexChange(current, previous);
354 }
355
356 QRect DolphinColumnView::visualRect(const QModelIndex& index) const
357 {
358 QRect rect = DolphinTreeView::visualRect(index);
359
360 const QModelIndex dolphinModelIndex = m_proxyModel->mapToSource(index);
361 const KFileItem item = m_dolphinModel->itemForIndex(dolphinModelIndex);
362 if (!item.isNull()) {
363 const int width = DolphinFileItemDelegate::nameColumnWidth(item.text(), viewOptions());
364 rect.setWidth(width);
365 }
366
367 return rect;
368 }
369
370 bool DolphinColumnView::acceptsDrop(const QModelIndex& index) const
371 {
372 if (index.isValid() && (index.column() == DolphinModel::Name)) {
373 // Accept drops above directories
374 const QModelIndex dolphinModelIndex = m_proxyModel->mapToSource(index);
375 const KFileItem item = m_dolphinModel->itemForIndex(dolphinModelIndex);
376 return !item.isNull() && item.isDir();
377 }
378
379 return false;
380 }
381
382 void DolphinColumnView::setZoomLevel(int level)
383 {
384 const int size = ZoomLevelInfo::iconSizeForZoomLevel(level);
385 ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
386
387 const bool showPreview = m_container->m_dolphinViewController->view()->showPreview();
388 if (showPreview) {
389 settings->setPreviewSize(size);
390 } else {
391 settings->setIconSize(size);
392 }
393
394 updateDecorationSize(showPreview);
395 }
396
397 void DolphinColumnView::slotEntered(const QModelIndex& index)
398 {
399 m_container->m_dolphinViewController->setItemView(this);
400 m_container->m_dolphinViewController->emitItemEntered(index);
401 }
402
403 void DolphinColumnView::requestActivation()
404 {
405 m_container->m_dolphinViewController->requestActivation();
406 if (!m_active) {
407 m_container->requestActivation(this);
408 selectionModel()->clear();
409 }
410 }
411
412 void DolphinColumnView::updateFont()
413 {
414 const ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
415 Q_ASSERT(settings != 0);
416
417 if (settings->useSystemFont()) {
418 m_font = KGlobalSettings::generalFont();
419 }
420 }
421
422 void DolphinColumnView::slotShowPreviewChanged()
423 {
424 const DolphinView* view = m_container->m_dolphinViewController->view();
425 updateDecorationSize(view->showPreview());
426 }
427
428 void DolphinColumnView::activate()
429 {
430 setFocus(Qt::OtherFocusReason);
431
432 if (KGlobalSettings::singleClick()) {
433 connect(this, SIGNAL(clicked(const QModelIndex&)),
434 m_container->m_dolphinViewController, SLOT(triggerItem(const QModelIndex&)));
435 } else {
436 connect(this, SIGNAL(doubleClicked(const QModelIndex&)),
437 m_container->m_dolphinViewController, SLOT(triggerItem(const QModelIndex&)));
438 }
439
440 if (selectionModel() && selectionModel()->currentIndex().isValid()) {
441 selectionModel()->setCurrentIndex(selectionModel()->currentIndex(), QItemSelectionModel::SelectCurrent);
442 }
443
444 updateBackground();
445 }
446
447 void DolphinColumnView::deactivate()
448 {
449 clearFocus();
450 if (KGlobalSettings::singleClick()) {
451 disconnect(this, SIGNAL(clicked(const QModelIndex&)),
452 m_container->m_dolphinViewController, SLOT(triggerItem(const QModelIndex&)));
453 } else {
454 disconnect(this, SIGNAL(doubleClicked(const QModelIndex&)),
455 m_container->m_dolphinViewController, SLOT(triggerItem(const QModelIndex&)));
456 }
457
458 // It is important to disconnect the connection to requestActivation() temporary, otherwise the internal
459 // clearing of the selection would result in activating the column again.
460 disconnect(selectionModel(), SIGNAL(selectionChanged(QItemSelection, QItemSelection)),
461 this, SLOT(requestActivation()));
462 const QModelIndex current = selectionModel()->currentIndex();
463 selectionModel()->clear();
464 selectionModel()->setCurrentIndex(current, QItemSelectionModel::NoUpdate);
465 connect(selectionModel(), SIGNAL(selectionChanged(QItemSelection, QItemSelection)),
466 this, SLOT(requestActivation()));
467
468 updateBackground();
469 }
470
471 void DolphinColumnView::updateDecorationSize(bool showPreview)
472 {
473 ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
474 const int iconSize = showPreview ? settings->previewSize() : settings->iconSize();
475 const QSize size(iconSize, iconSize);
476 setIconSize(size);
477
478 m_decorationSize = size;
479
480 doItemsLayout();
481 }
482
483 #include "dolphincolumnview.moc"