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 <kiconloader.h>
27 #include <QAbstractItemModel>
28 #include <QAbstractProxyModel>
29 #include <QFontMetrics>
32 #include <QStyleOptionViewItemV4>
34 DolphinFileItemDelegate::DolphinFileItemDelegate(QObject
* parent
) :
35 KFileItemDelegate(parent
),
36 m_hasMinimizedNameColumn(false)
40 DolphinFileItemDelegate::~DolphinFileItemDelegate()
44 void DolphinFileItemDelegate::paint(QPainter
* painter
,
45 const QStyleOptionViewItem
& option
,
46 const QModelIndex
& index
) const
48 const QAbstractProxyModel
* proxyModel
= static_cast<const QAbstractProxyModel
*>(index
.model());
49 const DolphinModel
* dolphinModel
= static_cast<const DolphinModel
*>(proxyModel
->sourceModel());
50 const bool isNameColumn
= (index
.column() == KDirModel::Name
);
52 if (m_hasMinimizedNameColumn
&& isNameColumn
) {
53 QStyleOptionViewItemV4
opt(option
);
54 adjustOptionWidth(opt
, proxyModel
, dolphinModel
, index
);
55 KFileItemDelegate::paint(painter
, opt
, index
);
57 KFileItemDelegate::paint(painter
, option
, index
);
60 if (dolphinModel
->hasRevisionData() && isNameColumn
) {
61 // The currently shown items are under revision control. Show the current revision
62 // state by adding an emblem.
63 const QModelIndex dirIndex
= proxyModel
->mapToSource(index
);
64 const QModelIndex revisionIndex
= dolphinModel
->index(dirIndex
.row(), DolphinModel::Revision
);
65 const QVariant data
= dolphinModel
->data(revisionIndex
, Qt::DecorationRole
);
66 const RevisionControlPlugin::RevisionState state
= static_cast<RevisionControlPlugin::RevisionState
>(data
.toInt());
68 if (state
!= RevisionControlPlugin::LocalRevision
) {
69 // TODO: extend KFileItemDelegate to be able to get the icon boundaries
70 const QRect
iconRect(option
.rect
.x(), option
.rect
.y(),
71 KIconLoader::SizeSmall
, KIconLoader::SizeSmall
);
72 const QPixmap emblem
= emblemForState(state
, iconRect
.size());
73 painter
->drawPixmap(iconRect
.x(), iconRect
.y(), emblem
);
78 int DolphinFileItemDelegate::nameColumnWidth(const QString
& name
, const QStyleOptionViewItem
& option
)
80 QFontMetrics
fontMetrics(option
.font
);
81 int width
= option
.decorationSize
.width() + fontMetrics
.width(name
) + 16;
83 const int defaultWidth
= option
.rect
.width();
84 if ((defaultWidth
> 0) && (defaultWidth
< width
)) {
90 void DolphinFileItemDelegate::adjustOptionWidth(QStyleOptionViewItemV4
& option
,
91 const QAbstractProxyModel
* proxyModel
,
92 const DolphinModel
* dolphinModel
,
93 const QModelIndex
& index
)
95 const QModelIndex dirIndex
= proxyModel
->mapToSource(index
);
96 const KFileItem item
= dolphinModel
->itemForIndex(dirIndex
);
98 // symbolic links are displayed in an italic font
100 option
.font
.setItalic(true);
103 const int width
= nameColumnWidth(item
.text(), option
);
104 option
.rect
.setWidth(width
);
108 QPixmap
DolphinFileItemDelegate::emblemForState(RevisionControlPlugin::RevisionState state
, const QSize
& size
)
110 // TODO #1: all icons that are use here will be replaced by revision control emblems provided by the
111 // Oxygen team before KDE 4.4
112 // TODO #2: cache the icons
114 case RevisionControlPlugin::LatestRevision
:
115 return KIcon("dialog-ok-apply").pixmap(size
);
116 case RevisionControlPlugin::ConflictingRevision
:
117 return KIcon("application-exit").pixmap(size
);
118 case RevisionControlPlugin::UpdateRequiredRevision
:
119 return KIcon("rating").pixmap(size
);
120 case RevisionControlPlugin::EditingRevision
:
121 return KIcon("emblem-important").pixmap(size
);