Go to the documentation of this file.00001 #include <QStringList>
00002 #include "rqt_mrta/config/params.h"
00003 #include "rqt_mrta/config/param_factory.h"
00004
00005 namespace rqt_mrta
00006 {
00007 namespace config
00008 {
00009 Params::Params(Params* parent) : ParamInterface("params", parent) {}
00010
00011 Params::Params(const QString& group_name, Params* parent)
00012 : ParamInterface(group_name, parent)
00013 {
00014 }
00015
00016 Params::~Params()
00017 {
00018 for (size_t index(0); index < params_.count(); index++)
00019 {
00020
00021
00022
00023
00024
00025 }
00026 params_.clear();
00027 }
00028
00029 QVector<ParamInterface*> Params::getChildren() const { return params_; }
00030
00031 ParamInterface* Params::getChild(size_t index) const { return params_[index]; }
00032
00033 ParamInterface* Params::getParam(const QString& relative_name) const
00034 {
00035 QStringList names(relative_name.split("/"));
00036 QString root_name(names[0]);
00037 if (root_name.isEmpty())
00038 {
00039 return NULL;
00040 }
00041 names.removeFirst();
00042 for (size_t i(0); i < params_.count(); i++)
00043 {
00044 if (params_[i]->getName() == root_name)
00045 {
00046 return !names.isEmpty() ? params_[i]->getParam(names.join("/"))
00047 : params_[i];
00048 }
00049 }
00050 return NULL;
00051 }
00052
00053 void Params::addParam(ParamInterface* param)
00054 {
00055 param->setParent(this);
00056 params_.append(param);
00057 connect(param, SIGNAL(changed()), this, SIGNAL(changed()));
00058 connect(param, SIGNAL(nameChanged(const QString&, const QString&)), this,
00059 SIGNAL(nameChanged(const QString&, const QString&)));
00060 connect(param, SIGNAL(typeChanged(const QString&, const QMetaType::Type&)),
00061 this, SIGNAL(typeChanged(const QString&, const QMetaType::Type&)));
00062 connect(param, SIGNAL(valueChanged(const QString&, const QVariant&)), this,
00063 SIGNAL(valueChanged(const QString&, const QVariant&)));
00064 connect(param, SIGNAL(defaultValueChanged(const QString&, const QVariant&)),
00065 this, SIGNAL(defaultValueChanged(const QString&, const QVariant&)));
00066 connect(param, SIGNAL(toolTipChanged(const QString&, const QString&)), this,
00067 SIGNAL(toolTipChanged(const QString&, const QString&)));
00068 connect(param, SIGNAL(added(const QString&)), this,
00069 SIGNAL(added(const QString&)));
00070 connect(param, SIGNAL(removed(const QString&)), this,
00071 SIGNAL(removed(const QString&)));
00072 connect(param, SIGNAL(cleared(const QString&)), this,
00073 SIGNAL(cleared(const QString&)));
00074 connect(param, SIGNAL(destroyed()), this, SLOT(paramDestroyed()));
00075 emit added(param->getFullName());
00076 emit changed();
00077 }
00078
00079 void Params::removeParam(const QString& relative_name)
00080 {
00081 ParamInterface* param = getParam(relative_name);
00082 if (param)
00083 {
00084 ParamInterface* parent = param->getParentParam();
00085 if (parent && parent != this)
00086 {
00087 parent->removeParam(param->getName());
00088 }
00089 else
00090 {
00091 size_t index(params_.indexOf(param));
00092 if (index == -1)
00093 {
00094 return;
00095 }
00096 QString full_name(params_[index]->getFullName());
00097
00098
00099
00100
00101
00102 params_.remove(index);
00103 emit removed(full_name);
00104 emit changed();
00105 }
00106 }
00107 }
00108
00109 void Params::clearParams()
00110 {
00111 if (!params_.isEmpty())
00112 {
00113 for (size_t i(0); i < params_.count(); i++)
00114 {
00115 if (params_[i])
00116 {
00117 delete params_[i];
00118 params_[i] = NULL;
00119 }
00120 }
00121 params_.clear();
00122 emit cleared(getFullName());
00123 emit changed();
00124 }
00125 }
00126
00127 bool Params::contains(const QString& full_name) const
00128 {
00129 QStringList names(full_name.split("/"));
00130 for (size_t index(0); index < params_.count(); index++)
00131 {
00132 if (params_[index]->getName() == names[0])
00133 {
00134 names.removeFirst();
00135 return names.isEmpty() || params_[index]->contains(names.join("/"));
00136 }
00137 }
00138 return false;
00139 }
00140
00141 size_t Params::count() const { return params_.count(); }
00142
00143 bool Params::isEmpty() const { return params_.isEmpty(); }
00144
00145 void Params::save(QSettings& settings) const
00146 {
00147 ParamInterface::save(settings);
00148 for (size_t index(0); index < params_.count(); ++index)
00149 {
00150 settings.beginGroup(params_[index]->getGroupName() + "_" +
00151 QString::number(index));
00152 params_[index]->save(settings);
00153 settings.endGroup();
00154 }
00155 }
00156
00157 void Params::load(QSettings& settings)
00158 {
00159 ParamInterface::load(settings);
00160 QStringList groups(Params::sortGroups(settings.childGroups()));
00161 clearParams();
00162 for (size_t index(0); index < groups.count(); index++)
00163 {
00164 ParamInterface* param = ParamFactory::newInstance(groups[index], this);
00165 addParam(param);
00166 settings.beginGroup(param->getGroupName() + "_" + QString::number(index));
00167 param->load(settings);
00168 settings.endGroup();
00169 }
00170 }
00171
00172 void Params::reset()
00173 {
00174 for (size_t index(0); index < params_.count(); index++)
00175 {
00176 params_[index]->reset();
00177 }
00178 }
00179
00180 void Params::write(QDataStream& stream) const
00181 {
00182 ParamInterface::write(stream);
00183 stream << params_.count();
00184 for (size_t index(0); index < params_.count(); ++index)
00185 {
00186 params_[index]->write(stream);
00187 }
00188 }
00189
00190 void Params::read(QDataStream& stream)
00191 {
00192 quint64 count;
00193 ParamInterface::read(stream);
00194 stream >> count;
00195 clearParams();
00196 for (size_t index(0); index < count; ++index)
00197 {
00198 params_[index]->read(stream);
00199 }
00200 }
00201
00202 Params& Params::operator=(const Params& config)
00203 {
00204 ParamInterface::operator=(config);
00205 clearParams();
00206 for (size_t index(0); index < config.params_.count(); index++)
00207 {
00208 addParam(config.params_[index]->clone());
00209 }
00210 return *this;
00211 }
00212
00213 ParamInterface* Params::clone() const
00214 {
00215 Params* params = new Params();
00216 *params = *this;
00217 return params;
00218 }
00219
00220 QString Params::validate() const
00221 {
00222 if (params_.isEmpty())
00223 {
00224 return "Enter the params parameters.";
00225 }
00226 QString validation(ParamInterface::validate());
00227 if (!validation.isEmpty())
00228 {
00229 return validation;
00230 }
00231 for (size_t i(0); i < params_.count(); i++)
00232 {
00233 validation = params_[i]->validate();
00234 if (!validation.isEmpty())
00235 {
00236 break;
00237 }
00238 }
00239 return validation;
00240 }
00241
00242 bool Params::isParams() const { return true; }
00243
00244 void Params::paramDestroyed()
00245 {
00246 ParamInterface* param = static_cast<ParamInterface*>(sender());
00247 int index(params_.indexOf(param));
00248 if (index != -1)
00249 {
00250 QString full_name(param->getFullName());
00251 params_.remove(index);
00252 emit removed(full_name);
00253 emit changed();
00254 }
00255 }
00256
00257 QStringList Params::sortGroups(const QStringList& groups)
00258 {
00259 QStringList sorted_groups;
00260 for (size_t index(0); index < groups.count(); index++)
00261 {
00262 QString group(groups.filter("_" + QString::number(index)).first());
00263 if (group.isEmpty())
00264 {
00265 ROS_ERROR("Invalid param index.");
00266 return sorted_groups;
00267 }
00268 sorted_groups.append(group);
00269 }
00270 return sorted_groups;
00271 }
00272
00273 QString Params::toYaml(const QString &prefix) const
00274 {
00275 QString yaml(ParamInterface::toYaml(prefix));
00276 for (size_t index(0); index < params_.count(); index++)
00277 {
00278 yaml += "\n" + params_[index]->toYaml(prefix + "\t");
00279 }
00280 return yaml;
00281 }
00282 }
00283 }