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>
25 #include <KGlobalSettings>
27 #include <KIconLoader>
28 #include <KStringHandler>
30 #include <QAbstractItemModel>
31 #include <QAbstractProxyModel>
32 #include <QFontMetrics>
35 #include <QStyleOptionViewItemV4>
37 DolphinFileItemDelegate::DolphinFileItemDelegate(QObject
* parent
) :
38 KFileItemDelegate(parent
),
39 m_hasMinimizedNameColumn(false),
42 m_cachedInactiveTextColorDirty(true)
44 setJobTransfersVisible(true);
45 connect(KGlobalSettings::self(), SIGNAL(kdisplayPaletteChanged()), this, SLOT(handleDisplayPaletteChange()));
48 DolphinFileItemDelegate::~DolphinFileItemDelegate()
52 void DolphinFileItemDelegate::paint(QPainter
* painter
,
53 const QStyleOptionViewItem
& option
,
54 const QModelIndex
& index
) const
56 const QAbstractProxyModel
* proxyModel
= static_cast<const QAbstractProxyModel
*>(index
.model());
57 const DolphinModel
* dolphinModel
= static_cast<const DolphinModel
*>(proxyModel
->sourceModel());
58 const bool isNameColumn
= (index
.column() == KDirModel::Name
);
60 QStyleOptionViewItemV4
opt(option
);
61 if (m_hasMinimizedNameColumn
&& isNameColumn
) {
62 adjustOptionWidth(opt
, proxyModel
, dolphinModel
, index
);
66 // Use the inactive text color for all columns except the name column. This indicates for the user that
67 // hovering other columns does not change the actions context.
68 QPalette palette
= opt
.palette
;
69 if (m_cachedInactiveTextColorDirty
) {
70 m_cachedInactiveTextColor
= KColorScheme(QPalette::Active
).foreground(KColorScheme::InactiveText
).color();
71 m_cachedInactiveTextColorDirty
= false;
73 palette
.setColor(QPalette::Text
, m_cachedInactiveTextColor
);
74 opt
.palette
= palette
;
77 if (dolphinModel
->hasVersionData() && isNameColumn
) {
78 // The currently shown items are under revision control. Show the current revision
79 // state by adding an emblem and changing the text tintColor.
80 const QModelIndex dirIndex
= proxyModel
->mapToSource(index
);
81 const QModelIndex revisionIndex
= dolphinModel
->index(dirIndex
.row(), DolphinModel::Version
, dirIndex
.parent());
82 const QVariant data
= dolphinModel
->data(revisionIndex
, Qt::DecorationRole
);
83 const KVersionControlPlugin::VersionState state
= static_cast<KVersionControlPlugin::VersionState
>(data
.toInt());
85 adjustOptionTextColor(opt
, state
);
87 KFileItemDelegate::paint(painter
, opt
, index
);
89 if (state
!= KVersionControlPlugin::UnversionedVersion
) {
90 const QRect rect
= iconRect(option
, index
);
91 const QPixmap emblem
= emblemForState(state
, rect
.size());
92 painter
->drawPixmap(rect
.x(), rect
.y() + rect
.height() - emblem
.height(), emblem
);
95 KFileItemDelegate::paint(painter
, opt
, index
);
99 int DolphinFileItemDelegate::nameColumnWidth(const QString
& name
, const QStyleOptionViewItem
& option
)
101 QFontMetrics
fontMetrics(option
.font
);
102 int width
= option
.decorationSize
.width() + fontMetrics
.width(KStringHandler::preProcessWrap(name
)) + 16;
104 const int defaultWidth
= option
.rect
.width();
105 if ((defaultWidth
> 0) && (defaultWidth
< width
)) {
106 width
= defaultWidth
;
111 void DolphinFileItemDelegate::handleDisplayPaletteChange()
113 m_cachedInactiveTextColorDirty
= true;
116 void DolphinFileItemDelegate::adjustOptionWidth(QStyleOptionViewItemV4
& option
,
117 const QAbstractProxyModel
* proxyModel
,
118 const DolphinModel
* dolphinModel
,
119 const QModelIndex
& index
)
121 const QModelIndex dirIndex
= proxyModel
->mapToSource(index
);
122 const KFileItem item
= dolphinModel
->itemForIndex(dirIndex
);
123 if (!item
.isNull()) {
124 // symbolic links are displayed in an italic font
126 option
.font
.setItalic(true);
129 const int width
= nameColumnWidth(item
.text(), option
);
130 option
.rect
.setWidth(width
);
134 void DolphinFileItemDelegate::adjustOptionTextColor(QStyleOptionViewItemV4
& option
,
135 KVersionControlPlugin::VersionState state
)
139 // Using hardcoded colors is generally a bad idea. In this case the colors just act
140 // as tint colors and are mixed with the current set text color. The tint colors
141 // have been optimized for the base colors of the corresponding Oxygen emblems.
143 case KVersionControlPlugin::UpdateRequiredVersion
: tintColor
= Qt::yellow
; break;
144 case KVersionControlPlugin::LocallyModifiedUnstagedVersion
: tintColor
= Qt::darkGreen
; break;
145 case KVersionControlPlugin::LocallyModifiedVersion
: tintColor
= Qt::green
; break;
146 case KVersionControlPlugin::AddedVersion
: tintColor
= Qt::green
; break;
147 case KVersionControlPlugin::RemovedVersion
: tintColor
= Qt::darkRed
; break;
148 case KVersionControlPlugin::ConflictingVersion
: tintColor
= Qt::red
; break;
149 case KVersionControlPlugin::UnversionedVersion
:
150 case KVersionControlPlugin::NormalVersion
:
152 // use the default text color
156 QPalette palette
= option
.palette
;
157 const QColor textColor
= palette
.color(QPalette::Text
);
158 tintColor
= QColor((tintColor
.red() + textColor
.red()) / 2,
159 (tintColor
.green() + textColor
.green()) / 2,
160 (tintColor
.blue() + textColor
.blue()) / 2,
161 (tintColor
.alpha() + textColor
.alpha()) / 2);
162 palette
.setColor(QPalette::Text
, tintColor
);
163 option
.palette
= palette
;
166 QPixmap
DolphinFileItemDelegate::emblemForState(KVersionControlPlugin::VersionState state
, const QSize
& size
) const
168 Q_ASSERT(state
<= KVersionControlPlugin::LocallyModifiedUnstagedVersion
);
169 if (m_cachedSize
!= size
) {
172 const int iconHeight
= size
.height();
173 int emblemHeight
= KIconLoader::SizeSmall
;
174 if (iconHeight
>= KIconLoader::SizeEnormous
) {
175 emblemHeight
= KIconLoader::SizeMedium
;
176 } else if (iconHeight
>= KIconLoader::SizeLarge
) {
177 emblemHeight
= KIconLoader::SizeSmallMedium
;
178 } else if (iconHeight
>= KIconLoader::SizeMedium
) {
179 emblemHeight
= KIconLoader::SizeSmall
;
181 emblemHeight
= KIconLoader::SizeSmall
/ 2;
184 const QSize
emblemSize(emblemHeight
, emblemHeight
);
185 for (int i
= KVersionControlPlugin::NormalVersion
; i
<= KVersionControlPlugin::LocallyModifiedUnstagedVersion
; ++i
) {
188 case KVersionControlPlugin::NormalVersion
:
189 iconName
= "vcs-normal";
191 case KVersionControlPlugin::UpdateRequiredVersion
:
192 iconName
= "vcs-update-required";
194 case KVersionControlPlugin::LocallyModifiedVersion
:
195 iconName
= "vcs-locally-modified";
197 case KVersionControlPlugin::LocallyModifiedUnstagedVersion
:
198 iconName
= "vcs-locally-modified-unstaged";
200 case KVersionControlPlugin::AddedVersion
:
201 iconName
= "vcs-added";
203 case KVersionControlPlugin::RemovedVersion
:
204 iconName
= "vcs-removed";
206 case KVersionControlPlugin::ConflictingVersion
:
207 iconName
= "vcs-conflicting";
209 case KVersionControlPlugin::UnversionedVersion
:
216 m_cachedEmblems
[i
] = KIcon(iconName
).pixmap(emblemSize
);
219 return m_cachedEmblems
[state
];