leocad/common/lc_application.cpp

475 lines
13 KiB
C++
Raw Normal View History

2013-08-09 06:57:18 +02:00
#include "lc_global.h"
#include <stdio.h>
#include "lc_application.h"
#include "lc_colors.h"
#include "lc_library.h"
#include "lc_profile.h"
#include "system.h"
#include "opengl.h"
#include "project.h"
#include "image.h"
2013-08-16 03:25:51 +02:00
#include "lc_mainwindow.h"
2013-08-09 06:57:18 +02:00
#include "lc_shortcuts.h"
2014-12-04 02:47:28 +01:00
#include "view.h"
2013-08-09 06:57:18 +02:00
lcApplication* g_App;
void lcPreferences::LoadDefaults()
{
2014-10-05 07:21:51 +02:00
mFixedAxes = lcGetProfileInt(LC_PROFILE_FIXED_AXES);
mMouseSensitivity = lcGetProfileInt(LC_PROFILE_MOUSE_SENSITIVITY);
mLightingMode = (lcLightingMode)lcGetProfileInt(LC_PROFILE_LIGHTING_MODE);
mDrawAxes = lcGetProfileInt(LC_PROFILE_DRAW_AXES);
mDrawEdgeLines = lcGetProfileInt(LC_PROFILE_DRAW_EDGE_LINES);
mLineWidth = lcGetProfileFloat(LC_PROFILE_LINE_WIDTH);
mDrawGridStuds = lcGetProfileInt(LC_PROFILE_GRID_STUDS);
mGridStudColor = lcGetProfileInt(LC_PROFILE_GRID_STUD_COLOR);
mDrawGridLines = lcGetProfileInt(LC_PROFILE_GRID_LINES);
mGridLineSpacing = lcGetProfileInt(LC_PROFILE_GRID_LINE_SPACING);
mGridLineColor = lcGetProfileInt(LC_PROFILE_GRID_LINE_COLOR);
}
void lcPreferences::SaveDefaults()
{
2014-10-05 07:21:51 +02:00
lcSetProfileInt(LC_PROFILE_FIXED_AXES, mFixedAxes);
lcSetProfileInt(LC_PROFILE_MOUSE_SENSITIVITY, mMouseSensitivity);
lcSetProfileInt(LC_PROFILE_LIGHTING_MODE, mLightingMode);
lcSetProfileInt(LC_PROFILE_DRAW_AXES, mDrawAxes);
lcSetProfileInt(LC_PROFILE_DRAW_EDGE_LINES, mDrawEdgeLines);
lcSetProfileFloat(LC_PROFILE_LINE_WIDTH, mLineWidth);
lcSetProfileInt(LC_PROFILE_GRID_STUDS, mDrawGridStuds);
lcSetProfileInt(LC_PROFILE_GRID_STUD_COLOR, mGridStudColor);
lcSetProfileInt(LC_PROFILE_GRID_LINES, mDrawGridLines);
lcSetProfileInt(LC_PROFILE_GRID_LINE_SPACING, mGridLineSpacing);
lcSetProfileInt(LC_PROFILE_GRID_LINE_COLOR, mGridLineColor);
}
2013-08-09 06:57:18 +02:00
lcApplication::lcApplication()
{
mProject = NULL;
mLibrary = NULL;
2013-08-09 06:57:18 +02:00
mClipboard = NULL;
mPreferences.LoadDefaults();
2013-08-09 06:57:18 +02:00
}
lcApplication::~lcApplication()
{
2015-01-08 06:40:22 +01:00
delete mProject;
delete mLibrary;
2013-08-09 06:57:18 +02:00
}
2014-12-04 02:47:28 +01:00
void lcApplication::SetProject(Project* Project)
{
delete mProject;
mProject = Project;
const lcArray<View*>& Views = gMainWindow->GetViews();
for (int ViewIdx = 0; ViewIdx < Views.GetSize(); ViewIdx++)
Views[ViewIdx]->SetModel(lcGetActiveModel());
2014-12-04 02:47:28 +01:00
lcGetPiecesLibrary()->RemoveTemporaryPieces();
2014-12-31 00:45:27 +01:00
lcGetActiveModel()->UpdateInterface();
gMainWindow->UpdateAllViews();
2014-12-04 02:47:28 +01:00
}
2014-12-16 00:55:17 +01:00
void lcApplication::SetClipboard(const QByteArray& Clipboard)
2013-08-09 06:57:18 +02:00
{
mClipboard = Clipboard;
2014-12-16 00:55:17 +01:00
gMainWindow->UpdatePaste(!mClipboard.isEmpty());
2013-08-09 06:57:18 +02:00
}
bool lcApplication::LoadPiecesLibrary(const char* LibPath, const char* LibraryInstallPath, const char* LDrawPath, const char* LibraryCachePath)
2013-08-09 06:57:18 +02:00
{
if (mLibrary == NULL)
mLibrary = new lcPiecesLibrary();
2013-08-09 06:57:18 +02:00
if (LibPath && LibPath[0])
return mLibrary->Load(LibPath, LibraryCachePath);
char* EnvPath = getenv("LEOCAD_LIB");
if (EnvPath && EnvPath[0])
2013-08-09 06:57:18 +02:00
{
return mLibrary->Load(EnvPath, LibraryCachePath);
2013-08-09 06:57:18 +02:00
}
char CustomPath[LC_MAXPATH];
strcpy(CustomPath, lcGetProfileString(LC_PROFILE_PARTS_LIBRARY));
2013-08-09 06:57:18 +02:00
if (CustomPath[0])
return mLibrary->Load(CustomPath, LibraryCachePath);
2013-08-09 06:57:18 +02:00
if (LibraryInstallPath && LibraryInstallPath[0])
{
char LibraryPath[LC_MAXPATH];
2013-08-09 06:57:18 +02:00
strcpy(LibraryPath, LibraryInstallPath);
2013-08-09 06:57:18 +02:00
int i = strlen(LibraryPath) - 1;
if ((LibraryPath[i] != '\\') && (LibraryPath[i] != '/'))
strcat(LibraryPath, "/");
2013-08-09 06:57:18 +02:00
strcat(LibraryPath, "library.bin");
2013-08-09 06:57:18 +02:00
if (mLibrary->Load(LibraryPath, LibraryCachePath))
{
mLibrary->SetOfficialPieces();
return true;
2013-08-09 06:57:18 +02:00
}
}
if (LDrawPath && LDrawPath[0])
{
char LibraryPath[LC_MAXPATH];
strcpy(LibraryPath, LDrawPath);
int i = strlen(LibraryPath) - 1;
if ((LibraryPath[i] != '\\') && (LibraryPath[i] != '/'))
strcat(LibraryPath, "/");
if (mLibrary->Load(LibraryPath, LibraryCachePath))
return true;
}
2013-08-09 06:57:18 +02:00
return false;
}
void lcApplication::ParseIntegerArgument(int* CurArg, int argc, char* argv[], int* Value)
{
if (argc > (*CurArg + 1))
{
(*CurArg)++;
int val;
if ((sscanf(argv[(*CurArg)], "%d", &val) == 1) && (val > 0))
*Value = val;
else
2013-08-16 03:31:30 +02:00
printf("Invalid value specified for the %s argument.", argv[(*CurArg) - 1]);
2013-08-09 06:57:18 +02:00
}
else
{
2014-07-06 08:04:09 +02:00
*Value = 0;
2013-08-16 03:31:30 +02:00
printf("Not enough parameters for the %s argument.", argv[(*CurArg) - 1]);
2013-08-09 06:57:18 +02:00
}
}
void lcApplication::ParseStringArgument(int* CurArg, int argc, char* argv[], char** Value)
{
if (argc > (*CurArg + 1))
{
(*CurArg)++;
*Value = argv[(*CurArg)];
}
else
{
2013-08-16 03:31:30 +02:00
printf("No path specified after the %s argument.", argv[(*CurArg) - 1]);
2013-08-09 06:57:18 +02:00
}
}
bool lcApplication::Initialize(int argc, char* argv[], const char* LibraryInstallPath, const char* LDrawPath, const char* LibraryCachePath)
2013-08-09 06:57:18 +02:00
{
char* LibPath = NULL;
// Image output options.
bool SaveImage = false;
bool SaveWavefront = false;
// bool ImageHighlight = false;
2013-08-09 06:57:18 +02:00
int ImageWidth = lcGetProfileInt(LC_PROFILE_IMAGE_WIDTH);
int ImageHeight = lcGetProfileInt(LC_PROFILE_IMAGE_HEIGHT);
2014-07-06 08:04:09 +02:00
lcStep ImageStart = 0;
lcStep ImageEnd = 0;
2013-08-09 06:57:18 +02:00
char* ImageName = NULL;
char* ProjectName = NULL;
char* WavefrontName = NULL;
2013-08-09 06:57:18 +02:00
// Parse the command line arguments.
for (int i = 1; i < argc; i++)
{
char* Param = argv[i];
if (Param[0] == '-')
{
if ((strcmp(Param, "-l") == 0) || (strcmp(Param, "--libpath") == 0))
2013-08-09 06:57:18 +02:00
{
ParseStringArgument(&i, argc, argv, &LibPath);
}
else if ((strcmp(Param, "-i") == 0) || (strcmp(Param, "--image") == 0))
{
SaveImage = true;
if ((argc > (i+1)) && (argv[i+1][0] != '-'))
{
i++;
ImageName = argv[i];
}
}
else if ((strcmp(Param, "-w") == 0) || (strcmp(Param, "--width") == 0))
{
ParseIntegerArgument(&i, argc, argv, &ImageWidth);
}
else if ((strcmp(Param, "-h") == 0) || (strcmp(Param, "--height") == 0))
{
ParseIntegerArgument(&i, argc, argv, &ImageHeight);
}
else if ((strcmp(Param, "-f") == 0) || (strcmp(Param, "--from") == 0))
{
2014-07-06 08:04:09 +02:00
int Step;
ParseIntegerArgument(&i, argc, argv, &Step);
ImageStart = Step;
2013-08-09 06:57:18 +02:00
}
else if ((strcmp(Param, "-t") == 0) || (strcmp(Param, "--to") == 0))
{
2014-07-06 08:04:09 +02:00
int Step;
ParseIntegerArgument(&i, argc, argv, &Step);
ImageEnd = Step;
2013-08-09 06:57:18 +02:00
}
// else if (strcmp(Param, "--highlight") == 0)
// ImageHighlight = true;
else if ((strcmp(Param, "-wf") == 0) || (strcmp(Param, "--wavefront") == 0))
{
SaveWavefront = true;
if ((argc > (i+1)) && (argv[i+1][0] != '-'))
{
i++;
WavefrontName = argv[i];
}
}
2013-08-09 06:57:18 +02:00
else if ((strcmp(Param, "-v") == 0) || (strcmp(Param, "--version") == 0))
{
printf("LeoCAD Version " LC_VERSION_TEXT "\n");
printf("Compiled " __DATE__ "\n");
2013-08-09 06:57:18 +02:00
return false;
}
else if ((strcmp(Param, "-?") == 0) || (strcmp(Param, "--help") == 0))
{
printf("Usage: leocad [options] [file]\n");
printf(" [options] can be:\n");
printf(" -l, --libpath <path>: Loads the Pieces Library from path.\n");
printf(" -i, --image <outfile.ext>: Saves a picture in the format specified by ext.\n");
printf(" -w, --width <width>: Sets the picture width.\n");
printf(" -h, --height <height>: Sets the picture height.\n");
printf(" -f, --from <time>: Sets the first frame or step to save pictures.\n");
printf(" -t, --to <time>: Sets the last frame or step to save pictures.\n");
// printf(" --highlight: Highlight pieces in the steps they appear.\n");
printf(" -wf, --wavefront <outfile.obj>: Exports the model to Wavefront format.\n");
2013-08-09 06:57:18 +02:00
printf(" \n");
return false;
}
else
printf("Unknown parameter: %s\n", Param);
2013-08-09 06:57:18 +02:00
}
else
{
ProjectName = Param;
}
}
if (!LoadPiecesLibrary(LibPath, LibraryInstallPath, LDrawPath, LibraryCachePath))
2013-08-09 06:57:18 +02:00
{
if (SaveImage || SaveWavefront)
2013-08-09 06:57:18 +02:00
{
fprintf(stderr, "ERROR: Cannot load pieces library.");
return false;
}
2014-09-11 21:55:34 +02:00
if (mLibrary->LoadBuiltinPieces())
gMainWindow->DoMessageBox("LeoCAD could not find a compatible Parts Library so only a small number of parts will be available.\n\n"
"Please visit http://www.leocad.org for information on how to download and install a library.", LC_MB_OK | LC_MB_ICONERROR);
else
gMainWindow->DoMessageBox("LeoCAD could not load Parts Library.\n\n"
"Please visit http://www.leocad.org for information on how to download and install a library.", LC_MB_OK | LC_MB_ICONERROR);
2013-08-09 06:57:18 +02:00
}
// Create a new project.
mProject = new Project();
// Load project.
if (ProjectName && gMainWindow->OpenProject(ProjectName))
2013-08-09 06:57:18 +02:00
{
if (SaveImage)
{
QString FileName;
2013-08-09 06:57:18 +02:00
if (ImageName)
FileName = ImageName;
else
FileName = ProjectName;
2013-08-09 06:57:18 +02:00
QString Extension = QFileInfo(FileName).suffix().toLower();
2013-08-09 06:57:18 +02:00
if (Extension.isEmpty())
{
FileName += lcGetProfileString(LC_PROFILE_IMAGE_EXTENSION);
}
else if (Extension != "bmp" && Extension != "jpg" && Extension != "jpeg" && Extension != "png")
{
FileName = FileName.left(FileName.length() - Extension.length() - 1);
FileName += lcGetProfileString(LC_PROFILE_IMAGE_EXTENSION);
}
2013-08-09 06:57:18 +02:00
if (ImageEnd < ImageStart)
ImageEnd = ImageStart;
else if (ImageStart > ImageEnd)
ImageStart = ImageEnd;
if ((ImageStart == 0) && (ImageEnd == 0))
{
ImageStart = ImageEnd = mProject->GetActiveModel()->GetCurrentStep();
}
else if ((ImageStart == 0) && (ImageEnd != 0))
{
ImageStart = ImageEnd;
}
else if ((ImageStart != 0) && (ImageEnd == 0))
{
ImageEnd = ImageStart;
}
2013-08-09 06:57:18 +02:00
if (ImageStart > 255)
ImageStart = 255;
2013-08-09 06:57:18 +02:00
if (ImageEnd > 255)
ImageEnd = 255;
2013-08-09 06:57:18 +02:00
QString Frame;
2013-08-09 06:57:18 +02:00
if (ImageStart != ImageEnd)
{
QString Extension = QFileInfo(FileName).suffix();
Frame = FileName.left(FileName.length() - Extension.length() - 1) + QLatin1String("%1.") + Extension;
}
else
Frame = FileName;
2013-08-09 06:57:18 +02:00
lcGetActiveModel()->SaveStepImages(Frame, ImageWidth, ImageHeight, ImageStart, ImageEnd);
}
2013-08-09 06:57:18 +02:00
if (SaveWavefront)
2013-08-09 06:57:18 +02:00
{
QString FileName;
if (WavefrontName)
FileName = WavefrontName;
else
FileName = ProjectName;
2013-08-09 06:57:18 +02:00
QString Extension = QFileInfo(FileName).suffix().toLower();
2013-08-09 06:57:18 +02:00
if (Extension.isEmpty())
{
FileName += ".obj";
}
else if (Extension != "obj")
{
FileName = FileName.left(FileName.length() - Extension.length() - 1);
FileName += ".obj";
}
mProject->ExportWavefront(FileName);
}
2013-08-09 06:57:18 +02:00
}
if (SaveImage || SaveWavefront)
return false;
2013-08-09 06:57:18 +02:00
lcLoadDefaultKeyboardShortcuts();
return true;
}
void lcApplication::Shutdown()
{
delete mLibrary;
mLibrary = NULL;
2013-08-09 06:57:18 +02:00
}
void lcApplication::ShowPreferencesDialog()
{
lcPreferencesDialogOptions Options;
int CurrentAASamples = lcGetProfileInt(LC_PROFILE_ANTIALIASING_SAMPLES);
Options.Preferences = mPreferences;
strcpy(Options.DefaultAuthor, lcGetProfileString(LC_PROFILE_DEFAULT_AUTHOR_NAME));
strcpy(Options.ProjectsPath, lcGetProfileString(LC_PROFILE_PROJECTS_PATH));
strcpy(Options.LibraryPath, lcGetProfileString(LC_PROFILE_PARTS_LIBRARY));
strcpy(Options.POVRayPath, lcGetProfileString(LC_PROFILE_POVRAY_PATH));
strcpy(Options.LGEOPath, lcGetProfileString(LC_PROFILE_POVRAY_LGEO_PATH));
Options.CheckForUpdates = lcGetProfileInt(LC_PROFILE_CHECK_UPDATES);
Options.AASamples = CurrentAASamples;
Options.Categories = gCategories;
Options.CategoriesModified = false;
Options.CategoriesDefault = false;
Options.KeyboardShortcuts = gKeyboardShortcuts;
Options.ShortcutsModified = false;
Options.ShortcutsDefault = false;
if (!gMainWindow->DoDialog(LC_DIALOG_PREFERENCES, &Options))
return;
bool LibraryChanged = strcmp(Options.LibraryPath, lcGetProfileString(LC_PROFILE_PARTS_LIBRARY));
bool AAChanged = CurrentAASamples != Options.AASamples;
mPreferences = Options.Preferences;
mPreferences.SaveDefaults();
lcSetProfileString(LC_PROFILE_DEFAULT_AUTHOR_NAME, Options.DefaultAuthor);
lcSetProfileString(LC_PROFILE_PROJECTS_PATH, Options.ProjectsPath);
lcSetProfileString(LC_PROFILE_PARTS_LIBRARY, Options.LibraryPath);
lcSetProfileString(LC_PROFILE_POVRAY_PATH, Options.POVRayPath);
lcSetProfileString(LC_PROFILE_POVRAY_LGEO_PATH, Options.LGEOPath);
lcSetProfileInt(LC_PROFILE_CHECK_UPDATES, Options.CheckForUpdates);
lcSetProfileInt(LC_PROFILE_ANTIALIASING_SAMPLES, Options.AASamples);
if (LibraryChanged && AAChanged)
gMainWindow->DoMessageBox("Parts library and Anti-aliasing changes will only take effect the next time you start LeoCAD.", LC_MB_OK);
else if (LibraryChanged)
gMainWindow->DoMessageBox("Parts library changes will only take effect the next time you start LeoCAD.", LC_MB_OK);
else if (AAChanged)
gMainWindow->DoMessageBox("Anti-aliasing changes will only take effect the next time you start LeoCAD.", LC_MB_OK);
if (Options.CategoriesModified)
{
if (Options.CategoriesDefault)
lcResetDefaultCategories();
else
{
gCategories = Options.Categories;
lcSaveDefaultCategories();
}
gMainWindow->UpdateCategories();
}
if (Options.ShortcutsModified)
{
if (Options.ShortcutsDefault)
lcResetDefaultKeyboardShortcuts();
else
{
gKeyboardShortcuts = Options.KeyboardShortcuts;
lcSaveDefaultKeyboardShortcuts();
}
gMainWindow->UpdateShortcuts();
}
// TODO: printing preferences
/*
strcpy(opts.strFooter, m_strFooter);
strcpy(opts.strHeader, m_strHeader);
*/
2014-05-03 23:16:48 +02:00
gMainWindow->UpdateAllViews();
}