1 | /* |
2 | * Copyright (C) by Klaas Freitag <freitag@owncloud.com> |
3 | * Copyright (C) by Olivier Goffart <ogoffart@woboq.com> |
4 | * |
5 | * |
6 | * This program is free software; you can redistribute it and/or modify |
7 | * it under the terms of the GNU General Public License as published by |
8 | * the Free Software Foundation; either version 2 of the License, or |
9 | * (at your option) any later version. |
10 | * |
11 | * This program is distributed in the hope that it will be useful, but |
12 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
13 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
14 | * for more details. |
15 | */ |
16 | |
17 | #include "activityitemdelegate.h" |
18 | #include "folderstatusmodel.h" |
19 | #include "folderman.h" |
20 | #include "accountstate.h" |
21 | #include <theme.h> |
22 | #include <account.h> |
23 | |
24 | #include <QFileIconProvider> |
25 | #include <QPainter> |
26 | #include <QApplication> |
27 | |
28 | namespace OCC { |
29 | |
30 | int ActivityItemDelegate::_iconHeight = 0; |
31 | int ActivityItemDelegate::_margin = 0; |
32 | |
33 | int ActivityItemDelegate::iconHeight() |
34 | { |
35 | if (_iconHeight == 0) { |
36 | QStyleOptionViewItem option; |
37 | QFont font = option.font; |
38 | |
39 | QFontMetrics fm(font); |
40 | |
41 | _iconHeight = qRound(fm.height() / 5.0 * 8.0); |
42 | } |
43 | return _iconHeight; |
44 | } |
45 | |
46 | int ActivityItemDelegate::rowHeight() |
47 | { |
48 | if (_margin == 0) { |
49 | QStyleOptionViewItem opt; |
50 | |
51 | QFont f = opt.font; |
52 | QFontMetrics fm(f); |
53 | |
54 | _margin = fm.height() / 4; |
55 | } |
56 | return iconHeight() + 2 * _margin; |
57 | } |
58 | |
59 | QSize ActivityItemDelegate::sizeHint(const QStyleOptionViewItem &option, |
60 | const QModelIndex & /* index */) const |
61 | { |
62 | QFont font = option.font; |
63 | |
64 | return QSize(0, rowHeight()); |
65 | } |
66 | |
67 | void ActivityItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, |
68 | const QModelIndex &index) const |
69 | { |
70 | QStyledItemDelegate::paint(painter, option, index); |
71 | |
72 | QFont font = option.font; |
73 | |
74 | QFontMetrics fm(font); |
75 | int margin = fm.height() / 4; |
76 | |
77 | painter->save(); |
78 | |
79 | QIcon actionIcon = qvariant_cast<QIcon>(index.data(ActionIconRole)); |
80 | QIcon userIcon = qvariant_cast<QIcon>(index.data(UserIconRole)); |
81 | QString actionText = qvariant_cast<QString>(index.data(ActionTextRole)); |
82 | QString pathText = qvariant_cast<QString>(index.data(PathRole)); |
83 | |
84 | QString remoteLink = qvariant_cast<QString>(index.data(LinkRole)); |
85 | QString timeText = qvariant_cast<QString>(index.data(PointInTimeRole)); |
86 | QString accountRole = qvariant_cast<QString>(index.data(AccountRole)); |
87 | bool accountOnline = qvariant_cast<bool>(index.data(AccountConnectedRole)); |
88 | |
89 | QRect actionIconRect = option.rect; |
90 | QRect userIconRect = option.rect; |
91 | |
92 | int iconHeight = qRound(fm.height() / 5.0 * 8.0); |
93 | int iconWidth = iconHeight; |
94 | |
95 | actionIconRect.setLeft(option.rect.left() + margin); |
96 | actionIconRect.setWidth(iconWidth); |
97 | actionIconRect.setHeight(iconHeight); |
98 | actionIconRect.setTop(actionIconRect.top() + margin); |
99 | userIconRect.setLeft(actionIconRect.right() + margin); |
100 | userIconRect.setWidth(iconWidth); |
101 | userIconRect.setHeight(iconHeight); |
102 | userIconRect.setTop(actionIconRect.top()); |
103 | |
104 | int textTopOffset = qRound((iconHeight - fm.height()) / 2.0); |
105 | // time rect |
106 | QRect timeBox; |
107 | int timeBoxWidth = fm.boundingRect(QLatin1String("4 hour(s) ago on longlongdomain.org" )).width(); // FIXME. |
108 | timeBox.setTop(actionIconRect.top() + textTopOffset); |
109 | timeBox.setLeft(option.rect.right() - timeBoxWidth - margin); |
110 | timeBox.setWidth(timeBoxWidth); |
111 | timeBox.setHeight(fm.height()); |
112 | |
113 | QRect actionTextBox = timeBox; |
114 | actionTextBox.setLeft(userIconRect.right() + margin); |
115 | actionTextBox.setRight(timeBox.left() - margin); |
116 | |
117 | /* === start drawing === */ |
118 | QPixmap pm = actionIcon.pixmap(iconWidth, iconHeight, QIcon::Normal); |
119 | painter->drawPixmap(QPoint(actionIconRect.left(), actionIconRect.top()), pm); |
120 | |
121 | pm = userIcon.pixmap(iconWidth, iconHeight, QIcon::Normal); |
122 | painter->drawPixmap(QPoint(userIconRect.left(), userIconRect.top()), pm); |
123 | |
124 | QPalette::ColorGroup cg = option.state & QStyle::State_Enabled |
125 | ? QPalette::Normal |
126 | : QPalette::Disabled; |
127 | if (cg == QPalette::Normal && !(option.state & QStyle::State_Active)) |
128 | cg = QPalette::Inactive; |
129 | if (option.state & QStyle::State_Selected) { |
130 | painter->setPen(option.palette.color(cg, QPalette::HighlightedText)); |
131 | } else { |
132 | painter->setPen(option.palette.color(cg, QPalette::Text)); |
133 | } |
134 | |
135 | const QString elidedAction = fm.elidedText(actionText, Qt::ElideRight, actionTextBox.width()); |
136 | painter->drawText(actionTextBox, elidedAction); |
137 | |
138 | int atPos = accountRole.lastIndexOf(QLatin1Char('@')); |
139 | if (atPos > -1) { |
140 | accountRole.remove(0, atPos + 1); |
141 | } |
142 | |
143 | QString timeStr; |
144 | if (accountOnline) { |
145 | timeStr = tr("%1 on %2" ).arg(timeText, accountRole); |
146 | } else { |
147 | timeStr = tr("%1 on %2 (disconnected)" ).arg(timeText, accountRole); |
148 | QPalette p = option.palette; |
149 | painter->setPen(p.color(QPalette::Disabled, QPalette::Text)); |
150 | } |
151 | const QString elidedTime = fm.elidedText(timeStr, Qt::ElideRight, timeBox.width()); |
152 | |
153 | painter->drawText(timeBox, elidedTime); |
154 | painter->restore(); |
155 | } |
156 | |
157 | bool ActivityItemDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, |
158 | const QStyleOptionViewItem &option, const QModelIndex &index) |
159 | { |
160 | return QStyledItemDelegate::editorEvent(event, model, option, index); |
161 | } |
162 | |
163 | } // namespace OCC |
164 | |