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