Pimcommon

customlogwidget.cpp
1/*
2 SPDX-FileCopyrightText: 2012-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6#include "customlogwidget.h"
7#include <KColorScheme>
8#include <QAbstractTextDocumentLayout>
9#include <QApplication>
10#include <QPainter>
11#include <QTextDocument>
12
13using namespace PimCommon;
14using namespace Qt::Literals::StringLiterals;
15
16LogItemDelegate::LogItemDelegate(QObject *parent)
17 : QStyledItemDelegate(parent)
18{
19}
20
21LogItemDelegate::~LogItemDelegate() = default;
22
23QTextDocument *LogItemDelegate::document(const QStyleOptionViewItem &option, const QModelIndex &index) const
24{
25 if (!index.isValid()) {
26 return nullptr;
27 }
28 auto document = new QTextDocument(nullptr);
29 document->setDocumentMargin(1);
30 const auto textColor = index.data(Qt::ForegroundRole).value<QColor>();
31 QStyleOptionViewItem option4 = option;
33
34 QString text = option4.text;
35
36 const QString content = QStringLiteral(
37 "<html style=\"color:%1\">"
38 "<body> %2")
39 .arg(textColor.name().toUpper(), text)
40 + "</table></body></html>"_L1;
41
42 document->setHtml(content);
43
44 return document;
45}
46
47void LogItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
48{
49 if (!index.isValid()) {
50 return;
51 }
52 QTextDocument *doc = document(option, index);
53 if (!doc) {
54 return;
55 }
56 doc->setTextWidth(option.rect.width());
58
59 QPen pen = painter->pen();
60
61 QStyleOptionViewItem opt(option);
62 opt.showDecorationSelected = true;
64 painter->save();
65 painter->translate(option.rect.topLeft());
66
67 doc->drawContents(painter);
68
69 painter->restore();
70 painter->setPen(pen);
71
72 delete doc;
73}
74
75QSize LogItemDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
76{
77 if (!index.isValid()) {
78 return {0, 0};
79 }
80
81 QTextDocument *doc = document(option, index);
82 if (!doc) {
83 return {0, 0};
84 }
85
86 const QSize size = doc->documentLayout()->documentSize().toSize();
87 delete doc;
88
89 return size;
90}
91
92QWidget *LogItemDelegate::createEditor(QWidget *, const QStyleOptionViewItem &, const QModelIndex &) const
93{
94 return nullptr;
95}
96
97CustomLogWidget::CustomLogWidget(QWidget *parent)
98 : QListWidget(parent)
99{
100 auto itemDelegate = new LogItemDelegate(this);
101 setItemDelegate(itemDelegate);
102}
103
104CustomLogWidget::~CustomLogWidget() = default;
105
106void CustomLogWidget::addTitleLogEntry(const QString &log)
107{
108 auto item = new QListWidgetItem(log);
110 QFont font = item->font();
111 font.setBold(true);
112 item->setFont(font);
113 item->setData(ItemLogType, Title);
114 addItem(item);
116}
117
118void CustomLogWidget::addInfoLogEntry(const QString &log)
119{
120 auto item = new QListWidgetItem(log);
122 item->setData(ItemLogType, Info);
123 addItem(item);
125}
126
127void CustomLogWidget::addErrorLogEntry(const QString &log)
128{
129 auto item = new QListWidgetItem(log);
131 item->setData(ItemLogType, Error);
132 addItem(item);
134}
135
136void CustomLogWidget::addEndLineLogEntry()
137{
138 auto item = new QListWidgetItem;
139 item->setData(ItemLogType, EndLine);
140 addItem(item);
142}
143
144QString CustomLogWidget::toHtml() const
145{
146 QString result = QStringLiteral("<html>\n<body>\n");
147 result += "<meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\">\n"_L1;
148 for (int i = 0; i < count(); ++i) {
150 const QString itemText(itemWidget->text());
152 LogType type = static_cast<LogType>(itemWidget->data(CustomLogWidget::ItemLogType).toInt());
153 switch (type) {
154 case Title:
155 logText = QStringLiteral("<font color=%1>%2</font>").arg(QColor(Qt::black).name(), itemText);
156 break;
157 case Error:
158 logText = QStringLiteral("<font color=%1>%2</font>").arg(QColor(Qt::red).name(), itemText);
159 break;
160 case Info:
161 logText = QStringLiteral("<font color=%1>%2</font>").arg(QColor(Qt::darkGreen).name(), itemText);
162 break;
163 case EndLine:
164 logText = QStringLiteral("<br/>");
165 break;
166 }
167 result += "<p>"_L1 + logText + "</p>"_L1 + QLatin1Char('\n');
168 }
169 result += "</body>\n</html>\n"_L1;
170 return result;
171}
172
173QString CustomLogWidget::toPlainText() const
174{
175 QString result;
176 for (int i = 0; i < count(); ++i) {
177 result += item(i)->text() + QLatin1Char('\n');
178 }
179 return result;
180}
181
182bool CustomLogWidget::isEmpty() const
183{
184 return count() == 0;
185}
186
187#include "moc_customlogwidget.cpp"
Q_SCRIPTABLE QStringList logText()
QString name(GameStandardAction id)
VehicleSection::Type type(QStringView coachNumber, QStringView coachClassification)
folderdialogacltab.h
virtual QSizeF documentSize() const const=0
QStyle * style()
void setBold(bool enable)
void addItem(QListWidgetItem *item)
QListWidgetItem * item(int row) const const
QWidget * itemWidget(QListWidgetItem *item) const const
void scrollToItem(const QListWidgetItem *item, QAbstractItemView::ScrollHint hint)
QFont font() const const
virtual void setData(int role, const QVariant &value)
void setFont(const QFont &font)
void setForeground(const QBrush &brush)
QString text() const const
QVariant data(int role) const const
bool isValid() const const
const QPen & pen() const const
void restore()
void save()
void setPen(Qt::PenStyle style)
void setRenderHint(RenderHint hint, bool on)
void translate(const QPoint &offset)
QSize toSize() const const
QString arg(Args &&... args) const const
PE_PanelItemViewItem
virtual void drawPrimitive(PrimitiveElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget) const const=0
virtual void initStyleOption(QStyleOptionViewItem *option, const QModelIndex &index) const const
ForegroundRole
QAbstractTextDocumentLayout * documentLayout() const const
void setDocumentMargin(qreal margin)
void drawContents(QPainter *p, const QRectF &rect)
void setHtml(const QString &html)
void setTextWidth(qreal width)
T value() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 4 2024 16:39:22 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.