]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphindetailsview.cpp
First step for having a details view, where only the icon + name act as selectable...
[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 #include "zoomlevelinfo.h"
31
32 #include "dolphin_detailsmodesettings.h"
33 #include "dolphin_generalsettings.h"
34
35 #include <kdirmodel.h>
36 #include <klocale.h>
37 #include <kmenu.h>
38
39 #include <QAbstractProxyModel>
40 #include <QAction>
41 #include <QApplication>
42 #include <QHeaderView>
43 #include <QRubberBand>
44 #include <QPainter>
45 #include <QScrollBar>
46
47 DolphinDetailsView::DolphinDetailsView(QWidget* parent, DolphinController* controller) :
48 QTreeView(parent),
49 m_autoResize(true),
50 m_expandingTogglePressed(false),
51 m_keyPressed(false),
52 m_useDefaultIndexAt(true),
53 m_controller(controller),
54 m_selectionManager(0),
55 m_font(),
56 m_decorationSize(),
57 m_showElasticBand(false),
58 m_elasticBandOrigin(),
59 m_elasticBandDestination()
60 {
61 const DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
62 Q_ASSERT(settings != 0);
63 Q_ASSERT(controller != 0);
64
65 setLayoutDirection(Qt::LeftToRight);
66 setAcceptDrops(true);
67 setSortingEnabled(true);
68 setUniformRowHeights(true);
69 setSelectionBehavior(SelectItems);
70 setDragDropMode(QAbstractItemView::DragDrop);
71 setDropIndicatorShown(false);
72 setAlternatingRowColors(true);
73 setRootIsDecorated(settings->expandableFolders());
74 setItemsExpandable(settings->expandableFolders());
75 setEditTriggers(QAbstractItemView::NoEditTriggers);
76
77 setMouseTracking(true);
78
79 const ViewProperties props(controller->url());
80 setSortIndicatorSection(props.sorting());
81 setSortIndicatorOrder(props.sortOrder());
82
83 QHeaderView* headerView = header();
84 connect(headerView, SIGNAL(sectionClicked(int)),
85 this, SLOT(synchronizeSortingState(int)));
86 headerView->setContextMenuPolicy(Qt::CustomContextMenu);
87 connect(headerView, SIGNAL(customContextMenuRequested(const QPoint&)),
88 this, SLOT(configureColumns(const QPoint&)));
89 connect(headerView, SIGNAL(sectionResized(int, int, int)),
90 this, SLOT(slotHeaderSectionResized(int, int, int)));
91 connect(headerView, SIGNAL(sectionHandleDoubleClicked(int)),
92 this, SLOT(disableAutoResizing()));
93
94 connect(parent, SIGNAL(sortingChanged(DolphinView::Sorting)),
95 this, SLOT(setSortIndicatorSection(DolphinView::Sorting)));
96 connect(parent, SIGNAL(sortOrderChanged(Qt::SortOrder)),
97 this, SLOT(setSortIndicatorOrder(Qt::SortOrder)));
98
99 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
100 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
101 // necessary connecting the signal 'singleClick()' or 'doubleClick' and to handle the
102 // RETURN-key in keyPressEvent().
103 if (KGlobalSettings::singleClick()) {
104 connect(this, SIGNAL(clicked(const QModelIndex&)),
105 controller, SLOT(triggerItem(const QModelIndex&)));
106 } else {
107 connect(this, SIGNAL(doubleClicked(const QModelIndex&)),
108 controller, SLOT(triggerItem(const QModelIndex&)));
109 }
110
111 if (DolphinSettings::instance().generalSettings()->showSelectionToggle()) {
112 m_selectionManager = new SelectionManager(this);
113 connect(m_selectionManager, SIGNAL(selectionChanged()),
114 this, SLOT(requestActivation()));
115 connect(m_controller, SIGNAL(urlChanged(const KUrl&)),
116 m_selectionManager, SLOT(reset()));
117 }
118
119 connect(this, SIGNAL(entered(const QModelIndex&)),
120 this, SLOT(slotEntered(const QModelIndex&)));
121 connect(this, SIGNAL(viewportEntered()),
122 controller, SLOT(emitViewportEntered()));
123 connect(controller, SIGNAL(zoomLevelChanged(int)),
124 this, SLOT(setZoomLevel(int)));
125 connect(controller->dolphinView(), SIGNAL(additionalInfoChanged()),
126 this, SLOT(updateColumnVisibility()));
127 connect(controller, SIGNAL(activationChanged(bool)),
128 this, SLOT(slotActivationChanged(bool)));
129
130 if (settings->useSystemFont()) {
131 m_font = KGlobalSettings::generalFont();
132 } else {
133 m_font = QFont(settings->fontFamily(),
134 settings->fontSize(),
135 settings->fontWeight(),
136 settings->italicFont());
137 }
138
139 setVerticalScrollMode(QTreeView::ScrollPerPixel);
140 setHorizontalScrollMode(QTreeView::ScrollPerPixel);
141
142 const DolphinView* view = controller->dolphinView();
143 connect(view, SIGNAL(showPreviewChanged()),
144 this, SLOT(slotShowPreviewChanged()));
145
146 updateDecorationSize(view->showPreview());
147
148 setFocus();
149 viewport()->installEventFilter(this);
150
151 connect(KGlobalSettings::self(), SIGNAL(kdisplayFontChanged()),
152 this, SLOT(updateFont()));
153
154 m_useDefaultIndexAt = false;
155 }
156
157 DolphinDetailsView::~DolphinDetailsView()
158 {
159 }
160
161 bool DolphinDetailsView::event(QEvent* event)
162 {
163 if (event->type() == QEvent::Polish) {
164 QHeaderView* headerView = header();
165 headerView->setResizeMode(QHeaderView::Interactive);
166 headerView->setMovable(false);
167
168 updateColumnVisibility();
169
170 hideColumn(DolphinModel::Rating);
171 hideColumn(DolphinModel::Tags);
172 } else if (event->type() == QEvent::UpdateRequest) {
173 // a wheel movement will scroll 4 items
174 if (model()->rowCount() > 0) {
175 verticalScrollBar()->setSingleStep((sizeHintForRow(0) / 3) * 4);
176 }
177 }
178
179 return QTreeView::event(event);
180 }
181
182 QStyleOptionViewItem DolphinDetailsView::viewOptions() const
183 {
184 QStyleOptionViewItem viewOptions = QTreeView::viewOptions();
185 viewOptions.font = m_font;
186 viewOptions.showDecorationSelected = true;
187 viewOptions.decorationSize = m_decorationSize;
188 return viewOptions;
189 }
190
191 void DolphinDetailsView::contextMenuEvent(QContextMenuEvent* event)
192 {
193 QTreeView::contextMenuEvent(event);
194 m_controller->triggerContextMenuRequest(event->pos());
195 }
196
197 void DolphinDetailsView::mousePressEvent(QMouseEvent* event)
198 {
199 m_controller->requestActivation();
200
201 const QModelIndex current = currentIndex();
202 QTreeView::mousePressEvent(event);
203
204 m_expandingTogglePressed = false;
205 const QModelIndex index = indexAt(event->pos());
206 const bool updateState = index.isValid() &&
207 (index.column() == DolphinModel::Name) &&
208 (event->button() == Qt::LeftButton);
209 if (updateState) {
210 // TODO: See comment in DolphinIconsView::mousePressEvent(). Only update
211 // the state if no expanding/collapsing area has been hit:
212 const QRect rect = visualRect(index);
213 if (event->pos().x() >= rect.x() + indentation()) {
214 setState(QAbstractItemView::DraggingState);
215 } else {
216 m_expandingTogglePressed = true;
217 }
218 }
219
220 if (!index.isValid() || (index.column() != DolphinModel::Name)) {
221 // the mouse press is done somewhere outside the filename column
222 if (QApplication::mouseButtons() & Qt::MidButton) {
223 m_controller->replaceUrlByClipboard();
224 }
225
226 const Qt::KeyboardModifiers modifier = QApplication::keyboardModifiers();
227 if (!(modifier & Qt::ShiftModifier) && !(modifier & Qt::ControlModifier)) {
228 clearSelection();
229 }
230
231 // restore the current index, other columns are handled as viewport area
232 selectionModel()->setCurrentIndex(current, QItemSelectionModel::Current);
233 }
234
235 if ((event->button() == Qt::LeftButton) && !m_expandingTogglePressed) {
236 m_showElasticBand = true;
237 const QPoint pos = contentsPos();
238 const QPoint scrollPos(horizontalScrollBar()->value(), verticalScrollBar()->value());
239 m_elasticBandOrigin = event->pos() + pos + scrollPos;
240 m_elasticBandDestination = m_elasticBandOrigin;
241 }
242 }
243
244 void DolphinDetailsView::mouseMoveEvent(QMouseEvent* event)
245 {
246 if (m_showElasticBand) {
247 const QPoint mousePos = event->pos();
248 const QModelIndex index = indexAt(mousePos);
249 if (!index.isValid()) {
250 // the destination of the selection rectangle is above the viewport. In this
251 // case QTreeView does no selection at all, which is not the wanted behavior
252 // in Dolphin -> select all items within the elastic band rectangle
253 clearSelection();
254 setState(DragSelectingState);
255
256 const int nameColumnWidth = header()->sectionSize(DolphinModel::Name);
257 QRect selRect = elasticBandRect();
258 const QRect nameColumnsRect(0, 0, nameColumnWidth, viewport()->height());
259 selRect = nameColumnsRect.intersected(selRect);
260
261 setSelection(selRect, QItemSelectionModel::Select);
262
263 }
264
265 // TODO: enable QTreeView::mouseMoveEvent(event) again, as soon
266 // as the Qt-issue #199631 has been fixed.
267 // QTreeView::mouseMoveEvent(event);
268 QAbstractItemView::mouseMoveEvent(event);
269 updateElasticBand();
270 } else {
271 // TODO: enable QTreeView::mouseMoveEvent(event) again, as soon
272 // as the Qt-issue #199631 has been fixed.
273 // QTreeView::mouseMoveEvent(event);
274 QAbstractItemView::mouseMoveEvent(event);
275 }
276
277 if (m_expandingTogglePressed) {
278 // Per default QTreeView starts either a selection or a drag operation when dragging
279 // the expanding toggle button (Qt-issue - see TODO comment in DolphinIconsView::mousePressEvent()).
280 // Turn off this behavior in Dolphin to stay predictable:
281 clearSelection();
282 setState(QAbstractItemView::NoState);
283 }
284 }
285
286 void DolphinDetailsView::mouseReleaseEvent(QMouseEvent* event)
287 {
288 const QModelIndex index = indexAt(event->pos());
289 if (index.isValid() && (index.column() == DolphinModel::Name)) {
290 QTreeView::mouseReleaseEvent(event);
291 } else {
292 // don't change the current index if the cursor is released
293 // above any other column than the name column, as the other
294 // columns act as viewport
295 const QModelIndex current = currentIndex();
296 QTreeView::mouseReleaseEvent(event);
297 selectionModel()->setCurrentIndex(current, QItemSelectionModel::Current);
298 }
299
300 m_expandingTogglePressed = false;
301 if (m_showElasticBand) {
302 setState(NoState);
303 updateElasticBand();
304 m_showElasticBand = false;
305 }
306 }
307
308 void DolphinDetailsView::startDrag(Qt::DropActions supportedActions)
309 {
310 DragAndDropHelper::startDrag(this, supportedActions);
311 m_showElasticBand = false;
312 }
313
314 void DolphinDetailsView::dragEnterEvent(QDragEnterEvent* event)
315 {
316 if (event->mimeData()->hasUrls()) {
317 event->acceptProposedAction();
318 }
319
320 if (m_showElasticBand) {
321 updateElasticBand();
322 m_showElasticBand = false;
323 }
324 }
325
326 void DolphinDetailsView::dragLeaveEvent(QDragLeaveEvent* event)
327 {
328 QTreeView::dragLeaveEvent(event);
329 setDirtyRegion(m_dropRect);
330 }
331
332 void DolphinDetailsView::dragMoveEvent(QDragMoveEvent* event)
333 {
334 QTreeView::dragMoveEvent(event);
335
336 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
337 setDirtyRegion(m_dropRect);
338 const QModelIndex index = indexAt(event->pos());
339 if (index.isValid() && (index.column() == DolphinModel::Name)) {
340 const KFileItem item = m_controller->itemForIndex(index);
341 if (!item.isNull() && item.isDir()) {
342 m_dropRect = visualRect(index);
343 } else {
344 m_dropRect.setSize(QSize()); // set as invalid
345 }
346 setDirtyRegion(m_dropRect);
347 }
348
349 if (event->mimeData()->hasUrls()) {
350 // accept url drops, independently from the destination item
351 event->acceptProposedAction();
352 }
353 }
354
355 void DolphinDetailsView::dropEvent(QDropEvent* event)
356 {
357 const QModelIndex index = indexAt(event->pos());
358 KFileItem item;
359 if (index.isValid() && (index.column() == DolphinModel::Name)) {
360 item = m_controller->itemForIndex(index);
361 }
362 m_controller->indicateDroppedUrls(item, m_controller->url(), event);
363 QTreeView::dropEvent(event);
364 }
365
366 void DolphinDetailsView::paintEvent(QPaintEvent* event)
367 {
368 QTreeView::paintEvent(event);
369 if (m_showElasticBand) {
370 // The following code has been taken from QListView
371 // and adapted to DolphinDetailsView.
372 // (C) 1992-2007 Trolltech ASA
373 QStyleOptionRubberBand opt;
374 opt.initFrom(this);
375 opt.shape = QRubberBand::Rectangle;
376 opt.opaque = false;
377 opt.rect = elasticBandRect();
378
379 QPainter painter(viewport());
380 painter.save();
381 style()->drawControl(QStyle::CE_RubberBand, &opt, &painter);
382 painter.restore();
383 }
384 }
385
386 void DolphinDetailsView::keyPressEvent(QKeyEvent* event)
387 {
388 // If the Control modifier is pressed, a multiple selection
389 // is done and DolphinDetailsView::currentChanged() may not
390 // not change the selection in a custom way.
391 m_keyPressed = !(event->modifiers() & Qt::ControlModifier);
392
393 QTreeView::keyPressEvent(event);
394 m_controller->handleKeyPressEvent(event);
395 }
396
397 void DolphinDetailsView::keyReleaseEvent(QKeyEvent* event)
398 {
399 QTreeView::keyReleaseEvent(event);
400 m_keyPressed = false;
401 }
402
403 void DolphinDetailsView::resizeEvent(QResizeEvent* event)
404 {
405 if (m_autoResize) {
406 resizeColumns();
407 }
408 QTreeView::resizeEvent(event);
409 }
410
411 void DolphinDetailsView::wheelEvent(QWheelEvent* event)
412 {
413 if (m_selectionManager != 0) {
414 m_selectionManager->reset();
415 }
416
417 // let Ctrl+wheel events propagate to the DolphinView for icon zooming
418 if (event->modifiers() & Qt::ControlModifier) {
419 event->ignore();
420 return;
421 }
422
423 QTreeView::wheelEvent(event);
424 }
425
426 void DolphinDetailsView::currentChanged(const QModelIndex& current, const QModelIndex& previous)
427 {
428 QTreeView::currentChanged(current, previous);
429
430 // Stay consistent with QListView: When changing the current index by key presses,
431 // also change the selection.
432 if (m_keyPressed) {
433 selectionModel()->select(current, QItemSelectionModel::ClearAndSelect);
434 }
435 }
436
437 bool DolphinDetailsView::eventFilter(QObject* watched, QEvent* event)
438 {
439 if ((watched == viewport()) && (event->type() == QEvent::Leave)) {
440 // if the mouse is above an item and moved very fast outside the widget,
441 // no viewportEntered() signal might be emitted although the mouse has been moved
442 // above the viewport
443 m_controller->emitViewportEntered();
444 }
445
446 return QTreeView::eventFilter(watched, event);
447 }
448
449 QModelIndex DolphinDetailsView::indexAt(const QPoint& point) const
450 {
451 // the blank portion of the name column counts as empty space
452 const QModelIndex index = QTreeView::indexAt(point);
453 const bool isAboveEmptySpace = !m_useDefaultIndexAt &&
454 (index.column() == KDirModel::Name) && !nameColumnRect(index).contains(point);
455 return isAboveEmptySpace ? QModelIndex() : index;
456 }
457
458 void DolphinDetailsView::setSelection(const QRect &rect, QItemSelectionModel::SelectionFlags command)
459 {
460 // We must override setSelection() as Qt calls it internally and when this happens
461 // we must ensure that the default indexAt() is used.
462 if (!m_showElasticBand) {
463 m_useDefaultIndexAt = true;
464 QTreeView::setSelection(rect, command);
465 m_useDefaultIndexAt = false;
466 } else {
467 // Select the items contained within the elastic band.
468
469 // TODO - would this still work if the columns could be re-ordered
470 // (which I want to implement eventually :))?
471
472 // Very naive implementation - clears all selected, then walks a tree,
473 // selecting all items whose nameColumnRect(...) intersects rect.
474
475 // Choose a sensible startIndex - a parentless index in the
476 // name column, as close to the top of the rect as we
477 // can find.
478 QRect normalisedRect = rect.normalized();
479 QModelIndex startIndex = QTreeView::indexAt(normalisedRect.topLeft());
480 if (startIndex.isValid()) {
481 while (startIndex.parent().isValid()) {
482 startIndex = startIndex.parent();
483 }
484 } else {
485 // just pick the topmost row for safety
486 model()->index(0, KDirModel::Name);
487 }
488 startIndex = model()->index(startIndex.row(), KDirModel::Name);
489 clearSelection();
490 setSelectionRecursive(startIndex, normalisedRect, command);
491 }
492 }
493
494 void DolphinDetailsView::setSortIndicatorSection(DolphinView::Sorting sorting)
495 {
496 QHeaderView* headerView = header();
497 headerView->setSortIndicator(sorting, headerView->sortIndicatorOrder());
498 }
499
500 void DolphinDetailsView::setSortIndicatorOrder(Qt::SortOrder sortOrder)
501 {
502 QHeaderView* headerView = header();
503 headerView->setSortIndicator(headerView->sortIndicatorSection(), sortOrder);
504 }
505
506 void DolphinDetailsView::synchronizeSortingState(int column)
507 {
508 // The sorting has already been changed in QTreeView if this slot is
509 // invoked, but Dolphin is not informed about this.
510 DolphinView::Sorting sorting = DolphinSortFilterProxyModel::sortingForColumn(column);
511 const Qt::SortOrder sortOrder = header()->sortIndicatorOrder();
512 m_controller->indicateSortingChange(sorting);
513 m_controller->indicateSortOrderChange(sortOrder);
514 }
515
516 void DolphinDetailsView::slotEntered(const QModelIndex& index)
517 {
518 if (index.column() == DolphinModel::Name) {
519 m_controller->emitItemEntered(index);
520 } else {
521 m_controller->emitViewportEntered();
522 }
523 }
524
525 void DolphinDetailsView::updateElasticBand()
526 {
527 if (m_showElasticBand) {
528 QRect dirtyRegion(elasticBandRect());
529 const QPoint scrollPos(horizontalScrollBar()->value(), verticalScrollBar()->value());
530 m_elasticBandDestination = viewport()->mapFromGlobal(QCursor::pos()) + scrollPos;
531 dirtyRegion = dirtyRegion.united(elasticBandRect());
532 setDirtyRegion(dirtyRegion);
533 }
534 }
535
536 QRect DolphinDetailsView::elasticBandRect() const
537 {
538 const QPoint pos(contentsPos());
539 const QPoint scrollPos(horizontalScrollBar()->value(), verticalScrollBar()->value());
540
541 const QPoint topLeft = m_elasticBandOrigin - pos - scrollPos;
542 const QPoint bottomRight = m_elasticBandDestination - pos - scrollPos;
543 return QRect(topLeft, bottomRight).normalized();
544 }
545
546 void DolphinDetailsView::setZoomLevel(int level)
547 {
548 const int size = ZoomLevelInfo::iconSizeForZoomLevel(level);
549 DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
550
551 const bool showPreview = m_controller->dolphinView()->showPreview();
552 if (showPreview) {
553 settings->setPreviewSize(size);
554 } else {
555 settings->setIconSize(size);
556 }
557
558 updateDecorationSize(showPreview);
559 }
560
561
562 void DolphinDetailsView::slotShowPreviewChanged()
563 {
564 const DolphinView* view = m_controller->dolphinView();
565 updateDecorationSize(view->showPreview());
566 }
567
568 void DolphinDetailsView::configureColumns(const QPoint& pos)
569 {
570 KMenu popup(this);
571 popup.addTitle(i18nc("@title:menu", "Columns"));
572
573 QHeaderView* headerView = header();
574 for (int i = DolphinModel::Size; i <= DolphinModel::Type; ++i) {
575 const int logicalIndex = headerView->logicalIndex(i);
576 const QString text = model()->headerData(i, Qt::Horizontal).toString();
577 QAction* action = popup.addAction(text);
578 action->setCheckable(true);
579 action->setChecked(!headerView->isSectionHidden(logicalIndex));
580 action->setData(i);
581 }
582
583 QAction* activatedAction = popup.exec(header()->mapToGlobal(pos));
584 if (activatedAction != 0) {
585 const bool show = activatedAction->isChecked();
586 const int columnIndex = activatedAction->data().toInt();
587
588 KFileItemDelegate::InformationList list = m_controller->dolphinView()->additionalInfo();
589 const KFileItemDelegate::Information info = infoForColumn(columnIndex);
590 if (show) {
591 Q_ASSERT(!list.contains(info));
592 list.append(info);
593 } else {
594 Q_ASSERT(list.contains(info));
595 const int index = list.indexOf(info);
596 list.removeAt(index);
597 }
598
599 m_controller->indicateAdditionalInfoChange(list);
600 setColumnHidden(columnIndex, !show);
601 }
602 }
603
604 void DolphinDetailsView::updateColumnVisibility()
605 {
606 const KFileItemDelegate::InformationList list = m_controller->dolphinView()->additionalInfo();
607 for (int i = DolphinModel::Size; i <= DolphinModel::Type; ++i) {
608 const KFileItemDelegate::Information info = infoForColumn(i);
609 const bool hide = !list.contains(info);
610 if (isColumnHidden(i) != hide) {
611 setColumnHidden(i, hide);
612 }
613 }
614
615 resizeColumns();
616 }
617
618 void DolphinDetailsView::slotHeaderSectionResized(int logicalIndex, int oldSize, int newSize)
619 {
620 Q_UNUSED(logicalIndex);
621 Q_UNUSED(oldSize);
622 Q_UNUSED(newSize);
623 if (QApplication::mouseButtons() & Qt::LeftButton) {
624 disableAutoResizing();
625 }
626 }
627
628 void DolphinDetailsView::slotActivationChanged(bool active)
629 {
630 setAlternatingRowColors(active);
631 }
632
633 void DolphinDetailsView::disableAutoResizing()
634 {
635 m_autoResize = false;
636 }
637
638 void DolphinDetailsView::requestActivation()
639 {
640 m_controller->requestActivation();
641 }
642
643 void DolphinDetailsView::updateFont()
644 {
645 const DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
646 Q_ASSERT(settings != 0);
647
648 if (settings->useSystemFont()) {
649 m_font = KGlobalSettings::generalFont();
650 }
651 }
652
653 void DolphinDetailsView::updateDecorationSize(bool showPreview)
654 {
655 DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
656 const int iconSize = showPreview ? settings->previewSize() : settings->iconSize();
657 setIconSize(QSize(iconSize, iconSize));
658 m_decorationSize = QSize(iconSize, iconSize);
659
660 if (m_selectionManager != 0) {
661 m_selectionManager->reset();
662 }
663
664 doItemsLayout();
665 }
666
667 QPoint DolphinDetailsView::contentsPos() const
668 {
669 // implementation note: the horizonal position is ignored currently, as no
670 // horizontal scrolling is done anyway during a selection
671 const QScrollBar* scrollbar = verticalScrollBar();
672 Q_ASSERT(scrollbar != 0);
673
674 const int maxHeight = maximumViewportSize().height();
675 const int height = scrollbar->maximum() - scrollbar->minimum() + 1;
676 const int visibleHeight = model()->rowCount() + 1 - height;
677 if (visibleHeight <= 0) {
678 return QPoint(0, 0);
679 }
680
681 const int y = scrollbar->sliderPosition() * maxHeight / visibleHeight;
682 return QPoint(0, y);
683 }
684
685 KFileItemDelegate::Information DolphinDetailsView::infoForColumn(int columnIndex) const
686 {
687 KFileItemDelegate::Information info = KFileItemDelegate::NoInformation;
688
689 switch (columnIndex) {
690 case DolphinModel::Size: info = KFileItemDelegate::Size; break;
691 case DolphinModel::ModifiedTime: info = KFileItemDelegate::ModificationTime; break;
692 case DolphinModel::Permissions: info = KFileItemDelegate::Permissions; break;
693 case DolphinModel::Owner: info = KFileItemDelegate::Owner; break;
694 case DolphinModel::Group: info = KFileItemDelegate::OwnerAndGroup; break;
695 case DolphinModel::Type: info = KFileItemDelegate::FriendlyMimeType; break;
696 default: break;
697 }
698
699 return info;
700 }
701
702 void DolphinDetailsView::resizeColumns()
703 {
704 // Using the resize mode QHeaderView::ResizeToContents is too slow (it takes
705 // around 3 seconds for each (!) resize operation when having > 10000 items).
706 // This gets a problem especially when opening large directories, where several
707 // resize operations are received for showing the currently available items during
708 // loading (the application hangs around 20 seconds when loading > 10000 items).
709
710 QHeaderView* headerView = header();
711 QFontMetrics fontMetrics(viewport()->font());
712
713 int columnWidth[KDirModel::ColumnCount];
714 columnWidth[KDirModel::Size] = fontMetrics.width("00000 Items");
715 columnWidth[KDirModel::ModifiedTime] = fontMetrics.width("0000-00-00 00:00");
716 columnWidth[KDirModel::Permissions] = fontMetrics.width("xxxxxxxxxx");
717 columnWidth[KDirModel::Owner] = fontMetrics.width("xxxxxxxxxx");
718 columnWidth[KDirModel::Group] = fontMetrics.width("xxxxxxxxxx");
719 columnWidth[KDirModel::Type] = fontMetrics.width("XXXX Xxxxxxx");
720
721 int requiredWidth = 0;
722 for (int i = KDirModel::Size; i <= KDirModel::Type; ++i) {
723 if (!isColumnHidden(i)) {
724 columnWidth[i] += 20; // provide a default gap
725 requiredWidth += columnWidth[i];
726 headerView->resizeSection(i, columnWidth[i]);
727 }
728 }
729
730 // resize the name column in a way that the whole available width is used
731 columnWidth[KDirModel::Name] = viewport()->width() - requiredWidth;
732 if (columnWidth[KDirModel::Name] < 120) {
733 columnWidth[KDirModel::Name] = 120;
734 }
735 headerView->resizeSection(KDirModel::Name, columnWidth[KDirModel::Name]);
736 }
737
738 QRect DolphinDetailsView::nameColumnRect(const QModelIndex& index) const
739 {
740 // TODO: The code guesses the width of the name, but it is defined
741 // by KFileItemDelegate. Find a way to tell the item delegate to
742 // use a specified width.
743 QRect guessedItemContentRect = visualRect(index);
744 const KFileItem fileItem = m_controller->itemForIndex(index);
745 if (!fileItem.isNull()) {
746 QStyleOptionViewItem itemStyle = viewOptions();
747 QFontMetrics fontMetrics(itemStyle.font);
748 const int itemContentWidth = itemStyle.decorationSize.width() + fontMetrics.width(fileItem.name());
749 guessedItemContentRect.setWidth(itemContentWidth);
750 }
751
752 return guessedItemContentRect;
753 }
754
755 void DolphinDetailsView::setSelectionRecursive(const QModelIndex& startIndex,
756 const QRect& rect,
757 QItemSelectionModel::SelectionFlags command)
758 {
759 if (!startIndex.isValid()) {
760 return;
761 }
762
763 // rect is assumed to be in viewport coordinates and normalized.
764 // Move down through the siblings of startIndex, exploring the children
765 // of any expanded nodes.
766 Q_ASSERT(rect.width() >= 0 && rect.height() >= 0);
767 QModelIndex currIndex = startIndex;
768 do {
769 const QModelIndex belowIndex = indexBelow(currIndex);
770 if (isExpanded(currIndex)) {
771 // If belowIndex exists and is above the top of rect, then we need not explore
772 // the children of currIndex as they will always be above "below". Otherwise,
773 // explore the children.
774 if (!belowIndex.isValid() || visualRect(belowIndex).bottom() >= rect.top()) {
775 setSelectionRecursive(currIndex.child(0, currIndex.column()), rect, command);
776 }
777 }
778
779 QRect itemContentRect = nameColumnRect(currIndex);
780 if (itemContentRect.top() > rect.bottom()) {
781 // All remaining items will be below itemContentRect, so we may cull.
782 return;
783 }
784
785 if (itemContentRect.intersects(rect)) {
786 selectionModel()->select(currIndex, QItemSelectionModel::Select);
787 }
788
789 currIndex = belowIndex;
790 } while (currIndex.isValid());
791 }
792
793 #include "dolphindetailsview.moc"