From 43772e985cc6c64ad6364d5d575eb19774d0a64c Mon Sep 17 00:00:00 2001 From: 0penBrain <48731257+0penBrain@users.noreply.github.com> Date: Mon, 20 Jun 2022 16:09:08 +0200 Subject: [PATCH] Gui: implement temporary blocker for console observer --- src/Base/ConsoleObserver.h | 38 ++++++++++++++++++++++++++++++++++++++ src/Gui/BitmapFactory.cpp | 12 +++++++----- src/Gui/MainWindow.cpp | 13 ++++++------- 3 files changed, 51 insertions(+), 12 deletions(-) diff --git a/src/Base/ConsoleObserver.h b/src/Base/ConsoleObserver.h index a75245373b..3d4df935b5 100644 --- a/src/Base/ConsoleObserver.h +++ b/src/Base/ConsoleObserver.h @@ -70,6 +70,44 @@ private: void Log (const char *sErr); }; +/** The ILoggerBlocker class + * This class allows to temporary block then automatically restore arbitrary message types + * on a particular console observer. + */ +class BaseExport ILoggerBlocker +{ +public: + // Constructor that will block message types passed as parameter. By default, all types are blocked. + inline explicit ILoggerBlocker(const char* co, ConsoleMsgFlags msgTypes = + ConsoleSingleton::MsgType_Txt | ConsoleSingleton::MsgType_Log | ConsoleSingleton::MsgType_Wrn | ConsoleSingleton::MsgType_Err); + // Disable copy & move constructors + ILoggerBlocker(ILoggerBlocker const&) = delete; + ILoggerBlocker(ILoggerBlocker const &&) = delete; + // Disable assignment & move-assignment operator + ILoggerBlocker& operator=(ILoggerBlocker const&) = delete; + ILoggerBlocker& operator=(ILoggerBlocker const&&) = delete; + // Destructor that will restore message type settings. + inline ~ILoggerBlocker(); +private: + ConsoleMsgFlags msgTypesBlocked = 0; // Stores message types blocked by the blocker + const char* conObs; // Stores console observer name that blocker acts on +}; + +ILoggerBlocker::ILoggerBlocker(const char* co, ConsoleMsgFlags msgTypes): +conObs(co) +{ + msgTypesBlocked = Console().SetEnabledMsgType(conObs, msgTypes, false); +} + +ILoggerBlocker::~ILoggerBlocker() +{ + auto debug = Console().SetEnabledMsgType(conObs, msgTypesBlocked, true); +#ifdef FC_DEBUG + if (debug != msgTypesBlocked) + Console().Warning("Enabled message types have been changed while ILoggerBlocker was set\n"); +#endif +} + class BaseExport RedirectStdOutput : public std::streambuf { public: diff --git a/src/Gui/BitmapFactory.cpp b/src/Gui/BitmapFactory.cpp index eeb436a237..7e6d1fd9bb 100644 --- a/src/Gui/BitmapFactory.cpp +++ b/src/Gui/BitmapFactory.cpp @@ -42,6 +42,7 @@ #include #include +#include #include "BitmapFactory.h" @@ -337,11 +338,12 @@ QPixmap BitmapFactoryInst::pixmapFromSvg(const QByteArray& originalContents, con image.fill(0x00000000); QPainter p(&image); - // tmp. disable the report window to suppress some bothering warnings - auto tmp = Base::Console().IsMsgTypeEnabled("ReportOutput", Base::ConsoleSingleton::MsgType_Wrn); - Base::Console().SetEnabledMsgType("ReportOutput", Base::ConsoleSingleton::MsgType_Wrn, false); - QSvgRenderer svg(contents); - Base::Console().SetEnabledMsgType("ReportOutput", Base::ConsoleSingleton::MsgType_Wrn, tmp); + QSvgRenderer svg; + { + // tmp. disable the report window to suppress some bothering warnings + const Base::ILoggerBlocker blocker("ReportOutput", Base::ConsoleSingleton::MsgType_Wrn); + svg.load(contents); + } svg.render(&p); p.end(); diff --git a/src/Gui/MainWindow.cpp b/src/Gui/MainWindow.cpp index e96afb9319..9e94227395 100644 --- a/src/Gui/MainWindow.cpp +++ b/src/Gui/MainWindow.cpp @@ -55,6 +55,7 @@ #include #include #include +#include #include #include #include @@ -1412,14 +1413,12 @@ void MainWindow::loadWindowSettings() pos.setX(qMin(qMax(pos.x(),x1-this->width()+30),x2-30)); pos.setY(qMin(qMax(pos.y(),y1-10),y2-10)); this->move(pos); - - // tmp. disable the report window to suppress some bothering warnings - auto tmp = Base::Console().IsMsgTypeEnabled("ReportOutput", Base::ConsoleSingleton::MsgType_Wrn); - Base::Console().SetEnabledMsgType("ReportOutput", Base::ConsoleSingleton::MsgType_Wrn, false); - this->restoreState(config.value(QString::fromLatin1("MainWindowState")).toByteArray()); + { + // tmp. disable the report window to suppress some bothering warnings + const Base::ILoggerBlocker blocker("ReportOutput", Base::ConsoleSingleton::MsgType_Wrn); + this->restoreState(config.value(QString::fromLatin1("MainWindowState")).toByteArray()); + } std::clog << "Main window restored" << std::endl; - Base::Console().SetEnabledMsgType("ReportOutput", Base::ConsoleSingleton::MsgType_Wrn, tmp); - bool max = config.value(QString::fromLatin1("Maximized"), false).toBool(); max ? showMaximized() : show();