diff --git a/.gitignore b/.gitignore index 5fa7b296..5ca79d78 100644 --- a/.gitignore +++ b/.gitignore @@ -15,6 +15,7 @@ *.qm *.ldr *.dat +*.mpd !/resources/*.ldr *.dae *.3ds diff --git a/common/lc_application.cpp b/common/lc_application.cpp index 5a69bb67..a163205c 100644 --- a/common/lc_application.cpp +++ b/common/lc_application.cpp @@ -82,63 +82,36 @@ void lcApplication::ExportClipboard(const QByteArray& Clipboard) SetClipboard(Clipboard); } -bool lcApplication::LoadPiecesLibrary(const char* LibPath, const char* LibraryInstallPath, const char* LDrawPath) +bool lcApplication::LoadPiecesLibrary(const QList>& LibraryPaths) { if (mLibrary == nullptr) mLibrary = new lcPiecesLibrary(); - if (LibPath && LibPath[0]) - return mLibrary->Load(LibPath); - char* EnvPath = getenv("LEOCAD_LIB"); if (EnvPath && EnvPath[0]) - { return mLibrary->Load(EnvPath); - } QString CustomPath = lcGetProfileString(LC_PROFILE_PARTS_LIBRARY); if (!CustomPath.isEmpty()) return mLibrary->Load(CustomPath); - if (LibraryInstallPath && LibraryInstallPath[0]) + for (const QPair& LibraryPathEntry : LibraryPaths) { - char LibraryPath[LC_MAXPATH]; - - strcpy(LibraryPath, LibraryInstallPath); - - size_t i = strlen(LibraryPath) - 1; - if ((LibraryPath[i] != '\\') && (LibraryPath[i] != '/')) - strcat(LibraryPath, "/"); - - strcat(LibraryPath, "library.bin"); - - if (mLibrary->Load(LibraryPath)) + if (mLibrary->Load(LibraryPathEntry.first)) { - mLibrary->SetOfficialPieces(); + if (LibraryPathEntry.second) + mLibrary->SetOfficialPieces(); + return true; } } - if (LDrawPath && LDrawPath[0]) - { - char LibraryPath[LC_MAXPATH]; - - strcpy(LibraryPath, LDrawPath); - - size_t i = strlen(LibraryPath) - 1; - if ((LibraryPath[i] != '\\') && (LibraryPath[i] != '/')) - strcat(LibraryPath, "/"); - - if (mLibrary->Load(LibraryPath)) - return true; - } - return false; } -void lcApplication::ParseIntegerArgument(int* CurArg, int argc, char* argv[], int* Value) +void lcApplication::ParseIntegerArgument(int* CurArg, int argc, char* argv[], int* Value) const { if (argc > (*CurArg + 1)) { @@ -160,7 +133,7 @@ void lcApplication::ParseIntegerArgument(int* CurArg, int argc, char* argv[], in } } -void lcApplication::ParseStringArgument(int* CurArg, int argc, char* argv[], char** Value) +void lcApplication::ParseStringArgument(int* CurArg, int argc, char* argv[], const char** Value) const { if (argc > (*CurArg + 1)) { @@ -173,12 +146,9 @@ void lcApplication::ParseStringArgument(int* CurArg, int argc, char* argv[], cha } } -bool lcApplication::Initialize(int argc, char* argv[], const char* LibraryInstallPath, const char* LDrawPath, bool& ShowWindow) +bool lcApplication::Initialize(int argc, char* argv[], QList>& LibraryPaths, bool& ShowWindow) { // todo: parse command line using Qt to handle multibyte strings. - char* LibPath = nullptr; - - // Image output options. bool SaveImage = false; bool SaveWavefront = false; bool Save3DS = false; @@ -198,7 +168,6 @@ bool lcApplication::Initialize(int argc, char* argv[], const char* LibraryInstal char* Save3DSName = nullptr; char* SaveCOLLADAName = nullptr; - // Parse the command line arguments. for (int i = 1; i < argc; i++) { char* Param = argv[i]; @@ -207,7 +176,13 @@ bool lcApplication::Initialize(int argc, char* argv[], const char* LibraryInstal { if ((strcmp(Param, "-l") == 0) || (strcmp(Param, "--libpath") == 0)) { + const char* LibPath = nullptr; ParseStringArgument(&i, argc, argv, &LibPath); + if (LibPath && LibPath[0]) + { + LibraryPaths.clear(); + LibraryPaths += qMakePair(LibPath, false); + } } else if ((strcmp(Param, "-i") == 0) || (strcmp(Param, "--image") == 0)) { @@ -345,7 +320,7 @@ bool lcApplication::Initialize(int argc, char* argv[], const char* LibraryInstal ShowWindow = !SaveImage && !SaveWavefront && !Save3DS && !SaveCOLLADA; - if (!LoadPiecesLibrary(LibPath, LibraryInstallPath, LDrawPath)) + if (!LoadPiecesLibrary(LibraryPaths)) { QString Message; diff --git a/common/lc_application.h b/common/lc_application.h index 8ff18532..d37f99ce 100644 --- a/common/lc_application.h +++ b/common/lc_application.h @@ -42,11 +42,11 @@ public: ~lcApplication(); void SetProject(Project* Project); - bool Initialize(int argc, char *argv[], const char* LibraryInstallPath, const char* LDrawPath, bool& ShowWindow); + bool Initialize(int argc, char *argv[], QList>& LibraryPaths, bool& ShowWindow); void Shutdown(); void ShowPreferencesDialog(); - bool LoadPiecesLibrary(const char* LibPath, const char* LibraryInstallPath, const char* LDrawPath); + bool LoadPiecesLibrary(const QList>& LibraryPaths); void SetClipboard(const QByteArray& Clipboard); void ExportClipboard(const QByteArray& Clipboard); @@ -57,8 +57,8 @@ public: QByteArray mClipboard; protected: - void ParseIntegerArgument(int* CurArg, int argc, char* argv[], int* Value); - void ParseStringArgument(int* CurArg, int argc, char* argv[], char** Value); + void ParseIntegerArgument(int* CurArg, int argc, char* argv[], int* Value) const; + void ParseStringArgument(int* CurArg, int argc, char* argv[], const char** Value) const; }; extern lcApplication* g_App; diff --git a/qt/qtmain.cpp b/qt/qtmain.cpp index c1685be9..a4daf594 100644 --- a/qt/qtmain.cpp +++ b/qt/qtmain.cpp @@ -147,37 +147,35 @@ int main(int argc, char *argv[]) qRegisterMetaTypeStreamOperators >("QList"); g_App = new lcApplication(); + QList> LibraryPaths; -#if defined(Q_OS_WIN) - char libPath[LC_MAXPATH], *ptr; - strcpy(libPath, argv[0]); - ptr = strrchr(libPath,'\\'); - if (ptr) - *(++ptr) = 0; - +#ifdef Q_OS_WIN lcRegisterShellFileTypes(); lcSehInit(); -#elif defined(Q_OS_MAC) - QDir bundlePath = QDir(QCoreApplication::applicationDirPath()); - bundlePath.cdUp(); - bundlePath.cdUp(); - bundlePath = QDir::cleanPath(bundlePath.absolutePath() + "/Contents/Resources/"); - QByteArray pathArray = bundlePath.absolutePath().toLocal8Bit(); - const char* libPath = pathArray.data(); -#else - const char* libPath = LC_INSTALL_PREFIX "/share/leocad/"; + + LibraryPaths += qMakePair(QDir::cleanPath(QCoreApplication::applicationDirPath() + "/library.bin"), true); +#endif + +#ifdef Q_OS_LINUX + LibraryPaths += qMakePair(QDir::cleanPath(QCoreApplication::applicationDirPath() + "/../share/leocad/library.bin"), true); +#endif + +#ifdef Q_OS_MAC + LibraryPaths += qMakePair(QDir::cleanPath(QCoreApplication::applicationDirPath() + "/../../Contents/Resources/library.bin"), true); +#endif + +#ifdef LC_INSTALL_PREFIX + LibraryPaths += qMakePair(QLatin1String(LC_INSTALL_PREFIX "/share/leocad/"), false); #endif #ifdef LC_LDRAW_LIBRARY_PATH - const char* LDrawPath = LC_LDRAW_LIBRARY_PATH; -#else - const char* LDrawPath = nullptr; + LibraryPaths += qMakePair(QLatin1String(LC_LDRAW_LIBRARY_PATH), false); #endif setlocale(LC_NUMERIC, "C"); bool ShowWindow; - if (!g_App->Initialize(argc, argv, libPath, LDrawPath, ShowWindow)) + if (!g_App->Initialize(argc, argv, LibraryPaths, ShowWindow)) return 1; int ExecReturn = 0;