PeriDyno 1.0.0
Loading...
Searching...
No Matches
PLogWidget.cpp
Go to the documentation of this file.
1#include "PLogWidget.h"
2
3#include "Platform.h"
4
5#include <QHeaderView>
6
7#include <QMenu>
8#include <QContextMenuEvent>
9#include <QString>
10#include <QTime>
11#include <QApplication>
12
13namespace dyno
14{
15 std::atomic<PLogWidget*> PLogWidget::gInstance;
16 std::mutex PLogWidget::gMutex;
17
18 class QTimeTableWidgetItem : public QTableWidgetItem
19 {
20 public:
22
23 virtual QSize sizeHint() const;
24 };
25
26 class PTableItemMessage : public QTableWidgetItem
27 {
28 public:
30 };
31
32 class PTableItemProgress : public QTableWidgetItem
33 {
34 public:
35 PTableItemProgress(const QString& Event, const float& Progress);
36 };
37
39 QTableWidgetItem(QTime::currentTime().toString("hh:mm:ss"))
40 {
41 setFont(QFont("Arial", 8));
42 // setTextColor(QColor(60, 60, 60));
43
44 setToolTip(text());
45 setStatusTip("Message recorded at " + text());
46 }
47
49 {
50 return QTableWidgetItem::sizeHint();
51 }
52
54 QTableWidgetItem(QString::fromUtf8(m.text.c_str()))
55 {
56 QString ToolTipPrefix;
57
58 if (m.type & Log::Error)
59 ToolTipPrefix += "Critical error: ";
60
61 setFont(QFont("Arial", 8));
62
63 QColor TextColor;
64
65 switch (m.type)
66 {
67 case Log::Warning:
68 {
69 //TextColor = Qt::black;
70 break;
71 }
72
73 case Log::Error:
74 {
75 TextColor = Qt::red;
76 //setTextColor(TextColor);
77 break;
78 }
79 }
80
81 QString text = QString::fromUtf8(m.text.c_str());
82
83 setToolTip(ToolTipPrefix + text);
84 setStatusTip(ToolTipPrefix + text);
85 }
86
87 PTableItemProgress::PTableItemProgress(const QString& Event, const float& Progress)
88 {
89 QString ProgressString = Event;
90
91 if (Progress == 100.0f)
92 ProgressString += "... Done";
93 else
94 ProgressString += QString::number(Progress, 'f', 2);
95
96 setText(ProgressString);
97 setFont(QFont("Arial", 7));
98 // setTextColor(Qt::blue);
99 }
100
102 {
103 PLogWidget* ins = gInstance.load(std::memory_order_acquire);
104 if (!ins) {
105 std::lock_guard<std::mutex> tLock(gMutex);
106 ins = gInstance.load(std::memory_order_relaxed);
107 if (!ins) {
108 ins = new PLogWidget();
109 gInstance.store(ins, std::memory_order_release);
110 }
111 }
112
113 return ins;
114 }
115
116 PLogWidget::PLogWidget(QWidget* pParent /*= NULL*/) :
117 QTableWidget(pParent)
118 {
120
121 setColumnCount(3);
122
123 QStringList HeaderLabels;
124
125 HeaderLabels << "time" << "" << "message";
126
127 setHorizontalHeaderLabels(HeaderLabels);
128 horizontalHeader()->setSectionResizeMode(0, QHeaderView::ResizeToContents);
129 horizontalHeader()->setSectionResizeMode(1, QHeaderView::Fixed);
130 horizontalHeader()->setSectionResizeMode(2, QHeaderView::Stretch);
131 horizontalHeader()->resizeSection(1, 25);
132 horizontalHeader()->setDefaultAlignment(Qt::AlignLeft);
133 horizontalHeader()->setVisible(false);
134
135 // Disable vertical header
136 verticalHeader()->setVisible(false);
137
138 setGridStyle(Qt::NoPen);
139
140 setAlternatingRowColors(true);
141
142 Log::sendMessage(Log::Info, "Finished");
143 }
144
145 void PLogWidget::setOutput(std::string filename)
146 {
147 Log::setOutput(filename);
148 }
149
151 {
152 mEnableLogging = mEnableLogging ? false : true;
153 }
154
156 {
157 if (!mEnableLogging)
158 return;
159
160 insertRow(0);
161
162 QIcon ItemIcon;
163
164 switch (m.type)
165 {
166 case (int)Log::Warning:
167 {
168 ItemIcon = getIcon("exclamation");
169 break;
170 }
171
172 case (int)Log::Error:
173 {
174 ItemIcon = getIcon("exclamation-red");
175 break;
176 }
177
178 case (int)Log::Info:
179 ItemIcon = getIcon("exclamation-white");
180 break;
181
182 case (int)Log::User:
183 ItemIcon = getIcon("user");
184 break;
185 }
186
187 setItem(0, 0, new QTimeTableWidgetItem());
188 setItem(0, 1, new QTableWidgetItem(ItemIcon, ""));
189 setItem(0, 2, new PTableItemMessage(m));
190 setRowHeight(0, 18);
191 }
192
194 {
195 if (currentRow() < 0)
196 return;
197
198 removeRow(currentRow());
199 }
200
202 {
203 clear();
204 setRowCount(0);
205 }
206
211
212 void PLogWidget::contextMenuEvent(QContextMenuEvent* pContextMenuEvent)
213 {
214 QMenu menu;
215 menu.setTitle("Log");
216
217 menu.addAction(getIcon("cross"), "Clear All", this, SLOT(onClearAll()));
218 menu.exec(pContextMenuEvent->globalPos());
219 }
220
221 QIcon PLogWidget::getIcon(const QString& name)
222 {
223 return QIcon(QString::fromLocal8Bit(getAssetPath().c_str()) + "icon/" + name + ".png");
224 }
225
227 {
228 return QSize(100, 100);
229 }
230
231 int PLogWidget::sizeHintForColumn(int column) const {
232 ensurePolished();
233
234 //TODO: viewOptions cannot not be recognized for Qt 6
235// int row_count = rowCount();
236// if (row_count > 0 && column == 0) {
237// auto idx = model()->index(0, 0);
238// auto vo = viewOptions();
239// auto hint = itemDelegate(idx)->sizeHint(vo, idx).width();
240// return hint + 1;
241// }
242
243 return QTableWidget::sizeHintForColumn(column);
244 }
245}
@ Warning
Warning information.
Definition Log.h:49
@ Info
Information to user.
Definition Log.h:48
@ User
User specific message.
Definition Log.h:51
@ Error
Error information while executing something.
Definition Log.h:50
static void setUserReceiver(void(*userFunc)(const Message &))
Set user function to receive newly sent messages to logger.
Definition Log.cpp:57
static void sendMessage(MessageType type, const std::string &text)
Add a new message to log.
Definition Log.cpp:41
static void setOutput(const std::string &filename)
Open file where to log the messages.
Definition Log.cpp:67
void contextMenuEvent(QContextMenuEvent *pContextMenuEvent)
static void RecieveLogMessage(const Log::Message &m)
QSize sizeHint() const override
static std::atomic< PLogWidget * > gInstance
Definition PLogWidget.h:52
PLogWidget(QWidget *pParent=NULL)
int sizeHintForColumn(int column) const override
void onPrintMessage(const Log::Message &m)
QIcon getIcon(const QString &name)
void onClear(void)
static void setOutput(std::string filename)
void onClearAll(void)
static std::mutex gMutex
Definition PLogWidget.h:53
static PLogWidget * instance()
PTableItemMessage(const Log::Message &m)
PTableItemProgress(const QString &Event, const float &Progress)
virtual QSize sizeHint() const
This is an implementation of AdditiveCCD based on peridyno.
Definition Array.h:25
MessageType type
Definition Log.h:55