Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Qt::FontRole data #45

Merged
merged 2 commits into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,13 @@
"queue": "cpp",
"semaphore": "cpp",
"streambuf": "cpp",
"*.rh": "cpp"
"*.rh": "cpp",
"codecvt": "cpp",
"list": "cpp",
"map": "cpp",
"future": "cpp",
"iomanip": "cpp",
"cinttypes": "cpp"
},
"C_Cpp.default.configurationProvider": "ms-vscode.cmake-tools",
"sonarlint.pathToCompileCommands": "${workspaceFolder}/build/compile_commands.json"
Expand Down
21 changes: 21 additions & 0 deletions include/qspdlog/qspdlog.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <QFont>
#include <QWidget>

namespace spdlog
Expand Down Expand Up @@ -149,6 +150,26 @@ class QSpdLog : public QWidget
std::optional<QBrush> getLoggerBackground(std::string_view loggerName
) const;

/**
* @brief Set the text QFont for the messages of the corresponding
* logger.
*
* @param std::string_view the name of the logger of which to set the
* font
* @param std::optional<QFont> the QFont object or std::nullopt
*/
void setLoggerFont(std::string_view loggerName, std::optional<QFont> font);

/**
* @brief Get the text QFont for the messages of the corresponding
* logger.
*
* @param std::string_view the name of the logger of which to get the
* font from
* @return std::optional<QFont> the QFont object or std::nullopt
*/
std::optional<QFont> getLoggerFont(std::string_view loggerName) const;

/**
* @brief Set the policy of the auto-scrolling feature.
*
Expand Down
4 changes: 2 additions & 2 deletions sample/sample.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <QTimer>
#include <QToolBar>
#include <QToolButton>
#include <thread>

#include "qspdlog/qabstract_spdlog_toolbar.hpp"
#include "qspdlog/qspdlog.hpp"
Expand Down Expand Up @@ -76,15 +77,14 @@ void configureToolbar(
&QAction::triggered,
[ logger ](bool) {
// generate 10 messages with random levels
for (int i = 0; i < 10; ++i) {
for (int i = 0; i < 10; ++i)
logger->log(
static_cast<spdlog::level::level_enum>(
rand() % spdlog::level::off
),
"Message {}",
i
);
}
});

generateMultipleAction->connect(
Expand Down
16 changes: 16 additions & 0 deletions src/qspdlog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ void QSpdLog::registerToolbar(QAbstractSpdLogToolBar* toolbarInterface)
);

_sourceModel->setLoggerForeground(value.loggerName, value.textColor);

QFont f;
f.setBold(value.fontBold);
_sourceModel->setLoggerFont(value.loggerName, f);
});
connect(
autoScrollPolicyCombo,
Expand Down Expand Up @@ -253,3 +257,15 @@ std::optional<QBrush> QSpdLog::getLoggerBackground(std::string_view loggerName
{
return _sourceModel->getLoggerBackground(loggerName);
}

void QSpdLog::setLoggerFont(
std::string_view loggerName, std::optional<QFont> font
)
{
_sourceModel->setLoggerFont(loggerName, font);
}

std::optional<QFont> QSpdLog::getLoggerFont(std::string_view loggerName) const
{
return _sourceModel->getLoggerFont(loggerName);
}
45 changes: 45 additions & 0 deletions src/qspdlog_model.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <QBrush>
#include <QDateTime>
#include <QFile>
#include <QFont>
#include <QIcon>

#include "qspdlog_model.hpp"
Expand Down Expand Up @@ -146,6 +147,14 @@
break;
}

case Qt::FontRole: {
std::string loggerName = _items[ index.row() ].loggerName;
if (_fontMappings.contains(loggerName))
return _fontMappings.at(loggerName);

break;
}

default: {
break;
}
Expand All @@ -163,6 +172,7 @@

return QVariant();
}

void QSpdLogModel::setLoggerForeground(
std::string_view loggerName, std::optional<QColor> color
)
Expand All @@ -189,6 +199,7 @@
);
}
}

std::optional<QColor> QSpdLogModel::getLoggerForeground(
std::string_view loggerName
) const
Expand All @@ -198,6 +209,7 @@

return std::nullopt;
}

void QSpdLogModel::setLoggerBackground(
std::string_view loggerName, std::optional<QBrush> brush
)
Expand All @@ -224,6 +236,7 @@
);
}
}

std::optional<QBrush> QSpdLogModel::getLoggerBackground(
std::string_view loggerName
) const
Expand All @@ -233,3 +246,35 @@

return std::nullopt;
}

void QSpdLogModel::setLoggerFont(
std::string_view loggerName, std::optional<QFont> font
)
{
int lastRow = this->rowCount() - 1;
if (lastRow < 0)
lastRow = 0;
int lastColumn = this->columnCount() - 1;
if (lastColumn < 0)
lastColumn = 0;

Check warning on line 259 in src/qspdlog_model.cpp

View check run for this annotation

Codecov / codecov/patch

src/qspdlog_model.cpp#L259

Added line #L259 was not covered by tests
if (font.has_value()) {
_fontMappings[ std::string(loggerName) ] = font.value();
emit dataChanged(
this->index(0), this->index(lastRow, lastColumn), { Qt::FontRole }
);
} else if (_fontMappings.contains(std::string(loggerName))) {
_fontMappings.erase(std::string(loggerName));
emit dataChanged(
this->index(0), this->index(lastRow, lastColumn), { Qt::FontRole }
);
}
}

std::optional<QFont> QSpdLogModel::getLoggerFont(std::string_view loggerName
) const
{
if (_fontMappings.contains(std::string(loggerName)))
return _fontMappings.at(std::string(loggerName));

return std::nullopt;
}
5 changes: 5 additions & 0 deletions src/qspdlog_model.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <QAbstractListModel>
#include <deque>
#include <optional>
#include <QFont>

class QSpdLogModel : public QAbstractListModel
{
Expand Down Expand Up @@ -32,6 +33,9 @@ class QSpdLogModel : public QAbstractListModel
void setLoggerBackground(std::string_view loggerName, std::optional<QBrush> brush);
std::optional<QBrush> getLoggerBackground(std::string_view loggerName) const;

void setLoggerFont(std::string_view loggerName, std::optional<QFont> font);
std::optional<QFont> getLoggerFont(std::string_view loggerName) const;

#pragma region QAbstractListModel
int rowCount(const QModelIndex& parent = QModelIndex()) const override;
int columnCount(const QModelIndex& parent = QModelIndex()) const override;
Expand All @@ -47,4 +51,5 @@ class QSpdLogModel : public QAbstractListModel
std::optional<std::size_t> _maxEntries;
std::map<std::string, QBrush> _backgroundMappings;
std::map<std::string, QColor> _foregroundMappings;
std::map<std::string, QFont> _fontMappings;
};
29 changes: 27 additions & 2 deletions src/qspdlog_style_dialog.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <QBoxLayout>
#include <QCheckBox>
#include <QDialogButtonBox>
#include <QLineEdit>

Expand All @@ -19,10 +20,13 @@ QSpdLogStyleDialog::QSpdLogStyleDialog(QWidget* parent)
QLineEdit* textColorEdit = new QLineEdit();
textColorEdit->setPlaceholderText("Text color");
textColorEdit->setObjectName("textColorEdit");
QCheckBox* checkBoxBold = new QCheckBox("Bold");
checkBoxBold->setObjectName("checkBoxBold");

layout->addWidget(loggerNameEdit);
layout->addWidget(backgroundColorEdit);
layout->addWidget(textColorEdit);
layout->addWidget(checkBoxBold);

QDialogButtonBox* buttonBox =
new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
Expand All @@ -33,23 +37,41 @@ QSpdLogStyleDialog::QSpdLogStyleDialog(QWidget* parent)
loggerNameEdit,
&QLineEdit::textChanged,
this,
[ this, backgroundColorEdit, textColorEdit ](const QString& name) {
[ this, backgroundColorEdit, textColorEdit, checkBoxBold ](
const QString& name
) {
std::string namestdstr = name.toStdString();
auto bg = _model->getLoggerBackground(namestdstr);
auto fg = _model->getLoggerForeground(namestdstr);
auto fnt = _model->getLoggerFont(namestdstr);

if (bg)
backgroundColorEdit->setText(bg.value().color().name());
else
backgroundColorEdit->setText("");

if (fg)
textColorEdit->setText(fg.value().name());
else
textColorEdit->setText("");

if (fnt) {
bool isBold = fnt->bold();
checkBoxBold->setChecked(isBold);
} else {
checkBoxBold->setChecked(false);
}
});

connect(
buttonBox,
&QDialogButtonBox::accepted,
this,
[ this, loggerNameEdit, backgroundColorEdit, textColorEdit ]() {
[ this,
loggerNameEdit,
backgroundColorEdit,
textColorEdit,
checkBoxBold ]() {
if (!loggerNameEdit->text().isEmpty())
reject();

Expand All @@ -65,8 +87,11 @@ QSpdLogStyleDialog::QSpdLogStyleDialog(QWidget* parent)
else
_result.textColor = std::nullopt;

_result.fontBold = checkBoxBold->isChecked();

accept();
});

connect(buttonBox, &QDialogButtonBox::rejected, this, [ this ]() {
reject();
});
Expand Down
1 change: 1 addition & 0 deletions src/qspdlog_style_dialog.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class QSpdLogStyleDialog : public QDialog
std::string loggerName;
std::optional<QColor> backgroundColor;
std::optional<QColor> textColor;
bool fontBold;
};

public:
Expand Down
Loading
Loading