00001
00002
00003
00004
00005
00006
00007 #include "ColorFilterMode.h"
00008 #include "FittingCurveCoefficients.h"
00009 #include <iostream>
00010 #include "Logger.h"
00011 #include "MainWindow.h"
00012 #include <QApplication>
00013 #include <QCoreApplication>
00014 #include <QDebug>
00015 #include <QDir>
00016 #include <QFileInfo>
00017 #include <QObject>
00018 #include <QProcessEnvironment>
00019 #include <QStyleFactory>
00020 #include "TranslatorContainer.h"
00021 #include "ZoomFactor.h"
00022
00023 using namespace std;
00024
00025 const QString CMD_DEBUG ("debug");
00026 const QString CMD_ERROR_REPORT ("errorreport");
00027 const QString CMD_FILE_CMD_SCRIPT ("filecmdscript");
00028 const QString CMD_GNUPLOT ("gnuplot");
00029 const QString CMD_HELP ("help");
00030 const QString CMD_REGRESSION ("regression");
00031 const QString CMD_RESET ("reset");
00032 const QString CMD_STYLES ("styles");
00033 const QString DASH ("-");
00034 const QString DASH_DEBUG ("-" + CMD_DEBUG);
00035 const QString DASH_ERROR_REPORT ("-" + CMD_ERROR_REPORT);
00036 const QString DASH_FILE_CMD_SCRIPT ("-" + CMD_FILE_CMD_SCRIPT);
00037 const QString DASH_GNUPLOT ("-" + CMD_GNUPLOT);
00038 const QString DASH_HELP ("-" + CMD_HELP);
00039 const QString DASH_REGRESSION ("-" + CMD_REGRESSION);
00040 const QString DASH_RESET ("-" + CMD_RESET);
00041 const QString DASH_STYLES ("-" + CMD_STYLES);
00042 const QString ENGAUGE_LOG_FILE ("engauge.log");
00043
00044
00045 bool checkFileExists (const QString &file);
00046 QString engaugeLogFilename ();
00047 bool engaugeLogFilenameAttempt (const QString &path,
00048 QString &pathAndFile);
00049 void parseCmdLine (int argc,
00050 char **argv,
00051 bool &isDebug,
00052 bool &isReset,
00053 QString &errorReportFile,
00054 QString &fileCmdScriptFile,
00055 bool &isErrorReportRegressionTest,
00056 bool &isGnuplot,
00057 QStringList &loadStartupFiles);
00058 void showStylesAndExit ();
00059
00060
00061 bool checkFileExists (const QString &file)
00062 {
00063 QFileInfo check (file);
00064 return check.exists() && check.isFile();
00065 }
00066
00067 QString engaugeLogFilename()
00068 {
00069 QString pathAndFile;
00070
00071 #if !defined(OSX_RELEASE) && !defined(WIN_RELEASE) && !defined(APPIMAGE_RELEASE)
00072 QProcessEnvironment env;
00073
00074
00075 if (!engaugeLogFilenameAttempt (QCoreApplication::applicationDirPath(), pathAndFile)) {
00076 if (!engaugeLogFilenameAttempt (env.value ("HOME"), pathAndFile)) {
00077 if (!engaugeLogFilenameAttempt (env.value ("TEMP"), pathAndFile)) {
00078 pathAndFile = ENGAUGE_LOG_FILE;
00079 }
00080 }
00081 }
00082 #endif
00083
00084 return pathAndFile;
00085 }
00086
00087 bool engaugeLogFilenameAttempt (const QString &path,
00088 QString &pathAndFile)
00089 {
00090 bool success = false;
00091
00092
00093 pathAndFile = QString ("%1%2%3")
00094 .arg (path)
00095 .arg (QDir::separator())
00096 .arg (ENGAUGE_LOG_FILE);
00097 QFile file (pathAndFile);
00098 if (file.open(QIODevice::WriteOnly | QIODevice::Text)) {
00099
00100 file.close();
00101 success = true;
00102 }
00103
00104 return success;
00105 }
00106
00107 int main(int argc, char *argv[])
00108 {
00109 qRegisterMetaType<ColorFilterMode> ("ColorFilterMode");
00110 qRegisterMetaType<FittingCurveCoefficients> ("FilterCurveCoefficients");
00111 qRegisterMetaType<ZoomFactor> ("ZoomFactor");
00112
00113 QApplication app(argc, argv);
00114
00115
00116 TranslatorContainer translatorContainer (app);
00117
00118
00119 bool isDebug, isReset, isGnuplot, isErrorReportRegressionTest;
00120 QString errorReportFile, fileCmdScriptFile;
00121 QStringList loadStartupFiles;
00122 parseCmdLine (argc,
00123 argv,
00124 isDebug,
00125 isReset,
00126 errorReportFile,
00127 fileCmdScriptFile,
00128 isErrorReportRegressionTest,
00129 isGnuplot,
00130 loadStartupFiles);
00131
00132
00133 initializeLogging ("engauge",
00134 engaugeLogFilename(),
00135 isDebug);
00136 LOG4CPP_INFO_S ((*mainCat)) << "main args=" << QApplication::arguments().join (" ").toLatin1().data();
00137
00138
00139 MainWindow w (errorReportFile,
00140 fileCmdScriptFile,
00141 isErrorReportRegressionTest,
00142 isGnuplot,
00143 isReset,
00144 loadStartupFiles);
00145 w.show();
00146
00147
00148 return app.exec();
00149 }
00150
00151 void parseCmdLine (int argc,
00152 char **argv,
00153 bool &isDebug,
00154 bool &isReset,
00155 QString &errorReportFile,
00156 QString &fileCmdScriptFile,
00157 bool &isErrorReportRegressionTest,
00158 bool &isGnuplot,
00159 QStringList &loadStartupFiles)
00160 {
00161 const int COLUMN_WIDTH = 20;
00162 bool showUsage = false;
00163
00164
00165 bool nextIsErrorReportFile = false;
00166 bool nextIsFileCmdScript = false;
00167
00168
00169 isDebug = false;
00170 isReset = false;
00171 errorReportFile = "";
00172 fileCmdScriptFile = "";
00173 isErrorReportRegressionTest = false;
00174 isGnuplot = false;
00175
00176 for (int i = 1; i < argc; i++) {
00177
00178 if (nextIsErrorReportFile) {
00179 errorReportFile = argv [i];
00180 showUsage |= !checkFileExists (errorReportFile);
00181 nextIsErrorReportFile = false;
00182 } else if (nextIsFileCmdScript) {
00183 fileCmdScriptFile = argv [i];
00184 showUsage |= !checkFileExists (fileCmdScriptFile);
00185 nextIsFileCmdScript = false;
00186 } else if (strcmp (argv [i], DASH_DEBUG.toLatin1().data()) == 0) {
00187 isDebug = true;
00188 } else if (strcmp (argv [i], DASH_ERROR_REPORT.toLatin1().data()) == 0) {
00189 nextIsErrorReportFile = true;
00190 } else if (strcmp (argv [i], DASH_FILE_CMD_SCRIPT.toLatin1().data()) == 0) {
00191 nextIsFileCmdScript = true;
00192 } else if (strcmp (argv [i], DASH_GNUPLOT.toLatin1().data()) == 0) {
00193 isGnuplot = true;
00194 } else if (strcmp (argv [i], DASH_HELP.toLatin1().data()) == 0) {
00195 showUsage = true;
00196 } else if (strcmp (argv [i], DASH_REGRESSION.toLatin1().data()) == 0) {
00197 isErrorReportRegressionTest = true;
00198 } else if (strcmp (argv [i], DASH_RESET.toLatin1().data()) == 0) {
00199 isReset = true;
00200 } else if (strcmp (argv [i], DASH_STYLES.toLatin1().data()) == 0) {
00201 showStylesAndExit ();
00202 } else if (strncmp (argv [i], DASH.toLatin1().data(), 1) == 0) {
00203 showUsage = true;
00204 } else {
00205
00206
00207 QString fileName = argv [i];
00208 QFileInfo fInfo (fileName);
00209 if (fInfo.isRelative()) {
00210 fileName = fInfo.absoluteFilePath();
00211 }
00212 loadStartupFiles << fileName;
00213 }
00214 }
00215
00216 if (showUsage || nextIsErrorReportFile) {
00217
00218 cerr << "Usage: engauge "
00219 << "[" << DASH_DEBUG.toLatin1().data() << "] "
00220 << "[" << DASH_ERROR_REPORT.toLatin1().data() << " <file>] "
00221 << "[" << DASH_FILE_CMD_SCRIPT.toLatin1().data() << " <file> "
00222 << "[" << DASH_GNUPLOT.toLatin1().data() << "] "
00223 << "[" << DASH_HELP.toLatin1().data() << "] "
00224 << "[" << DASH_REGRESSION.toLatin1().data() << "] "
00225 << "[" << DASH_RESET.toLatin1().data () << "] "
00226 << "[" << DASH_STYLES.toLatin1().data () << "] "
00227 << "[<load_file1>] [<load_file2>] ..." << endl
00228 << " " << DASH_DEBUG.leftJustified(COLUMN_WIDTH, ' ').toLatin1().data()
00229 << QObject::tr ("Enables extra debug information. Used for debugging").toLatin1().data() << endl
00230 << " " << DASH_ERROR_REPORT.leftJustified(COLUMN_WIDTH, ' ').toLatin1().data()
00231 << QObject::tr ("Specifies an error report file as input. Used for debugging and testing").toLatin1().data() << endl
00232 << " " << DASH_FILE_CMD_SCRIPT.leftJustified(COLUMN_WIDTH, ' ').toLatin1().data()
00233 << QObject::tr ("Specifies a file command script file as input. Used for debugging and testing").toLatin1().data() << endl
00234 << " " << DASH_GNUPLOT.leftJustified(COLUMN_WIDTH, ' ').toLatin1().data()
00235 << QObject::tr ("Output diagnostic gnuplot input files. Used for debugging").toLatin1().data() << endl
00236 << " " << DASH_HELP.leftJustified(COLUMN_WIDTH, ' ').toLatin1().data()
00237 << QObject::tr ("Show this help information").toLatin1().data() << endl
00238 << " " << DASH_REGRESSION.leftJustified(COLUMN_WIDTH, ' ').toLatin1().data()
00239 << QObject::tr ("Executes the error report file or file command script. Used for regression testing").toLatin1().data() << endl
00240 << " " << DASH_RESET.leftJustified(COLUMN_WIDTH, ' ').toLatin1().data()
00241 << QObject::tr ("Removes all stored settings, including window positions. Used when windows start up offscreen").toLatin1().data() << endl
00242 << " " << DASH_STYLES.leftJustified(COLUMN_WIDTH, ' ').toLatin1().data()
00243 << QObject::tr ("Show a list of available styles that can be used with the -style command").toLatin1().data() << endl
00244 << " " << QString ("<load file> ").leftJustified(COLUMN_WIDTH, ' ').toLatin1().data()
00245 << QObject::tr ("File(s) to be imported or opened at startup").toLatin1().data() << endl;
00246
00247 exit (0);
00248 }
00249 }
00250
00251 void showStylesAndExit ()
00252 {
00253 cout << "Available styles: " << QStyleFactory::keys ().join (", ").toLatin1().data() << endl;
00254 exit (0);
00255 }