]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphindetailsview.cpp
As the Qt-issue 160611 is solved in Qt4.4 and Fredrik has improved KFileItemDelegate...
[dolphin.git] / src / dolphindetailsview.cpp
1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz *
3 * peter.penz@gmx.at *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
19 ***************************************************************************/
20
21 #include "dolphindetailsview.h"
22
23 #include "dolphinmodel.h"
24 #include "dolphincontroller.h"
25 #include "dolphinsettings.h"
26 #include "dolphinsortfilterproxymodel.h"
27 #include "draganddrophelper.h"
28 #include "selectionmanager.h"
29 #include "viewproperties.h"
30
31 #include "dolphin_detailsmodesettings.h"
32 #include "dolphin_generalsettings.h"
33
34 #include <kdirmodel.h>
35 #include <klocale.h>
36 #include <kmenu.h>
37
38 #include <QAbstractProxyModel>
39 #include <QAction>
40 #include <QApplication>
41 #include <QHeaderView>
42 #include <QRubberBand>
43 #include <QPainter>
44 #include <QScrollBar>
45
46 DolphinDetailsView::DolphinDetailsView(QWidget* parent, DolphinController* controller) :
47 QTreeView(parent),
48 m_autoResize(true),
49 m_controller(controller),
50 m_font(),
51 m_decorationSize(),
52 m_showElasticBand(false),
53 m_elasticBandOrigin(),
54 m_elasticBandDestination()
55 {
56 const DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
57 Q_ASSERT(settings != 0);
58 Q_ASSERT(controller != 0);
59
60 setAcceptDrops(true);
61 setSortingEnabled(true);
62 setUniformRowHeights(true);
63 setSelectionBehavior(SelectItems);
64 setDragDropMode(QAbstractItemView::DragDrop);
65 setDropIndicatorShown(false);
66 setAlternatingRowColors(true);
67 setRootIsDecorated(settings->expandableFolders());
68 setItemsExpandable(settings->expandableFolders());
69
70 setMouseTracking(true);
71 viewport()->setAttribute(Qt::WA_Hover);
72
73 const ViewProperties props(controller->url());
74 setSortIndicatorSection(props.sorting());
75 setSortIndicatorOrder(props.sortOrder());
76
77 QHeaderView* headerView = header();
78 connect(headerView, SIGNAL(sectionClicked(int)),
79 this, SLOT(synchronizeSortingState(int)));
80 headerView->setContextMenuPolicy(Qt::CustomContextMenu);
81 connect(headerView, SIGNAL(customContextMenuRequested(const QPoint&)),
82 this, SLOT(configureColumns(const QPoint&)));
83 connect(headerView, SIGNAL(sectionResized(int, int, int)),
84 this, SLOT(slotHeaderSectionResized(int, int, int)));
85 connect(headerView, SIGNAL(sectionHandleDoubleClicked(int)),
86 this, SLOT(disableAutoResizing()));
87
88 connect(parent, SIGNAL(sortingChanged(DolphinView::Sorting)),
89 this, SLOT(setSortIndicatorSection(DolphinView::Sorting)));
90 connect(parent, SIGNAL(sortOrderChanged(Qt::SortOrder)),
91 this, SLOT(setSortIndicatorOrder(Qt::SortOrder)));
92
93 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
94 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
95 // necessary connecting the signal 'singleClick()' or 'doubleClick' and to handle the
96 // RETURN-key in keyPressEvent().
97 if (KGlobalSettings::singleClick()) {
98 connect(this, SIGNAL(clicked(const QModelIndex&)),
99 controller, SLOT(triggerItem(const QModelIndex&)));
100 if (DolphinSettings::instance().generalSettings()->showSelectionToggle()) {
101 SelectionManager* selManager = new SelectionManager(this);
102 connect(selManager, SIGNAL(selectionChanged()),
103 this, SLOT(requestActivation()));
104 connect(m_controller, SIGNAL(urlChanged(const KUrl&)),
105 selManager, SLOT(reset()));
106 }
107 } else {
108 connect(this, SIGNAL(doubleClicked(const QModelIndex&)),
109 controller, SLOT(triggerItem(const QModelIndex&)));
110 }
111 connect(this, SIGNAL(entered(const QModelIndex&)),
112 this, SLOT(slotEntered(const QModelIndex&)));
113 connect(this, SIGNAL(viewportEntered()),
114 controller, SLOT(emitViewportEntered()));
115 connect(controller, SIGNAL(zoomIn()),
116 this, SLOT(zoomIn()));
117 connect(controller, SIGNAL(zoomOut()),
118 this, SLOT(zoomOut()));
119 connect(controller->dolphinView(), SIGNAL(additionalInfoChanged()),
120 this, SLOT(updateColumnVisibility()));
121
122 if (settings->useSystemFont()) {
123 m_font = KGlobalSettings::generalFont();
124 } else {
125 m_font = QFont(settings->fontFamily(),
126 settings->fontSize(),
127 settings->fontWeight(),
128 settings->italicFont());
129 }
130
131 setVerticalScrollMode(QTreeView::ScrollPerPixel);
132 setHorizontalScrollMode(QTreeView::ScrollPerPixel);
133
134 updateDecorationSize();
135
136 setFocus();
137
138 connect(KGlobalSettings::self(), SIGNAL(kdisplayFontChanged()),
139 this, SLOT(updateFont()));
140 }
141
142 DolphinDetailsView::~DolphinDetailsView()
143 {
144 }
145
146 bool DolphinDetailsView::event(QEvent* event)
147 {
148 if (event->type() == QEvent::Polish) {
149 QHeaderView* headerView = header();
150 headerView->setResizeMode(QHeaderView::Interactive);
151 headerView->setMovable(false);
152
153 updateColumnVisibility();
154
155 hideColumn(DolphinModel::Rating);
156 hideColumn(DolphinModel::Tags);
157 } else if (event->type() == QEvent::UpdateRequest) {
158 // a wheel movement will scroll 4 items
159 if (model()->rowCount() > 0) {
160 verticalScrollBar()->setSingleStep((sizeHintForRow(0) / 3) * 4);
161 }
162 }
163
164 return QTreeView::event(event);
165 }
166
167 QStyleOptionViewItem DolphinDetailsView::viewOptions() const
168 {
169 QStyleOptionViewItem viewOptions = QTreeView::viewOptions();
170 viewOptions.font = m_font;
171 viewOptions.showDecorationSelected = true;
172 viewOptions.decorationSize = m_decorationSize;
173 return viewOptions;
174 }
175
176 void DolphinDetailsView::contextMenuEvent(QContextMenuEvent* event)
177 {
178 QTreeView::contextMenuEvent(event);
179 m_controller->triggerContextMenuRequest(event->pos());
180 }
181
182 void DolphinDetailsView::mousePressEvent(QMouseEvent* event)
183 {
184 m_controller->requestActivation();
185
186 QTreeView::mousePressEvent(event);
187
188 const QModelIndex index = indexAt(event->pos());
189 if (!index.isValid() || (index.column() != DolphinModel::Name)) {
190 const Qt::KeyboardModifiers modifier = QApplication::keyboardModifiers();
191 if (!(modifier & Qt::ShiftModifier) && !(modifier & Qt::ControlModifier)) {
192 clearSelection();
193 }
194 }
195
196 if (event->button() == Qt::LeftButton) {
197 m_showElasticBand = true;
198
199 const QPoint pos(contentsPos());
200 m_elasticBandOrigin = event->pos();
201 m_elasticBandOrigin.setX(m_elasticBandOrigin.x() + pos.x());
202 m_elasticBandOrigin.setY(m_elasticBandOrigin.y() + pos.y());
203 m_elasticBandDestination = event->pos();
204 }
205 }
206
207 void DolphinDetailsView::mouseMoveEvent(QMouseEvent* event)
208 {
209 if (m_showElasticBand) {
210 const QPoint mousePos = event->pos();
211 const QModelIndex index = indexAt(mousePos);
212 if (!index.isValid()) {
213 // the destination of the selection rectangle is above the viewport. In this
214 // case QTreeView does no selection at all, which is not the wanted behavior
215 // in Dolphin -> select all items within the elastic band rectangle
216 clearSelection();
217
218 const int nameColumnWidth = header()->sectionSize(DolphinModel::Name);
219 QRect selRect = QRect(m_elasticBandOrigin, m_elasticBandDestination).normalized();
220 const QRect nameColumnsRect(0, 0, nameColumnWidth, viewport()->height());
221 selRect = nameColumnsRect.intersected(selRect);
222
223 setSelection(selRect, QItemSelectionModel::Select);
224 }
225
226 // TODO: enable QTreeView::mouseMoveEvent(event) again, as soon
227 // as the Qt-issue #199631 has been fixed.
228 // QTreeView::mouseMoveEvent(event);
229 QAbstractItemView::mouseMoveEvent(event);
230 updateElasticBand();
231 } else {
232 // TODO: enable QTreeView::mouseMoveEvent(event) again, as soon
233 // as the Qt-issue #199631 has been fixed.
234 // QTreeView::mouseMoveEvent(event);
235 QAbstractItemView::mouseMoveEvent(event);
236 }
237 }
238
239 void DolphinDetailsView::mouseReleaseEvent(QMouseEvent* event)
240 {
241 QTreeView::mouseReleaseEvent(event);
242 if (m_showElasticBand) {
243 updateElasticBand();
244 m_showElasticBand = false;
245 }
246 }
247
248 void DolphinDetailsView::startDrag(Qt::DropActions supportedActions)
249 {
250 DragAndDropHelper::startDrag(this, supportedActions);
251 }
252
253 void DolphinDetailsView::dragEnterEvent(QDragEnterEvent* event)
254 {
255 if (event->mimeData()->hasUrls()) {
256 event->acceptProposedAction();
257 }
258
259 if (m_showElasticBand) {
260 updateElasticBand();
261 m_showElasticBand = false;
262 }
263 }
264
265 void DolphinDetailsView::dragLeaveEvent(QDragLeaveEvent* event)
266 {
267 QTreeView::dragLeaveEvent(event);
268 setDirtyRegion(m_dropRect);
269 }
270
271 void DolphinDetailsView::dragMoveEvent(QDragMoveEvent* event)
272 {
273 QTreeView::dragMoveEvent(event);
274
275 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
276 setDirtyRegion(m_dropRect);
277 const QModelIndex index = indexAt(event->pos());
278 if (index.isValid() && (index.column() == DolphinModel::Name)) {
279 const KFileItem item = m_controller->itemForIndex(index);
280 if (!item.isNull() && item.isDir()) {
281 m_dropRect = visualRect(index);
282 } else {
283 m_dropRect.setSize(QSize()); // set as invalid
284 }
285 setDirtyRegion(m_dropRect);
286 }
287
288 if (event->mimeData()->hasUrls()) {
289 // accept url drops, independently from the destination item
290 event->acceptProposedAction();
291 }
292 }
293
294 void DolphinDetailsView::dropEvent(QDropEvent* event)
295 {
296 const KUrl::List urls = KUrl::List::fromMimeData(event->mimeData());
297 if (!urls.isEmpty()) {
298 event->acceptProposedAction();
299 const QModelIndex index = indexAt(event->pos());
300 KFileItem item;
301 if (index.isValid() && (index.column() == DolphinModel::Name)) {
302 item = m_controller->itemForIndex(index);
303 }
304 m_controller->indicateDroppedUrls(urls,
305 m_controller->url(),
306 item);
307 }
308 QTreeView::dropEvent(event);
309 }
310
311 void DolphinDetailsView::paintEvent(QPaintEvent* event)
312 {
313 QTreeView::paintEvent(event);
314 if (m_showElasticBand) {
315 // The following code has been taken from QListView
316 // and adapted to DolphinDetailsView.
317 // (C) 1992-2007 Trolltech ASA
318 QStyleOptionRubberBand opt;
319 opt.initFrom(this);
320 opt.shape = QRubberBand::Rectangle;
321 opt.opaque = false;
322 opt.rect = elasticBandRect();
323
324 QPainter painter(viewport());
325 painter.save();
326 style()->drawControl(QStyle::CE_RubberBand, &opt, &painter);
327 painter.restore();
328 }
329 }
330
331 void DolphinDetailsView::keyPressEvent(QKeyEvent* event)
332 {
333 QTreeView::keyPressEvent(event);
334 m_controller->handleKeyPressEvent(event);
335 }
336
337 void DolphinDetailsView::resizeEvent(QResizeEvent* event)
338 {
339 if (m_autoResize) {
340 resizeColumns();
341 }
342 QTreeView::resizeEvent(event);
343 }
344
345 void DolphinDetailsView::wheelEvent(QWheelEvent* event)
346 {
347 // let Ctrl+wheel events propagate to the DolphinView for icon zooming
348 if (event->modifiers() & Qt::ControlModifier) {
349 event->ignore();
350 return;
351 }
352 QTreeView::wheelEvent(event);
353 }
354
355 void DolphinDetailsView::currentChanged(const QModelIndex& current, const QModelIndex& previous)
356 {
357 QTreeView::currentChanged(current, previous);
358 selectionModel()->select(current, QItemSelectionModel::ClearAndSelect);
359 }
360
361 void DolphinDetailsView::setSortIndicatorSection(DolphinView::Sorting sorting)
362 {
363 QHeaderView* headerView = header();
364 headerView->setSortIndicator(sorting, headerView->sortIndicatorOrder());
365 }
366
367 void DolphinDetailsView::setSortIndicatorOrder(Qt::SortOrder sortOrder)
368 {
369 QHeaderView* headerView = header();
370 headerView->setSortIndicator(headerView->sortIndicatorSection(), sortOrder);
371 }
372
373 void DolphinDetailsView::synchronizeSortingState(int column)
374 {
375 // The sorting has already been changed in QTreeView if this slot is
376 // invoked, but Dolphin is not informed about this.
377 DolphinView::Sorting sorting = DolphinSortFilterProxyModel::sortingForColumn(column);
378 const Qt::SortOrder sortOrder = header()->sortIndicatorOrder();
379 m_controller->indicateSortingChange(sorting);
380 m_controller->indicateSortOrderChange(sortOrder);
381 }
382
383 void DolphinDetailsView::slotEntered(const QModelIndex& index)
384 {
385 const QPoint pos = viewport()->mapFromGlobal(QCursor::pos());
386 const int nameColumnWidth = header()->sectionSize(DolphinModel::Name);
387 if (pos.x() < nameColumnWidth) {
388 m_controller->emitItemEntered(index);
389 }
390 else {
391 m_controller->emitViewportEntered();
392 }
393 }
394
395 void DolphinDetailsView::updateElasticBand()
396 {
397 if (m_showElasticBand) {
398 QRect dirtyRegion(elasticBandRect());
399 m_elasticBandDestination = viewport()->mapFromGlobal(QCursor::pos());
400 dirtyRegion = dirtyRegion.united(elasticBandRect());
401 setDirtyRegion(dirtyRegion);
402 }
403 }
404
405 QRect DolphinDetailsView::elasticBandRect() const
406 {
407 const QPoint pos(contentsPos());
408 const QPoint topLeft(m_elasticBandOrigin.x() - pos.x(), m_elasticBandOrigin.y() - pos.y());
409 return QRect(topLeft, m_elasticBandDestination).normalized();
410 }
411
412 void DolphinDetailsView::zoomIn()
413 {
414 if (isZoomInPossible()) {
415 DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
416 switch (settings->iconSize()) {
417 case KIconLoader::SizeSmall: settings->setIconSize(KIconLoader::SizeMedium); break;
418 case KIconLoader::SizeMedium: settings->setIconSize(KIconLoader::SizeLarge); break;
419 default: Q_ASSERT(false); break;
420 }
421 updateDecorationSize();
422 }
423 }
424
425 void DolphinDetailsView::zoomOut()
426 {
427 if (isZoomOutPossible()) {
428 DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
429 switch (settings->iconSize()) {
430 case KIconLoader::SizeLarge: settings->setIconSize(KIconLoader::SizeMedium); break;
431 case KIconLoader::SizeMedium: settings->setIconSize(KIconLoader::SizeSmall); break;
432 default: Q_ASSERT(false); break;
433 }
434 updateDecorationSize();
435 }
436 }
437
438 void DolphinDetailsView::configureColumns(const QPoint& pos)
439 {
440 KMenu popup(this);
441 popup.addTitle(i18nc("@title:menu", "Columns"));
442
443 QHeaderView* headerView = header();
444 for (int i = DolphinModel::Size; i <= DolphinModel::Type; ++i) {
445 const int logicalIndex = headerView->logicalIndex(i);
446 const QString text = model()->headerData(i, Qt::Horizontal).toString();
447 QAction* action = popup.addAction(text);
448 action->setCheckable(true);
449 action->setChecked(!headerView->isSectionHidden(logicalIndex));
450 action->setData(i);
451 }
452
453 QAction* activatedAction = popup.exec(header()->mapToGlobal(pos));
454 if (activatedAction != 0) {
455 const bool show = activatedAction->isChecked();
456 const int columnIndex = activatedAction->data().toInt();
457
458 KFileItemDelegate::InformationList list = m_controller->dolphinView()->additionalInfo();
459 const KFileItemDelegate::Information info = infoForColumn(columnIndex);
460 if (show) {
461 Q_ASSERT(!list.contains(info));
462 list.append(info);
463 } else {
464 Q_ASSERT(list.contains(info));
465 const int index = list.indexOf(info);
466 list.removeAt(index);
467 }
468
469 m_controller->indicateAdditionalInfoChange(list);
470 setColumnHidden(columnIndex, !show);
471 }
472 }
473
474 void DolphinDetailsView::updateColumnVisibility()
475 {
476 const KFileItemDelegate::InformationList list = m_controller->dolphinView()->additionalInfo();
477 for (int i = DolphinModel::Size; i <= DolphinModel::Type; ++i) {
478 const KFileItemDelegate::Information info = infoForColumn(i);
479 const bool hide = !list.contains(info);
480 if (isColumnHidden(i) != hide) {
481 setColumnHidden(i, hide);
482 }
483 }
484
485 resizeColumns();
486 }
487
488 void DolphinDetailsView::slotHeaderSectionResized(int logicalIndex, int oldSize, int newSize)
489 {
490 Q_UNUSED(logicalIndex);
491 Q_UNUSED(oldSize);
492 Q_UNUSED(newSize);
493 if (QApplication::mouseButtons() & Qt::LeftButton) {
494 disableAutoResizing();
495 }
496 }
497
498 void DolphinDetailsView::disableAutoResizing()
499 {
500 m_autoResize = false;
501 }
502
503 void DolphinDetailsView::requestActivation()
504 {
505 m_controller->requestActivation();
506 }
507
508 void DolphinDetailsView::updateFont()
509 {
510 const DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
511 Q_ASSERT(settings != 0);
512
513 if (settings->useSystemFont()) {
514 m_font = KGlobalSettings::generalFont();
515 }
516 }
517
518 bool DolphinDetailsView::isZoomInPossible() const
519 {
520 DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
521 return settings->iconSize() < KIconLoader::SizeLarge;
522 }
523
524 bool DolphinDetailsView::isZoomOutPossible() const
525 {
526 DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
527 return settings->iconSize() > KIconLoader::SizeSmall;
528 }
529
530 void DolphinDetailsView::updateDecorationSize()
531 {
532 DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
533 const int iconSize = settings->iconSize();
534 setIconSize(QSize(iconSize, iconSize));
535 m_decorationSize = QSize(iconSize, iconSize);
536
537 m_controller->setZoomInPossible(isZoomInPossible());
538 m_controller->setZoomOutPossible(isZoomOutPossible());
539
540 doItemsLayout();
541 }
542
543 QPoint DolphinDetailsView::contentsPos() const
544 {
545 // implementation note: the horizonal position is ignored currently, as no
546 // horizontal scrolling is done anyway during a selection
547 const QScrollBar* scrollbar = verticalScrollBar();
548 Q_ASSERT(scrollbar != 0);
549
550 const int maxHeight = maximumViewportSize().height();
551 const int height = scrollbar->maximum() - scrollbar->minimum() + 1;
552 const int visibleHeight = model()->rowCount() + 1 - height;
553 if (visibleHeight <= 0) {
554 return QPoint(0, 0);
555 }
556
557 const int y = scrollbar->sliderPosition() * maxHeight / visibleHeight;
558 return QPoint(0, y);
559 }
560
561 KFileItemDelegate::Information DolphinDetailsView::infoForColumn(int columnIndex) const
562 {
563 KFileItemDelegate::Information info = KFileItemDelegate::NoInformation;
564
565 switch (columnIndex) {
566 case DolphinModel::Size: info = KFileItemDelegate::Size; break;
567 case DolphinModel::ModifiedTime: info = KFileItemDelegate::ModificationTime; break;
568 case DolphinModel::Permissions: info = KFileItemDelegate::Permissions; break;
569 case DolphinModel::Owner: info = KFileItemDelegate::Owner; break;
570 case DolphinModel::Group: info = KFileItemDelegate::OwnerAndGroup; break;
571 case DolphinModel::Type: info = KFileItemDelegate::FriendlyMimeType; break;
572 default: break;
573 }
574
575 return info;
576 }
577
578 void DolphinDetailsView::resizeColumns()
579 {
580 // Using the resize mode QHeaderView::ResizeToContents is too slow (it takes
581 // around 3 seconds for each (!) resize operation when having > 10000 items).
582 // This gets a problem especially when opening large directories, where several
583 // resize operations are received for showing the currently available items during
584 // loading (the application hangs around 20 seconds when loading > 10000 items).
585
586 QHeaderView* headerView = header();
587 QFontMetrics fontMetrics(viewport()->font());
588
589 int columnWidth[KDirModel::ColumnCount];
590 columnWidth[KDirModel::Size] = fontMetrics.width("00000 Items");
591 columnWidth[KDirModel::ModifiedTime] = fontMetrics.width("0000-00-00 00:00");
592 columnWidth[KDirModel::Permissions] = fontMetrics.width("xxxxxxxxxx");
593 columnWidth[KDirModel::Owner] = fontMetrics.width("xxxxxxxxxx");
594 columnWidth[KDirModel::Group] = fontMetrics.width("xxxxxxxxxx");
595 columnWidth[KDirModel::Type] = fontMetrics.width("XXXX Xxxxxxx");
596
597 int requiredWidth = 0;
598 for (int i = KDirModel::Size; i <= KDirModel::Type; ++i) {
599 if (!isColumnHidden(i)) {
600 columnWidth[i] += 20; // provide a default gap
601 requiredWidth += columnWidth[i];
602 headerView->resizeSection(i, columnWidth[i]);
603 }
604 }
605
606 // resize the name column in a way that the whole available width is used
607 columnWidth[KDirModel::Name] = viewport()->width() - requiredWidth;
608 if (columnWidth[KDirModel::Name] < 120) {
609 columnWidth[KDirModel::Name] = 120;
610 }
611 headerView->resizeSection(KDirModel::Name, columnWidth[KDirModel::Name]);
612 }
613
614 #include "dolphindetailsview.moc"