From a8ada5e6857d6dec25edf8ef57af9f27a91471c1 Mon Sep 17 00:00:00 2001 From: Peter Penz Date: Thu, 20 Mar 2008 16:58:59 +0000 Subject: [PATCH] QListView does not support having a margin for grids. Originally it has been tried to bypass this by overwriting QListView::visualRect(), but this has some side effects (see #155378 and #155575). The clean approach is to return a proper size hint in the file item delegate. Currently a custom item delegate has been made for Dolphin, but we'll discuss whether it makes sense providing this feature already in KFileItemDelegate... BUG: 155378 BUG: 155575 CCMAIL: fredrik@kde.org svn path=/trunk/KDE/kdebase/apps/; revision=788095 --- src/CMakeLists.txt | 1 + src/dolphinfileitemdelegate.cpp | 61 +++++++++++++++++++++++++++++++++ src/dolphinfileitemdelegate.h | 48 ++++++++++++++++++++++++++ src/dolphiniconsview.cpp | 50 ++++----------------------- src/dolphiniconsview.h | 3 -- src/dolphinview.cpp | 4 +-- src/dolphinview.h | 3 +- 7 files changed, 120 insertions(+), 50 deletions(-) create mode 100644 src/dolphinfileitemdelegate.cpp create mode 100644 src/dolphinfileitemdelegate.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index fcecacd440..6af8fc9f33 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -18,6 +18,7 @@ set(dolphinprivate_LIB_SRCS dolphincolumnview.cpp dolphincolumnwidget.cpp dolphindropcontroller.cpp + dolphinfileitemdelegate.cpp dolphinsortfilterproxymodel.cpp draganddrophelper.cpp dolphinmodel.cpp diff --git a/src/dolphinfileitemdelegate.cpp b/src/dolphinfileitemdelegate.cpp new file mode 100644 index 0000000000..a9ecc2a71d --- /dev/null +++ b/src/dolphinfileitemdelegate.cpp @@ -0,0 +1,61 @@ +/*************************************************************************** + * 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" + +DolphinFileItemDelegate::DolphinFileItemDelegate(QObject* parent) : + KFileItemDelegate(parent), + m_maxSize(0, 0) +{ +} + +DolphinFileItemDelegate::~DolphinFileItemDelegate() +{ +} + +void DolphinFileItemDelegate::setMaximumSize(const QSize& size) +{ + m_maxSize = size; +} + + +QSize DolphinFileItemDelegate::maximumSize() const +{ + return m_maxSize; +} + +QSize DolphinFileItemDelegate::sizeHint(const QStyleOptionViewItem& option, + const QModelIndex& index) const +{ + QSize size = KFileItemDelegate::sizeHint(option, index); + + const int maxWidth = m_maxSize.width(); + if ((maxWidth > 0) && (size.width() > maxWidth)) { + size.setWidth(maxWidth); + } + + const int maxHeight = m_maxSize.height(); + if ((maxHeight > 0) && (size.height() > maxHeight)) { + size.setHeight(maxHeight); + } + + return size; +} + +#include "dolphinfileitemdelegate.moc" diff --git a/src/dolphinfileitemdelegate.h b/src/dolphinfileitemdelegate.h new file mode 100644 index 0000000000..2f0167cd98 --- /dev/null +++ b/src/dolphinfileitemdelegate.h @@ -0,0 +1,48 @@ +/*************************************************************************** + * 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 + +/** + * @brief Extends KFileItemDelegate with the ability to set + * a maximum size. + */ +class DolphinFileItemDelegate : public KFileItemDelegate +{ + Q_OBJECT + +public: + explicit DolphinFileItemDelegate(QObject* parent = 0); + virtual ~DolphinFileItemDelegate(); + + void setMaximumSize(const QSize& size); + QSize maximumSize() const; + + /** @see QItemDelegate::sizeHint() */ + virtual QSize sizeHint(const QStyleOptionViewItem& option, + const QModelIndex& index) const; + +private: + QSize m_maxSize; +}; + +#endif diff --git a/src/dolphiniconsview.cpp b/src/dolphiniconsview.cpp index 45fbf25b28..6235fcd871 100644 --- a/src/dolphiniconsview.cpp +++ b/src/dolphiniconsview.cpp @@ -21,6 +21,7 @@ #include "dolphincategorydrawer.h" #include "dolphincontroller.h" +#include "dolphinfileitemdelegate.h" #include "dolphinsettings.h" #include "dolphin_iconsmodesettings.h" #include "dolphin_generalsettings.h" @@ -132,50 +133,6 @@ DolphinIconsView::~DolphinIconsView() m_categoryDrawer = 0; } -QRect DolphinIconsView::visualRect(const QModelIndex& index) const -{ - const bool leftToRightFlow = (flow() == QListView::LeftToRight); - - QRect itemRect = KCategorizedView::visualRect(index); - - const int maxWidth = m_itemSize.width(); - const int maxHeight = m_itemSize.height(); - - if (itemRect.width() > maxWidth) { - // assure that the maximum item width is not exceeded - if (leftToRightFlow) { - const int left = itemRect.left() + (itemRect.width() - maxWidth) / 2; - itemRect.setLeft(left); - } - itemRect.setWidth(maxWidth); - } - - if (itemRect.height() > maxHeight) { - // assure that the maximum item height is not exceeded - if (!leftToRightFlow) { - const int top = itemRect.top() + (itemRect.height() - maxHeight) / 2; - itemRect.setTop(top); - } - itemRect.setHeight(maxHeight); - } - - KCategorizedSortFilterProxyModel* proxyModel = dynamic_cast(model()); - if (leftToRightFlow && !proxyModel->isCategorizedModel()) { - // TODO: QListView::visualRect() calculates a wrong position of the items under - // certain circumstances (e. g. if the text is too long). This issue is bypassed - // by the following code (I'll try create a patch for Qt but as Dolphin must also work with - // Qt 4.3.0 this workaround must get applied at least for KDE 4.0). - const IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings(); - const int margin = settings->gridSpacing(); - const int gridWidth = gridSize().width(); - const int gridIndex = (itemRect.left() - margin + 1) / gridWidth; - const int centerInc = (maxWidth - itemRect.width()) / 2; - itemRect.moveLeft((gridIndex * gridWidth) + margin + centerInc); - } - - return itemRect; -} - QStyleOptionViewItem DolphinIconsView::viewOptions() const { QStyleOptionViewItem viewOptions = KCategorizedView::viewOptions(); @@ -461,6 +418,11 @@ void DolphinIconsView::updateGridSize(bool showPreview, int additionalInfoCount) m_controller->setZoomInPossible(isZoomInPossible()); m_controller->setZoomOutPossible(isZoomOutPossible()); + + DolphinFileItemDelegate* delegate = qobject_cast(itemDelegate()); + if (delegate != 0) { + delegate->setMaximumSize(m_itemSize); + } } int DolphinIconsView::additionalInfoCount() const diff --git a/src/dolphiniconsview.h b/src/dolphiniconsview.h index d96d7c7e90..e8e63eaf87 100644 --- a/src/dolphiniconsview.h +++ b/src/dolphiniconsview.h @@ -48,9 +48,6 @@ public: explicit DolphinIconsView(QWidget* parent, DolphinController* controller); virtual ~DolphinIconsView(); - /** @see QAbstractItemView::visualRect() */ - virtual QRect visualRect(const QModelIndex& index) const; - protected: virtual QStyleOptionViewItem viewOptions() const; virtual void contextMenuEvent(QContextMenuEvent* event); diff --git a/src/dolphinview.cpp b/src/dolphinview.cpp index 257809b670..4800b9e9ab 100644 --- a/src/dolphinview.cpp +++ b/src/dolphinview.cpp @@ -31,7 +31,6 @@ #include #include #include -#include #include #include #include @@ -50,6 +49,7 @@ #include "dolphinmodel.h" #include "dolphincolumnview.h" #include "dolphincontroller.h" +#include "dolphinfileitemdelegate.h" #include "dolphinsortfilterproxymodel.h" #include "dolphindetailsview.h" #include "dolphiniconsview.h" @@ -909,7 +909,7 @@ void DolphinView::createView() m_controller->setItemView(view); - m_fileItemDelegate = new KFileItemDelegate(view); + m_fileItemDelegate = new DolphinFileItemDelegate(view); view->setItemDelegate(m_fileItemDelegate); view->setModel(m_proxyModel); diff --git a/src/dolphinview.h b/src/dolphinview.h index 4f29647613..f7d95c6e28 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; @@ -635,7 +636,7 @@ private: DolphinIconsView* m_iconsView; DolphinDetailsView* m_detailsView; DolphinColumnView* m_columnView; - KFileItemDelegate* m_fileItemDelegate; + DolphinFileItemDelegate* m_fileItemDelegate; QItemSelectionModel* m_selectionModel; DolphinModel* m_dolphinModel;