]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphindetailsview.cpp
SVN_SILENT: coding style fix
[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 setState(DragSelectingState);
256
257 const int nameColumnWidth = header()->sectionSize(DolphinModel::Name);
258 QRect selRect = elasticBandRect();
259 const QRect nameColumnsRect(0, 0, nameColumnWidth, viewport()->height());
260 selRect = nameColumnsRect.intersected(selRect);
261
262 setSelection(selRect, QItemSelectionModel::Select);
263
264 }
265
266 // TODO: enable QTreeView::mouseMoveEvent(event) again, as soon
267 // as the Qt-issue #199631 has been fixed.
268 // QTreeView::mouseMoveEvent(event);
269 QAbstractItemView::mouseMoveEvent(event);
270 updateElasticBand();
271 } else {
272 // TODO: enable QTreeView::mouseMoveEvent(event) again, as soon
273 // as the Qt-issue #199631 has been fixed.
274 // QTreeView::mouseMoveEvent(event);
275 QAbstractItemView::mouseMoveEvent(event);
276 }
277
278 if (m_expandingTogglePressed) {
279 // Per default QTreeView starts either a selection or a drag operation when dragging
280 // the expanding toggle button (Qt-issue - see TODO comment in DolphinIconsView::mousePressEvent()).
281 // Turn off this behavior in Dolphin to stay predictable:
282 clearSelection();
283 setState(QAbstractItemView::NoState);
284 }
285 }
286
287 void DolphinDetailsView::mouseReleaseEvent(QMouseEvent* event)
288 {
289 const QModelIndex index = indexAt(event->pos());
290 if (index.isValid() && (index.column() == DolphinModel::Name)) {
291 QTreeView::mouseReleaseEvent(event);
292 } else {
293 // don't change the current index if the cursor is released
294 // above any other column than the name column, as the other
295 // columns act as viewport
296 const QModelIndex current = currentIndex();
297 QTreeView::mouseReleaseEvent(event);
298 selectionModel()->setCurrentIndex(current, QItemSelectionModel::Current);
299 }
300
301 m_expandingTogglePressed = false;
302 if (m_showElasticBand) {
303 setState(NoState);
304 updateElasticBand();
305 m_showElasticBand = false;
306 }
307 }
308
309 void DolphinDetailsView::startDrag(Qt::DropActions supportedActions)
310 {
311 DragAndDropHelper::startDrag(this, supportedActions);
312 m_showElasticBand = false;
313 }
314
315 void DolphinDetailsView::dragEnterEvent(QDragEnterEvent* event)
316 {
317 if (event->mimeData()->hasUrls()) {
318 event->acceptProposedAction();
319 }
320
321 if (m_showElasticBand) {
322 updateElasticBand();
323 m_showElasticBand = false;
324 }
325 }
326
327 void DolphinDetailsView::dragLeaveEvent(QDragLeaveEvent* event)
328 {
329 QTreeView::dragLeaveEvent(event);
330 setDirtyRegion(m_dropRect);
331 }
332
333 void DolphinDetailsView::dragMoveEvent(QDragMoveEvent* event)
334 {
335 QTreeView::dragMoveEvent(event);
336
337 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
338 setDirtyRegion(m_dropRect);
339 const QModelIndex index = indexAt(event->pos());
340 if (index.isValid() && (index.column() == DolphinModel::Name)) {
341 const KFileItem item = m_controller->itemForIndex(index);
342 if (!item.isNull() && item.isDir()) {
343 m_dropRect = visualRect(index);
344 } else {
345 m_dropRect.setSize(QSize()); // set as invalid
346 }
347 setDirtyRegion(m_dropRect);
348 }
349
350 if (event->mimeData()->hasUrls()) {
351 // accept url drops, independently from the destination item
352 event->acceptProposedAction();
353 }
354 }
355
356 void DolphinDetailsView::dropEvent(QDropEvent* event)
357 {
358 const QModelIndex index = indexAt(event->pos());
359 KFileItem item;
360 if (index.isValid() && (index.column() == DolphinModel::Name)) {
361 item = m_controller->itemForIndex(index);
362 }
363 m_controller->indicateDroppedUrls(item, m_controller->url(), event);
364 QTreeView::dropEvent(event);
365 }
366
367 void DolphinDetailsView::paintEvent(QPaintEvent* event)
368 {
369 QTreeView::paintEvent(event);
370 if (m_showElasticBand) {
371 // The following code has been taken from QListView
372 // and adapted to DolphinDetailsView.
373 // (C) 1992-2007 Trolltech ASA
374 QStyleOptionRubberBand opt;
375 opt.initFrom(this);
376 opt.shape = QRubberBand::Rectangle;
377 opt.opaque = false;
378 opt.rect = elasticBandRect();
379
380 QPainter painter(viewport());
381 painter.save();
382 style()->drawControl(QStyle::CE_RubberBand, &opt, &painter);
383 painter.restore();
384 }
385 }
386
387 void DolphinDetailsView::keyPressEvent(QKeyEvent* event)
388 {
389 // If the Control modifier is pressed, a multiple selection
390 // is done and DolphinDetailsView::currentChanged() may not
391 // not change the selection in a custom way.
392 m_keyPressed = !(event->modifiers() & Qt::ControlModifier);
393
394 QTreeView::keyPressEvent(event);
395 m_controller->handleKeyPressEvent(event);
396 }
397
398 void DolphinDetailsView::keyReleaseEvent(QKeyEvent* event)
399 {
400 QTreeView::keyReleaseEvent(event);
401 m_keyPressed = false;
402 }
403
404 void DolphinDetailsView::resizeEvent(QResizeEvent* event)
405 {
406 if (m_autoResize) {
407 resizeColumns();
408 }
409 QTreeView::resizeEvent(event);
410 }
411
412 void DolphinDetailsView::wheelEvent(QWheelEvent* event)
413 {
414 if (m_selectionManager != 0) {
415 m_selectionManager->reset();
416 }
417
418 // let Ctrl+wheel events propagate to the DolphinView for icon zooming
419 if (event->modifiers() & Qt::ControlModifier) {
420 event->ignore();
421 return;
422 }
423
424 QTreeView::wheelEvent(event);
425 }
426
427 void DolphinDetailsView::currentChanged(const QModelIndex& current, const QModelIndex& previous)
428 {
429 QTreeView::currentChanged(current, previous);
430
431 // Stay consistent with QListView: When changing the current index by key presses,
432 // also change the selection.
433 if (m_keyPressed) {
434 selectionModel()->select(current, QItemSelectionModel::ClearAndSelect);
435 }
436 }
437
438 bool DolphinDetailsView::eventFilter(QObject* watched, QEvent* event)
439 {
440 if ((watched == viewport()) && (event->type() == QEvent::Leave)) {
441 // if the mouse is above an item and moved very fast outside the widget,
442 // no viewportEntered() signal might be emitted although the mouse has been moved
443 // above the viewport
444 m_controller->emitViewportEntered();
445 }
446
447 return QTreeView::eventFilter(watched, event);
448 }
449
450 QModelIndex DolphinDetailsView::indexAt(const QPoint& point) const
451 {
452 // the blank portion of the name column counts as empty space
453 const QModelIndex index = QTreeView::indexAt(point);
454 const bool isAboveEmptySpace = !m_useDefaultIndexAt &&
455 (index.column() == KDirModel::Name) && !nameColumnRect(index).contains(point);
456 return isAboveEmptySpace ? QModelIndex() : index;
457 }
458
459 void DolphinDetailsView::setSelection(const QRect &rect, QItemSelectionModel::SelectionFlags command)
460 {
461 // We must override setSelection() as Qt calls it internally and when this happens
462 // we must ensure that the default indexAt() is used.
463 if (!m_showElasticBand) {
464 m_useDefaultIndexAt = true;
465 QTreeView::setSelection(rect, command);
466 m_useDefaultIndexAt = false;
467 } else {
468 // Select the items contained within the elastic band.
469
470 // TODO - would this still work if the columns could be re-ordered
471 // (which I want to implement eventually :))?
472
473 // Very naive implementation - clears all selected, then walks a tree,
474 // selecting all items whose nameColumnRect(...) intersects rect.
475
476 // Choose a sensible startIndex - a parentless index in the
477 // name column, as close to the top of the rect as we
478 // can find.
479 QRect normalisedRect = rect.normalized();
480 QModelIndex startIndex = QTreeView::indexAt(normalisedRect.topLeft());
481 if (startIndex.isValid()) {
482 while (startIndex.parent().isValid()) {
483 startIndex = startIndex.parent();
484 }
485 } else {
486 // just pick the topmost row for safety
487 model()->index(0, KDirModel::Name);
488 }
489 startIndex = model()->index(startIndex.row(), KDirModel::Name);
490 clearSelection();
491 setSelectionRecursive(startIndex, normalisedRect, command);
492 }
493 }
494
495 void DolphinDetailsView::setSortIndicatorSection(DolphinView::Sorting sorting)
496 {
497 QHeaderView* headerView = header();
498 headerView->setSortIndicator(sorting, headerView->sortIndicatorOrder());
499 }
500
501 void DolphinDetailsView::setSortIndicatorOrder(Qt::SortOrder sortOrder)
502 {
503 QHeaderView* headerView = header();
504 headerView->setSortIndicator(headerView->sortIndicatorSection(), sortOrder);
505 }
506
507 void DolphinDetailsView::synchronizeSortingState(int column)
508 {
509 // The sorting has already been changed in QTreeView if this slot is
510 // invoked, but Dolphin is not informed about this.
511 DolphinView::Sorting sorting = DolphinSortFilterProxyModel::sortingForColumn(column);
512 const Qt::SortOrder sortOrder = header()->sortIndicatorOrder();
513 m_controller->indicateSortingChange(sorting);
514 m_controller->indicateSortOrderChange(sortOrder);
515 }
516
517 void DolphinDetailsView::slotEntered(const QModelIndex& index)
518 {
519 if (index.column() == DolphinModel::Name) {
520 m_controller->emitItemEntered(index);
521 } else {
522 m_controller->emitViewportEntered();
523 }
524 }
525
526 void DolphinDetailsView::updateElasticBand()
527 {
528 if (m_showElasticBand) {
529 QRect dirtyRegion(elasticBandRect());
530 const QPoint scrollPos(horizontalScrollBar()->value(), verticalScrollBar()->value());
531 m_elasticBandDestination = viewport()->mapFromGlobal(QCursor::pos()) + scrollPos;
532 dirtyRegion = dirtyRegion.united(elasticBandRect());
533 setDirtyRegion(dirtyRegion);
534 }
535 }
536
537 QRect DolphinDetailsView::elasticBandRect() const
538 {
539 const QPoint pos(contentsPos());
540 const QPoint scrollPos(horizontalScrollBar()->value(), verticalScrollBar()->value());
541
542 const QPoint topLeft = m_elasticBandOrigin - pos - scrollPos;
543 const QPoint bottomRight = m_elasticBandDestination - pos - scrollPos;
544 return QRect(topLeft, bottomRight).normalized();
545 }
546
547 void DolphinDetailsView::setZoomLevel(int level)
548 {
549 const int size = ZoomLevelInfo::iconSizeForZoomLevel(level);
550 DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
551
552 const bool showPreview = m_controller->dolphinView()->showPreview();
553 if (showPreview) {
554 settings->setPreviewSize(size);
555 } else {
556 settings->setIconSize(size);
557 }
558
559 updateDecorationSize(showPreview);
560 }
561
562
563 void DolphinDetailsView::slotShowPreviewChanged()
564 {
565 const DolphinView* view = m_controller->dolphinView();
566 updateDecorationSize(view->showPreview());
567 }
568
569 void DolphinDetailsView::configureColumns(const QPoint& pos)
570 {
571 KMenu popup(this);
572 popup.addTitle(i18nc("@title:menu", "Columns"));
573
574 QHeaderView* headerView = header();
575 for (int i = DolphinModel::Size; i <= DolphinModel::Type; ++i) {
576 const int logicalIndex = headerView->logicalIndex(i);
577 const QString text = model()->headerData(i, Qt::Horizontal).toString();
578 QAction* action = popup.addAction(text);
579 action->setCheckable(true);
580 action->setChecked(!headerView->isSectionHidden(logicalIndex));
581 action->setData(i);
582 }
583
584 QAction* activatedAction = popup.exec(header()->mapToGlobal(pos));
585 if (activatedAction != 0) {
586 const bool show = activatedAction->isChecked();
587 const int columnIndex = activatedAction->data().toInt();
588
589 KFileItemDelegate::InformationList list = m_controller->dolphinView()->additionalInfo();
590 const KFileItemDelegate::Information info = infoForColumn(columnIndex);
591 if (show) {
592 Q_ASSERT(!list.contains(info));
593 list.append(info);
594 } else {
595 Q_ASSERT(list.contains(info));
596 const int index = list.indexOf(info);
597 list.removeAt(index);
598 }
599
600 m_controller->indicateAdditionalInfoChange(list);
601 setColumnHidden(columnIndex, !show);
602 }
603 }
604
605 void DolphinDetailsView::updateColumnVisibility()
606 {
607 const KFileItemDelegate::InformationList list = m_controller->dolphinView()->additionalInfo();
608 for (int i = DolphinModel::Size; i <= DolphinModel::Type; ++i) {
609 const KFileItemDelegate::Information info = infoForColumn(i);
610 const bool hide = !list.contains(info);
611 if (isColumnHidden(i) != hide) {
612 setColumnHidden(i, hide);
613 }
614 }
615
616 resizeColumns();
617 }
618
619 void DolphinDetailsView::slotHeaderSectionResized(int logicalIndex, int oldSize, int newSize)
620 {
621 Q_UNUSED(logicalIndex);
622 Q_UNUSED(oldSize);
623 Q_UNUSED(newSize);
624 if (QApplication::mouseButtons() & Qt::LeftButton) {
625 disableAutoResizing();
626 }
627 }
628
629 void DolphinDetailsView::slotActivationChanged(bool active)
630 {
631 setAlternatingRowColors(active);
632 }
633
634 void DolphinDetailsView::disableAutoResizing()
635 {
636 m_autoResize = false;
637 }
638
639 void DolphinDetailsView::requestActivation()
640 {
641 m_controller->requestActivation();
642 }
643
644 void DolphinDetailsView::updateFont()
645 {
646 const DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
647 Q_ASSERT(settings != 0);
648
649 if (settings->useSystemFont()) {
650 m_font = KGlobalSettings::generalFont();
651 }
652 }
653
654 void DolphinDetailsView::updateDecorationSize(bool showPreview)
655 {
656 DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
657 const int iconSize = showPreview ? settings->previewSize() : settings->iconSize();
658 setIconSize(QSize(iconSize, iconSize));
659 m_decorationSize = QSize(iconSize, iconSize);
660
661 if (m_selectionManager != 0) {
662 m_selectionManager->reset();
663 }
664
665 doItemsLayout();
666 }
667
668 QPoint DolphinDetailsView::contentsPos() const
669 {
670 // implementation note: the horizonal position is ignored currently, as no
671 // horizontal scrolling is done anyway during a selection
672 const QScrollBar* scrollbar = verticalScrollBar();
673 Q_ASSERT(scrollbar != 0);
674
675 const int maxHeight = maximumViewportSize().height();
676 const int height = scrollbar->maximum() - scrollbar->minimum() + 1;
677 const int visibleHeight = model()->rowCount() + 1 - height;
678 if (visibleHeight <= 0) {
679 return QPoint(0, 0);
680 }
681
682 const int y = scrollbar->sliderPosition() * maxHeight / visibleHeight;
683 return QPoint(0, y);
684 }
685
686 KFileItemDelegate::Information DolphinDetailsView::infoForColumn(int columnIndex) const
687 {
688 KFileItemDelegate::Information info = KFileItemDelegate::NoInformation;
689
690 switch (columnIndex) {
691 case DolphinModel::Size: info = KFileItemDelegate::Size; break;
692 case DolphinModel::ModifiedTime: info = KFileItemDelegate::ModificationTime; break;
693 case DolphinModel::Permissions: info = KFileItemDelegate::Permissions; break;
694 case DolphinModel::Owner: info = KFileItemDelegate::Owner; break;
695 case DolphinModel::Group: info = KFileItemDelegate::OwnerAndGroup; break;
696 case DolphinModel::Type: info = KFileItemDelegate::FriendlyMimeType; break;
697 default: break;
698 }
699
700 return info;
701 }
702
703 void DolphinDetailsView::resizeColumns()
704 {
705 // Using the resize mode QHeaderView::ResizeToContents is too slow (it takes
706 // around 3 seconds for each (!) resize operation when having > 10000 items).
707 // This gets a problem especially when opening large directories, where several
708 // resize operations are received for showing the currently available items during
709 // loading (the application hangs around 20 seconds when loading > 10000 items).
710
711 QHeaderView* headerView = header();
712 QFontMetrics fontMetrics(viewport()->font());
713
714 int columnWidth[KDirModel::ColumnCount];
715 columnWidth[KDirModel::Size] = fontMetrics.width("00000 Items");
716 columnWidth[KDirModel::ModifiedTime] = fontMetrics.width("0000-00-00 00:00");
717 columnWidth[KDirModel::Permissions] = fontMetrics.width("xxxxxxxxxx");
718 columnWidth[KDirModel::Owner] = fontMetrics.width("xxxxxxxxxx");
719 columnWidth[KDirModel::Group] = fontMetrics.width("xxxxxxxxxx");
720 columnWidth[KDirModel::Type] = fontMetrics.width("XXXX Xxxxxxx");
721
722 int requiredWidth = 0;
723 for (int i = KDirModel::Size; i <= KDirModel::Type; ++i) {
724 if (!isColumnHidden(i)) {
725 columnWidth[i] += 20; // provide a default gap
726 requiredWidth += columnWidth[i];
727 headerView->resizeSection(i, columnWidth[i]);
728 }
729 }
730
731 // resize the name column in a way that the whole available width is used
732 columnWidth[KDirModel::Name] = viewport()->width() - requiredWidth;
733 if (columnWidth[KDirModel::Name] < 120) {
734 columnWidth[KDirModel::Name] = 120;
735 }
736 headerView->resizeSection(KDirModel::Name, columnWidth[KDirModel::Name]);
737 }
738
739 QRect DolphinDetailsView::nameColumnRect(const QModelIndex& index) const
740 {
741 QRect rect = visualRect(index);
742 const KFileItem item = m_controller->itemForIndex(index);
743 if (!item.isNull()) {
744 const int width = DolphinFileItemDelegate::nameColumnWidth(item.name(), viewOptions());
745 rect.setWidth(width);
746 }
747
748 return rect;
749 }
750
751 void DolphinDetailsView::setSelectionRecursive(const QModelIndex& startIndex,
752 const QRect& rect,
753 QItemSelectionModel::SelectionFlags command)
754 {
755 if (!startIndex.isValid()) {
756 return;
757 }
758
759 // rect is assumed to be in viewport coordinates and normalized.
760 // Move down through the siblings of startIndex, exploring the children
761 // of any expanded nodes.
762 Q_ASSERT(rect.width() >= 0 && rect.height() >= 0);
763 QModelIndex currIndex = startIndex;
764 do {
765 const QModelIndex belowIndex = indexBelow(currIndex);
766 if (isExpanded(currIndex)) {
767 // If belowIndex exists and is above the top of rect, then we need not explore
768 // the children of currIndex as they will always be above "below". Otherwise,
769 // explore the children.
770 if (!belowIndex.isValid() || visualRect(belowIndex).bottom() >= rect.top()) {
771 setSelectionRecursive(currIndex.child(0, currIndex.column()), rect, command);
772 }
773 }
774
775 QRect itemContentRect = nameColumnRect(currIndex);
776 if (itemContentRect.top() > rect.bottom()) {
777 // All remaining items will be below itemContentRect, so we may cull.
778 return;
779 }
780
781 if (itemContentRect.intersects(rect)) {
782 selectionModel()->select(currIndex, QItemSelectionModel::Select);
783 }
784
785 currIndex = belowIndex;
786 } while (currIndex.isValid());
787 }
788
789 #include "dolphindetailsview.moc"