Engauge Digitizer  2
 All Classes Files Functions Variables Enumerations Enumerator Friends Pages
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 <iostream>
9 #include "Logger.h"
10 #include "MainWindow.h"
11 #include <QApplication>
12 #include <QCoreApplication>
13 #include <QDebug>
14 #include <QDir>
15 #include <QFileInfo>
16 #include <QObject>
17 #include <QProcessEnvironment>
18 #include "TranslatorContainer.h"
19 
20 using namespace std;
21 
22 const QString CMD_DEBUG ("debug");
23 const QString CMD_ERROR_REPORT ("errorreport");
24 const QString CMD_FILE_CMD_SCRIPT ("filecmdscript");
25 const QString CMD_GNUPLOT ("gnuplot");
26 const QString CMD_HELP ("help");
27 const QString CMD_REGRESSION ("regression");
28 const QString DASH ("-");
29 const QString DASH_DEBUG ("-" + CMD_DEBUG);
30 const QString DASH_ERROR_REPORT ("-" + CMD_ERROR_REPORT);
31 const QString DASH_FILE_CMD_SCRIPT ("-" + CMD_FILE_CMD_SCRIPT);
32 const QString DASH_GNUPLOT ("-" + CMD_GNUPLOT);
33 const QString DASH_HELP ("-" + CMD_HELP);
34 const QString DASH_REGRESSION ("-" + CMD_REGRESSION);
35 const QString ENGAUGE_LOG_FILE ("engauge.log");
36 
37 // Prototypes
38 bool checkFileExists (const QString &file);
39 QString engaugeLogFilename ();
40 bool engaugeLogFilenameAttempt (const QString &path,
41  QString &pathAndFile);
42 void parseCmdLine (int argc,
43  char **argv,
44  bool &isDebug,
45  QString &errorReportFile,
46  QString &fileCmdScriptFile,
47  bool &isRegressionTest,
48  bool &isGnuplot,
49  QStringList &loadStartupFiles);
50 
51 // Functions
52 bool checkFileExists (const QString &file)
53 {
54  QFileInfo check (file);
55  return check.exists() && check.isFile();
56 }
57 
58 QString engaugeLogFilename()
59 {
60  QString pathAndFile; // Return empty value in OSX which is unused
61 
62 #ifndef OSX_RELEASE
63  QProcessEnvironment env;
64 
65  // Make multiple attempts until a directory is found where the log file can be written
66  if (!engaugeLogFilenameAttempt (QCoreApplication::applicationDirPath(), pathAndFile)) {
67  if (!engaugeLogFilenameAttempt (env.value ("HOME"), pathAndFile)) {
68  if (!engaugeLogFilenameAttempt (env.value ("TEMP"), pathAndFile)) {
69  pathAndFile = ENGAUGE_LOG_FILE; // Current directory will have to do
70  }
71  }
72  }
73 #endif
74 
75  return pathAndFile;
76 }
77 
78 bool engaugeLogFilenameAttempt (const QString &path,
79  QString &pathAndFile)
80 {
81  bool success = false;
82 
83  // Test if file can be opened. Checking permissions on directory is unreliable in Windows/OSX
84  pathAndFile = QString ("%1%2%3")
85  .arg (path)
86  .arg (QDir::separator())
87  .arg (ENGAUGE_LOG_FILE);
88  QFile file (pathAndFile);
89  if (file.open(QIODevice::WriteOnly | QIODevice::Text)) {
90  // Success
91  file.close();
92  success = true;
93  }
94 
95  return success;
96 }
97 
98 int main(int argc, char *argv[])
99 {
100  qRegisterMetaType<ColorFilterMode> ("ColorFilterMode");
101 
102  QApplication app(argc, argv);
103 
104  // Translations
105  TranslatorContainer translatorContainer (app); // Must exist until execution terminates
106 
107  // Command line
108  bool isDebug, isGnuplot, isRegressionTest;
109  QString errorReportFile, fileCmdScriptFile;
110  QStringList loadStartupFiles;
111  parseCmdLine (argc,
112  argv,
113  isDebug,
114  errorReportFile,
115  fileCmdScriptFile,
116  isRegressionTest,
117  isGnuplot,
118  loadStartupFiles);
119 
120  // Logging
121  initializeLogging ("engauge",
122  engaugeLogFilename(),
123  isDebug);
124  LOG4CPP_INFO_S ((*mainCat)) << "main args=" << QApplication::arguments().join (" ").toLatin1().data();
125 
126  // Create and show main window
127  MainWindow w (errorReportFile,
128  fileCmdScriptFile,
129  isRegressionTest,
130  isGnuplot,
131  loadStartupFiles);
132  w.show();
133 
134  // Event loop
135  return app.exec();
136 }
137 
138 void parseCmdLine (int argc,
139  char **argv,
140  bool &isDebug,
141  QString &errorReportFile,
142  QString &fileCmdScriptFile,
143  bool &isRegressionTest,
144  bool &isGnuplot,
145  QStringList &loadStartupFiles)
146 {
147  const int COLUMN_WIDTH = 20;
148  bool showUsage = false;
149 
150  // State
151  bool nextIsErrorReportFile = false;
152  bool nextIsFileCmdScript = false;
153 
154  // Defaults
155  isDebug = false;
156  errorReportFile = "";
157  fileCmdScriptFile = "";
158  isRegressionTest = false;
159  isGnuplot = false;
160 
161  for (int i = 1; i < argc; i++) {
162 
163  if (nextIsErrorReportFile) {
164  errorReportFile = argv [i];
165  showUsage |= !checkFileExists (errorReportFile);
166  nextIsErrorReportFile = false;
167  } else if (nextIsFileCmdScript) {
168  fileCmdScriptFile = argv [i];
169  showUsage |= !checkFileExists (fileCmdScriptFile);
170  nextIsFileCmdScript = false;
171  } else if (strcmp (argv [i], DASH_DEBUG.toLatin1().data()) == 0) {
172  isDebug = true;
173  } else if (strcmp (argv [i], DASH_ERROR_REPORT.toLatin1().data()) == 0) {
174  nextIsErrorReportFile = true;
175  } else if (strcmp (argv [i], DASH_FILE_CMD_SCRIPT.toLatin1().data()) == 0) {
176  nextIsFileCmdScript = true;
177  } else if (strcmp (argv [i], DASH_GNUPLOT.toLatin1().data()) == 0) {
178  isGnuplot = true;
179  } else if (strcmp (argv [i], DASH_HELP.toLatin1().data()) == 0) {
180  showUsage = true; // User requested help
181  } else if (strcmp (argv [i], DASH_REGRESSION.toLatin1().data()) == 0) {
182  isRegressionTest = true;
183  } else if (strncmp (argv [i], DASH.toLatin1().data(), 1) == 0) {
184  showUsage = true; // User entered an unrecognized token
185  } else {
186  // MainWindow will change current directory (which is often some obscure application directory),
187  // so relative paths must be changed in advance to absolute so the files can still be found
188  QString fileName = argv [i];
189  QFileInfo fInfo (fileName);
190  if (fInfo.isRelative()) {
191  fileName = fInfo.absoluteFilePath();
192  }
193  loadStartupFiles << fileName; // Save file name
194  }
195  }
196 
197  if (showUsage || nextIsErrorReportFile) {
198 
199  cerr << "Usage: engauge "
200  << "[" << DASH_DEBUG.toLatin1().data() << "] "
201  << "[" << DASH_ERROR_REPORT.toLatin1().data() << " <file>] "
202  << "[" << DASH_FILE_CMD_SCRIPT.toLatin1().data() << " <file> "
203  << "[" << DASH_GNUPLOT.toLatin1().data() << "] "
204  << "[" << DASH_HELP.toLatin1().data() << "] "
205  << "[" << DASH_REGRESSION.toLatin1().data() << "] "
206  << "[<load_file1>] [<load_file2>] ..." << endl
207  << " " << DASH_DEBUG.leftJustified(COLUMN_WIDTH, ' ').toLatin1().data()
208  << QObject::tr ("Enables extra debug information. Used for debugging").toLatin1().data() << endl
209  << " " << DASH_ERROR_REPORT.leftJustified(COLUMN_WIDTH, ' ').toLatin1().data()
210  << QObject::tr ("Specifies an error report file as input. Used for debugging and testing").toLatin1().data() << endl
211  << " " << DASH_FILE_CMD_SCRIPT.leftJustified(COLUMN_WIDTH, ' ').toLatin1().data()
212  << QObject::tr ("Specifies a file command script file as input. Used for debugging and testing").toLatin1().data() << endl
213  << " " << DASH_GNUPLOT.leftJustified(COLUMN_WIDTH, ' ').toLatin1().data()
214  << QObject::tr ("Output diagnostic gnuplot input files. Used for debugging").toLatin1().data() << endl
215  << " " << DASH_HELP.leftJustified(COLUMN_WIDTH, ' ').toLatin1().data()
216  << QObject::tr ("Show this help information").toLatin1().data() << endl
217  << " " << DASH_REGRESSION.leftJustified(COLUMN_WIDTH, ' ').toLatin1().data()
218  << QObject::tr ("Executes the error report file or file command script. Used for regression testing").toLatin1().data() << endl
219  << " " << QString ("<load file> ").leftJustified(COLUMN_WIDTH, ' ').toLatin1().data()
220  << QObject::tr ("File(s) to be imported or opened at startup").toLatin1().data() << endl;
221 
222  exit (0);
223  }
224 }
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:82