From e99c776878be7c0418cd38ed20d134b731394361 Mon Sep 17 00:00:00 2001 From: Leonardo Zide Date: Wed, 30 Dec 2020 18:22:02 -0800 Subject: [PATCH] More readable startup code. --- common/lc_application.cpp | 40 +++++++++++++++------------------------ common/lc_application.h | 9 ++++++++- qt/qtmain.cpp | 7 ++++--- 3 files changed, 27 insertions(+), 29 deletions(-) diff --git a/common/lc_application.cpp b/common/lc_application.cpp index 14317c51..6ed413fa 100644 --- a/common/lc_application.cpp +++ b/common/lc_application.cpp @@ -314,7 +314,7 @@ bool lcApplication::LoadPartsLibrary(const QList>& LibraryP return false; } -bool lcApplication::Initialize(QList>& LibraryPaths, bool& ShowWindow) +lcStartupMode lcApplication::Initialize(QList>& LibraryPaths) { bool OnlyUseLibraryPaths = false; bool SaveImage = false; @@ -696,8 +696,7 @@ bool lcApplication::Initialize(QList>& LibraryPaths, bool& #endif printf("Compiled " __DATE__ "\n"); - ShowWindow = false; - return true; + return lcStartupMode::Success; } else if (Param == QLatin1String("-?") || Param == QLatin1String("--help")) { @@ -736,8 +735,7 @@ bool lcApplication::Initialize(QList>& LibraryPaths, bool& printf(" -?, --help: Display this help message and exit.\n"); printf(" \n"); - ShowWindow = false; - return true; + return lcStartupMode::Success; } else { @@ -747,9 +745,14 @@ bool lcApplication::Initialize(QList>& LibraryPaths, bool& } if (!ParseOK) + return lcStartupMode::Error; + + const bool SaveAndExit = (SaveImage || SaveWavefront || Save3DS || SaveCOLLADA || SaveHTML); + + if (SaveAndExit && ProjectName.isEmpty()) { - ShowWindow = false; - return false; + printf("No file name specified.\n"); + return lcStartupMode::Error; } if (AASamples > 1) @@ -770,20 +773,7 @@ bool lcApplication::Initialize(QList>& LibraryPaths, bool& lcLoadDefaultKeyboardShortcuts(); lcLoadDefaultMouseShortcuts(); - if (SaveImage || SaveWavefront || Save3DS || SaveCOLLADA || SaveHTML) - { - ShowWindow = false; - - if (ProjectName.isEmpty()) - { - printf("No file name specified.\n"); - return false; - } - } - else - ShowWindow = true; - - if (!LoadPartsLibrary(LibraryPaths, OnlyUseLibraryPaths, ShowWindow)) + if (!LoadPartsLibrary(LibraryPaths, OnlyUseLibraryPaths, !SaveAndExit)) { QString Message; @@ -792,7 +782,7 @@ bool lcApplication::Initialize(QList>& LibraryPaths, bool& else Message = tr("LeoCAD could not load Parts Library.\n\nPlease visit https://www.leocad.org for information on how to download and install a library."); - if (ShowWindow) + if (!SaveAndExit) QMessageBox::information(gMainWindow, tr("LeoCAD"), Message); else fprintf(stderr, "%s", Message.toLatin1().constData()); @@ -803,7 +793,7 @@ bool lcApplication::Initialize(QList>& LibraryPaths, bool& Project* NewProject = new Project(); SetProject(NewProject); - if (ShowWindow && ProjectName.isEmpty() && lcGetProfileInt(LC_PROFILE_AUTOLOAD_MOSTRECENT)) + if (!SaveAndExit && ProjectName.isEmpty() && lcGetProfileInt(LC_PROFILE_AUTOLOAD_MOSTRECENT)) ProjectName = lcGetProfileString(LC_PROFILE_RECENT_FILE1); if (!ProjectName.isEmpty() && gMainWindow->OpenProject(ProjectName)) @@ -1016,7 +1006,7 @@ bool lcApplication::Initialize(QList>& LibraryPaths, bool& } } - if (ShowWindow) + if (!SaveAndExit) { gMainWindow->SetColorIndex(lcGetColorIndex(7)); gMainWindow->GetPartSelectionWidget()->SetDefaultPart(); @@ -1024,7 +1014,7 @@ bool lcApplication::Initialize(QList>& LibraryPaths, bool& gMainWindow->show(); } - return true; + return SaveAndExit ? lcStartupMode::Success : lcStartupMode::ShowWindow; } void lcApplication::Shutdown() diff --git a/common/lc_application.h b/common/lc_application.h index 775306c1..e05aa8ec 100644 --- a/common/lc_application.h +++ b/common/lc_application.h @@ -70,6 +70,13 @@ public: int mDrawPreviewAxis; }; +enum class lcStartupMode +{ + ShowWindow, + Success, + Error +}; + class lcApplication : public QApplication { Q_OBJECT @@ -79,7 +86,7 @@ public: ~lcApplication(); void SetProject(Project* Project); - bool Initialize(QList>& LibraryPaths, bool& ShowWindow); + lcStartupMode Initialize(QList>& LibraryPaths); void Shutdown(); void ShowPreferencesDialog(); void SaveTabLayout() const; diff --git a/qt/qtmain.cpp b/qt/qtmain.cpp index 6411bfad..3d43ef71 100644 --- a/qt/qtmain.cpp +++ b/qt/qtmain.cpp @@ -181,13 +181,14 @@ int main(int argc, char *argv[]) setlocale(LC_NUMERIC, "C"); - bool ShowWindow; - if (!Application.Initialize(LibraryPaths, ShowWindow)) + lcStartupMode StartupMode = Application.Initialize(LibraryPaths); + + if (StartupMode == lcStartupMode::Error) return 1; int ExecReturn = 0; - if (ShowWindow) + if (StartupMode == lcStartupMode::ShowWindow) { #if !LC_DISABLE_UPDATE_CHECK lcDoInitialUpdateCheck();