Erste lauffähige Version
This commit is contained in:
141
src/rawview.cpp
Normal file
141
src/rawview.cpp
Normal file
@@ -0,0 +1,141 @@
|
||||
#include "rawview.h"
|
||||
#include <QScrollBar>
|
||||
#include <QTextDocument>
|
||||
#include <QTextCursor>
|
||||
#include <QFont>
|
||||
#include <QPalette>
|
||||
#include <QApplication>
|
||||
|
||||
RawView::RawView(QWidget *parent)
|
||||
: QWidget(parent)
|
||||
{
|
||||
setupUi();
|
||||
applyColorScheme();
|
||||
}
|
||||
|
||||
void RawView::setupUi()
|
||||
{
|
||||
auto *mainLayout = new QVBoxLayout(this);
|
||||
mainLayout->setContentsMargins(4, 4, 4, 4);
|
||||
mainLayout->setSpacing(4);
|
||||
|
||||
// ── Toolbar ──────────────────────────────────────────────────────────────
|
||||
auto *toolbar = new QHBoxLayout();
|
||||
|
||||
m_searchEdit = new QLineEdit(this);
|
||||
m_searchEdit->setPlaceholderText(tr("Search… (Enter)"));
|
||||
m_searchEdit->setClearButtonEnabled(true);
|
||||
connect(m_searchEdit, &QLineEdit::textChanged, this, &RawView::onSearch);
|
||||
|
||||
m_autoScrollCb = new QCheckBox(tr("Auto-scroll"), this);
|
||||
m_autoScrollCb->setChecked(true);
|
||||
connect(m_autoScrollCb, &QCheckBox::toggled, this, &RawView::onAutoScrollToggled);
|
||||
|
||||
m_clearBtn = new QPushButton(tr("Clear"), this);
|
||||
connect(m_clearBtn, &QPushButton::clicked, this, &RawView::clear);
|
||||
|
||||
m_lineCountLbl = new QLabel(tr("Lines: 0"), this);
|
||||
m_lineCountLbl->setMinimumWidth(90);
|
||||
|
||||
toolbar->addWidget(new QLabel(tr("Search:"), this));
|
||||
toolbar->addWidget(m_searchEdit, 1);
|
||||
toolbar->addWidget(m_autoScrollCb);
|
||||
toolbar->addWidget(m_lineCountLbl);
|
||||
toolbar->addWidget(m_clearBtn);
|
||||
mainLayout->addLayout(toolbar);
|
||||
|
||||
// ── Text area ────────────────────────────────────────────────────────────
|
||||
m_textEdit = new QPlainTextEdit(this);
|
||||
m_textEdit->setReadOnly(true);
|
||||
m_textEdit->setLineWrapMode(QPlainTextEdit::NoWrap); // horizontal scroll
|
||||
// Practically unlimited history (Qt uses a block count limit internally)
|
||||
m_textEdit->setMaximumBlockCount(0); // 0 = unlimited
|
||||
|
||||
// Monospaced font for alignment
|
||||
QFont monoFont("Monospace");
|
||||
monoFont.setStyleHint(QFont::Monospace);
|
||||
monoFont.setPointSize(10);
|
||||
m_textEdit->setFont(monoFont);
|
||||
|
||||
// Don't let the text edit swallow horizontal scroll events from the viewport
|
||||
m_textEdit->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
|
||||
m_textEdit->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
|
||||
|
||||
connect(m_textEdit->verticalScrollBar(), &QScrollBar::valueChanged,
|
||||
this, &RawView::onScrollValueChanged);
|
||||
|
||||
mainLayout->addWidget(m_textEdit, 1);
|
||||
}
|
||||
|
||||
void RawView::applyColorScheme()
|
||||
{
|
||||
// Dark terminal style
|
||||
QPalette p = m_textEdit->palette();
|
||||
p.setColor(QPalette::Base, QColor(0x1e, 0x1e, 0x1e));
|
||||
p.setColor(QPalette::Text, QColor(0xd4, 0xd4, 0xd4));
|
||||
m_textEdit->setPalette(p);
|
||||
}
|
||||
|
||||
void RawView::appendLine(const QString &line)
|
||||
{
|
||||
++m_lineCount;
|
||||
m_lineCountLbl->setText(tr("Lines: %1").arg(m_lineCount));
|
||||
|
||||
// Highlight lines that contain a tag like [WDG]
|
||||
static const QRegularExpression tagRe(R"(\[[A-Z][A-Z0-9_]*\])",
|
||||
QRegularExpression::CaseInsensitiveOption);
|
||||
if (tagRe.match(line).hasMatch()) {
|
||||
// Append as HTML so we can colour it differently
|
||||
QTextCursor cursor(m_textEdit->document());
|
||||
cursor.movePosition(QTextCursor::End);
|
||||
QTextCharFormat fmt;
|
||||
fmt.setForeground(QColor(0x56, 0xb6, 0xc2)); // cyan-ish
|
||||
cursor.insertText(line + '\n', fmt);
|
||||
} else {
|
||||
m_textEdit->appendPlainText(line);
|
||||
}
|
||||
|
||||
if (m_autoScroll)
|
||||
m_textEdit->verticalScrollBar()->setValue(
|
||||
m_textEdit->verticalScrollBar()->maximum());
|
||||
}
|
||||
|
||||
void RawView::clear()
|
||||
{
|
||||
m_textEdit->clear();
|
||||
m_lineCount = 0;
|
||||
m_lineCountLbl->setText(tr("Lines: 0"));
|
||||
}
|
||||
|
||||
void RawView::onSearch(const QString &text)
|
||||
{
|
||||
// Simple incremental search – highlight first match
|
||||
QTextDocument *doc = m_textEdit->document();
|
||||
QTextCursor cursor = doc->find(text);
|
||||
if (!cursor.isNull()) {
|
||||
m_textEdit->setTextCursor(cursor);
|
||||
m_searchEdit->setStyleSheet(QString());
|
||||
} else if (!text.isEmpty()) {
|
||||
m_searchEdit->setStyleSheet("background: #5c2222;");
|
||||
} else {
|
||||
m_searchEdit->setStyleSheet(QString());
|
||||
}
|
||||
}
|
||||
|
||||
void RawView::onScrollValueChanged(int value)
|
||||
{
|
||||
const int max = m_textEdit->verticalScrollBar()->maximum();
|
||||
// If user scrolled away from bottom, disable auto-scroll
|
||||
if (value < max - 5) {
|
||||
m_autoScroll = false;
|
||||
m_autoScrollCb->setChecked(false);
|
||||
}
|
||||
}
|
||||
|
||||
void RawView::onAutoScrollToggled(bool enabled)
|
||||
{
|
||||
m_autoScroll = enabled;
|
||||
if (enabled)
|
||||
m_textEdit->verticalScrollBar()->setValue(
|
||||
m_textEdit->verticalScrollBar()->maximum());
|
||||
}
|
||||
Reference in New Issue
Block a user