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