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
This commit is contained in:
Peter Penz 2008-03-20 16:58:59 +00:00
parent f738a9ff2b
commit a8ada5e685
7 changed files with 120 additions and 50 deletions

View file

@ -18,6 +18,7 @@ set(dolphinprivate_LIB_SRCS
dolphincolumnview.cpp
dolphincolumnwidget.cpp
dolphindropcontroller.cpp
dolphinfileitemdelegate.cpp
dolphinsortfilterproxymodel.cpp
draganddrophelper.cpp
dolphinmodel.cpp

View file

@ -0,0 +1,61 @@
/***************************************************************************
* Copyright (C) 2008 by Peter Penz <peter.penz@gmx.at> *
* *
* 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"

View file

@ -0,0 +1,48 @@
/***************************************************************************
* Copyright (C) 2008 by Peter Penz <peter.penz@gmx.at> *
* *
* 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 <kfileitemdelegate.h>
/**
* @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

View file

@ -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<KCategorizedSortFilterProxyModel*>(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<DolphinFileItemDelegate*>(itemDelegate());
if (delegate != 0) {
delegate->setMaximumSize(m_itemSize);
}
}
int DolphinIconsView::additionalInfoCount() const

View file

@ -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);

View file

@ -31,7 +31,6 @@
#include <kactioncollection.h>
#include <kcolorscheme.h>
#include <kdirlister.h>
#include <kfileitemdelegate.h>
#include <kiconeffect.h>
#include <klocale.h>
#include <kio/deletejob.h>
@ -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);

View file

@ -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;