]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinfileitemdelegate.cpp
Just change the text color for revisioned files instead of using a completely filled...
[dolphin.git] / src / dolphinfileitemdelegate.cpp
1 /***************************************************************************
2 * Copyright (C) 2008 by Peter Penz <peter.penz@gmx.at> *
3 * *
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. *
8 * *
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. *
13 * *
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 ***************************************************************************/
19
20 #include "dolphinfileitemdelegate.h"
21
22 #include <dolphinmodel.h>
23 #include <kfileitem.h>
24
25 #include <QAbstractItemModel>
26 #include <QAbstractProxyModel>
27 #include <QFontMetrics>
28 #include <QPalette>
29 #include <QPainter>
30 #include <QStyleOptionViewItemV4>
31
32 DolphinFileItemDelegate::DolphinFileItemDelegate(QObject* parent) :
33 KFileItemDelegate(parent),
34 m_hasMinimizedNameColumn(false)
35 {
36 }
37
38 DolphinFileItemDelegate::~DolphinFileItemDelegate()
39 {
40 }
41
42 void DolphinFileItemDelegate::paint(QPainter* painter,
43 const QStyleOptionViewItem& option,
44 const QModelIndex& index) const
45 {
46 const QAbstractProxyModel* proxyModel = static_cast<const QAbstractProxyModel*>(index.model());
47 const DolphinModel* dolphinModel = static_cast<const DolphinModel*>(proxyModel->sourceModel());
48 const bool useMinimizedNameColumn = m_hasMinimizedNameColumn && (index.column() == KDirModel::Name);
49
50 if (dolphinModel->hasRevisionData()) {
51 // The currently shown items are under revision control. Show the current revision
52 // state by adjusting the text color.
53 const QModelIndex dirIndex = proxyModel->mapToSource(index);
54 const QModelIndex revisionIndex = dolphinModel->index(dirIndex.row(), DolphinModel::Revision);
55 const QVariant data = dolphinModel->data(revisionIndex, Qt::DecorationRole);
56 const DolphinModel::RevisionState state = static_cast<DolphinModel::RevisionState>(data.toInt());
57
58 if (state != DolphinModel::LocalRevision) {
59 QStyleOptionViewItemV4 opt(option);
60 // TODO: use different colors for different states
61 opt.palette.setColor(QPalette::Text, QColor(40, 150, 40));
62 if (useMinimizedNameColumn) {
63 adjustOptionWidth(opt, proxyModel, dolphinModel, index);
64 }
65 KFileItemDelegate::paint(painter, opt, index);
66 return;
67 }
68 } else if (useMinimizedNameColumn) {
69 QStyleOptionViewItemV4 opt(option);
70 adjustOptionWidth(opt, proxyModel, dolphinModel, index);
71 KFileItemDelegate::paint(painter, opt, index);
72 return;
73 }
74
75 KFileItemDelegate::paint(painter, option, index);
76 }
77
78 int DolphinFileItemDelegate::nameColumnWidth(const QString& name, const QStyleOptionViewItem& option)
79 {
80 QFontMetrics fontMetrics(option.font);
81 int width = option.decorationSize.width() + fontMetrics.width(name) + 16;
82
83 const int defaultWidth = option.rect.width();
84 if ((defaultWidth > 0) && (defaultWidth < width)) {
85 width = defaultWidth;
86 }
87 return width;
88 }
89
90 void DolphinFileItemDelegate::adjustOptionWidth(QStyleOptionViewItemV4& option,
91 const QAbstractProxyModel* proxyModel,
92 const DolphinModel* dolphinModel,
93 const QModelIndex& index)
94 {
95 const QModelIndex dirIndex = proxyModel->mapToSource(index);
96 const KFileItem item = dolphinModel->itemForIndex(dirIndex);
97 if (!item.isNull()) {
98 // symbolic links are displayed in an italic font
99 if (item.isLink()) {
100 option.font.setItalic(true);
101 }
102
103 const int width = nameColumnWidth(item.text(), option);
104 option.rect.setWidth(width);
105 }
106 }
107