Erste lauffähige Version
This commit is contained in:
86
src/tagpanel.cpp
Normal file
86
src/tagpanel.cpp
Normal file
@@ -0,0 +1,86 @@
|
||||
#include "tagpanel.h"
|
||||
#include <QHeaderView>
|
||||
#include <QRegularExpression>
|
||||
|
||||
TagPanel::TagPanel(const QString &tag, QWidget *parent)
|
||||
: QGroupBox(QStringLiteral("[%1]").arg(tag), parent)
|
||||
, m_tag(tag)
|
||||
{
|
||||
auto *layout = new QVBoxLayout(this);
|
||||
layout->setContentsMargins(6, 12, 6, 6);
|
||||
layout->setSpacing(4);
|
||||
|
||||
// Timestamp line
|
||||
m_timestampLabel = new QLabel(tr("No data yet"), this);
|
||||
m_timestampLabel->setStyleSheet("color: gray; font-size: 10px;");
|
||||
layout->addWidget(m_timestampLabel);
|
||||
|
||||
// Table for key=value pairs
|
||||
m_table = new QTableWidget(0, 2, this);
|
||||
m_table->setHorizontalHeaderLabels({tr("Key"), tr("Value")});
|
||||
m_table->horizontalHeader()->setStretchLastSection(true);
|
||||
m_table->verticalHeader()->hide();
|
||||
m_table->setEditTriggers(QAbstractItemView::NoEditTriggers);
|
||||
m_table->setAlternatingRowColors(true);
|
||||
m_table->setMaximumHeight(200);
|
||||
m_table->verticalHeader()->setDefaultSectionSize(22);
|
||||
layout->addWidget(m_table);
|
||||
|
||||
// Fallback label for non-kv data
|
||||
m_rawLabel = new QLabel(this);
|
||||
m_rawLabel->setWordWrap(true);
|
||||
m_rawLabel->setStyleSheet("font-family: monospace;");
|
||||
m_rawLabel->hide();
|
||||
layout->addWidget(m_rawLabel);
|
||||
}
|
||||
|
||||
void TagPanel::update(const QString &value)
|
||||
{
|
||||
m_timestampLabel->setText(
|
||||
QDateTime::currentDateTime().toString("hh:mm:ss.zzz"));
|
||||
|
||||
// Detect key=value format: word=anything (space separated)
|
||||
static const QRegularExpression kvRe(R"((\w+)=(\S+))");
|
||||
auto it = kvRe.globalMatch(value);
|
||||
if (it.hasNext()) {
|
||||
parseKeyValue(value);
|
||||
m_rawLabel->hide();
|
||||
m_table->show();
|
||||
} else {
|
||||
showRaw(value);
|
||||
m_table->hide();
|
||||
m_rawLabel->show();
|
||||
}
|
||||
}
|
||||
|
||||
void TagPanel::parseKeyValue(const QString &value)
|
||||
{
|
||||
static const QRegularExpression kvRe(R"((\w+)=(\S+))");
|
||||
auto it = kvRe.globalMatch(value);
|
||||
while (it.hasNext()) {
|
||||
const auto match = it.next();
|
||||
const QString key = match.captured(1);
|
||||
const QString val = match.captured(2);
|
||||
ensureRow(key);
|
||||
const int row = m_keys.indexOf(key);
|
||||
m_table->item(row, 1)->setText(val);
|
||||
// Flash highlight
|
||||
m_table->item(row, 1)->setBackground(QColor(0x2d, 0x5a, 0x2d));
|
||||
}
|
||||
}
|
||||
|
||||
void TagPanel::showRaw(const QString &value)
|
||||
{
|
||||
m_rawLabel->setText(value);
|
||||
}
|
||||
|
||||
void TagPanel::ensureRow(const QString &key)
|
||||
{
|
||||
if (m_keys.contains(key))
|
||||
return;
|
||||
m_keys.append(key);
|
||||
const int row = m_table->rowCount();
|
||||
m_table->insertRow(row);
|
||||
m_table->setItem(row, 0, new QTableWidgetItem(key));
|
||||
m_table->setItem(row, 1, new QTableWidgetItem(QString()));
|
||||
}
|
||||
Reference in New Issue
Block a user