Add color lines button

Signed-off-by: Yihe Li <winmikedows@hotmail.com>
This commit is contained in:
Yihe Li 2025-04-15 05:07:56 +08:00
parent 4ac6a0629b
commit 1ee1bab067
No known key found for this signature in database
7 changed files with 55 additions and 2 deletions

View File

@ -149,3 +149,15 @@ bool LogModel::wrapLines() const
{
return m_lineWrap;
}
void LogModel::setColorLines(bool state)
{
if (m_colorLines != state) {
m_colorLines = state;
}
}
bool LogModel::colorLines() const
{
return m_colorLines;
}

View File

@ -27,6 +27,8 @@ class LogModel : public QAbstractListModel {
void setLineWrap(bool state);
bool wrapLines() const;
void setColorLines(bool state);
bool colorLines() const;
enum Roles { LevelRole = Qt::UserRole };
@ -47,6 +49,7 @@ class LogModel : public QAbstractListModel {
QString m_overflowMessage = "OVERFLOW";
bool m_suspended = false;
bool m_lineWrap = true;
bool m_colorLines = true;
private:
Q_DISABLE_COPY(LogModel)

View File

@ -180,6 +180,13 @@ void LogPage::modelStateToUI()
ui->text->setWordWrap(false);
ui->wrapCheckbox->setCheckState(Qt::Unchecked);
}
if (m_model->colorLines()) {
ui->text->setColorLines(true);
ui->colorCheckbox->setCheckState(Qt::Checked);
} else {
ui->text->setColorLines(false);
ui->colorCheckbox->setCheckState(Qt::Unchecked);
}
if (m_model->suspended()) {
ui->trackLogCheckbox->setCheckState(Qt::Unchecked);
} else {
@ -193,6 +200,7 @@ void LogPage::UIToModelState()
return;
}
m_model->setLineWrap(ui->wrapCheckbox->checkState() == Qt::Checked);
m_model->setColorLines(ui->colorCheckbox->checkState() == Qt::Checked);
m_model->suspend(ui->trackLogCheckbox->checkState() != Qt::Checked);
}
@ -282,6 +290,14 @@ void LogPage::on_wrapCheckbox_clicked(bool checked)
m_model->setLineWrap(checked);
}
void LogPage::on_colorCheckbox_clicked(bool checked)
{
ui->text->setColorLines(checked);
if (!m_model)
return;
m_model->setColorLines(checked);
}
void LogPage::on_findButton_clicked()
{
auto modifiers = QApplication::keyboardModifiers();

View File

@ -82,6 +82,7 @@ class LogPage : public QWidget, public BasePage {
void on_trackLogCheckbox_clicked(bool checked);
void on_wrapCheckbox_clicked(bool checked);
void on_colorCheckbox_clicked(bool checked);
void on_findButton_clicked();
void findActivated();

View File

@ -74,6 +74,16 @@
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="colorCheckbox">
<property name="text">
<string>Color lines</string>
</property>
<property name="checked">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
@ -170,6 +180,7 @@
<tabstop>tabWidget</tabstop>
<tabstop>trackLogCheckbox</tabstop>
<tabstop>wrapCheckbox</tabstop>
<tabstop>colorCheckbox</tabstop>
<tabstop>btnCopy</tabstop>
<tabstop>btnPaste</tabstop>
<tabstop>btnClear</tabstop>

View File

@ -60,6 +60,14 @@ void LogView::setWordWrap(bool wrapping)
}
}
void LogView::setColorLines(bool colorLines)
{
if (m_colorLines == colorLines)
return;
m_colorLines = colorLines;
repopulate();
}
void LogView::setModel(QAbstractItemModel* model)
{
if (m_model) {
@ -130,11 +138,11 @@ void LogView::rowsInserted(const QModelIndex& parent, int first, int last)
format.setFont(font.value<QFont>());
}
auto fg = m_model->data(idx, Qt::ForegroundRole);
if (fg.isValid()) {
if (fg.isValid() && m_colorLines) {
format.setForeground(fg.value<QColor>());
}
auto bg = m_model->data(idx, Qt::BackgroundRole);
if (bg.isValid()) {
if (bg.isValid() && m_colorLines) {
format.setBackground(bg.value<QColor>());
}
cursor.movePosition(QTextCursor::End);

View File

@ -15,6 +15,7 @@ class LogView : public QPlainTextEdit {
public slots:
void setWordWrap(bool wrapping);
void setColorLines(bool colorLines);
void findNext(const QString& what, bool reverse);
void scrollToBottom();
@ -32,4 +33,5 @@ class LogView : public QPlainTextEdit {
QTextCharFormat* m_defaultFormat = nullptr;
bool m_scroll = false;
bool m_scrolling = false;
bool m_colorLines = true;
};