1// SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd.
2//
3// SPDX-License-Identifier: GPL-3.0-or-later
4
5#ifndef TASKDELEGATE_H
6#define TASKDELEGATE_H
7
8#include "taskmodel.h"
9
10#include <QObject>
11#include <QFont>
12#include <QStyledItemDelegate>
13
14class TaskDelegate : public QStyledItemDelegate
15{
16 Q_OBJECT
17
18 friend class TaskView; // for using Positions::minimumSize()
19
20public:
21 explicit TaskDelegate(QObject * parent = nullptr);
22 ~TaskDelegate() override;
23 void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
24 QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override;
25
26 // TaskView uses this method if the size of the taskview changes
27 void emitSizeHintChanged(const QModelIndex &index);
28
29 void currentChanged(const QModelIndex &current, const QModelIndex &previous);
30
31private:
32 void generateGradientPixmap(int width, int height, QColor color, bool selected) const;
33
34 mutable int cachedHeight = 0;
35 mutable QFont cachedFont;
36
37 class Positions
38 {
39 public:
40 Positions(const QStyleOptionViewItem &options, TaskModel *model) :
41 totalWidth(options.rect.width()),
42 maxFileLength(model->sizeOfFile(options.font)),
43 maxLineLength(model->getSizeOfLineNumber(options.font)),
44 realFileLength(maxFileLength),
45 top(options.rect.top()),
46 bottom(options.rect.bottom())
47 {
48 int flexibleArea = lineAreaLeft() - textAreaLeft() - ITEM_SPACING;
49 if (maxFileLength > flexibleArea / 2)
50 realFileLength = flexibleArea / 2;
51 fontHeight = QFontMetrics(options.font).height();
52 }
53
54 int getTop() const { return top + ITEM_MARGIN; }
55 int left() const { return ITEM_MARGIN; }
56 int right() const { return totalWidth - ITEM_MARGIN; }
57 int getBottom() const { return bottom; }
58 int firstLineHeight() const { return fontHeight + 1; }
59 static int minimumHeight() { return taskIconHeight() + 2 * ITEM_MARGIN; }
60
61 int taskIconLeft() const { return left(); }
62 static int taskIconWidth() { return TASK_ICON_SIZE; }
63 static int taskIconHeight() { return TASK_ICON_SIZE; }
64 int taskIconRight() const { return taskIconLeft() + taskIconWidth(); }
65 QRect taskIcon() const { return QRect(taskIconLeft(), getTop(), taskIconWidth(), taskIconHeight()); }
66
67 int textAreaLeft() const { return taskIconRight() + ITEM_SPACING; }
68 int textAreaWidth() const { return textAreaRight() - textAreaLeft(); }
69 int textAreaRight() const { return fileAreaLeft() - ITEM_SPACING; }
70 QRect textArea() const { return QRect(textAreaLeft(), getTop(), textAreaWidth(), firstLineHeight()); }
71
72 int fileAreaLeft() const { return fileAreaRight() - fileAreaWidth(); }
73 int fileAreaWidth() const { return realFileLength; }
74 int fileAreaRight() const { return lineAreaLeft() - ITEM_SPACING; }
75 QRect fileArea() const { return QRect(fileAreaLeft(), getTop(), fileAreaWidth(), firstLineHeight()); }
76
77 int lineAreaLeft() const { return lineAreaRight() - lineAreaWidth(); }
78 int lineAreaWidth() const { return maxLineLength; }
79 int lineAreaRight() const { return right(); }
80 QRect lineArea() const { return QRect(lineAreaLeft(), getTop(), lineAreaWidth(), firstLineHeight()); }
81
82 private:
83 int totalWidth = 0;
84 int maxFileLength = 0;
85 int maxLineLength = 0;
86 int realFileLength = 0;
87 int top = 0;
88 int bottom = 0;
89 int fontHeight = 0;
90
91 static const int TASK_ICON_SIZE = 16;
92 static const int ITEM_MARGIN = 2;
93 static const int ITEM_SPACING = 2 * ITEM_MARGIN;
94 };
95};
96
97#endif // TASKDELEGATE_H
98