add more spacing
[personal-kdebase.git] / apps / dolphin / src / dolphinfileitemdelegate.cpp
blob3fa05cbb5f808f2e9c64e1328440454bab34d2eb
1 /***************************************************************************
2 * Copyright (C) 2008 by Peter Penz <peter.penz@gmx.at> *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18 ***************************************************************************/
20 #include "dolphinfileitemdelegate.h"
22 #include <QAbstractItemModel>
23 #include <QAbstractProxyModel>
24 #include <QFontMetrics>
26 #include <kdirmodel.h>
27 #include <kfileitem.h>
29 DolphinFileItemDelegate::DolphinFileItemDelegate(QObject* parent) :
30 KFileItemDelegate(parent),
31 m_hasMinimizedNameColumn(false)
35 DolphinFileItemDelegate::~DolphinFileItemDelegate()
39 void DolphinFileItemDelegate::paint(QPainter* painter,
40 const QStyleOptionViewItem& option,
41 const QModelIndex& index) const
43 if (m_hasMinimizedNameColumn && (index.column() == KDirModel::Name)) {
44 QStyleOptionViewItemV4 opt(option);
46 const QAbstractProxyModel* proxyModel = static_cast<const QAbstractProxyModel*>(index.model());
47 const KDirModel* dirModel = static_cast<const KDirModel*>(proxyModel->sourceModel());
48 const QModelIndex dirIndex = proxyModel->mapToSource(index);
49 const KFileItem item = dirModel->itemForIndex(dirIndex);
50 if (!item.isNull()) {
51 const int width = nameColumnWidth(item.text(), opt);
52 opt.rect.setWidth(width);
54 KFileItemDelegate::paint(painter, opt, index);
55 } else {
56 KFileItemDelegate::paint(painter, option, index);
60 int DolphinFileItemDelegate::nameColumnWidth(const QString& name, const QStyleOptionViewItem& option)
62 QFontMetrics fontMetrics(option.font);
63 int width = option.decorationSize.width() + fontMetrics.width(name) + 16;
65 const int defaultWidth = option.rect.width();
66 if ((defaultWidth > 0) && (defaultWidth < width)) {
67 width = defaultWidth;
69 return width;