1 /***************************************************************************
2 * Copyright (C) 2008 by Peter Penz <peter.penz@gmx.at> *
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. *
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. *
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 <dolphinmodel.h>
23 #include <kfileitem.h>
25 #include <QAbstractItemModel>
26 #include <QAbstractProxyModel>
27 #include <QFontMetrics>
30 DolphinFileItemDelegate::DolphinFileItemDelegate(QObject
* parent
) :
31 KFileItemDelegate(parent
),
32 m_hasMinimizedNameColumn(false)
36 DolphinFileItemDelegate::~DolphinFileItemDelegate()
40 void DolphinFileItemDelegate::paint(QPainter
* painter
,
41 const QStyleOptionViewItem
& option
,
42 const QModelIndex
& index
) const
44 const QAbstractProxyModel
* proxyModel
= static_cast<const QAbstractProxyModel
*>(index
.model());
45 const DolphinModel
* dolphinModel
= static_cast<const DolphinModel
*>(proxyModel
->sourceModel());
47 if (m_hasMinimizedNameColumn
&& (index
.column() == KDirModel::Name
)) {
48 QStyleOptionViewItemV4
opt(option
);
50 const QModelIndex dirIndex
= proxyModel
->mapToSource(index
);
51 const KFileItem item
= dolphinModel
->itemForIndex(dirIndex
);
53 // symbolic links are displayed in an italic font
55 opt
.font
.setItalic(true);
58 const int width
= nameColumnWidth(item
.text(), opt
);
59 opt
.rect
.setWidth(width
);
61 KFileItemDelegate::paint(painter
, opt
, index
);
63 KFileItemDelegate::paint(painter
, option
, index
);
66 if (dolphinModel
->hasRevisionData()) {
67 // The currently shown items are under revision control. Show the current revision
68 // state above the decoration.
69 const QModelIndex dirIndex
= proxyModel
->mapToSource(index
);
70 const QModelIndex revisionIndex
= dolphinModel
->index(dirIndex
.row(), DolphinModel::Revision
);
71 const QVariant data
= dolphinModel
->data(revisionIndex
, Qt::DecorationRole
);
72 const DolphinModel::RevisionState state
= static_cast<DolphinModel::RevisionState
>(data
.toInt());
74 if (state
!= DolphinModel::LocalRevision
) {
75 // TODO: The following code is just a proof of concept. Icons will be used later...
76 QColor
color(200, 0, 0, 32);
78 case DolphinModel::LatestRevision
: color
= QColor(0, 180, 0, 32); break;
82 painter
->fillRect(option
.rect
, color
);
87 int DolphinFileItemDelegate::nameColumnWidth(const QString
& name
, const QStyleOptionViewItem
& option
)
89 QFontMetrics
fontMetrics(option
.font
);
90 int width
= option
.decorationSize
.width() + fontMetrics
.width(name
) + 16;
92 const int defaultWidth
= option
.rect
.width();
93 if ((defaultWidth
> 0) && (defaultWidth
< width
)) {