From 994095076cccdb703b2bb285d4118bd9c654fd93 Mon Sep 17 00:00:00 2001 From: Peter Penz Date: Wed, 15 Oct 2008 21:23:42 +0000 Subject: [PATCH] Assure that the item delegate draws the hover effect and the selection for the details view only above the icon + name. Open issue: The performance when selecting files by the rubberband is too slow (will be fixed before KDE 4.2). CCBUG: 165999 CCMAIL: simon@etotheipiplusone.com svn path=/trunk/KDE/kdebase/apps/; revision=871874 --- src/CMakeLists.txt | 5 ++- src/dolphindetailsview.cpp | 18 ++++---- src/dolphinfileitemdelegate.cpp | 66 +++++++++++++++++++++++++++++ src/dolphinfileitemdelegate.h | 74 +++++++++++++++++++++++++++++++++ src/dolphinview.cpp | 5 ++- src/dolphinview.h | 4 +- 6 files changed, 155 insertions(+), 17 deletions(-) create mode 100644 src/dolphinfileitemdelegate.cpp create mode 100644 src/dolphinfileitemdelegate.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 18c597a6f..5f676a4eb 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -26,14 +26,15 @@ set(dolphinprivate_LIB_SRCS dolphincolumnview.cpp dolphincolumnwidget.cpp dolphindropcontroller.cpp - dolphinsortfilterproxymodel.cpp - draganddrophelper.cpp + dolphinfileitemdelegate.cpp dolphinmodel.cpp dolphinsettings.cpp + dolphinsortfilterproxymodel.cpp dolphintooltip.cpp dolphincategorydrawer.cpp dolphinview.cpp dolphinviewactionhandler.cpp + draganddrophelper.cpp folderexpander.cpp ktooltip.cpp kballoontipdelegate.cpp diff --git a/src/dolphindetailsview.cpp b/src/dolphindetailsview.cpp index 045ea6a6a..08eef4307 100644 --- a/src/dolphindetailsview.cpp +++ b/src/dolphindetailsview.cpp @@ -22,6 +22,7 @@ #include "dolphinmodel.h" #include "dolphincontroller.h" +#include "dolphinfileitemdelegate.h" #include "dolphinsettings.h" #include "dolphinsortfilterproxymodel.h" #include "draganddrophelper.h" @@ -737,19 +738,14 @@ void DolphinDetailsView::resizeColumns() QRect DolphinDetailsView::nameColumnRect(const QModelIndex& index) const { - // TODO: The code guesses the width of the name, but it is defined - // by KFileItemDelegate. Find a way to tell the item delegate to - // use a specified width. - QRect guessedItemContentRect = visualRect(index); - const KFileItem fileItem = m_controller->itemForIndex(index); - if (!fileItem.isNull()) { - QStyleOptionViewItem itemStyle = viewOptions(); - QFontMetrics fontMetrics(itemStyle.font); - const int itemContentWidth = itemStyle.decorationSize.width() + fontMetrics.width(fileItem.name()); - guessedItemContentRect.setWidth(itemContentWidth); + QRect rect = visualRect(index); + const KFileItem item = m_controller->itemForIndex(index); + if (!item.isNull()) { + const int width = DolphinFileItemDelegate::nameColumnWidth(item.name(), viewOptions()); + rect.setWidth(width); } - return guessedItemContentRect; + return rect; } void DolphinDetailsView::setSelectionRecursive(const QModelIndex& startIndex, diff --git a/src/dolphinfileitemdelegate.cpp b/src/dolphinfileitemdelegate.cpp new file mode 100644 index 000000000..53c1d781e --- /dev/null +++ b/src/dolphinfileitemdelegate.cpp @@ -0,0 +1,66 @@ +/*************************************************************************** + * Copyright (C) 2008 by Peter Penz * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * + ***************************************************************************/ + +#include "dolphinfileitemdelegate.h" + +#include +#include +#include + +#include +#include + +DolphinFileItemDelegate::DolphinFileItemDelegate(QObject* parent) : + KFileItemDelegate(parent), + m_hasMinimizedNameColumn(false) +{ +} + +DolphinFileItemDelegate::~DolphinFileItemDelegate() +{ +} + +void DolphinFileItemDelegate::paint(QPainter* painter, + const QStyleOptionViewItem& option, + const QModelIndex& index) const +{ + if (m_hasMinimizedNameColumn && (index.column() == KDirModel::Name)) { + QStyleOptionViewItemV4 opt(option); + + const QAbstractProxyModel* proxyModel = static_cast(index.model()); + const KDirModel* dirModel = static_cast(proxyModel->sourceModel()); + const QModelIndex dirIndex = proxyModel->mapToSource(index); + const KFileItem item = dirModel->itemForIndex(dirIndex); + if (!item.isNull()) { + const int width = nameColumnWidth(item.name(), opt); + opt.rect.setWidth(width); + } + KFileItemDelegate::paint(painter, opt, index); + } else { + KFileItemDelegate::paint(painter, option, index); + } +} + +int DolphinFileItemDelegate::nameColumnWidth(const QString& name, const QStyleOptionViewItem& option) +{ + QFontMetrics fontMetrics(option.font); + return option.decorationSize.width() + fontMetrics.width(name) + 16; +} + +#include "dolphinfileitemdelegate.moc" diff --git a/src/dolphinfileitemdelegate.h b/src/dolphinfileitemdelegate.h new file mode 100644 index 000000000..589ba7178 --- /dev/null +++ b/src/dolphinfileitemdelegate.h @@ -0,0 +1,74 @@ +/*************************************************************************** + * Copyright (C) 2008 by Peter Penz * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * + ***************************************************************************/ + +#ifndef DOLPHINFILEITEMDELEGATE_H +#define DOLPHINFILEITEMDELEGATE_H + +#include + +/** + * Extends KFileItemDelegate by the ability to show the hover effect + * and the selection in a minimized way for the name column of + * the details view. + * + * Note that this is a workaround, as Qt does not support having custom + * shapes within the visual rect of an item view. The visual part of + * workaround is handled inside DolphinFileItemDelegate, the behavior + * changes are handled in DolphinDetailsView. + */ +class DolphinFileItemDelegate : public KFileItemDelegate +{ +public: + explicit DolphinFileItemDelegate(QObject* parent = 0); + virtual ~DolphinFileItemDelegate(); + + /** + * If \a minimized is true, the hover effect and the selection are + * only drawn above the icon and text of an item. Per default + * \a minimized is false, which means that the whole visual rect is + * used like in KFileItemDelegate. + */ + void setMinimizedNameColumn(bool minimized); + bool hasMinimizedNameColumn() const; + + virtual void paint(QPainter* painter, + const QStyleOptionViewItem& option, + const QModelIndex& index) const; + + /** + * Returns the minimized width of the name column for the name \a name. This method + * is also used in DolphinDetailsView to handle the selection of items correctly. + */ + static int nameColumnWidth(const QString& name, const QStyleOptionViewItem& option); + +private: + bool m_hasMinimizedNameColumn; +}; + +inline void DolphinFileItemDelegate::setMinimizedNameColumn(bool minimized) +{ + m_hasMinimizedNameColumn = minimized; +} + +inline bool DolphinFileItemDelegate::hasMinimizedNameColumn() const +{ + return m_hasMinimizedNameColumn; +} + +#endif diff --git a/src/dolphinview.cpp b/src/dolphinview.cpp index 2c4efca60..d0e1b8bd2 100644 --- a/src/dolphinview.cpp +++ b/src/dolphinview.cpp @@ -31,7 +31,6 @@ #include #include #include -#include #include #include #include @@ -52,6 +51,7 @@ #include "dolphinmodel.h" #include "dolphincolumnview.h" #include "dolphincontroller.h" +#include "dolphinfileitemdelegate.h" #include "dolphinsortfilterproxymodel.h" #include "dolphindetailsview.h" #include "dolphin_detailsmodesettings.h" @@ -1209,8 +1209,9 @@ void DolphinView::createView() m_controller->setItemView(view); - m_fileItemDelegate = new KFileItemDelegate(view); + m_fileItemDelegate = new DolphinFileItemDelegate(view); m_fileItemDelegate->setShowToolTipWhenElided(false); + m_fileItemDelegate->setMinimizedNameColumn(m_mode == DetailsView); view->setItemDelegate(m_fileItemDelegate); view->setModel(m_proxyModel); diff --git a/src/dolphinview.h b/src/dolphinview.h index d2d97aec2..2a70352e7 100644 --- a/src/dolphinview.h +++ b/src/dolphinview.h @@ -41,6 +41,7 @@ class DolphinController; class DolphinColumnView; class DolphinDetailsView; +class DolphinFileItemDelegate; class DolphinIconsView; class DolphinMainWindow; class DolphinModel; @@ -49,7 +50,6 @@ class KFilePreviewGenerator; class KAction; class KActionCollection; class KDirLister; -class KFileItemDelegate; class KUrl; class KToggleAction; class ToolTipManager; @@ -708,7 +708,7 @@ private: DolphinIconsView* m_iconsView; DolphinDetailsView* m_detailsView; DolphinColumnView* m_columnView; - KFileItemDelegate* m_fileItemDelegate; + DolphinFileItemDelegate* m_fileItemDelegate; QItemSelectionModel* m_selectionModel; DolphinModel* m_dolphinModel; -- 2.47.3