1 /***************************************************************************
2 * Copyright (C) 2008 by Peter Penz <peter.penz19@gmail.com> *
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 <KColorScheme>
26 #include <KIconLoader>
27 #include <KStringHandler>
29 #include <QAbstractItemModel>
30 #include <QAbstractProxyModel>
31 #include <QFontMetrics>
34 #include <QStyleOptionViewItemV4>
36 DolphinFileItemDelegate::DolphinFileItemDelegate(QObject
* parent
) :
37 KFileItemDelegate(parent
),
38 m_hasMinimizedNameColumn(false),
42 setJobTransfersVisible(true);
45 DolphinFileItemDelegate::~DolphinFileItemDelegate()
49 void DolphinFileItemDelegate::paint(QPainter
* painter
,
50 const QStyleOptionViewItem
& option
,
51 const QModelIndex
& index
) const
53 const QAbstractProxyModel
* proxyModel
= static_cast<const QAbstractProxyModel
*>(index
.model());
54 const DolphinModel
* dolphinModel
= static_cast<const DolphinModel
*>(proxyModel
->sourceModel());
55 const bool isNameColumn
= (index
.column() == KDirModel::Name
);
57 QStyleOptionViewItemV4
opt(option
);
58 if (m_hasMinimizedNameColumn
&& isNameColumn
) {
59 adjustOptionWidth(opt
, proxyModel
, dolphinModel
, index
);
63 // Use the inactive text color for all columns except the name column. This indicates for the user that
64 // hovering other columns does not change the actions context.
65 QPalette palette
= opt
.palette
;
66 const QColor textColor
= KColorScheme(QPalette::Active
).foreground(KColorScheme::InactiveText
).color();
67 palette
.setColor(QPalette::Text
, textColor
);
68 opt
.palette
= palette
;
71 if (dolphinModel
->hasVersionData() && isNameColumn
) {
72 // The currently shown items are under revision control. Show the current revision
73 // state by adding an emblem and changing the text tintColor.
74 const QModelIndex dirIndex
= proxyModel
->mapToSource(index
);
75 const QModelIndex revisionIndex
= dolphinModel
->index(dirIndex
.row(), DolphinModel::Version
, dirIndex
.parent());
76 const QVariant data
= dolphinModel
->data(revisionIndex
, Qt::DecorationRole
);
77 const KVersionControlPlugin::VersionState state
= static_cast<KVersionControlPlugin::VersionState
>(data
.toInt());
79 adjustOptionTextColor(opt
, state
);
81 KFileItemDelegate::paint(painter
, opt
, index
);
83 if (state
!= KVersionControlPlugin::UnversionedVersion
) {
84 const QRect rect
= iconRect(option
, index
);
85 const QPixmap emblem
= emblemForState(state
, rect
.size());
86 painter
->drawPixmap(rect
.x(), rect
.y() + rect
.height() - emblem
.height(), emblem
);
89 KFileItemDelegate::paint(painter
, opt
, index
);
93 int DolphinFileItemDelegate::nameColumnWidth(const QString
& name
, const QStyleOptionViewItem
& option
)
95 QFontMetrics
fontMetrics(option
.font
);
96 int width
= option
.decorationSize
.width() + fontMetrics
.width(KStringHandler::preProcessWrap(name
)) + 16;
98 const int defaultWidth
= option
.rect
.width();
99 if ((defaultWidth
> 0) && (defaultWidth
< width
)) {
100 width
= defaultWidth
;
105 void DolphinFileItemDelegate::adjustOptionWidth(QStyleOptionViewItemV4
& option
,
106 const QAbstractProxyModel
* proxyModel
,
107 const DolphinModel
* dolphinModel
,
108 const QModelIndex
& index
)
110 const QModelIndex dirIndex
= proxyModel
->mapToSource(index
);
111 const KFileItem item
= dolphinModel
->itemForIndex(dirIndex
);
112 if (!item
.isNull()) {
113 // symbolic links are displayed in an italic font
115 option
.font
.setItalic(true);
118 const int width
= nameColumnWidth(item
.text(), option
);
119 option
.rect
.setWidth(width
);
123 void DolphinFileItemDelegate::adjustOptionTextColor(QStyleOptionViewItemV4
& option
,
124 KVersionControlPlugin::VersionState state
)
128 // Using hardcoded colors is generally a bad idea. In this case the colors just act
129 // as tint colors and are mixed with the current set text color. The tint colors
130 // have been optimized for the base colors of the corresponding Oxygen emblems.
132 case KVersionControlPlugin::UpdateRequiredVersion
: tintColor
= Qt::yellow
; break;
133 case KVersionControlPlugin::LocallyModifiedUnstagedVersion
: tintColor
= Qt::darkRed
; break;
134 case KVersionControlPlugin::LocallyModifiedVersion
: tintColor
= Qt::green
; break;
135 case KVersionControlPlugin::AddedVersion
: tintColor
= Qt::darkGreen
; break;
136 case KVersionControlPlugin::RemovedVersion
: tintColor
= Qt::darkRed
; break;
137 case KVersionControlPlugin::ConflictingVersion
: tintColor
= Qt::red
; break;
138 case KVersionControlPlugin::UnversionedVersion
:
139 case KVersionControlPlugin::NormalVersion
:
141 // use the default text color
145 QPalette palette
= option
.palette
;
146 const QColor textColor
= palette
.color(QPalette::Text
);
147 tintColor
= QColor((tintColor
.red() + textColor
.red()) / 2,
148 (tintColor
.green() + textColor
.green()) / 2,
149 (tintColor
.blue() + textColor
.blue()) / 2,
150 (tintColor
.alpha() + textColor
.alpha()) / 2);
151 palette
.setColor(QPalette::Text
, tintColor
);
152 option
.palette
= palette
;
155 QPixmap
DolphinFileItemDelegate::emblemForState(KVersionControlPlugin::VersionState state
, const QSize
& size
) const
157 Q_ASSERT(state
<= KVersionControlPlugin::LocallyModifiedUnstagedVersion
);
158 if (m_cachedSize
!= size
) {
161 const int iconHeight
= size
.height();
162 int emblemHeight
= KIconLoader::SizeSmall
;
163 if (iconHeight
>= KIconLoader::SizeEnormous
) {
164 emblemHeight
= KIconLoader::SizeMedium
;
165 } else if (iconHeight
>= KIconLoader::SizeLarge
) {
166 emblemHeight
= KIconLoader::SizeSmallMedium
;
167 } else if (iconHeight
>= KIconLoader::SizeMedium
) {
168 emblemHeight
= KIconLoader::SizeSmall
;
170 emblemHeight
= KIconLoader::SizeSmall
/ 2;
173 const QSize
emblemSize(emblemHeight
, emblemHeight
);
174 for (int i
= KVersionControlPlugin::NormalVersion
; i
<= KVersionControlPlugin::LocallyModifiedUnstagedVersion
; ++i
) {
177 case KVersionControlPlugin::NormalVersion
:
178 iconName
= "vcs-normal";
180 case KVersionControlPlugin::UpdateRequiredVersion
:
181 iconName
= "vcs-update-required";
183 case KVersionControlPlugin::LocallyModifiedVersion
:
184 iconName
= "vcs-locally-modified";
186 case KVersionControlPlugin::LocallyModifiedUnstagedVersion
:
187 iconName
= "vcs-locally-modified-unstaged";
189 case KVersionControlPlugin::AddedVersion
:
190 iconName
= "vcs-added";
192 case KVersionControlPlugin::RemovedVersion
:
193 iconName
= "vcs-removed";
195 case KVersionControlPlugin::ConflictingVersion
:
196 iconName
= "vcs-conflicting";
198 case KVersionControlPlugin::UnversionedVersion
:
205 m_cachedEmblems
[i
] = KIcon(iconName
).pixmap(emblemSize
);
208 return m_cachedEmblems
[state
];