From 2feb0c92c9606c39fa370ec8a79507fccfe6eeec Mon Sep 17 00:00:00 2001 From: Ladislav Michl Date: Thu, 12 Sep 2024 10:32:47 +0200 Subject: [PATCH 1/5] Main: inGuiMode() helper --- src/Main/MainGui.cpp | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/Main/MainGui.cpp b/src/Main/MainGui.cpp index 912c16570c..f2ddb98215 100644 --- a/src/Main/MainGui.cpp +++ b/src/Main/MainGui.cpp @@ -95,6 +95,16 @@ private: FILE* file; }; +static bool inGuiMode() +{ + // if console option is set then run in cmd mode + if (App::Application::Config()["Console"] == "1") { + return false; + } + return App::Application::Config()["RunMode"] == "Gui" + || App::Application::Config()["RunMode"] == "Internal"; +} + static void DisplayInfo(const QString& msg, bool preformatted = true) { if (App::Application::Config()["Console"] == "1") { @@ -312,12 +322,7 @@ int main(int argc, char** argv) std::streambuf* oldcerr = std::cerr.rdbuf(&stdcerr); try { - // if console option is set then run in cmd mode - if (App::Application::Config()["Console"] == "1") { - App::Application::runApplication(); - } - if (App::Application::Config()["RunMode"] == "Gui" - || App::Application::Config()["RunMode"] == "Internal") { + if (inGuiMode()) { Gui::Application::runApplication(); } else { From 0876b850f0e0f145de01b6951ea902603262113b Mon Sep 17 00:00:00 2001 From: Ladislav Michl Date: Thu, 17 Apr 2025 17:47:41 +0200 Subject: [PATCH 2/5] Main: use inGuiMode() consistently Dialogs to show exception follow different logic than presenting gui itself. Unify that. --- src/Main/MainGui.cpp | 50 ++++++++++++++++++++++---------------------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/src/Main/MainGui.cpp b/src/Main/MainGui.cpp index f2ddb98215..4d34d7d2db 100644 --- a/src/Main/MainGui.cpp +++ b/src/Main/MainGui.cpp @@ -105,33 +105,33 @@ static bool inGuiMode() || App::Application::Config()["RunMode"] == "Internal"; } -static void DisplayInfo(const QString& msg, bool preformatted = true) +static void displayInfo(const QString& msg, bool preformatted = true) { - if (App::Application::Config()["Console"] == "1") { - std::cout << msg.toStdString(); - return; + if (inGuiMode()) { + QString appName = QString::fromStdString(App::Application::Config()["ExeName"]); + QMessageBox msgBox; + msgBox.setIcon(QMessageBox::Information); + msgBox.setWindowTitle(appName); + msgBox.setDetailedText(msg); + msgBox.setText(preformatted ? QStringLiteral("
%1
").arg(msg) : msg); + msgBox.exec(); + } + else { + std::cout << msg.toStdString(); } - - QString appName = QString::fromStdString(App::Application::Config()["ExeName"]); - QMessageBox msgBox; - msgBox.setIcon(QMessageBox::Information); - msgBox.setWindowTitle(appName); - msgBox.setDetailedText(msg); - msgBox.setText(preformatted ? QStringLiteral("
%1
").arg(msg) : msg); - msgBox.exec(); } -static void DisplayCritical(const QString& msg, bool preformatted = true) +static void displayCritical(const QString& msg, bool preformatted = true) { - if (App::Application::Config()["Console"] == "1") { - std::cerr << msg.toStdString(); - return; + if (inGuiMode()) { + QString appName = QString::fromStdString(App::Application::Config()["ExeName"]); + QString title = QObject::tr("Initialization of %1 failed").arg(appName); + QString text = preformatted ? QStringLiteral("
%1
").arg(msg) : msg; + QMessageBox::critical(nullptr, title, text); + } + else { + std::cerr << msg.toStdString(); } - - QString appName = QString::fromStdString(App::Application::Config()["ExeName"]); - QString title = QObject::tr("Initialization of %1 failed").arg(appName); - QString text = preformatted ? QStringLiteral("
%1
").arg(msg) : msg; - QMessageBox::critical(nullptr, title, text); } int main(int argc, char** argv) @@ -254,7 +254,7 @@ int main(int argc, char** argv) catch (const Base::UnknownProgramOption& e) { QApplication app(argc, argv); QString msg = QString::fromLatin1(e.what()); - DisplayCritical(msg); + displayCritical(msg); exit(1); } catch (const Base::ProgramInformation& e) { @@ -271,7 +271,7 @@ int main(int argc, char** argv) msg = data; } - DisplayInfo(msg); + displayInfo(msg); exit(0); } catch (const Base::Exception& e) { @@ -298,7 +298,7 @@ int main(int argc, char** argv) "\nPlease contact the application's support team for more information.\n\n"); } - DisplayCritical(msg, false); + displayCritical(msg, false); exit(100); } catch (...) { @@ -309,7 +309,7 @@ int main(int argc, char** argv) QObject::tr("Unknown runtime error occurred while initializing %1.\n\n" "Please contact the application's support team for more information.\n\n") .arg(appName); - DisplayCritical(msg, false); + displayCritical(msg, false); exit(101); } From 2bae2a3242a0937e7f9a04f1680368ffdb515a53 Mon Sep 17 00:00:00 2001 From: Ladislav Michl Date: Thu, 17 Apr 2025 17:52:57 +0200 Subject: [PATCH 3/5] Main: create QString from std::string directly --- src/Main/MainGui.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Main/MainGui.cpp b/src/Main/MainGui.cpp index 4d34d7d2db..07d801b6bf 100644 --- a/src/Main/MainGui.cpp +++ b/src/Main/MainGui.cpp @@ -226,7 +226,7 @@ int main(int argc, char** argv) #endif // to set window icon on wayland, the desktop file has to be available to the compositor QGuiApplication::setDesktopFileName( - QString::fromLatin1(App::Application::Config()["DesktopFileName"].c_str())); + QString::fromStdString(App::Application::Config()["DesktopFileName"])); #if defined(_MSC_VER) // create a dump file when the application crashes @@ -277,7 +277,7 @@ int main(int argc, char** argv) catch (const Base::Exception& e) { // Popup an own dialog box instead of that one of Windows QApplication app(argc, argv); - QString appName = QString::fromLatin1(App::Application::Config()["ExeName"].c_str()); + QString appName = QString::fromStdString(App::Application::Config()["ExeName"]); QString msg; msg = QObject::tr("While initializing %1 the following exception occurred: '%2'\n\n" "Python is searching for its files in the following directories:\n%3\n\n" @@ -304,7 +304,7 @@ int main(int argc, char** argv) catch (...) { // Popup an own dialog box instead of that one of Windows QApplication app(argc, argv); - QString appName = QString::fromLatin1(App::Application::Config()["ExeName"].c_str()); + QString appName = QString::fromStdString(App::Application::Config()["ExeName"]); QString msg = QObject::tr("Unknown runtime error occurred while initializing %1.\n\n" "Please contact the application's support team for more information.\n\n") From 0276ff13f57b992b2a1aa1ac3f49972e47056283 Mon Sep 17 00:00:00 2001 From: Ladislav Michl Date: Fri, 18 Apr 2025 19:11:17 +0200 Subject: [PATCH 4/5] Main: output exception stream directly to stdout printf is used to output formated stringstream. Not only introductory information is somewhat repeated, but text can be sent directly to standard output. --- src/Main/MainCmd.cpp | 32 ++++++++++++++------------------ src/Main/MainPy.cpp | 10 ++++------ 2 files changed, 18 insertions(+), 24 deletions(-) diff --git a/src/Main/MainCmd.cpp b/src/Main/MainCmd.cpp index 515d726845..4f75e013b6 100644 --- a/src/Main/MainCmd.cpp +++ b/src/Main/MainCmd.cpp @@ -36,8 +36,7 @@ #endif // HAVE_CONFIG_H #include -#include - +#include #include // FreeCAD Base header @@ -109,31 +108,28 @@ int main(int argc, char** argv) } catch (const Base::Exception& e) { std::string appName = App::Application::Config()["ExeName"]; - std::stringstream msg; - msg << "While initializing " << appName << " the following exception occurred: '" - << e.what() << "'\n\n"; - msg << "Python is searching for its runtime files in the following directories:\n" - << Base::Interpreter().getPythonPath() << "\n\n"; - msg << "Python version information:\n" << Py_GetVersion() << "\n"; + std::cout << "While initializing " << appName << " the following exception occurred: '" + << e.what() << "'\n\n"; + std::cout << "Python is searching for its runtime files in the following directories:\n" + << Base::Interpreter().getPythonPath() << "\n\n"; + std::cout << "Python version information:\n" << Py_GetVersion() << "\n"; const char* pythonhome = getenv("PYTHONHOME"); if (pythonhome) { - msg << "\nThe environment variable PYTHONHOME is set to '" << pythonhome << "'."; - msg << "\nSetting this environment variable might cause Python to fail. Please contact " - "your administrator to unset it on your system.\n\n"; + std::cout << "\nThe environment variable PYTHONHOME is set to '" << pythonhome << "'."; + std::cout << "\nSetting this environment variable might cause Python to fail. " + "Please contact your administrator to unset it on your system."; } else { - msg << "\nPlease contact the application's support team for more information.\n\n"; + std::cout << "\nPlease contact the application's support team for more information."; } - - printf("Initialization of %s failed:\n%s", appName.c_str(), msg.str().c_str()); + std::cout << std::endl; exit(100); } catch (...) { std::string appName = App::Application::Config()["ExeName"]; - std::stringstream msg; - msg << "Unknown runtime error occurred while initializing " << appName << ".\n\n"; - msg << "Please contact the application's support team for more information.\n\n"; - printf("Initialization of %s failed:\n%s", appName.c_str(), msg.str().c_str()); + std::cout << "Unknown runtime error occurred while initializing " << appName << ".\n\n"; + std::cout << "Please contact the application's support team for more information."; + std::cout << std::endl; exit(101); } diff --git a/src/Main/MainPy.cpp b/src/Main/MainPy.cpp index a09c20f303..554af37e70 100644 --- a/src/Main/MainPy.cpp +++ b/src/Main/MainPy.cpp @@ -45,7 +45,6 @@ #endif // HAVE_CONFIG_H #include -#include #include #include @@ -176,11 +175,10 @@ PyMOD_INIT_FUNC(FreeCAD) } catch (const Base::Exception& e) { std::string appName = App::Application::Config()["ExeName"]; - std::stringstream msg; - msg << "While initializing " << appName << " the following exception occurred: '" - << e.what() << "'\n\n"; - msg << "\nPlease contact the application's support team for more information.\n\n"; - printf("Initialization of %s failed:\n%s", appName.c_str(), msg.str().c_str()); + std::cout << "While initializing " << appName << " the following exception occurred: '" + << e.what() << "'\n\n"; + std::cout << "Please contact the application's support team for more information." + << std::endl; } Base::EmptySequencer* seq = new Base::EmptySequencer(); From 8a0f94d59fc013977ff2ed7f43e4e79e2768fab0 Mon Sep 17 00:00:00 2001 From: Ladislav Michl Date: Fri, 18 Apr 2025 19:07:00 +0200 Subject: [PATCH 5/5] Main: unistd.h is not needed --- src/Main/MainCmd.cpp | 4 ---- src/Main/MainGui.cpp | 5 ----- src/Main/MainPy.cpp | 4 ---- 3 files changed, 13 deletions(-) diff --git a/src/Main/MainCmd.cpp b/src/Main/MainCmd.cpp index 4f75e013b6..54430ff2c8 100644 --- a/src/Main/MainCmd.cpp +++ b/src/Main/MainCmd.cpp @@ -27,10 +27,6 @@ #undef _PreComp_ #endif -#ifdef FC_OS_LINUX -#include -#endif - #if HAVE_CONFIG_H #include #endif // HAVE_CONFIG_H diff --git a/src/Main/MainGui.cpp b/src/Main/MainGui.cpp index 07d801b6bf..645e6df06e 100644 --- a/src/Main/MainGui.cpp +++ b/src/Main/MainGui.cpp @@ -28,15 +28,10 @@ #include #endif - #ifdef _PreComp_ #undef _PreComp_ #endif -#ifdef FC_OS_LINUX -#include -#endif - #if HAVE_CONFIG_H #include #endif // HAVE_CONFIG_H diff --git a/src/Main/MainPy.cpp b/src/Main/MainPy.cpp index 554af37e70..82d00e7d18 100644 --- a/src/Main/MainPy.cpp +++ b/src/Main/MainPy.cpp @@ -31,10 +31,6 @@ #include #endif -#if defined(FC_OS_LINUX) || defined(FC_OS_BSD) -#include -#endif - #ifdef FC_OS_MACOSX #include #include