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>
26 #include <kstringhandler.h>
28 #include <QAbstractItemModel>
29 #include <QAbstractProxyModel>
30 #include <QFontMetrics>
33 #include <QStyleOptionViewItemV4>
35 DolphinFileItemDelegate::DolphinFileItemDelegate(QObject
* parent
) :
36 KFileItemDelegate(parent
),
37 m_hasMinimizedNameColumn(false),
41 setJobTransfersVisible(true);
44 DolphinFileItemDelegate::~DolphinFileItemDelegate()
48 void DolphinFileItemDelegate::paint(QPainter
* painter
,
49 const QStyleOptionViewItem
& option
,
50 const QModelIndex
& index
) const
52 const QAbstractProxyModel
* proxyModel
= static_cast<const QAbstractProxyModel
*>(index
.model());
53 const DolphinModel
* dolphinModel
= static_cast<const DolphinModel
*>(proxyModel
->sourceModel());
54 const bool isNameColumn
= (index
.column() == KDirModel::Name
);
56 QStyleOptionViewItemV4
opt(option
);
57 if (m_hasMinimizedNameColumn
&& isNameColumn
) {
58 adjustOptionWidth(opt
, proxyModel
, dolphinModel
, index
);
61 if (dolphinModel
->hasVersionData() && isNameColumn
) {
62 // The currently shown items are under revision control. Show the current revision
63 // state by adding an emblem and changing the text tintColor.
64 const QModelIndex dirIndex
= proxyModel
->mapToSource(index
);
65 const QModelIndex revisionIndex
= dolphinModel
->index(dirIndex
.row(), DolphinModel::Version
, dirIndex
.parent());
66 const QVariant data
= dolphinModel
->data(revisionIndex
, Qt::DecorationRole
);
67 const KVersionControlPlugin::VersionState state
= static_cast<KVersionControlPlugin::VersionState
>(data
.toInt());
69 adjustOptionTextColor(opt
, state
);
71 KFileItemDelegate::paint(painter
, opt
, index
);
73 if (state
!= KVersionControlPlugin::UnversionedVersion
) {
74 const QRect rect
= iconRect(option
, index
);
75 const QPixmap emblem
= emblemForState(state
, rect
.size());
76 painter
->drawPixmap(rect
.x(), rect
.y() + rect
.height() - emblem
.height(), emblem
);
79 KFileItemDelegate::paint(painter
, opt
, index
);
83 int DolphinFileItemDelegate::nameColumnWidth(const QString
& name
, const QStyleOptionViewItem
& option
)
85 QFontMetrics
fontMetrics(option
.font
);
86 int width
= option
.decorationSize
.width() + fontMetrics
.width(KStringHandler::preProcessWrap(name
)) + 16;
88 const int defaultWidth
= option
.rect
.width();
89 if ((defaultWidth
> 0) && (defaultWidth
< width
)) {
95 void DolphinFileItemDelegate::adjustOptionWidth(QStyleOptionViewItemV4
& option
,
96 const QAbstractProxyModel
* proxyModel
,
97 const DolphinModel
* dolphinModel
,
98 const QModelIndex
& index
)
100 const QModelIndex dirIndex
= proxyModel
->mapToSource(index
);
101 const KFileItem item
= dolphinModel
->itemForIndex(dirIndex
);
102 if (!item
.isNull()) {
103 // symbolic links are displayed in an italic font
105 option
.font
.setItalic(true);
108 const int width
= nameColumnWidth(item
.text(), option
);
109 option
.rect
.setWidth(width
);
113 void DolphinFileItemDelegate::adjustOptionTextColor(QStyleOptionViewItemV4
& option
,
114 KVersionControlPlugin::VersionState state
)
118 // Using hardcoded colors is generally a bad idea. In this case the colors just act
119 // as tint colors and are mixed with the current set text color. The tint colors
120 // have been optimized for the base colors of the corresponding Oxygen emblems.
122 case KVersionControlPlugin::UpdateRequiredVersion
: tintColor
= Qt::yellow
; break;
123 case KVersionControlPlugin::LocallyModifiedVersion
: tintColor
= Qt::green
; break;
124 case KVersionControlPlugin::AddedVersion
: tintColor
= Qt::darkGreen
; break;
125 case KVersionControlPlugin::RemovedVersion
: tintColor
= Qt::darkRed
; break;
126 case KVersionControlPlugin::ConflictingVersion
: tintColor
= Qt::red
; break;
127 case KVersionControlPlugin::UnversionedVersion
:
128 case KVersionControlPlugin::NormalVersion
:
130 // use the default text color
134 QPalette palette
= option
.palette
;
135 const QColor textColor
= palette
.color(QPalette::Text
);
136 tintColor
= QColor((tintColor
.red() + textColor
.red()) / 2,
137 (tintColor
.green() + textColor
.green()) / 2,
138 (tintColor
.blue() + textColor
.blue()) / 2,
139 (tintColor
.alpha() + textColor
.alpha()) / 2);
140 palette
.setColor(QPalette::Text
, tintColor
);
141 option
.palette
= palette
;
144 QPixmap
DolphinFileItemDelegate::emblemForState(KVersionControlPlugin::VersionState state
, const QSize
& size
) const
146 Q_ASSERT(state
<= KVersionControlPlugin::ConflictingVersion
);
147 if (m_cachedSize
!= size
) {
150 const int iconHeight
= size
.height();
151 int emblemHeight
= KIconLoader::SizeSmall
;
152 if (iconHeight
>= KIconLoader::SizeEnormous
) {
153 emblemHeight
= KIconLoader::SizeMedium
;
154 } else if (iconHeight
>= KIconLoader::SizeLarge
) {
155 emblemHeight
= KIconLoader::SizeSmallMedium
;
156 } else if (iconHeight
>= KIconLoader::SizeMedium
) {
157 emblemHeight
= KIconLoader::SizeSmall
;
159 emblemHeight
= KIconLoader::SizeSmall
/ 2;
162 const QSize
emblemSize(emblemHeight
, emblemHeight
);
163 for (int i
= KVersionControlPlugin::NormalVersion
; i
<= KVersionControlPlugin::ConflictingVersion
; ++i
) {
166 case KVersionControlPlugin::NormalVersion
:
167 iconName
= "vcs-normal";
169 case KVersionControlPlugin::UpdateRequiredVersion
:
170 iconName
= "vcs-update-required";
172 case KVersionControlPlugin::LocallyModifiedVersion
:
173 iconName
= "vcs-locally-modified";
175 case KVersionControlPlugin::LocallyModifiedUnstagedVersion
:
176 iconName
= "vcs-locally-modified-unstaged";
178 case KVersionControlPlugin::AddedVersion
:
179 iconName
= "vcs-added";
181 case KVersionControlPlugin::RemovedVersion
:
182 iconName
= "vcs-removed";
184 case KVersionControlPlugin::ConflictingVersion
:
185 iconName
= "vcs-conflicting";
187 case KVersionControlPlugin::UnversionedVersion
:
194 m_cachedEmblems
[i
] = KIcon(iconName
).pixmap(emblemSize
);
197 return m_cachedEmblems
[state
];