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