| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292 | /*=============================================================================    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 "ctkCmdLineModuleObjectTreeWalker_p.h"#include <QObject>#include <QStack>#include <QVariant>namespace {static QString PREFIX_EXECUTABLE = "executable:";static QString PREFIX_PARAMETER_GROUP = "paramGroup:";static QString PREFIX_PARAMETER = "parameter:";}//----------------------------------------------------------------------------ctkCmdLineModuleObjectTreeWalker::ctkCmdLineModuleObjectTreeWalker(QObject *root)  : RootObject(root), CurrentObject(0), CurrentToken(NoToken),    AtEnd(false){}//----------------------------------------------------------------------------ctkCmdLineModuleObjectTreeWalker::~ctkCmdLineModuleObjectTreeWalker(){}//----------------------------------------------------------------------------void ctkCmdLineModuleObjectTreeWalker::setRootObject(QObject* root){  RootObject = root;  clear();}//----------------------------------------------------------------------------void ctkCmdLineModuleObjectTreeWalker::clear(){  CurrentToken = NoToken;  CurrentObject = 0;  ObjectStack.clear();}//----------------------------------------------------------------------------bool ctkCmdLineModuleObjectTreeWalker::atEnd() const{  return AtEnd || RootObject == 0;}//----------------------------------------------------------------------------bool ctkCmdLineModuleObjectTreeWalker::isParameterGroup() const{  return CurrentToken == ParameterGroup;}//----------------------------------------------------------------------------bool ctkCmdLineModuleObjectTreeWalker::isParameter() const{  return CurrentToken == Parameter;}//----------------------------------------------------------------------------QString ctkCmdLineModuleObjectTreeWalker::name() const{  if (CurrentObject == 0) return QString();  switch(CurrentToken)  {  case Executable: return CurrentObject->objectName().mid(PREFIX_EXECUTABLE.size());  case ParameterGroup: return CurrentObject->objectName().mid(PREFIX_PARAMETER_GROUP.size());  case Parameter: return CurrentObject->objectName().mid(PREFIX_PARAMETER.size());  default: return QString();  }}//----------------------------------------------------------------------------QString ctkCmdLineModuleObjectTreeWalker::label() const{  if (CurrentObject == 0) return QString();  switch(CurrentToken)  {  case Executable: return CurrentObject->objectName().mid(PREFIX_EXECUTABLE.size());  case ParameterGroup: return property("title").toString();  case Parameter: return property("label").toString();  default: return QString();  }}//----------------------------------------------------------------------------QVariant ctkCmdLineModuleObjectTreeWalker::value(const QString &propertyName) const{  QString valProp = propertyName;  if (valProp.isEmpty())  {    valProp = property("valueProperty").toString();  }  return property(valProp);}//----------------------------------------------------------------------------void ctkCmdLineModuleObjectTreeWalker::setValue(const QVariant& value, const QString &propertyName){  QString valProp = propertyName;  if (valProp.isEmpty())  {    valProp = property("valueProperty").toString();  }  if (!valProp.isEmpty())  {    CurrentObject->setProperty(qPrintable(valProp), value);  }}//----------------------------------------------------------------------------QString ctkCmdLineModuleObjectTreeWalker::flag() const{  QVariant v = property("flag");  return v.isValid() ? v.toString() : QString();}//----------------------------------------------------------------------------QString ctkCmdLineModuleObjectTreeWalker::longFlag() const{  QVariant v = property("longflag");  return v.isValid() ? v.toString() : QString();}//----------------------------------------------------------------------------int ctkCmdLineModuleObjectTreeWalker::index() const{  QVariant v = property("index");  return v.isValid() ? v.toInt() : -1;}//----------------------------------------------------------------------------bool ctkCmdLineModuleObjectTreeWalker::isMultiple() const{  QVariant v = property("multiple");  return v.isValid() ? v.toBool() : false;}//----------------------------------------------------------------------------QVariant ctkCmdLineModuleObjectTreeWalker::property(const QString &propName) const{  if (CurrentObject == 0) return QVariant();  // First try to get a prefixed property  QVariant res = prefixedProperty(propName);  // Try to get a property with the original name  if (!res.isValid()) res = CurrentObject->property(qPrintable(propName));  return res;}//----------------------------------------------------------------------------ctkCmdLineModuleObjectTreeWalker::TokenType ctkCmdLineModuleObjectTreeWalker::readNext(){  if (AtEnd) return NoToken;  QObject* curr = 0;  if (CurrentObject == 0)  {    curr = RootObject;    if (setCurrent(curr)) return CurrentToken;  }  else  {    curr = CurrentObject;  }  while (true)  {    if (curr)    {      QObjectList children = curr->children();      QListIterator<QObject*> i(children);      i.toBack();      while (i.hasPrevious())      {        ObjectStack.push(i.previous());      }      if (children.isEmpty())      {        curr = 0;      }      else      {        curr = ObjectStack.pop();        if (setCurrent(curr)) return CurrentToken;      }      continue;    }    if (ObjectStack.isEmpty()) break;    curr = ObjectStack.pop();    if (setCurrent(curr)) return CurrentToken;  }  AtEnd = true;  CurrentObject = 0;  CurrentToken = NoToken;  return NoToken;}//----------------------------------------------------------------------------bool ctkCmdLineModuleObjectTreeWalker::readNextExecutable(){  while (!(readNext() == Executable || AtEnd));  return !AtEnd;}//----------------------------------------------------------------------------bool ctkCmdLineModuleObjectTreeWalker::readNextParameterGroup(){  while (!(readNext() == ParameterGroup || AtEnd));  return !AtEnd;}//----------------------------------------------------------------------------bool ctkCmdLineModuleObjectTreeWalker::readNextParameter(){  while (!(readNext() == Parameter || AtEnd));  return !AtEnd;}//----------------------------------------------------------------------------ctkCmdLineModuleObjectTreeWalker::TokenType ctkCmdLineModuleObjectTreeWalker::tokenType() const{  return CurrentToken;}//----------------------------------------------------------------------------QVariant ctkCmdLineModuleObjectTreeWalker::prefixedProperty(const QString& propName) const{  if (CurrentObject == 0) return QString();  QString prefixedName;  switch(CurrentToken)  {  case ctkCmdLineModuleObjectTreeWalker::Executable: prefixedName = PREFIX_EXECUTABLE + propName;  case ctkCmdLineModuleObjectTreeWalker::ParameterGroup: prefixedName = PREFIX_PARAMETER_GROUP + propName;  case ctkCmdLineModuleObjectTreeWalker::Parameter: prefixedName = PREFIX_PARAMETER + propName;  default: ;  }  return CurrentObject->property(qPrintable(prefixedName));}//----------------------------------------------------------------------------ctkCmdLineModuleObjectTreeWalker::TokenTypectkCmdLineModuleObjectTreeWalker::token(QObject* obj){  if (obj == 0) return ctkCmdLineModuleObjectTreeWalker::NoToken;  QString name = obj->objectName();  if (name.startsWith(PREFIX_EXECUTABLE)) return ctkCmdLineModuleObjectTreeWalker::Executable;  if (name.startsWith(PREFIX_PARAMETER_GROUP)) return ctkCmdLineModuleObjectTreeWalker::ParameterGroup;  if (name.startsWith(PREFIX_PARAMETER)) return ctkCmdLineModuleObjectTreeWalker::Parameter;  return ctkCmdLineModuleObjectTreeWalker::NoToken;}//----------------------------------------------------------------------------bool ctkCmdLineModuleObjectTreeWalker::setCurrent(QObject* obj){  ctkCmdLineModuleObjectTreeWalker::TokenType t = token(obj);  if (t != ctkCmdLineModuleObjectTreeWalker::NoToken)  {    CurrentObject = obj;    CurrentToken = t;    return true;  }  return false;}
 |