File indexing completed on 2024-09-15 12:00:02
0001 /* 0002 SPDX-FileCopyrightText: 2007 Peter Penz <peter.penz@gmx.at> 0003 0004 SPDX-License-Identifier: LGPL-2.0-only 0005 */ 0006 0007 #include "kdiroperatordetailview_p.h" 0008 0009 #include <kdirlister.h> 0010 #include <kdirmodel.h> 0011 0012 #include <QApplication> 0013 #include <QDragEnterEvent> 0014 #include <QEvent> 0015 #include <QHeaderView> 0016 #include <QListView> 0017 #include <QMimeData> 0018 #include <QScrollBar> 0019 0020 KDirOperatorDetailView::KDirOperatorDetailView(QWidget *parent) 0021 : QTreeView(parent) 0022 , m_hideDetailColumns(false) 0023 { 0024 setRootIsDecorated(false); 0025 setSortingEnabled(true); 0026 setUniformRowHeights(true); 0027 setDragDropMode(QListView::DragOnly); 0028 setSelectionBehavior(QAbstractItemView::SelectRows); 0029 setEditTriggers(QAbstractItemView::NoEditTriggers); 0030 setVerticalScrollMode(QListView::ScrollPerPixel); 0031 setHorizontalScrollMode(QListView::ScrollPerPixel); 0032 0033 const QFontMetrics metrics(viewport()->font()); 0034 const int singleStep = metrics.height() * QApplication::wheelScrollLines(); 0035 0036 verticalScrollBar()->setSingleStep(singleStep); 0037 horizontalScrollBar()->setSingleStep(singleStep); 0038 } 0039 0040 KDirOperatorDetailView::~KDirOperatorDetailView() 0041 { 0042 } 0043 0044 bool KDirOperatorDetailView::setViewMode(KFile::FileView viewMode) 0045 { 0046 bool tree = false; 0047 0048 if (KFile::isDetailView(viewMode)) { 0049 m_hideDetailColumns = false; 0050 setAlternatingRowColors(true); 0051 } else if (KFile::isTreeView(viewMode)) { 0052 m_hideDetailColumns = true; 0053 setAlternatingRowColors(false); 0054 tree = true; 0055 } else if (KFile::isDetailTreeView(viewMode)) { 0056 m_hideDetailColumns = false; 0057 setAlternatingRowColors(true); 0058 tree = true; 0059 } else { 0060 return false; 0061 } 0062 0063 setRootIsDecorated(tree); 0064 setItemsExpandable(tree); 0065 // This allows to have a horizontal scrollbar in case this view is used as 0066 // a plain treeview instead of cutting off filenames, especially useful when 0067 // using KDirOperator in horizontally limited parts of an app. 0068 if (tree && m_hideDetailColumns) { 0069 header()->setSectionResizeMode(QHeaderView::ResizeToContents); 0070 } else { 0071 header()->setSectionResizeMode(QHeaderView::Interactive); 0072 } 0073 0074 return true; 0075 } 0076 0077 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) 0078 void KDirOperatorDetailView::initViewItemOption(QStyleOptionViewItem *option) const 0079 { 0080 QTreeView::initViewItemOption(option); 0081 option->textElideMode = Qt::ElideMiddle; 0082 } 0083 #else 0084 QStyleOptionViewItem KDirOperatorDetailView::viewOptions() const 0085 { 0086 QStyleOptionViewItem viewOptions = QTreeView::viewOptions(); 0087 viewOptions.textElideMode = Qt::ElideMiddle; 0088 return viewOptions; 0089 } 0090 #endif 0091 0092 bool KDirOperatorDetailView::event(QEvent *event) 0093 { 0094 if (event->type() == QEvent::Polish) { 0095 QHeaderView *headerView = header(); 0096 headerView->setSectionResizeMode(0, QHeaderView::Stretch); 0097 headerView->setSectionResizeMode(1, QHeaderView::ResizeToContents); 0098 headerView->setSectionResizeMode(2, QHeaderView::ResizeToContents); 0099 headerView->setStretchLastSection(false); 0100 headerView->setSectionsMovable(false); 0101 0102 setColumnHidden(KDirModel::Size, m_hideDetailColumns); 0103 setColumnHidden(KDirModel::ModifiedTime, m_hideDetailColumns); 0104 hideColumn(KDirModel::Type); 0105 hideColumn(KDirModel::Permissions); 0106 hideColumn(KDirModel::Owner); 0107 hideColumn(KDirModel::Group); 0108 } else if (event->type() == QEvent::UpdateRequest) { 0109 // A wheel movement will scroll 4 items 0110 if (model()->rowCount()) { 0111 verticalScrollBar()->setSingleStep((sizeHintForRow(0) / 3) * 4); 0112 } 0113 } 0114 0115 return QTreeView::event(event); 0116 } 0117 0118 void KDirOperatorDetailView::dragEnterEvent(QDragEnterEvent *event) 0119 { 0120 if (event->mimeData()->hasUrls()) { 0121 event->acceptProposedAction(); 0122 } 0123 } 0124 0125 void KDirOperatorDetailView::mousePressEvent(QMouseEvent *event) 0126 { 0127 QTreeView::mousePressEvent(event); 0128 0129 const QModelIndex index = indexAt(event->pos()); 0130 if (!index.isValid() || (index.column() != KDirModel::Name)) { 0131 const Qt::KeyboardModifiers modifiers = QApplication::keyboardModifiers(); 0132 if (!(modifiers & Qt::ShiftModifier) && !(modifiers & Qt::ControlModifier)) { 0133 clearSelection(); 0134 } 0135 } 0136 } 0137 0138 void KDirOperatorDetailView::currentChanged(const QModelIndex ¤t, const QModelIndex &previous) 0139 { 0140 QTreeView::currentChanged(current, previous); 0141 } 0142 0143 #include "moc_kdiroperatordetailview_p.cpp"