]> cloud.milkyroute.net Git - dolphin.git/blob - src/views/dolphincolumnview.cpp
Indicate the shown directory of a column by QStyle::PE_FrameFocusRect instead of...
[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 QPainter painter(viewport());
276
277 QStyleOptionViewItemV4 option;
278 option.initFrom(this);
279 option.rect = visualRect(proxyIndex);
280 option.state = QStyle::State_Enabled | QStyle::State_HasFocus;
281 option.viewItemPosition = QStyleOptionViewItemV4::OnlyOne;
282 style()->drawPrimitive(QStyle::PE_FrameFocusRect, &option, &painter, this);
283 }
284 }
285
286 DolphinTreeView::paintEvent(event);
287 }
288
289 void DolphinColumnView::mousePressEvent(QMouseEvent* event)
290 {
291 requestActivation();
292 if (!indexAt(event->pos()).isValid() && (QApplication::mouseButtons() & Qt::MidButton)) {
293 m_container->m_dolphinViewController->replaceUrlByClipboard();
294 }
295
296 DolphinTreeView::mousePressEvent(event);
297 }
298
299 void DolphinColumnView::keyPressEvent(QKeyEvent* event)
300 {
301 DolphinTreeView::keyPressEvent(event);
302
303 DolphinViewController* controller = m_container->m_dolphinViewController;
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 dolphinModelIndex = m_proxyModel->mapToSource(currentIndex());
310 const KFileItem item = m_dolphinModel->itemForIndex(dolphinModelIndex);
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 requestActivation();
331 DolphinTreeView::contextMenuEvent(event);
332 m_container->m_dolphinViewController->triggerContextMenuRequest(event->pos());
333 }
334
335 void DolphinColumnView::wheelEvent(QWheelEvent* event)
336 {
337 const int step = m_decorationSize.height();
338 verticalScrollBar()->setSingleStep(step);
339 DolphinTreeView::wheelEvent(event);
340 }
341
342 void DolphinColumnView::leaveEvent(QEvent* event)
343 {
344 DolphinTreeView::leaveEvent(event);
345 // if the mouse is above an item and moved very fast outside the widget,
346 // no viewportEntered() signal might be emitted although the mouse has been moved
347 // above the viewport
348 m_container->m_dolphinViewController->emitViewportEntered();
349 }
350
351 void DolphinColumnView::currentChanged(const QModelIndex& current, const QModelIndex& previous)
352 {
353 DolphinTreeView::currentChanged(current, previous);
354 m_extensionsFactory->handleCurrentIndexChange(current, previous);
355 }
356
357 QRect DolphinColumnView::visualRect(const QModelIndex& index) const
358 {
359 QRect rect = DolphinTreeView::visualRect(index);
360
361 const QModelIndex dolphinModelIndex = m_proxyModel->mapToSource(index);
362 const KFileItem item = m_dolphinModel->itemForIndex(dolphinModelIndex);
363 if (!item.isNull()) {
364 const int width = DolphinFileItemDelegate::nameColumnWidth(item.text(), viewOptions());
365 rect.setWidth(width);
366 }
367
368 return rect;
369 }
370
371 bool DolphinColumnView::acceptsDrop(const QModelIndex& index) const
372 {
373 if (index.isValid() && (index.column() == DolphinModel::Name)) {
374 // Accept drops above directories
375 const QModelIndex dolphinModelIndex = m_proxyModel->mapToSource(index);
376 const KFileItem item = m_dolphinModel->itemForIndex(dolphinModelIndex);
377 return !item.isNull() && item.isDir();
378 }
379
380 return false;
381 }
382
383 void DolphinColumnView::setZoomLevel(int level)
384 {
385 const int size = ZoomLevelInfo::iconSizeForZoomLevel(level);
386 ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
387
388 const bool showPreview = m_container->m_dolphinViewController->view()->showPreview();
389 if (showPreview) {
390 settings->setPreviewSize(size);
391 } else {
392 settings->setIconSize(size);
393 }
394
395 updateDecorationSize(showPreview);
396 }
397
398 void DolphinColumnView::slotEntered(const QModelIndex& index)
399 {
400 m_container->m_dolphinViewController->setItemView(this);
401 m_container->m_dolphinViewController->emitItemEntered(index);
402 }
403
404 void DolphinColumnView::requestActivation()
405 {
406 m_container->m_dolphinViewController->requestActivation();
407 if (!m_active) {
408 m_container->requestActivation(this);
409 selectionModel()->clear();
410 }
411 }
412
413 void DolphinColumnView::updateFont()
414 {
415 const ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
416 Q_ASSERT(settings != 0);
417
418 if (settings->useSystemFont()) {
419 m_font = KGlobalSettings::generalFont();
420 }
421 }
422
423 void DolphinColumnView::slotShowPreviewChanged()
424 {
425 const DolphinView* view = m_container->m_dolphinViewController->view();
426 updateDecorationSize(view->showPreview());
427 }
428
429 void DolphinColumnView::activate()
430 {
431 setFocus(Qt::OtherFocusReason);
432
433 if (KGlobalSettings::singleClick()) {
434 connect(this, SIGNAL(clicked(const QModelIndex&)),
435 m_container->m_dolphinViewController, SLOT(triggerItem(const QModelIndex&)));
436 } else {
437 connect(this, SIGNAL(doubleClicked(const QModelIndex&)),
438 m_container->m_dolphinViewController, SLOT(triggerItem(const QModelIndex&)));
439 }
440
441 if (selectionModel() && selectionModel()->currentIndex().isValid()) {
442 selectionModel()->setCurrentIndex(selectionModel()->currentIndex(), QItemSelectionModel::SelectCurrent);
443 }
444
445 updateBackground();
446 }
447
448 void DolphinColumnView::deactivate()
449 {
450 clearFocus();
451 if (KGlobalSettings::singleClick()) {
452 disconnect(this, SIGNAL(clicked(const QModelIndex&)),
453 m_container->m_dolphinViewController, SLOT(triggerItem(const QModelIndex&)));
454 } else {
455 disconnect(this, SIGNAL(doubleClicked(const QModelIndex&)),
456 m_container->m_dolphinViewController, SLOT(triggerItem(const QModelIndex&)));
457 }
458
459 // It is important to disconnect the connection to requestActivation() temporary, otherwise the internal
460 // clearing of the selection would result in activating the column again.
461 disconnect(selectionModel(), SIGNAL(selectionChanged(QItemSelection, QItemSelection)),
462 this, SLOT(requestActivation()));
463 const QModelIndex current = selectionModel()->currentIndex();
464 selectionModel()->clear();
465 selectionModel()->setCurrentIndex(current, QItemSelectionModel::NoUpdate);
466 connect(selectionModel(), SIGNAL(selectionChanged(QItemSelection, QItemSelection)),
467 this, SLOT(requestActivation()));
468
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"