Engauge Digitizer  2
main.cpp
1 /******************************************************************************************************
2  * (C) 2014 markummitchell@github.com. This file is part of Engauge Digitizer, which is released *
3  * under GNU General Public License version 2 (GPLv2) or (at your option) any later version. See file *
4  * LICENSE or go to gnu.org/licenses for details. Distribution requires prior written permission. *
5  ******************************************************************************************************/
6 
7 #include "ColorFilterMode.h"
8 #include "FittingCurveCoefficients.h"
9 #include <iostream>
10 #include "Logger.h"
11 #include "MainWindow.h"
12 #include <QApplication>
13 #include <QCoreApplication>
14 #include <QDebug>
15 #include <QDir>
16 #include <QFileInfo>
17 #include <QObject>
18 #include <QProcessEnvironment>
19 #include <QStyleFactory>
20 #include "TranslatorContainer.h"
21 #include "ZoomFactor.h"
22 
23 using namespace std;
24 
25 const QString CMD_DEBUG ("debug");
26 const QString CMD_ERROR_REPORT ("errorreport");
27 const QString CMD_FILE_CMD_SCRIPT ("filecmdscript");
28 const QString CMD_GNUPLOT ("gnuplot");
29 const QString CMD_HELP ("help");
30 const QString CMD_REGRESSION ("regression");
31 const QString CMD_RESET ("reset");
32 const QString CMD_STYLES ("styles"); // Not to be confused with -style option that qt handles
33 const QString DASH ("-");
34 const QString DASH_DEBUG ("-" + CMD_DEBUG);
35 const QString DASH_ERROR_REPORT ("-" + CMD_ERROR_REPORT);
36 const QString DASH_FILE_CMD_SCRIPT ("-" + CMD_FILE_CMD_SCRIPT);
37 const QString DASH_GNUPLOT ("-" + CMD_GNUPLOT);
38 const QString DASH_HELP ("-" + CMD_HELP);
39 const QString DASH_REGRESSION ("-" + CMD_REGRESSION);
40 const QString DASH_RESET ("-" + CMD_RESET);
41 const QString DASH_STYLES ("-" + CMD_STYLES);
42 const QString ENGAUGE_LOG_FILE ("engauge.log");
43 
44 // Prototypes
45 bool checkFileExists (const QString &file);
46 QString engaugeLogFilename ();
47 bool engaugeLogFilenameAttempt (const QString &path,
48  QString &pathAndFile);
49 void parseCmdLine (int argc,
50  char **argv,
51  bool &isDebug,
52  bool &isReset,
53  QString &errorReportFile,
54  QString &fileCmdScriptFile,
55  bool &isErrorReportRegressionTest,
56  bool &isGnuplot,
57  QStringList &loadStartupFiles);
58 void showStylesAndExit ();
59 
60 // Functions
61 bool checkFileExists (const QString &file)
62 {
63  QFileInfo check (file);
64  return check.exists() && check.isFile();
65 }
66 
67 QString engaugeLogFilename()
68 {
69  QString pathAndFile; // Return empty value in OSX which is unused
70 
71 #if !defined(OSX_RELEASE) && !defined(WIN_RELEASE) && !defined(APPIMAGE_RELEASE)
72  QProcessEnvironment env;
73 
74  // Make multiple attempts until a directory is found where the log file can be written
75  if (!engaugeLogFilenameAttempt (QCoreApplication::applicationDirPath(), pathAndFile)) {
76  if (!engaugeLogFilenameAttempt (env.value ("HOME"), pathAndFile)) {
77  if (!engaugeLogFilenameAttempt (env.value ("TEMP"), pathAndFile)) {
78  pathAndFile = ENGAUGE_LOG_FILE; // Current directory will have to do
79  }
80  }
81  }
82 #endif
83 
84  return pathAndFile;
85 }
86 
87 bool engaugeLogFilenameAttempt (const QString &path,
88  QString &pathAndFile)
89 {
90  bool success = false;
91 
92  // Test if file can be opened. Checking permissions on directory is unreliable in Windows/OSX
93  pathAndFile = QString ("%1%2%3")
94  .arg (path)
95  .arg (QDir::separator())
96  .arg (ENGAUGE_LOG_FILE);
97  QFile file (pathAndFile);
98  if (file.open(QIODevice::WriteOnly | QIODevice::Text)) {
99  // Success
100  file.close();
101  success = true;
102  }
103 
104  return success;
105 }
106 
107 int main(int argc, char *argv[])
108 {
109  qRegisterMetaType<ColorFilterMode> ("ColorFilterMode");
110  qRegisterMetaType<FittingCurveCoefficients> ("FilterCurveCoefficients");
111  qRegisterMetaType<ZoomFactor> ("ZoomFactor");
112 
113  QApplication app(argc, argv);
114 
115  // Translations
116  TranslatorContainer translatorContainer (app); // Must exist until execution terminates
117 
118  // Command line
119  bool isDebug, isReset, isGnuplot, isErrorReportRegressionTest;
120  QString errorReportFile, fileCmdScriptFile;
121  QStringList loadStartupFiles;
122  parseCmdLine (argc,
123  argv,
124  isDebug,
125  isReset,
126  errorReportFile,
127  fileCmdScriptFile,
128  isErrorReportRegressionTest,
129  isGnuplot,
130  loadStartupFiles);
131 
132  // Logging
133  initializeLogging ("engauge",
134  engaugeLogFilename(),
135  isDebug);
136  LOG4CPP_INFO_S ((*mainCat)) << "main args=" << QApplication::arguments().join (" ").toLatin1().data();
137 
138  // Create and show main window
139  MainWindow w (errorReportFile,
140  fileCmdScriptFile,
141  isErrorReportRegressionTest,
142  isGnuplot,
143  isReset,
144  loadStartupFiles);
145  w.show();
146 
147  // Event loop
148  return app.exec();
149 }
150 
151 void parseCmdLine (int argc,
152  char **argv,
153  bool &isDebug,
154  bool &isReset,
155  QString &errorReportFile,
156  QString &fileCmdScriptFile,
157  bool &isErrorReportRegressionTest,
158  bool &isGnuplot,
159  QStringList &loadStartupFiles)
160 {
161  const int COLUMN_WIDTH = 20;
162  bool showUsage = false;
163 
164  // State
165  bool nextIsErrorReportFile = false;
166  bool nextIsFileCmdScript = false;
167 
168  // Defaults
169  isDebug = false;
170  isReset = false;
171  errorReportFile = "";
172  fileCmdScriptFile = "";
173  isErrorReportRegressionTest = false;
174  isGnuplot = false;
175 
176  for (int i = 1; i < argc; i++) {
177 
178  if (nextIsErrorReportFile) {
179  errorReportFile = argv [i];
180  showUsage |= !checkFileExists (errorReportFile);
181  nextIsErrorReportFile = false;
182  } else if (nextIsFileCmdScript) {
183  fileCmdScriptFile = argv [i];
184  showUsage |= !checkFileExists (fileCmdScriptFile);
185  nextIsFileCmdScript = false;
186  } else if (strcmp (argv [i], DASH_DEBUG.toLatin1().data()) == 0) {
187  isDebug = true;
188  } else if (strcmp (argv [i], DASH_ERROR_REPORT.toLatin1().data()) == 0) {
189  nextIsErrorReportFile = true;
190  } else if (strcmp (argv [i], DASH_FILE_CMD_SCRIPT.toLatin1().data()) == 0) {
191  nextIsFileCmdScript = true;
192  } else if (strcmp (argv [i], DASH_GNUPLOT.toLatin1().data()) == 0) {
193  isGnuplot = true;
194  } else if (strcmp (argv [i], DASH_HELP.toLatin1().data()) == 0) {
195  showUsage = true; // User requested help
196  } else if (strcmp (argv [i], DASH_REGRESSION.toLatin1().data()) == 0) {
197  isErrorReportRegressionTest = true;
198  } else if (strcmp (argv [i], DASH_RESET.toLatin1().data()) == 0) {
199  isReset = true;
200  } else if (strcmp (argv [i], DASH_STYLES.toLatin1().data()) == 0) {
201  showStylesAndExit ();
202  } else if (strncmp (argv [i], DASH.toLatin1().data(), 1) == 0) {
203  showUsage = true; // User entered an unrecognized token
204  } else {
205  // MainWindow will change current directory (which is often some obscure application directory),
206  // so relative paths must be changed in advance to absolute so the files can still be found
207  QString fileName = argv [i];
208  QFileInfo fInfo (fileName);
209  if (fInfo.isRelative()) {
210  fileName = fInfo.absoluteFilePath();
211  }
212  loadStartupFiles << fileName; // Save file name
213  }
214  }
215 
216  if (showUsage || nextIsErrorReportFile) {
217 
218  cerr << "Usage: engauge "
219  << "[" << DASH_DEBUG.toLatin1().data() << "] "
220  << "[" << DASH_ERROR_REPORT.toLatin1().data() << " <file>] "
221  << "[" << DASH_FILE_CMD_SCRIPT.toLatin1().data() << " <file> "
222  << "[" << DASH_GNUPLOT.toLatin1().data() << "] "
223  << "[" << DASH_HELP.toLatin1().data() << "] "
224  << "[" << DASH_REGRESSION.toLatin1().data() << "] "
225  << "[" << DASH_RESET.toLatin1().data () << "] "
226  << "[" << DASH_STYLES.toLatin1().data () << "] "
227  << "[<load_file1>] [<load_file2>] ..." << endl
228  << " " << DASH_DEBUG.leftJustified(COLUMN_WIDTH, ' ').toLatin1().data()
229  << QObject::tr ("Enables extra debug information. Used for debugging").toLatin1().data() << endl
230  << " " << DASH_ERROR_REPORT.leftJustified(COLUMN_WIDTH, ' ').toLatin1().data()
231  << QObject::tr ("Specifies an error report file as input. Used for debugging and testing").toLatin1().data() << endl
232  << " " << DASH_FILE_CMD_SCRIPT.leftJustified(COLUMN_WIDTH, ' ').toLatin1().data()
233  << QObject::tr ("Specifies a file command script file as input. Used for debugging and testing").toLatin1().data() << endl
234  << " " << DASH_GNUPLOT.leftJustified(COLUMN_WIDTH, ' ').toLatin1().data()
235  << QObject::tr ("Output diagnostic gnuplot input files. Used for debugging").toLatin1().data() << endl
236  << " " << DASH_HELP.leftJustified(COLUMN_WIDTH, ' ').toLatin1().data()
237  << QObject::tr ("Show this help information").toLatin1().data() << endl
238  << " " << DASH_REGRESSION.leftJustified(COLUMN_WIDTH, ' ').toLatin1().data()
239  << QObject::tr ("Executes the error report file or file command script. Used for regression testing").toLatin1().data() << endl
240  << " " << DASH_RESET.leftJustified(COLUMN_WIDTH, ' ').toLatin1().data()
241  << QObject::tr ("Removes all stored settings, including window positions. Used when windows start up offscreen").toLatin1().data() << endl
242  << " " << DASH_STYLES.leftJustified(COLUMN_WIDTH, ' ').toLatin1().data()
243  << QObject::tr ("Show a list of available styles that can be used with the -style command").toLatin1().data() << endl
244  << " " << QString ("<load file> ").leftJustified(COLUMN_WIDTH, ' ').toLatin1().data()
245  << QObject::tr ("File(s) to be imported or opened at startup").toLatin1().data() << endl;
246 
247  exit (0);
248  }
249 }
250 
251 void showStylesAndExit ()
252 {
253  cout << "Available styles: " << QStyleFactory::keys ().join (", ").toLatin1().data() << endl;
254  exit (0);
255 }
Class that stores QTranslator objects for the duration of application execution.
Main window consisting of menu, graphics scene, status bar and optional toolbars as a Single Document...
Definition: MainWindow.h:89