6#include
"UserInterface.h"
12#include <QRegularExpression>
14#include "Application.h"
17#include "IException.h"
20#include "Preference.h"
21#include "ProgramLauncher.h"
48 FileName setup =
"$HOME/.Isis/history";
50 if ( !setup.fileExists() ) {
51 setup.dir().mkpath(
".");
54 catch (IException &) {
85 FileName setup =
"$HOME/.Isis/history";
87 if ( !setup.fileExists() ) {
88 setup.dir().mkpath(
".");
91 catch (IException &) {
101 p_gui = Gui::Create(*
this, argc, argv);
156 for (
unsigned int currArgument = 1; currArgument <
p_cmdline.size(); currArgument ++) {
158 vector<QString> paramValue;
164 if (paramName[0] ==
'-')
167 for (
unsigned int value = 0; value < paramValue.size(); value++) {
168 IString thisValue = paramValue[value];
169 QString token = thisValue.Token(
"$").ToQt();
173 while (thisValue !=
"") {
176 int j = toInt( thisValue.substr(0, 1).c_str() ) - 1;
178 thisValue.replace(0, 1,
"");
179 token = thisValue.Token(
"$").ToQt();
181 catch (IException &e) {
184 token = thisValue.Token(
"$").ToQt();
191 paramValue[value] = newValue;
195 catch (IException &e) {
196 throw IException(IException::User,
"Invalid command line", _FILEINFO_);
203 if(paramValue.size() == 1) {
204 cout <<
"=" << paramValue[0] <<
" ";
206 else if (paramValue.size() > 1) {
209 for (
unsigned int value = 0; value < paramValue.size(); value++) {
213 cout << paramValue[value] << endl;
239 QString fileName( FileName(
p_errList).expanded() );
240 os.open(fileName.toLatin1().data(), std::ios::app);
244 QString msg =
"Unable to create error list [" +
p_errList
245 +
"] Disk may be full or directory permissions not writeable";
246 throw IException(IException::User, msg, _FILEINFO_);
249 for (
int j = 0; j < (int)
p_batchList[i].size(); j++) {
266 Preference &p = Preference::Preferences();
267 PvlGroup &grp = p.findGroup(
"UserInterface", Isis::Pvl::Traverse);
268 if (grp[
"HistoryRecording"][0] ==
"Off")
272 Isis::FileName histFile(grp[
"HistoryPath"][0] +
"/" +
ProgramName() +
".par");
288 if ( histFile.fileExists() ) {
289 hist.read( histFile.expanded() );
292 catch (IException &) {
296 hist.addGroup( cmdLine.findGroup(
"UserParameters") );
299 while( hist.groups() > toInt(grp[
"HistoryLength"][0]) ) {
300 hist.deleteGroup(
"UserParameters");
305 hist.write( histFile.expanded() );
307 catch (IException &) {
330 catch (IException &e) {
331 QString msg =
"The batchlist file [" + file +
"] could not be opened";
332 throw IException(IException::User, msg, _FILEINFO_);
337 for (
int i = 0; i < temp.
LineCount(); i++) {
342 t = IString(t).Replace(
"\t",
" ",
true).ToQt();
344 t = IString(t).Compress().ToQt().trimmed();
346 t = IString(t).Replace(
" ,",
",",
true).ToQt();
347 t = IString(t).Replace(
", ",
",",
true).ToQt();
349 t = IString(t).Replace(
" ",
",",
true).ToQt();
354 foreach (QString token, tokens) {
356 token = token.remove( QRegularExpression(
"[\"']") );
366 QString msg =
"The number of columns must be constant in batchlist";
367 throw IException(IException::User, msg, _FILEINFO_);
372 QString msg =
"The list file [" + file +
"] does not contain any data";
373 throw IException(IException::User, msg, _FILEINFO_);
396 args.prepend(
"someapp");
399 c_args = (
char**)malloc(
sizeof(
char*)*args.size());
401 for (
int i = 0; i < args.size(); i++) {
402 c_args[i] = (
char*)malloc(
sizeof(
char)*args[i].size()+1);
403 strcpy(c_args[i], args[i].toLatin1().data());
431 if ( (argc == 1) && (file.name() !=
"unitTest") ) {
436 for (
int i = 0; i < argc; i++) {
441 vector<QString> options;
442 options.push_back(
"-GUI");
443 options.push_back(
"-NOGUI");
444 options.push_back(
"-BATCHLIST");
445 options.push_back(
"-LAST");
446 options.push_back(
"-RESTORE");
447 options.push_back(
"-WEBHELP");
448 options.push_back(
"-HELP");
449 options.push_back(
"-ERRLIST");
450 options.push_back(
"-ONERROR");
451 options.push_back(
"-SAVE");
452 options.push_back(
"-INFO");
453 options.push_back(
"-PREFERENCE");
454 options.push_back(
"-LOG");
455 options.push_back(
"-VERBOSE");
456 options.push_back(
"-PID");
458 bool usedDashLast =
false;
459 bool usedDashRestore =
false;
469 for (
unsigned int currArgument = 1; currArgument < (unsigned)argc; currArgument++) {
471 vector<QString> paramValue;
477 if (paramName[0] ==
'-') {
478 paramName = paramName.toUpper();
482 if (paramValue.size() > 1) {
483 QString msg =
"Invalid value for reserve parameter ["
485 throw IException(IException::User, msg, _FILEINFO_);
495 if (paramName ==
"-LAST") {
502 if (paramName ==
"-RESTORE") {
503 usedDashRestore =
true;
507 QString realValue =
"";
509 if ( paramValue.size() ) {
510 realValue = paramValue[0];
522 catch (IException &e) {
523 throw IException(e, IException::User,
"Invalid command line", _FILEINFO_);
530 cout << BuildNewCommandLineFromPvl(temp) << endl;
535 || usedDashRestore) ) {
536 QString msg =
"-BATCHLIST cannot be used with -GUI, -SAVE, -RESTORE, ";
538 throw IException(IException::User, msg, _FILEINFO_);
543 QString msg =
"-ERRLIST and -ONERROR=continue cannot be used without ";
544 msg +=
" the -BATCHLIST option";
545 throw IException(IException::User, msg, _FILEINFO_);
550 QString UserInterface::BuildNewCommandLineFromPvl(Pvl temp){
551 PvlGroup group = temp.group(0);
552 int numKeywords = group.keywords();
555 for(
int i = 0; i < numKeywords; i++){
556 PvlKeyword key = group[i];
557 returnVal += key.name();
559 returnVal += QString(key);
576 Isis::FileName hist(file);
577 if ( hist.fileExists() ) {
579 Isis::Pvl lab( hist.expanded() );
581 int g = lab.groups() - 1;
582 if (g >= 0 && lab.group(g).isNamed(
"UserParameters") ) {
583 Isis::PvlGroup &up = lab.group(g);
585 for (
int k = 0; k < up.keywords(); k++) {
586 QString keyword = up[k].name();
588 vector<QString> values;
590 for (
int i = 0; i < up[k].size(); i++) {
591 values.push_back(up[k][i]);
596 bool matchesDefault =
false;
597 if (values.size() == 1 && paramData->internalDefault == values[0])
598 matchesDefault =
true;
600 if (!matchesDefault) {
602 (values.size() == paramData->defaultValues.size());
604 for (
int i = 0; matchesDefault && i < (int)values.size(); i++) {
605 matchesDefault = matchesDefault &&
606 values[i] == paramData->defaultValues[i];
610 if (!matchesDefault) {
612 commandline += keyword +
"=";
613 foreach(QString val, values) {
614 commandline += val +
" ";
622 for (
int o = lab.objects() - 1; o >= 0; o--) {
624 Isis::PvlObject &obj = lab.object(o);
625 for (
int g = obj.groups() - 1; g >= 0; g--) {
626 Isis::PvlGroup &up = obj.group(g);
627 if ( up.isNamed(
"UserParameters") ) {
628 for (
int k = 0; k < up.keywords(); k++) {
629 QString keyword = up[k].name();
630 QString value = up[k][0];
644 QString msg =
"The history file [" + file +
"] is for a different application or corrupt, "\
645 "please fix or delete this file";
646 throw IException(IException::User, msg, _FILEINFO_);
650 QString msg =
"The history file [" + file +
"] does not exist";
651 throw IException(IException::User, msg, _FILEINFO_);
674 const QString value) {
676 bool unitTest =
false;
677 if (FileName(
p_progName).name() ==
"unitTest") {
680 Preference &p = Preference::Preferences();
682 if (name ==
"-GUI") {
685 else if (name ==
"-NOGUI") {
688 else if (name ==
"-BATCHLIST") {
691 else if (name ==
"-LAST") {
695 histFile =
"./" + FileName(
p_progName).name() +
".par";
698 PvlGroup &grp = p.findGroup(
"UserInterface", Isis::Pvl::Traverse);
699 histFile = grp[
"HistoryPath"][0] +
"/" + FileName(
p_progName).name() +
".par";
704 else if(name ==
"-RESTORE") {
707 else if(name ==
"-WEBHELP") {
708 Isis::PvlGroup &pref = Isis::Preference::Preferences().findGroup(
"UserInterface");
709 QString command = pref[
"GuiHelpBrowser"];
710 command +=
" $ISISROOT/docs/Application/presentation/Tabbed/";
714 throw IException(IException::Programmer,
715 "Evaluating -WEBHELP should only throw this exception during a unitTest",
724 else if (name ==
"-INFO") {
728 if (value.size() != 0) {
732 else if (name ==
"-HELP") {
733 if (value.size() == 0) {
735 params.setTerminator(
"");
746 key.addValue(
"*" + def);
758 param.setTerminator(
"");
763 param += PvlKeyword(
"ParameterName", key);
764 param += PvlKeyword(
"Brief",
ParamBrief(k, j) );
765 param += PvlKeyword(
"Type",
ParamType(k, j) );
767 param += PvlKeyword(
"PixelType",
PixelType(k, j) );
777 param += PvlKeyword(
"GreaterThanOrEqual",
781 param += PvlKeyword(
"GreaterThan",
787 param += PvlKeyword(
"LessThanOrEqual",
791 param += PvlKeyword(
"LessThan",
796 PvlKeyword key(
"LessThan");
803 PvlKeyword key(
"LessThanOrEqual");
810 PvlKeyword key(
"NotEqual");
817 PvlKeyword key(
"GreaterThan");
824 PvlKeyword key(
"GreaterThanOrEqual");
831 PvlKeyword key(
"Inclusions");
838 PvlKeyword key(
"Exclusions");
845 param += PvlKeyword(
"Odd",
ParamOdd(k, j) );
852 PvlKeyword include(
"Inclusions");
859 PvlKeyword exclude(
"Exclusions");
875 throw IException(IException::Programmer,
876 "Evaluating -HELP should only throw this exception during a unitTest",
884 else if (name ==
"-PID") {
887 else if (name ==
"-ERRLIST") {
891 QString msg =
"-ERRLIST expects a file name";
892 throw IException(IException::User, msg, _FILEINFO_);
895 if ( FileName(
p_errList).fileExists() ) {
899 else if (name ==
"-ONERROR") {
900 if (value.toUpper() ==
"CONTINUE") {
904 else if (value.toUpper() ==
"ABORT") {
909 QString msg =
"[" + value
910 +
"] is an invalid value for -ONERROR, options are ABORT or CONTINUE";
911 throw IException(IException::User, msg, _FILEINFO_);
914 else if (name ==
"-SAVE") {
915 if (value.size() == 0) {
922 else if (name ==
"-PREFERENCE") {
924 p_preference = value;
926 else if (name ==
"-LOG") {
927 if( value.isEmpty() ) {
928 p.findGroup(
"SessionLog")[
"FileOutput"].setValue(
"On");
931 p.findGroup(
"SessionLog")[
"FileOutput"].setValue(
"On");
932 p.findGroup(
"SessionLog")[
"FileName"].setValue(value);
936 else if (name ==
"-VERBOSE") {
937 p.findGroup(
"SessionLog")[
"TerminalOutput"].setValue(
"On");
942 QString msg =
"-GUI and -PID are incompatible arguments";
943 throw IException(IException::Unknown, msg, _FILEINFO_);
962 std::vector<QString> &value) {
964 QString paramValue =
"";
969 if ( !paramName.contains(
"=") ) {
974 if (QString(
p_cmdline[curPos + 1]).compare(
"=") == 0) {
983 else if ( paramName.endsWith(
"=") ) {
984 paramName = paramName.mid(0, paramName.size() - 1);
994 else if (paramName.indexOf(
"=") > 0) {
995 QString parameterLiteral =
p_cmdline[curPos];
996 paramName = parameterLiteral.mid( 0, parameterLiteral.indexOf(
"=") );
997 paramValue = parameterLiteral.mid(parameterLiteral.indexOf(
"=") + 1);
1002 QString msg =
"Unknown parameter [" + QString(
p_cmdline[curPos])
1004 throw IException(IException::User, msg, _FILEINFO_);
1011 paramValue = paramValue.trimmed();
1013 if (paramValue.length() > 0 && paramValue[0] !=
'(') {
1018 if (paramValue.length() > 1 && paramValue.mid(0, 2) ==
"\\(") {
1019 paramValue = paramValue.mid(1);
1022 else if (paramValue.length() > 2 && paramValue.mid(0, 3) ==
"\\\\(") {
1023 paramValue = paramValue.mid(1);
1026 value.push_back(paramValue);
1028 else if ( paramValue.length() ) {
1048 std::vector<QString> &reservedParams) {
1049 for (
unsigned int currArgument = 1; currArgument < (unsigned)
p_cmdline.size();
1052 QString paramName =
p_cmdline[currArgument];
1053 QString trueParamValue =
"";
1054 vector<QString> paramValue;
1057 if (paramName[0] ==
'-') {
1061 paramName = paramName.toUpper();
1064 if ( paramValue.size() ) {
1065 trueParamValue = paramValue[0].toUpper();
1072 if (fullReservedName == paramName) {
1094 std::vector<QString> values;
1096 bool inDoubleQuotes =
false;
1097 bool inSingleQuotes =
false;
1098 bool arrayClosed =
false;
1099 bool nextElementStarted =
false;
1100 QString currElement =
"";
1102 for (
int strPos = 0; strPos < arrayString.size(); strPos++) {
1104 if (arrayString[strPos] !=
'(') {
1105 QString msg =
"Invalid array format [" + arrayString +
"]";
1106 throw IException(IException::User, msg, _FILEINFO_);
1113 if ( arrayString[strPos] ==
'\\' && strPos + 1 < (
int)arrayString.size() ) {
1114 currElement += arrayString[strPos+1];
1119 else if (arrayString[strPos] ==
'\\') {
1120 QString msg =
"Invalid array format [" + arrayString +
"]";
1121 throw IException(IException::User, msg, _FILEINFO_);
1125 if (!inDoubleQuotes && !inSingleQuotes) {
1127 QString msg =
"Invalid array format [" + arrayString +
"]";
1128 throw IException(IException::User, msg, _FILEINFO_);
1131 nextElementStarted = (nextElementStarted || arrayString[strPos] !=
' ');
1133 if (!nextElementStarted) {
1137 if (arrayString[strPos] ==
'"') {
1138 inDoubleQuotes =
true;
1140 else if (arrayString[strPos] ==
'\'') {
1141 inSingleQuotes =
true;
1143 else if (arrayString[strPos] ==
',') {
1144 values.push_back(currElement);
1146 nextElementStarted =
false;
1148 else if (arrayString[strPos] ==
')') {
1149 values.push_back(currElement);
1152 nextElementStarted =
false;
1154 else if (nextElementStarted && arrayString[strPos] ==
' ') {
1156 bool onlyWhite =
true;
1158 for(
int pos = strPos;
1159 onlyWhite && arrayString[pos] !=
',' && arrayString[pos] !=
')' &&
1160 pos < arrayString.size(); pos++) {
1161 onlyWhite &= (arrayString[pos] ==
' ');
1165 currElement += arrayString[strPos];
1168 else if (nextElementStarted) {
1169 currElement += arrayString[strPos];
1172 else if (inSingleQuotes) {
1173 if(arrayString[strPos] ==
'\'') {
1174 inSingleQuotes =
false;
1177 currElement += arrayString[strPos];
1182 if (arrayString[strPos] ==
'"') {
1183 inDoubleQuotes =
false;
1186 currElement += arrayString[strPos];
1191 if (!arrayClosed || currElement !=
"") {
1192 QString msg =
"Invalid array format [" + arrayString +
"]";
1193 throw IException(IException::User, msg, _FILEINFO_);
1219 std::vector<QString> &reservedParams,
1220 bool handleNoMatches) {
1222 int matchOption = -1;
1224 for (
int option = 0; option < (int)reservedParams.size(); option++) {
1226 if ( reservedParams[option].startsWith(unresolvedParam) ) {
1227 if (matchOption >= 0) {
1228 QString msg =
"Ambiguous Reserve Parameter ["
1229 + unresolvedParam +
"]. Please clarify.";
1230 throw IException(IException::User, msg, _FILEINFO_);
1233 matchOption = option;
1237 if (handleNoMatches) {
1239 if (matchOption < 0) {
1240 QString msg =
"Invalid Reserve Parameter Option ["
1241 + unresolvedParam +
"]. Choices are ";
1244 for (
int option = 0; option < (int)reservedParams.size(); option++) {
1246 if (reservedParams[option].compare(
"-PID") == 0) {
1250 msgOptions += reservedParams[option];
1263 msg +=
" [" + msgOptions +
"]";
1265 throw IException(IException::User, msg, _FILEINFO_);
1268 if (matchOption < 0) {
1272 return reservedParams[matchOption];
static void checkX11()
check to see if X is available
static void RunSystemCommand(QString commandLine)
This runs arbitrary system commands.
Provides access to sequential ASCII stream I/O.
bool GetLine(QString &line, const bool skipComments=true)
Gets next line from file.
void Open(const QString &filename, const char *openmode="input", const char *extension="")
Opens a text file.
int LineCount(const int &maxLinesToRead=0)
Counts number of lines in file.
QString p_saveFile
FileName to save last history to.
int p_parentId
This is a status to indicate if the GUI is running or not.
void preProcess(QString fullReservedName, std::vector< QString > &reservedParams)
This parses the command line and looks for the specified reserved parameter name passed.
std::vector< std::vector< QString > > p_batchList
Vector of batchlist data.
QString resolveParameter(QString &name, std::vector< QString > &reservedParams, bool handleNoMatches=true)
This resolves a reserved parameter token on the command line to its fullname.
QString p_progName
Name of program to run.
QString p_infoFileName
FileName to save debugging info.
QString GetInfoFileName()
This method returns the filename where the debugging info is stored when the "-info" tag is used.
std::vector< char * > p_cmdline
This variable will contain argv.
bool p_interactive
Boolean value representing whether the program is interactive or not.
bool p_info
Boolean value representing if it's in debug mode.
UserInterface(const QString &xmlfile, int &argc, char *argv[])
Constructs an UserInterface object.
void loadBatchList(const QString file)
Loads the user entered batchlist file into a private variable for later use.
void loadCommandLine(QVector< QString > &args, bool ignoreAppName=true)
This is used to load the command line into p_cmdline and the Aml object using information contained i...
bool GetInfoFlag()
This method returns the flag state of info.
std::vector< QString > readArray(QString arrayString)
This interprets an array value from the command line.
void evaluateOption(const QString name, const QString value)
This interprets the "-" options for reserved parameters.
void loadHistory(const QString file)
Loads the previous history for the program.
bool p_abortOnError
Boolean value representing whether to abort or continue on error.
Gui * p_gui
Pointer to the gui object.
QString p_errList
FileName to write batchlist line that caused error on.
int BatchListSize()
Returns the size of the batchlist.
void SetErrorList(int i)
This method adds the line specified in the BatchList that the error occured on.
void SaveHistory()
Saves the user parameter information in the history of the program for later use.
void SetBatchList(int i)
Clears the gui parameters and sets the batch list information at line i as the new parameters.
~UserInterface()
Destroys the UserInterface object.
void getNextParameter(unsigned int &curPos, QString &unresolvedParam, std::vector< QString > &value)
This gets the next parameter in the list of arguments.
void CommandLine(Isis::Pvl &lab) const
Creates a QString which could be used as a command line.
QString ParamOdd(const int &group, const int ¶m) const
Returns whether the selected parameter has a restriction on odd values or not.
int ParamExcludeSize(const int &group, const int ¶m) const
Returns the number of parameters excluded in this parameter's exclusions.
QString ParamLessThan(const int &group, const int ¶m, const int &great) const
Returns the name of the specified lessThan parameter.
QString ParamLessThanOrEqual(const int &group, const int ¶m, const int &les) const
Returns the name of the specified lessThanOrEqual parameter.
QString ParamBrief(const int &group, const int ¶m) const
Returns the brief description of a parameter in a specified group.
QString ParamListInclude(const int &group, const int ¶m, const int &option, const int &include) const
Returns the parameter name to be included if this option is selected.
int ParamListIncludeSize(const int &group, const int ¶m, const int &option) const
Returns the number of items in a parameters list include section.
QString ParamListValue(const int &group, const int ¶m, const int &option) const
Returns the option value for a specific option to a parameter.
int NumGroups() const
Returns the number of groups found in the XML.
int ParamListExcludeSize(const int &group, const int ¶m, const int &option) const
Returns the number of items in a parameters list exclude section.
QString PixelType(const int &group, const int ¶m) const
Returns the default pixel type from the XML.
int ParamGreaterThanSize(const int &group, const int ¶m) const
Returns the number of values in the parameters greater than list.
QString ParamNotEqual(const int &group, const int ¶m, const int ¬Eq) const
Returns the name of the specified notEqual parameter.
QString ParamGreaterThanOrEqual(const int &group, const int ¶m, const int &great) const
Returns the name of the specified greaterThanOrEqual parameter.
QString ParamMaximumInclusive(const int &group, const int ¶m) const
Returns whether the maximum value is inclusive or not.
QString ParamType(const int &group, const int ¶m) const
Returns the parameter type of a parameter in a specified group.
int ParamListSize(const int &group, const int ¶m) const
Returns the number of options in the specified parameter's list.
int NumParams(const int &) const
Returns the number of parameters in a group.
QString ParamExclude(const int &group, const int ¶m, const int &exclude) const
Returns the name of the specified excluded parameter.
const IsisParameterData * ReturnParam(const QString ¶mName) const
Returns a pointer to a parameter whose name starts with paramName.
IsisAml(const QString &xmlfile)
Constructs an IsisAml object and internalizes the XML data in the given file name.
QString ParamListExclude(const int &group, const int ¶m, const int &option, const int &exclude) const
Returns the parameter name to be excluded if this option is selected.
int ParamLessThanOrEqualSize(const int &group, const int ¶m) const
Returns the number of values in the parameters less than or equal list.
QString ParamListBrief(const int &group, const int ¶m, const int &option) const
Returns the brief description for a specific option to a parameter.
QString ParamDefault(const int &group, const int ¶m) const
Returns the default for a parameter in a specified group.
int ParamLessThanSize(const int &group, const int ¶m) const
Returns the number of values in the parameters less than list.
QString ParamInclude(const int &group, const int ¶m, const int &include) const
Returns the name of the specified included parameter.
int ParamGreaterThanOrEqualSize(const int &group, const int ¶m) const
Returns the number of values in the parameters greater than or equal list.
QString ParamInternalDefault(const int &group, const int ¶m) const
Returns the internal default for a parameter in a specified group.
int ParamNotEqualSize(const int &group, const int ¶m) const
Returns the number of values in the not equal list.
QString ProgramName() const
Returns the Program name.
int ParamIncludeSize(const int &group, const int ¶m) const
Returns the number of parameters included in this parameter's inclusions.
QString ParamMinimumInclusive(const int &group, const int ¶m) const
Returns whether the minimum value is inclusive or not.
QString ParamGreaterThan(const int &group, const int ¶m, const int &great) const
Returns the name of the specified greaterThan parameter.
QString ParamMinimum(const int &group, const int ¶m) const
Returns the minimum value of a parameter in a specified group.
void Clear(const QString ¶mName)
Clears the value(s) in the named parameter.
void VerifyAll()
Verify all parameters.
QString ParamName(const int &group, const int ¶m) const
Returns the parameter name.
void PutAsString(const QString ¶mName, const QString &value)
Allows the insertion of a value for any parameter.
QString ParamMaximum(const int &group, const int ¶m) const
Returns the maximum value of a parameter in a specified group.
This is free and unencumbered software released into the public domain.
Namespace for the standard library.