File indexing completed on 2024-11-03 03:41:04

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 void KDirOperatorDetailView::initViewItemOption(QStyleOptionViewItem *option) const
0078 {
0079     QTreeView::initViewItemOption(option);
0080     option->textElideMode = Qt::ElideMiddle;
0081 }
0082 
0083 bool KDirOperatorDetailView::event(QEvent *event)
0084 {
0085     if (event->type() == QEvent::Polish) {
0086         QHeaderView *headerView = header();
0087         headerView->setSectionResizeMode(0, QHeaderView::Stretch);
0088         headerView->setSectionResizeMode(1, QHeaderView::ResizeToContents);
0089         headerView->setSectionResizeMode(2, QHeaderView::ResizeToContents);
0090         headerView->setStretchLastSection(false);
0091         headerView->setSectionsMovable(false);
0092 
0093         setColumnHidden(KDirModel::Size, m_hideDetailColumns);
0094         setColumnHidden(KDirModel::ModifiedTime, m_hideDetailColumns);
0095         hideColumn(KDirModel::Type);
0096         hideColumn(KDirModel::Permissions);
0097         hideColumn(KDirModel::Owner);
0098         hideColumn(KDirModel::Group);
0099     } else if (event->type() == QEvent::UpdateRequest) {
0100         // A wheel movement will scroll 4 items
0101         if (model()->rowCount()) {
0102             verticalScrollBar()->setSingleStep((sizeHintForRow(0) / 3) * 4);
0103         }
0104     }
0105 
0106     return QTreeView::event(event);
0107 }
0108 
0109 void KDirOperatorDetailView::dragEnterEvent(QDragEnterEvent *event)
0110 {
0111     if (event->mimeData()->hasUrls()) {
0112         event->acceptProposedAction();
0113     }
0114 }
0115 
0116 void KDirOperatorDetailView::mousePressEvent(QMouseEvent *event)
0117 {
0118     QTreeView::mousePressEvent(event);
0119 
0120     const QModelIndex index = indexAt(event->pos());
0121     if (!index.isValid() || (index.column() != KDirModel::Name)) {
0122         const Qt::KeyboardModifiers modifiers = QApplication::keyboardModifiers();
0123         if (!(modifiers & Qt::ShiftModifier) && !(modifiers & Qt::ControlModifier)) {
0124             clearSelection();
0125         }
0126     }
0127 }
0128 
0129 void KDirOperatorDetailView::currentChanged(const QModelIndex &current, const QModelIndex &previous)
0130 {
0131     QTreeView::currentChanged(current, previous);
0132 }
0133 
0134 #include "moc_kdiroperatordetailview_p.cpp"