]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinfileitemdelegate.cpp
The apply button in the settings dialog now disables itself when settings are applied...
[dolphin.git] / src / dolphinfileitemdelegate.cpp
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 ***************************************************************************/
19
20 #include "dolphinfileitemdelegate.h"
21
22 #include <QAbstractItemModel>
23 #include <QAbstractProxyModel>
24 #include <QFontMetrics>
25
26 #include <kdirmodel.h>
27 #include <kfileitem.h>
28
29 DolphinFileItemDelegate::DolphinFileItemDelegate(QObject* parent) :
30 KFileItemDelegate(parent),
31 m_hasMinimizedNameColumn(false)
32 {
33 }
34
35 DolphinFileItemDelegate::~DolphinFileItemDelegate()
36 {
37 }
38
39 void DolphinFileItemDelegate::paint(QPainter* painter,
40 const QStyleOptionViewItem& option,
41 const QModelIndex& index) const
42 {
43 if (m_hasMinimizedNameColumn && (index.column() == KDirModel::Name)) {
44 QStyleOptionViewItemV4 opt(option);
45
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);
53 }
54 KFileItemDelegate::paint(painter, opt, index);
55 } else {
56 KFileItemDelegate::paint(painter, option, index);
57 }
58 }
59
60 int DolphinFileItemDelegate::nameColumnWidth(const QString& name, const QStyleOptionViewItem& option)
61 {
62 QFontMetrics fontMetrics(option.font);
63 int width = option.decorationSize.width() + fontMetrics.width(name) + 16;
64
65 const int defaultWidth = option.rect.width();
66 if ((defaultWidth > 0) && (defaultWidth < width)) {
67 width = defaultWidth;
68 }
69 return width;
70 }
71