4#include "G4UImanager.hh"
10 G4UImanager::GetUIpointer()->SetCoutDestination(
this);
18 QString fullQString = QString::fromStdString(coutString);
23 QRegularExpression lineBreakRegex(
"\r?\n|\u2028");
24 QStringList lines = fullQString.split(lineBreakRegex, Qt::KeepEmptyParts);
26 for (
const QString& line : lines) {
28 QString htmlLine = ansiToHtml(line);
39 QString fullQString = QString::fromStdString(cerrString);
43 QRegularExpression lineBreakRegex(
"\\r?\\n|\\u2028");
44 QStringList lines = fullQString.split(lineBreakRegex, Qt::KeepEmptyParts);
46 for (
const QString& line : lines) {
47 QString htmlLine = ansiToHtml(line);
62QString GUI_Session::ansiToHtml(
const QString& ansiText) {
64 htmlText.reserve(ansiText.length() * 1.2);
68 bool isUnderlined =
false;
69 QString currentColor =
"";
72 QMap<int, QString> colorMap;
73 colorMap[30] =
"black";
75 colorMap[32] =
"green";
76 colorMap[33] =
"darkorange";
77 colorMap[34] =
"blue";
78 colorMap[35] =
"magenta";
79 colorMap[36] =
"cyan";
80 colorMap[37] =
"grey";
83 QRegularExpression ansiRegex(
"\x1B\\[([0-9;]*)m");
86 QRegularExpressionMatchIterator i = ansiRegex.globalMatch(ansiText);
89 QRegularExpressionMatch match = i.next();
90 int currentPos = match.capturedStart();
93 if (currentPos > lastPos) {
94 QString textSegment = ansiText.mid(lastPos, currentPos - lastPos);
96 htmlText += textSegment.toHtmlEscaped();
100 QString codesStr = match.captured(1);
101 QStringList codes = codesStr.split(
';', Qt::SkipEmptyParts);
103 if (codes.isEmpty()) {
109 bool nextBold = isBold;
110 bool nextUnderlined = isUnderlined;
111 QString nextColor = currentColor;
112 bool resetDetected =
false;
114 for (
const QString& codeStr : codes) {
116 int code = codeStr.toInt(&ok);
121 resetDetected =
true;
123 nextUnderlined =
false;
129 else if (code == 1) {
133 else if (code == 4) {
135 nextUnderlined =
true;
137 else if (code == 22) {
141 else if (code == 24) {
143 nextUnderlined =
false;
145 else if (code >= 30 && code <= 37) {
147 nextColor = colorMap.value(code,
"");
149 else if (code == 39) {
159 if (isUnderlined && !nextUnderlined) { htmlText +=
"</u>"; }
160 if (isBold && !nextBold) { htmlText +=
"</b>"; }
161 if (!currentColor.isEmpty() && currentColor != nextColor) { htmlText +=
"</font>"; }
165 if (isUnderlined) htmlText +=
"</u>";
166 if (isBold) htmlText +=
"</b>";
167 if (!currentColor.isEmpty()) htmlText +=
"</font>";
171 if (!currentColor.isEmpty() && currentColor != nextColor) {
173 if (!nextColor.isEmpty()) { htmlText += QString(
"<font color=\"%1\">").arg(nextColor); }
175 else if (currentColor.isEmpty() && !nextColor.isEmpty()) {
177 htmlText += QString(
"<font color=\"%1\">").arg(nextColor);
180 if (!isBold && nextBold) { htmlText +=
"<b>"; }
181 if (!isUnderlined && nextUnderlined) { htmlText +=
"<u>"; }
185 isUnderlined = nextUnderlined;
186 currentColor = nextColor;
189 lastPos = match.capturedEnd();
193 if (lastPos < ansiText.length()) {
194 QString textSegment = ansiText.mid(lastPos);
195 htmlText += textSegment.toHtmlEscaped();
199 if (isUnderlined) { htmlText +=
"</u>"; }
200 if (isBold) { htmlText +=
"</b>"; }
201 if (!currentColor.isEmpty()) { htmlText +=
"</font>"; }
210 if (
auto* UIM = G4UImanager::GetUIpointer()) {
213 UIM->SetCoutDestination(
nullptr);
std::shared_ptr< GLogger > log
A Qt widget that displays read-only log text with a compact "top bar" UI.
void appendLog(const QString &text)
Appends a log line to the internal history and updates the display.
void info(int level, Args &&... args) const
GUI_Session(const std::shared_ptr< GOptions > &gopt, GBoard *board)
Constructs a GUI_Session.
G4int ReceiveG4cerr(const G4String &cerrString) override
Receives error output from Geant4 and forwards it to the board.
~GUI_Session() override
Destructor.
G4int ReceiveG4cout(const G4String &coutString) override
Receives standard output from Geant4 and forwards it to the board.
constexpr const char * GBOARD_LOGGER