Repository navigation
Expand file tree
/
Copy pathdesktopitemdelegate.cpp
More file actions
282 lines (243 loc) · 10 KB
/
Copy pathdesktopitemdelegate.cpp
File metadata and controls
282 lines (243 loc) · 10 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/*
Copyright (C) 2013 Hong Jen Yee (PCMan) <pcman.tw@gmail.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "desktopitemdelegate.h"
#include "foldermodel.h"
#include <QApplication>
#include <QListView>
#include <QPainter>
#include <QIcon>
#include <QTextLayout>
#include <QTextOption>
#include <QTextLine>
#include <QPainterPath>
#include <QDebug>
#include <QProcess>
#include <QFileInfo>
#include <QStorageInfo>
#include "extattrs.h"
#include "itemdelegatefunctions.h"
using namespace Filer;
DesktopItemDelegate::DesktopItemDelegate(QListView* view, QObject* parent):
QStyledItemDelegate(parent ? parent : view),
view_(view),
symlinkIcon_(QIcon::fromTheme("emblem-symbolic-link")),
shadowColor_(0, 0, 0) {
}
// FIXME: we need to figure out a way to derive from Fm::FolderItemDelegate to avoid code duplication.
void DesktopItemDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const {
Q_ASSERT(index.isValid());
QStyleOptionViewItem opt = option;
initStyleOption(&opt, index);
/*
// Draw shadow
// qDebug() << "index:" << index;
QPen origPen = painter->pen();
QRect shadowRect(QPoint(0, 0), QSize(1600, 33));
QLinearGradient linearGradient(0,0,0,33);
linearGradient.setColorAt(0.00, QColor::fromRgbF(0, 0, 0, 0.3));
linearGradient.setColorAt(0.33, QColor::fromRgbF(0, 0, 0, 0.2));
linearGradient.setColorAt(1.00, QColor::fromRgbF(0, 0, 0, 0.0));
painter->fillRect(shadowRect, linearGradient);
painter->setPen(origPen);
painter->save();
painter->setClipRect(option.rect);
*/
opt.decorationAlignment = Qt::AlignHCenter | Qt::AlignTop;
opt.displayAlignment = Qt::AlignTop | Qt::AlignHCenter;
// draw the icon
QIcon::Mode iconMode;
if(opt.state & QStyle::State_Enabled) {
if(opt.state & QStyle::State_Selected)
iconMode = QIcon::Selected;
else {
iconMode = QIcon::Normal;
}
}
else
iconMode = QIcon::Disabled;
QPoint iconPos(opt.rect.x() + (opt.rect.width() - opt.decorationSize.width()) / 2, opt.rect.y());
// probono: If the icon is selected, draw a light rounded rect in the background
if(opt.state & QStyle::State_Selected) {
QRectF boundRect = QRectF();
boundRect.setTop(iconPos.y());
boundRect.setLeft(iconPos.x());
boundRect.setHeight(opt.decorationSize.height());
boundRect.setWidth(opt.decorationSize.width());
painter->setRenderHint(QPainter::Antialiasing);
QPainterPath path = QPainterPath();
path.addRoundedRect(boundRect, 4, 4);
painter->fillPath(path, QColor::fromRgbF(196, 196, 196, 0.5)); // Light gray, 50% alpha
}
FmFileInfo* file = static_cast<FmFileInfo*>(index.data(Fm::FolderModel::FileInfoRole).value<void*>());
if(file) {
QPixmap pixmap = opt.icon.pixmap(opt.decorationSize, iconMode);
painter->drawPixmap(iconPos, pixmap);
}
if(file) {
if(fm_file_info_is_symlink(file)) {
// draw some emblems for the item if needed
// we only support symlink emblem at the moment
QPoint symlinkPos = iconPos;
symlinkPos.setY(symlinkPos.y() + opt.decorationSize.height() / 2);
painter->drawPixmap(symlinkPos, symlinkIcon_.pixmap(opt.decorationSize / 2, iconMode));
}
}
QString path = QString::fromUtf8(fm_path_to_str(fm_file_info_get_path(file))); // argh! So complicated!
// probono: git labels
// FIXME: Why doesn't this work?
// Fm::drawGitEmblem(path, painter, iconPos, opt, file ,iconMode);
// ======================================= Start ugly code duplication
// probono: git labels
QString mimetype = QString::fromUtf8(fm_mime_type_get_type(fm_mime_type_ref(fm_file_info_get_mime_type(file))));
if (mimetype == "inode/directory" && QFileInfo(path + "/.git").exists()) {
// Check git status
QProcess p;
QProcessEnvironment env;
env.insert("LANG", "C");
p.setProcessEnvironment(env);
p.setProgram("git");
p.setWorkingDirectory(path);
p.setArguments({"status", "-s"});
p.start();
p.waitForFinished();
bool gitHasChanges = p.readAllStandardOutput().length();
QIcon emblemIcon;
emblemIcon = QIcon::fromTheme("emblem-symbolic-git");
// if(gitHasChanges) {
// emblemIcon = QIcon::fromTheme("emblem-symbolic-git-red");
// }
QPoint emblemPos = iconPos;
emblemPos.setX(emblemPos.x() + opt.decorationSize.width()/4);
emblemPos.setY(emblemPos.y() + opt.decorationSize.height()/2.6);
if(gitHasChanges) {
painter->setOpacity(0.5);
} else {
painter->setOpacity(0.1);
}
painter->drawPixmap(emblemPos, emblemIcon.pixmap(opt.decorationSize / 2, iconMode));
painter->setOpacity(1);
}
// ======================================= End ugly code duplication
// probono: Draw label emblems
bool ok;
QString emblem = Fm::getAttributeValueQString(path, "EMBLEM", ok);
if (ok) {
QIcon emblemIcon = QIcon::fromTheme(emblem);
QPoint emblemIconPos(opt.decorationSize.width() * 0.75 + opt.rect.x() + (opt.rect.width() - opt.decorationSize.width()) / 2, opt.rect.y());
painter->drawPixmap(emblemIconPos, emblemIcon.pixmap(opt.decorationSize / 2, iconMode));
}
// Calculate size of textRect
int leftAndRightSpace = 20;
QRectF textRect(opt.rect.x() + leftAndRightSpace,
opt.rect.y() + 3 + opt.decorationSize.height(),
opt.rect.width() - leftAndRightSpace * 2,
opt.rect.height() - opt.decorationSize.height());
QFont desktopFont = opt.font;
desktopFont.setBold(true);
QTextLayout layout(opt.text, desktopFont);
QTextOption textOption;
textOption.setAlignment(opt.displayAlignment);
textOption.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere);
textOption.setTextDirection(opt.direction);
layout.setTextOption(textOption);
qreal height = 0;
qreal width = 0;
int visibleLines = 0;
layout.beginLayout();
QString elidedText;
for(;;) {
QTextLine line = layout.createLine();
if(!line.isValid())
break;
line.setLineWidth(textRect.width());
QFontMetrics fontMetrics(desktopFont);
height += fontMetrics.leading();
line.setPosition(QPointF(0, height));
if((height + line.height() + textRect.y()) + 3 > textRect.bottom()) {
// if part of this line falls outside the textRect, ignore it and quit.
QTextLine lastLine = layout.lineAt(visibleLines - 1);
elidedText = opt.text.mid(lastLine.textStart());
opt.textElideMode = Qt::ElideMiddle; // probono: Put ... in the middle, not at the end so that we can see the suffix
elidedText = fontMetrics.elidedText(elidedText, opt.textElideMode, textRect.width());
break;
}
height += line.height();
++ visibleLines;
width = qMax(width, line.naturalTextWidth());
}
layout.endLayout();
// probono: draw background rounded rect for selected item
QRectF boundRect = layout.boundingRect();
int additionalSpace = 1;
boundRect.setWidth(width + 16 * additionalSpace);
// boundRect.setHeight(boundRect.height() + 2 * additionalSpace); // probono: This was wrong, resulting in some background rounded rects being too high
boundRect.setHeight(height + additionalSpace);
boundRect.moveTo(textRect.x() - 8*additionalSpace + (textRect.width() - width)/2,
textRect.y() - additionalSpace);
if((opt.state & QStyle::State_Selected) && opt.widget) {
// qDebug("w: %f, h:%f, m:%f", boundRect.width(), boundRect.height(), layout.minimumWidth());
painter->setFont(desktopFont);
// painter->fillRect(boundRect, opt.palette.highlight());
painter->setRenderHint(QPainter::Antialiasing);
QPainterPath path = QPainterPath();
path.addRoundedRect(boundRect, 8, 8);
painter->fillPath(path, opt.palette.highlight());
}
// draw shadow, FIXME: is it possible to use QGraphicsDropShadowEffect here?
QPen prevPen = painter->pen();
painter->setPen(QPen(shadowColor_));
for(int i = 0; i < visibleLines; ++i) {
QTextLine line = layout.lineAt(i);
if(i == (visibleLines - 1) && !elidedText.isEmpty()) { // the last line, draw elided text
QPointF pos(textRect.x() + line.position().x() + 1, textRect.y() + line.y() + line.ascent() + 1);
painter->setFont(desktopFont);
painter->drawText(pos, elidedText);
}
else {
line.draw(painter, textRect.topLeft() + QPointF(1, 1));
}
}
painter->setPen(prevPen);
// draw text
for(int i = 0; i < visibleLines; ++i) {
QTextLine line = layout.lineAt(i);
QPen prevPen = painter->pen();
painter->setPen(QPen(textColor_));
if(i == (visibleLines - 1) && !elidedText.isEmpty()) { // the last line, draw elided text
QPointF pos(textRect.x() + line.position().x(), textRect.y() + line.y() + line.ascent());
painter->drawText(pos, elidedText);
}
else {
line.draw(painter, textRect.topLeft());
}
painter->setPen(prevPen);
}
if(opt.state & QStyle::State_HasFocus) {
// FIXME: draw focus rect
}
// painter->restore();
}
QSize DesktopItemDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const {
QVariant value = index.data(Qt::SizeHintRole);
if(value.isValid())
return qvariant_cast<QSize>(value);
QStyleOptionViewItem opt = option;
initStyleOption(&opt, index);
// use grid size as size hint
QSize gridSize = view_->gridSize();
return QSize(gridSize.width() -2, gridSize.height() - 2);
}
DesktopItemDelegate::~DesktopItemDelegate() {
}