Changeset View
Changeset View
Standalone View
Standalone View
src/filewidgets/kdiroperatoriconview.cpp
- This file was added.
| 1 | /***************************************************************************** | ||||
|---|---|---|---|---|---|
| 2 | * Copyright (C) 2007 by Peter Penz <peter.penz@gmx.at> * | ||||
| 3 | * Copyright (C) 2019 by Méven Car <meven.car@kdemail.net> * | ||||
| 4 | * * | ||||
| 5 | * This library is free software; you can redistribute it and/or * | ||||
| 6 | * modify it under the terms of the GNU Library General Public * | ||||
| 7 | * License version 2 as published by the Free Software Foundation. * | ||||
| 8 | * * | ||||
| 9 | * This library is distributed in the hope that it will be useful, * | ||||
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * | ||||
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * | ||||
| 12 | * Library General Public License for more details. * | ||||
| 13 | * * | ||||
| 14 | * You should have received a copy of the GNU Library General Public License * | ||||
| 15 | * along with this library; see the file COPYING.LIB. If not, write to * | ||||
| 16 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * | ||||
| 17 | * Boston, MA 02110-1301, USA. * | ||||
| 18 | *****************************************************************************/ | ||||
| 19 | | ||||
| 20 | #include "kdiroperatoriconview_p.h" | ||||
| 21 | #include <QWidget> | ||||
| 22 | #include <QListView> | ||||
| 23 | #include <QDragEnterEvent> | ||||
| 24 | #include <QApplication> | ||||
| 25 | #include <QScrollBar> | ||||
| 26 | #include <QMimeData> | ||||
| 27 | | ||||
| 28 | #include <KIconLoader> | ||||
| 29 | #include <KFileItemDelegate> | ||||
| 30 | | ||||
| 31 | KDirOperatorIconView::KDirOperatorIconView(QWidget *parent, Kind kind) : | ||||
| 32 | QListView(parent) | ||||
| 33 | { | ||||
| 34 | viewKind = kind; | ||||
| 35 | | ||||
| 36 | setViewMode(QListView::IconMode); | ||||
| 37 | setResizeMode(QListView::Adjust); | ||||
| 38 | setSpacing(0); | ||||
| 39 | setMovement(QListView::Static); | ||||
| 40 | setDragDropMode(QListView::DragOnly); | ||||
| 41 | setVerticalScrollMode(QListView::ScrollPerPixel); | ||||
| 42 | setHorizontalScrollMode(QListView::ScrollPerPixel); | ||||
| 43 | setEditTriggers(QAbstractItemView::NoEditTriggers); | ||||
| 44 | setWordWrap(true); | ||||
| 45 | if (viewKind == KDirOperatorIconView::Icon) { | ||||
| 46 | // Icons View | ||||
| 47 | setFlow(QListView::LeftToRight); | ||||
| 48 | } else { | ||||
| 49 | // Compact View | ||||
| 50 | setFlow(QListView::TopToBottom); | ||||
| 51 | setIconSize(QSize(KIconLoader::SizeSmall, KIconLoader::SizeSmall)); | ||||
| 52 | } | ||||
| 53 | // triggers a layout update when the icon size has changed | ||||
| 54 | connect(this, &QAbstractItemView::iconSizeChanged, this, &KDirOperatorIconView::updateLayout); | ||||
| 55 | } | ||||
| 56 | | ||||
| 57 | KDirOperatorIconView::~KDirOperatorIconView() | ||||
| 58 | { | ||||
| 59 | } | ||||
| 60 | | ||||
| 61 | QStyleOptionViewItem KDirOperatorIconView::viewOptions() const | ||||
| 62 | { | ||||
| 63 | QStyleOptionViewItem viewOptions = QListView::viewOptions(); | ||||
| 64 | viewOptions.showDecorationSelected = true; | ||||
| 65 | if (viewKind == KDirOperatorIconView::Compact) { | ||||
| 66 | viewOptions.decorationPosition = QStyleOptionViewItem::Left; | ||||
| 67 | viewOptions.displayAlignment = Qt::AlignLeft | Qt::AlignVCenter; | ||||
| 68 | } else { | ||||
| 69 | viewOptions.decorationPosition = QStyleOptionViewItem::Top; | ||||
| 70 | viewOptions.displayAlignment = Qt::AlignCenter; | ||||
| 71 | } | ||||
| 72 | | ||||
| 73 | return viewOptions; | ||||
| 74 | } | ||||
| 75 | | ||||
| 76 | void KDirOperatorIconView::dragEnterEvent(QDragEnterEvent *event) | ||||
| 77 | { | ||||
| 78 | if (event->mimeData()->hasUrls()) { | ||||
| 79 | event->acceptProposedAction(); | ||||
| 80 | } | ||||
| 81 | } | ||||
| 82 | | ||||
| 83 | void KDirOperatorIconView::mousePressEvent(QMouseEvent *event) | ||||
| 84 | { | ||||
| 85 | if (!indexAt(event->pos()).isValid()) { | ||||
| 86 | const Qt::KeyboardModifiers modifiers = QApplication::keyboardModifiers(); | ||||
| 87 | if (!(modifiers & Qt::ShiftModifier) && !(modifiers & Qt::ControlModifier)) { | ||||
| 88 | clearSelection(); | ||||
| 89 | } | ||||
| 90 | } | ||||
| 91 | | ||||
| 92 | QListView::mousePressEvent(event); | ||||
| 93 | } | ||||
| 94 | | ||||
| 95 | void KDirOperatorIconView::wheelEvent(QWheelEvent *event) | ||||
| 96 | { | ||||
| 97 | QListView::wheelEvent(event); | ||||
| 98 | | ||||
| 99 | // apply the vertical wheel event to the horizontal scrollbar, as | ||||
| 100 | // the items are aligned from left to right | ||||
| 101 | if (event->orientation() == Qt::Vertical) { | ||||
| 102 | QWheelEvent horizEvent(event->pos(), | ||||
| 103 | event->delta(), | ||||
| 104 | event->buttons(), | ||||
| 105 | event->modifiers(), | ||||
| 106 | Qt::Horizontal); | ||||
| 107 | QApplication::sendEvent(horizontalScrollBar(), &horizEvent); | ||||
| 108 | } | ||||
| 109 | } | ||||
| 110 | | ||||
| 111 | void KDirOperatorIconView::resizeEvent(QResizeEvent *event) { | ||||
| 112 | Q_UNUSED(event); | ||||
| 113 | updateLayout(); | ||||
| 114 | } | ||||
| 115 | | ||||
| 116 | void KDirOperatorIconView::updateLayout() { | ||||
| 117 | if (viewKind == Kind::Compact) { | ||||
| 118 | setGridSize(QSize()); | ||||
| 119 | KFileItemDelegate *delegate = qobject_cast<KFileItemDelegate *>(itemDelegate()); | ||||
| 120 | if (delegate) { | ||||
| 121 | delegate->setMaximumSize(QSize()); | ||||
| 122 | } | ||||
| 123 | } else { | ||||
| 124 | const QFontMetrics metrics(viewport()->font()); | ||||
| 125 | | ||||
| 126 | const int height = iconSize().height() + metrics.height() * 2.5; | ||||
| 127 | const int minWidth = qMax(height, metrics.height() * 5); | ||||
| 128 | | ||||
| 129 | const int scrollBarWidth = verticalScrollBar()->sizeHint().width(); | ||||
| 130 | | ||||
| 131 | // Subtract 1 px to prevent flickering when resizing the window | ||||
| 132 | // For Oxygen a column is missing after showing the dialog without resizing it, | ||||
| 133 | // therefore subtract 4 more (scaled) pixels | ||||
| 134 | const int viewPortWidth = contentsRect().width() - scrollBarWidth - 1 - 4 * devicePixelRatioF(); | ||||
| 135 | const int itemsInRow = qMax(1, viewPortWidth / minWidth); | ||||
| 136 | const int remainingWidth = viewPortWidth - (minWidth * itemsInRow); | ||||
| 137 | const int width = minWidth + (remainingWidth / itemsInRow); | ||||
| 138 | | ||||
| 139 | const QSize itemSize(width, height); | ||||
| 140 | | ||||
| 141 | setGridSize(itemSize); | ||||
| 142 | KFileItemDelegate *delegate = qobject_cast<KFileItemDelegate *>(itemDelegate()); | ||||
| 143 | if (delegate) { | ||||
| 144 | delegate->setMaximumSize(itemSize); | ||||
| 145 | } | ||||
| 146 | } | ||||
| 147 | } | ||||