1/*
2 SPDX-FileCopyrightText: 2007 Peter Penz <peter.penz@gmx.at>
3
4 SPDX-License-Identifier: LGPL-2.0-only
5*/
6
7#include "kdiroperatordetailview_p.h"
8
9#include <kdirlister.h>
10#include <kdirmodel.h>
11
12#include <QApplication>
13#include <QDragEnterEvent>
14#include <QEvent>
15#include <QHeaderView>
16#include <QListView>
17#include <QMimeData>
18#include <QScrollBar>
19
20KDirOperatorDetailView::KDirOperatorDetailView(QWidget *parent)
21 : QTreeView(parent)
22 , m_hideDetailColumns(false)
23{
24 setRootIsDecorated(false);
25 setSortingEnabled(true);
26 setUniformRowHeights(true);
27 setDragDropMode(QListView::DragOnly);
28 setSelectionBehavior(QAbstractItemView::SelectRows);
29 setEditTriggers(QAbstractItemView::NoEditTriggers);
30 setVerticalScrollMode(QListView::ScrollPerPixel);
31 setHorizontalScrollMode(QListView::ScrollPerPixel);
32
33 const QFontMetrics metrics(viewport()->font());
34 const int singleStep = metrics.height() * QApplication::wheelScrollLines();
35
36 verticalScrollBar()->setSingleStep(singleStep);
37 horizontalScrollBar()->setSingleStep(singleStep);
38}
39
40KDirOperatorDetailView::~KDirOperatorDetailView()
41{
42}
43
44bool KDirOperatorDetailView::setViewMode(KFile::FileView viewMode)
45{
46 bool tree = false;
47
48 if (KFile::isDetailView(view: viewMode)) {
49 m_hideDetailColumns = false;
50 setAlternatingRowColors(true);
51 } else if (KFile::isTreeView(view: viewMode)) {
52 m_hideDetailColumns = true;
53 setAlternatingRowColors(false);
54 tree = true;
55 } else if (KFile::isDetailTreeView(view: viewMode)) {
56 m_hideDetailColumns = false;
57 setAlternatingRowColors(true);
58 tree = true;
59 } else {
60 return false;
61 }
62
63 setRootIsDecorated(tree);
64 setItemsExpandable(tree);
65 // This allows to have a horizontal scrollbar in case this view is used as
66 // a plain treeview instead of cutting off filenames, especially useful when
67 // using KDirOperator in horizontally limited parts of an app.
68 if (tree && m_hideDetailColumns) {
69 header()->setSectionResizeMode(QHeaderView::ResizeToContents);
70 } else {
71 header()->setSectionResizeMode(QHeaderView::Interactive);
72 }
73
74 return true;
75}
76
77void KDirOperatorDetailView::initViewItemOption(QStyleOptionViewItem *option) const
78{
79 QTreeView::initViewItemOption(option);
80 option->textElideMode = Qt::ElideMiddle;
81}
82
83bool KDirOperatorDetailView::event(QEvent *event)
84{
85 if (event->type() == QEvent::Polish) {
86 QHeaderView *headerView = header();
87 headerView->setSectionResizeMode(logicalIndex: 0, mode: QHeaderView::Stretch);
88 headerView->setSectionResizeMode(logicalIndex: 1, mode: QHeaderView::ResizeToContents);
89 headerView->setSectionResizeMode(logicalIndex: 2, mode: QHeaderView::ResizeToContents);
90 headerView->setStretchLastSection(false);
91 headerView->setSectionsMovable(false);
92
93 setColumnHidden(column: KDirModel::Size, hide: m_hideDetailColumns);
94 setColumnHidden(column: KDirModel::ModifiedTime, hide: m_hideDetailColumns);
95 hideColumn(column: KDirModel::Type);
96 hideColumn(column: KDirModel::Permissions);
97 hideColumn(column: KDirModel::Owner);
98 hideColumn(column: KDirModel::Group);
99 } else if (event->type() == QEvent::UpdateRequest) {
100 // A wheel movement will scroll 4 items
101 if (model()->rowCount()) {
102 verticalScrollBar()->setSingleStep((sizeHintForRow(row: 0) / 3) * 4);
103 }
104 }
105
106 return QTreeView::event(event);
107}
108
109void KDirOperatorDetailView::dragEnterEvent(QDragEnterEvent *event)
110{
111 if (event->mimeData()->hasUrls()) {
112 event->acceptProposedAction();
113 }
114}
115
116void KDirOperatorDetailView::mousePressEvent(QMouseEvent *event)
117{
118 QTreeView::mousePressEvent(event);
119
120 const QModelIndex index = indexAt(p: event->pos());
121 if (!index.isValid() || (index.column() != KDirModel::Name)) {
122 const Qt::KeyboardModifiers modifiers = QApplication::keyboardModifiers();
123 if (!(modifiers & Qt::ShiftModifier) && !(modifiers & Qt::ControlModifier)) {
124 clearSelection();
125 }
126 }
127}
128
129void KDirOperatorDetailView::currentChanged(const QModelIndex &current, const QModelIndex &previous)
130{
131 QTreeView::currentChanged(current, previous);
132}
133
134#include "moc_kdiroperatordetailview_p.cpp"
135

source code of kio/src/filewidgets/kdiroperatordetailview.cpp