diff --git a/.vscode/settings.json b/.vscode/settings.json index e1bccd7..d7f4282 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -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" diff --git a/include/qspdlog/qspdlog.hpp b/include/qspdlog/qspdlog.hpp index 75fce2d..0754c0f 100644 --- a/include/qspdlog/qspdlog.hpp +++ b/include/qspdlog/qspdlog.hpp @@ -1,5 +1,6 @@ #pragma once +#include #include namespace spdlog @@ -149,6 +150,26 @@ class QSpdLog : public QWidget std::optional 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 the QFont object or std::nullopt + */ + void setLoggerFont(std::string_view loggerName, std::optional 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 the QFont object or std::nullopt + */ + std::optional getLoggerFont(std::string_view loggerName) const; + /** * @brief Set the policy of the auto-scrolling feature. * diff --git a/sample/sample.cpp b/sample/sample.cpp index 1912c94..d15e525 100644 --- a/sample/sample.cpp +++ b/sample/sample.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #include "qspdlog/qabstract_spdlog_toolbar.hpp" #include "qspdlog/qspdlog.hpp" @@ -76,7 +77,7 @@ 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( rand() % spdlog::level::off @@ -84,7 +85,6 @@ void configureToolbar( "Message {}", i ); - } }); generateMultipleAction->connect( diff --git a/src/qspdlog.cpp b/src/qspdlog.cpp index db838aa..9ecc0c5 100644 --- a/src/qspdlog.cpp +++ b/src/qspdlog.cpp @@ -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, @@ -253,3 +257,15 @@ std::optional QSpdLog::getLoggerBackground(std::string_view loggerName { return _sourceModel->getLoggerBackground(loggerName); } + +void QSpdLog::setLoggerFont( + std::string_view loggerName, std::optional font +) +{ + _sourceModel->setLoggerFont(loggerName, font); +} + +std::optional QSpdLog::getLoggerFont(std::string_view loggerName) const +{ + return _sourceModel->getLoggerFont(loggerName); +} \ No newline at end of file diff --git a/src/qspdlog_model.cpp b/src/qspdlog_model.cpp index e0d0a76..1b0ac78 100644 --- a/src/qspdlog_model.cpp +++ b/src/qspdlog_model.cpp @@ -1,6 +1,7 @@ #include #include #include +#include #include #include "qspdlog_model.hpp" @@ -146,6 +147,14 @@ QVariant QSpdLogModel::data(const QModelIndex& index, int role) const break; } + case Qt::FontRole: { + std::string loggerName = _items[ index.row() ].loggerName; + if (_fontMappings.contains(loggerName)) + return _fontMappings.at(loggerName); + + break; + } + default: { break; } @@ -163,6 +172,7 @@ QVariant QSpdLogModel::headerData( return QVariant(); } + void QSpdLogModel::setLoggerForeground( std::string_view loggerName, std::optional color ) @@ -189,6 +199,7 @@ void QSpdLogModel::setLoggerForeground( ); } } + std::optional QSpdLogModel::getLoggerForeground( std::string_view loggerName ) const @@ -198,6 +209,7 @@ std::optional QSpdLogModel::getLoggerForeground( return std::nullopt; } + void QSpdLogModel::setLoggerBackground( std::string_view loggerName, std::optional brush ) @@ -224,6 +236,7 @@ void QSpdLogModel::setLoggerBackground( ); } } + std::optional QSpdLogModel::getLoggerBackground( std::string_view loggerName ) const @@ -233,3 +246,35 @@ std::optional QSpdLogModel::getLoggerBackground( return std::nullopt; } + +void QSpdLogModel::setLoggerFont( + std::string_view loggerName, std::optional font +) +{ + int lastRow = this->rowCount() - 1; + if (lastRow < 0) + lastRow = 0; + int lastColumn = this->columnCount() - 1; + if (lastColumn < 0) + lastColumn = 0; + 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 QSpdLogModel::getLoggerFont(std::string_view loggerName +) const +{ + if (_fontMappings.contains(std::string(loggerName))) + return _fontMappings.at(std::string(loggerName)); + + return std::nullopt; +} \ No newline at end of file diff --git a/src/qspdlog_model.hpp b/src/qspdlog_model.hpp index 6c5b1df..183d592 100644 --- a/src/qspdlog_model.hpp +++ b/src/qspdlog_model.hpp @@ -3,6 +3,7 @@ #include #include #include +#include class QSpdLogModel : public QAbstractListModel { @@ -32,6 +33,9 @@ class QSpdLogModel : public QAbstractListModel void setLoggerBackground(std::string_view loggerName, std::optional brush); std::optional getLoggerBackground(std::string_view loggerName) const; + void setLoggerFont(std::string_view loggerName, std::optional font); + std::optional 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; @@ -47,4 +51,5 @@ class QSpdLogModel : public QAbstractListModel std::optional _maxEntries; std::map _backgroundMappings; std::map _foregroundMappings; + std::map _fontMappings; }; diff --git a/src/qspdlog_style_dialog.cpp b/src/qspdlog_style_dialog.cpp index f9b25cc..7bac4d3 100644 --- a/src/qspdlog_style_dialog.cpp +++ b/src/qspdlog_style_dialog.cpp @@ -1,4 +1,5 @@ #include +#include #include #include @@ -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); @@ -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(); @@ -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(); }); diff --git a/src/qspdlog_style_dialog.hpp b/src/qspdlog_style_dialog.hpp index 3a33afc..f68445e 100644 --- a/src/qspdlog_style_dialog.hpp +++ b/src/qspdlog_style_dialog.hpp @@ -14,6 +14,7 @@ class QSpdLogStyleDialog : public QDialog std::string loggerName; std::optional backgroundColor; std::optional textColor; + bool fontBold; }; public: diff --git a/test/test_qspdlog.cpp b/test/test_qspdlog.cpp index db95da3..bea4cec 100644 --- a/test/test_qspdlog.cpp +++ b/test/test_qspdlog.cpp @@ -15,6 +15,7 @@ #include #include #include +#include #include "qspdlog/qabstract_spdlog_toolbar.hpp" #include "qspdlog/qspdlog.hpp" @@ -169,6 +170,21 @@ private slots: QCOMPARE(widget.getLoggerForeground("test"), std::nullopt); } + void fontTest() + { + QSpdLog widget; + QFont testFont; + testFont.setBold(true); + QCOMPARE(widget.getLoggerBackground("test"), std::nullopt); + widget.setLoggerFont("test", testFont); + QCOMPARE(widget.getLoggerFont("test"), testFont); + QCOMPARE(widget.getLoggerFont("test2"), std::nullopt); + widget.setLoggerFont("test", std::nullopt); + widget.setLoggerFont("test2", testFont); + QCOMPARE(widget.getLoggerFont("test"), std::nullopt); + QCOMPARE(widget.getLoggerFont("test2"), testFont); + } + void runToolbarTests() { std::vector> toolbars; @@ -480,8 +496,10 @@ private slots: widget.registerToolbar(toolbar.get()); QAction* style = toolbar->style(); + QFont f; + f.setBold(true); auto dialogManipThread = - manipulateStyleDialog("test", "#ff0000", "#00ff00", true); + manipulateStyleDialog("test", "#ff0000", "#00ff00", f, true); style->trigger(); dialogManipThread.join(); @@ -504,6 +522,10 @@ private slots: model->data(index, Qt::ForegroundRole).value(), QColor("#00ff00") ); + QCOMPARE( + model->data(index, Qt::FontRole).value(), + f + ); index = model->index(1, 3); QCOMPARE( @@ -516,9 +538,12 @@ private slots: QCOMPARE( model->data(index, Qt::ForegroundRole).value(), QColor {} ); + QCOMPARE( + model->data(index, Qt::FontRole).value(), QFont {} + ); dialogManipThread = - manipulateStyleDialog("test", std::nullopt, std::nullopt, true); + manipulateStyleDialog("test", std::nullopt, std::nullopt, std::nullopt, true); style->trigger(); dialogManipThread.join(); @@ -535,8 +560,12 @@ private slots: model->data(index, Qt::ForegroundRole).value(), QColor("#00ff00") ); + QCOMPARE( + model->data(index, Qt::FontRole).value(), + f + ); - dialogManipThread = manipulateStyleDialog("test", "", "", true); + dialogManipThread = manipulateStyleDialog("test", "", "", QFont(), true); style->trigger(); dialogManipThread.join(); @@ -551,8 +580,11 @@ private slots: QCOMPARE( model->data(index, Qt::ForegroundRole).value(), QColor {} ); + QCOMPARE( + model->data(index, Qt::FontRole).value(), QFont {} + ); - dialogManipThread = manipulateStyleDialog("test", "", "", false); + dialogManipThread = manipulateStyleDialog("test", "", "", QFont(), false); style->trigger(); dialogManipThread.join(); @@ -567,6 +599,9 @@ private slots: QCOMPARE( model->data(index, Qt::ForegroundRole).value(), QColor {} ); + QCOMPARE( + model->data(index, Qt::FontRole).value(), QFont {} + ); index = model->index(1, 3); QCOMPARE( model->data(index, Qt::DisplayRole).value(), @@ -578,6 +613,9 @@ private slots: QCOMPARE( model->data(index, Qt::ForegroundRole).value(), QColor {} ); + QCOMPARE( + model->data(index, Qt::FontRole).value(), QFont {} + ); } private: @@ -585,12 +623,14 @@ private slots: std::optional name, std::optional background, std::optional foreground, + std::optional font, bool accept ) const { return std::thread([ n = std::move(name), bg = std::move(background), fg = std::move(foreground), + fnt = std::move(font), accept ] { QDialog* dialog; bool success = QTest::qWaitFor( @@ -619,6 +659,7 @@ private slots: [ name = std::move(n), background = std::move(bg), foreground = std::move(fg), + font = std::move(fnt), accept, dialog ] { QVERIFY(dialog); @@ -631,6 +672,9 @@ private slots: QLineEdit* textColorEdit = dialog->findChild("textColorEdit"); QVERIFY(textColorEdit); + QCheckBox* checkBoxBold = + dialog->findChild("checkBoxBold"); + QVERIFY(checkBoxBold); if (name) loggerNameEdit->setText(name.value()); @@ -640,6 +684,9 @@ private slots: if (foreground) textColorEdit->setText(foreground.value()); + + if (font) + checkBoxBold->setChecked(font.value().bold()); QDialogButtonBox* buttonBox = dialog->findChild("buttonBox");