123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267 |
- /*=============================================================================
- Library: CTK
- Copyright (c) 2010 Brigham and Women's Hospital (BWH) All Rights Reserved.
- 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 "ctkModuleDescription.h"
- #include <iostream>
- #include "QFile"
- #include "QTextStream"
- //----------------------------------------------------------------------------
- ctkModuleDescription::ctkModuleDescription()
- {
- }
- //----------------------------------------------------------------------------
- ctkModuleDescription::ctkModuleDescription(const ctkModuleDescription &md)
- : QHash<QString, QString>( QHash<QString, QString>( md ) )
- {
- this->ParameterGroups = md.ParameterGroups;
- this->Logo = md.Logo;
- }
- //----------------------------------------------------------------------------
- void ctkModuleDescription::operator=(const ctkModuleDescription &md)
- {
- QHash<QString, QString>::operator=(md);
- this->ParameterGroups = md.ParameterGroups;
- this->Logo = md.Logo;
- }
- //----------------------------------------------------------------------------
- QTextStream & operator<<(QTextStream &os, const ctkModuleDescription &module)
- {
- os << QHash<QString, QString>(module);
- //os << "Logo: " << module.GetLogo() << endl;
- os << "ParameterGroups: " << endl;
- foreach( const ctkModuleParameterGroup& it, module.parameterGroups())
- { os << it; }
- return os;
- }
- //----------------------------------------------------------------------------
- bool ctkModuleDescription::hasReturnParameters() const
- {
- // iterate over each parameter group
- QVector<ctkModuleParameterGroup>::const_iterator pgbeginit
- = this->ParameterGroups.begin();
- QVector<ctkModuleParameterGroup>::const_iterator pgendit
- = this->ParameterGroups.end();
- QVector<ctkModuleParameterGroup>::const_iterator pgit;
-
- for (pgit = pgbeginit; pgit != pgendit; ++pgit)
- {
- // iterate over each parameter in this group
- QVector<ctkModuleParameter>::const_iterator pbeginit
- = (*pgit).parameters().begin();
- QVector<ctkModuleParameter>::const_iterator pendit
- = (*pgit).parameters().end();
- QVector<ctkModuleParameter>::const_iterator pit;
- for (pit = pbeginit; pit != pendit; ++pit)
- {
- if ((*pit).isReturnParameter())
- {
- return true;
- }
- }
- }
- return false;
- }
- //----------------------------------------------------------------------------
- bool ctkModuleDescription::setParameterDefaultValue(const QString& name, const QString& value)
- {
- ctkModuleParameter* param = parameter( name );
- if ( param )
- {
- (*param)[ "Default" ] = value;
- return true;
- }
- return false;
- }
- //----------------------------------------------------------------------------
- ctkModuleParameter* ctkModuleDescription::parameter(const QString& name)
- {
- // iterate over each parameter group
- QVector<ctkModuleParameterGroup>::iterator pgbeginit
- = this->ParameterGroups.begin();
- QVector<ctkModuleParameterGroup>::iterator pgendit
- = this->ParameterGroups.end();
- QVector<ctkModuleParameterGroup>::iterator pgit;
-
- for (pgit = pgbeginit; pgit != pgendit; ++pgit)
- {
- // iterate over each parameter in this group
- QVector<ctkModuleParameter>::iterator pbeginit
- = (*pgit).parameters().begin();
- QVector<ctkModuleParameter>::iterator pendit
- = (*pgit).parameters().end();
- QVector<ctkModuleParameter>::iterator pit;
- for (pit = pbeginit; pit != pendit; ++pit)
- {
- if ((*pit)["Name"] == name)
- {
- return &(*pit);
- }
- }
- }
- return NULL;
- }
- //----------------------------------------------------------------------------
- void ctkModuleDescription ::setLogo(const QIcon& logo)
- {
- this->Logo = logo;
- }
- //----------------------------------------------------------------------------
- const QIcon& ctkModuleDescription::logo() const
- {
- return this->Logo;
- }
- //----------------------------------------------------------------------------
- bool ctkModuleDescription ::readParameterFile(const QString& filename)
- {
- bool modified = false;
- QFile file(filename);
- if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
- {
- std::cout << "Parameter file " << filename.toStdString( ) << " could not be opened." << endl;
- return false;
- }
- QTextStream in(&file);
- while (!in.atEnd())
- {
- QString line = in.readLine();
- // split the line into key: value
- QString key, value;
- line = line.trimmed();
- QStringList list = line.split( "=" );
- key = list[ 0 ].trimmed();
- if ( list.size() == 1 )
- {
- continue;
- }
- value = list[ 1 ].trimmed();
-
- // std::cout << "key=" << key << ", value=" << value << "!" << endl;
- ctkModuleParameter *param = this->parameter( key );
- if ( param )
- {
- if (value != (*param)["Default"] )
- {
- (*param)["Default"] = value;
- modified = true;
- // multiple="true" may have to be handled differently
- }
- }
- }
- return modified;
- }
- //----------------------------------------------------------------------------
- bool ctkModuleDescription::
- writeParameterFile(const QString& filename, bool withHandlesToBulkParameters)
- {
- QFile rtp(filename);
- if (!rtp.open(QIODevice::WriteOnly | QIODevice::Text))
- {
- std::cout << "Parameter file " << filename.toStdString() << " could not be opened for writing." << endl;
- return false;
- }
- QTextStream in(&rtp);
- // iterate over each parameter group
- QVector<ctkModuleParameterGroup>::const_iterator pgbeginit
- = this->ParameterGroups.begin();
- QVector<ctkModuleParameterGroup>::const_iterator pgendit
- = this->ParameterGroups.end();
- QVector<ctkModuleParameterGroup>::const_iterator pgit;
-
- for (pgit = pgbeginit; pgit != pgendit; ++pgit)
- {
- // iterate over each parameter in this group
- QVector<ctkModuleParameter>::const_iterator pbeginit
- = (*pgit).parameters().begin();
- QVector<ctkModuleParameter>::const_iterator pendit
- = (*pgit).parameters().end();
- QVector<ctkModuleParameter>::const_iterator pit;
- for (pit = pbeginit; pit != pendit; ++pit)
- {
- // write out all parameters or just the ones that are not bulk parameters
- if (withHandlesToBulkParameters
- || (!withHandlesToBulkParameters
- && ((*pit)[ "Tag" ] != "image"
- && (*pit)[ "Tag" ] != "geometry"
- && (*pit)[ "Tag" ] != "transform"
- && (*pit)[ "Tag" ] != "table"
- && (*pit)[ "Tag" ] != "measurement"
- && (*pit)[ "Tag" ] != "point" // point and region are special
- && (*pit)[ "Tag" ] != "region")))
- {
- in << (*pit)[ "Name" ] << " = " << (*pit)[ "Default" ] << endl;
- // multiple="true" may have to be handled differently
- }
- }
- }
- return true;
- }
- void ctkModuleDescription::addParameterGroup( const ctkModuleParameterGroup &group )
- {
- this->ParameterGroups.push_back(group);
- }
- const QVector<ctkModuleParameterGroup>& ctkModuleDescription::parameterGroups() const
- {
- return this->ParameterGroups;
- }
- QVector<ctkModuleParameterGroup>& ctkModuleDescription::parameterGroups()
- {
- return this->ParameterGroups;
- }
- void ctkModuleDescription::setParameterGroups( const QVector<ctkModuleParameterGroup>& groups )
- {
- this->ParameterGroups = groups;
- }
|