This repository was archived by the owner on Jun 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSubmodulesDelegate.cpp
More file actions
101 lines (79 loc) · 3.25 KB
/
SubmodulesDelegate.cpp
File metadata and controls
101 lines (79 loc) · 3.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include "SubmodulesDelegate.h"
#include <QAbstractItemView>
#include <QPainter>
#include <QToolTip>
#include <QHelpEvent>
#include "libGitWrap/ObjectId.hpp"
#include "libGitWrap/Submodule.hpp"
#include "libGitWrap/Result.hpp"
SubmodulesViewDelegate::SubmodulesViewDelegate( QObject* parent )
: QItemDelegate( parent )
{
}
void SubmodulesViewDelegate::paint( QPainter* painter, const QStyleOptionViewItem& option,
const QModelIndex& index ) const
{
if( index.column() != 0 )
{
QItemDelegate::paint( painter, option, index );
return;
}
drawBackground( painter, option, index );
doDrawDisplay( painter, option, option.rect.adjusted( 1, 1, -1, -1 ), index );
QIcon theDecoration( index.data(Qt::DecorationRole).value<QIcon>() );
QRect decorationRect(QPoint(0, 0), option.decorationSize);
if ( option.decorationAlignment.testFlag(Qt::AlignVCenter) )
{
QPoint newCenter( decorationRect.center().x(), option.rect.center().y() );
decorationRect.moveCenter( newCenter );
}
drawDecoration( painter, option, decorationRect, theDecoration.pixmap(decorationRect.size()) );
drawFocus( painter, option, option.rect );
}
void SubmodulesViewDelegate::doDrawDisplay(QPainter *painter, const QStyleOptionViewItem &option,
const QRect &rect, const QModelIndex &index) const
{
const QFontMetrics& fm = option.fontMetrics;
QRect textRect = option.rect;
textRect.moveLeft( option.decorationSize.width() );
textRect.setBottom( textRect.top() + fm.lineSpacing() );
QVariant submoduleData = index.data(Qt::UserRole + 1);
if ( submoduleData.canConvert<Git::Submodule>() )
{
Git::Submodule submodule = index.data(Qt::UserRole + 1).value<Git::Submodule>();
QFont f( option.font );
f.setBold( true );
painter->setFont( f );
painter->drawText( textRect, trUtf8("Name: %1").arg(submodule.name()) );
textRect.moveTop( textRect.top() + fm.lineSpacing() );
painter->setFont( option.font );
painter->drawText( textRect, trUtf8("Revision: %1").arg(submodule.wdOid().toString()) );
}
}
bool SubmodulesViewDelegate::helpEvent(QHelpEvent *event, QAbstractItemView *view,
const QStyleOptionViewItem &option, const QModelIndex &index)
{
if ( !event || !view )
return false;
QVariant submoduleData = index.data(Qt::UserRole + 1);
if ( !submoduleData.canConvert<Git::Submodule>() )
return false;
Git::Submodule submodule = index.data(Qt::UserRole + 1).value<Git::Submodule>();
if ( event->type() == QEvent::ToolTip )
{
Git::Result r;
QString tooltip =
trUtf8("URL: %1\nPath: %2")
.arg(submodule.url(r))
.arg(submodule.path(r));
QToolTip::showText( event->globalPos(), tooltip, view );
return true;
}
return QItemDelegate::helpEvent( event, view, option, index );
}
QSize SubmodulesViewDelegate::sizeHint( const QStyleOptionViewItem& option,
const QModelIndex& index ) const
{
const QFontMetrics& fm = option.fontMetrics;
return QSize( 200, 2 + 2 * fm.lineSpacing() );
}