More readable startup code.

This commit is contained in:
Leonardo Zide 2020-12-30 18:22:02 -08:00
parent 9f6c0abf23
commit e99c776878
3 changed files with 27 additions and 29 deletions

View file

@ -314,7 +314,7 @@ bool lcApplication::LoadPartsLibrary(const QList<QPair<QString, bool>>& LibraryP
return false;
}
bool lcApplication::Initialize(QList<QPair<QString, bool>>& LibraryPaths, bool& ShowWindow)
lcStartupMode lcApplication::Initialize(QList<QPair<QString, bool>>& LibraryPaths)
{
bool OnlyUseLibraryPaths = false;
bool SaveImage = false;
@ -696,8 +696,7 @@ bool lcApplication::Initialize(QList<QPair<QString, bool>>& 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<QPair<QString, bool>>& 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<QPair<QString, bool>>& 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<QPair<QString, bool>>& 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<QPair<QString, bool>>& 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<QPair<QString, bool>>& 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<QPair<QString, bool>>& LibraryPaths, bool&
}
}
if (ShowWindow)
if (!SaveAndExit)
{
gMainWindow->SetColorIndex(lcGetColorIndex(7));
gMainWindow->GetPartSelectionWidget()->SetDefaultPart();
@ -1024,7 +1014,7 @@ bool lcApplication::Initialize(QList<QPair<QString, bool>>& LibraryPaths, bool&
gMainWindow->show();
}
return true;
return SaveAndExit ? lcStartupMode::Success : lcStartupMode::ShowWindow;
}
void lcApplication::Shutdown()

View file

@ -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<QPair<QString, bool>>& LibraryPaths, bool& ShowWindow);
lcStartupMode Initialize(QList<QPair<QString, bool>>& LibraryPaths);
void Shutdown();
void ShowPreferencesDialog();
void SaveTabLayout() const;

View file

@ -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();