123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181 |
- /*=============================================================================
-
- Library: CTK
-
- Copyright (c) German Cancer Research Center,
- Division of Medical and Biological Informatics
-
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
-
- http://www.apache.org/licenses/LICENSE-2.0
-
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
-
- =============================================================================*/
- #include "ctkCmdLineModuleInstance.h"
- #include "ctkCmdLineModuleDescription.h"
- #include "ctkCmdLineModuleParameter.h"
- #include "ctkCmdLineModuleParameterGroup.h"
- #include "ctkCmdLineModuleReference.h"
- #include "ctkCmdLineModuleProcessTask.h"
- #include "ctkException.h"
- #include <QStringList>
- #include <QDebug>
- #include <QFuture>
- struct ctkCmdLineModuleInstancePrivate
- {
- ctkCmdLineModuleInstancePrivate(ctkCmdLineModuleInstance* qq,
- const ctkCmdLineModuleReference& moduleRef)
- : ModuleReference(moduleRef), q(qq)
- {
- }
- QString normalizeFlag(const QString& flag)
- {
- return flag.trimmed().remove(QRegExp("^-*"));
- }
- ctkCmdLineModuleReference ModuleReference;
- QList<QString> ParameterNames;
- private:
- ctkCmdLineModuleInstance* q;
- };
- ctkCmdLineModuleInstance::ctkCmdLineModuleInstance(const ctkCmdLineModuleReference& moduleRef)
- : d(new ctkCmdLineModuleInstancePrivate(this, moduleRef))
- {
- }
- ctkCmdLineModuleInstance::~ctkCmdLineModuleInstance()
- {
- }
- QList<QString> ctkCmdLineModuleInstance::parameterNames() const
- {
- if (!d->ParameterNames.isEmpty()) return d->ParameterNames;
- foreach (ctkCmdLineModuleParameterGroup paramGroup,
- moduleReference().description().parameterGroups())
- {
- foreach (ctkCmdLineModuleParameter param, paramGroup.parameters())
- {
- d->ParameterNames.push_back(param.name());
- }
- }
- return d->ParameterNames;
- }
- ctkCmdLineModuleReference ctkCmdLineModuleInstance::moduleReference() const
- {
- return d->ModuleReference;
- }
- QString ctkCmdLineModuleInstance::location() const
- {
- return d->ModuleReference.location();
- }
- QStringList ctkCmdLineModuleInstance::commandLineArguments() const
- {
- QStringList cmdLineArgs;
- QHash<int, QString> indexedArgs;
- QHash<QString,QVariant> currentValues = values();
- ctkCmdLineModuleDescription description = moduleReference().description();
- QHashIterator<QString,QVariant> valuesIter(currentValues);
- while(valuesIter.hasNext())
- {
- valuesIter.next();
- ctkCmdLineModuleParameter parameter = description.parameter(valuesIter.key());
- if (parameter.index() > -1)
- {
- indexedArgs.insert(parameter.index(), valuesIter.value().toString());
- }
- else
- {
- QString argFlag;
- if (parameter.longFlag().isEmpty())
- {
- argFlag = QString("-") + d->normalizeFlag(parameter.flag());
- }
- else
- {
- argFlag = QString("--") + d->normalizeFlag(parameter.longFlag());
- }
- QStringList args;
- if (parameter.multiple())
- {
- args = valuesIter.value().toString().split(',', QString::SkipEmptyParts);
- }
- else
- {
- args.push_back(valuesIter.value().toString());
- }
- foreach(QString arg, args)
- {
- cmdLineArgs << argFlag << arg;
- }
- }
- }
- QList<int> indexes = indexedArgs.keys();
- qSort(indexes.begin(), indexes.end());
- foreach(int index, indexes)
- {
- cmdLineArgs << indexedArgs[index];
- }
- return cmdLineArgs;
- }
- QFuture<QString> ctkCmdLineModuleInstance::run() const
- {
- QStringList args = commandLineArguments();
- // Instances of ctkCmdLineModuleProcessTask are auto-deleted by the
- // thread pool.
- ctkCmdLineModuleProcessTask* moduleProcess =
- new ctkCmdLineModuleProcessTask(d->ModuleReference.location(), args);
- return moduleProcess->start();
- }
- QHash<QString, QVariant> ctkCmdLineModuleInstance::values() const
- {
- QHash<QString,QVariant> result;
- foreach(QString parameterName, parameterNames())
- {
- result.insert(parameterName, value(parameterName));
- }
- return result;
- }
- void ctkCmdLineModuleInstance::setValues(const QHash<QString, QVariant> &values)
- {
- QHashIterator<QString,QVariant> iter(values);
- while(iter.hasNext())
- {
- iter.next();
- setValue(iter.key(), iter.value());
- }
- }
|