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