]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinfileitemdelegate.cpp
Enable Dolphin to show the revision states of files that are under revision control...
[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 <QPainter>
29
30 DolphinFileItemDelegate::DolphinFileItemDelegate(QObject* parent) :
31 KFileItemDelegate(parent),
32 m_hasMinimizedNameColumn(false)
33 {
34 }
35
36 DolphinFileItemDelegate::~DolphinFileItemDelegate()
37 {
38 }
39
40 void DolphinFileItemDelegate::paint(QPainter* painter,
41 const QStyleOptionViewItem& option,
42 const QModelIndex& index) const
43 {
44 const QAbstractProxyModel* proxyModel = static_cast<const QAbstractProxyModel*>(index.model());
45 const DolphinModel* dolphinModel = static_cast<const DolphinModel*>(proxyModel->sourceModel());
46
47 if (m_hasMinimizedNameColumn && (index.column() == KDirModel::Name)) {
48 QStyleOptionViewItemV4 opt(option);
49
50 const QModelIndex dirIndex = proxyModel->mapToSource(index);
51 const KFileItem item = dolphinModel->itemForIndex(dirIndex);
52 if (!item.isNull()) {
53 // symbolic links are displayed in an italic font
54 if (item.isLink()) {
55 opt.font.setItalic(true);
56 }
57
58 const int width = nameColumnWidth(item.text(), opt);
59 opt.rect.setWidth(width);
60 }
61 KFileItemDelegate::paint(painter, opt, index);
62 } else {
63 KFileItemDelegate::paint(painter, option, index);
64 }
65
66 if (dolphinModel->hasRevisionData()) {
67 // The currently shown items are under revision control. Show the current revision
68 // state above the decoration.
69 const QModelIndex dirIndex = proxyModel->mapToSource(index);
70 const QModelIndex revisionIndex = dolphinModel->index(dirIndex.row(), DolphinModel::Revision);
71 const QVariant data = dolphinModel->data(revisionIndex, Qt::DecorationRole);
72 const DolphinModel::RevisionState state = static_cast<DolphinModel::RevisionState>(data.toInt());
73
74 if (state != DolphinModel::LocalRevision) {
75 // TODO: The following code is just a proof of concept. Icons will be used later...
76 QColor color(200, 0, 0, 32);
77 switch (state) {
78 case DolphinModel::LatestRevision: color = QColor(0, 180, 0, 32); break;
79 // ...
80 default: break;
81 }
82 painter->fillRect(option.rect, color);
83 }
84 }
85 }
86
87 int DolphinFileItemDelegate::nameColumnWidth(const QString& name, const QStyleOptionViewItem& option)
88 {
89 QFontMetrics fontMetrics(option.font);
90 int width = option.decorationSize.width() + fontMetrics.width(name) + 16;
91
92 const int defaultWidth = option.rect.width();
93 if ((defaultWidth > 0) && (defaultWidth < width)) {
94 width = defaultWidth;
95 }
96 return width;
97 }
98