Go to the documentation of this file.00001 #include <QStringList>
00002 #include "rqt_mrta/config/args.h"
00003 #include "rqt_mrta/config/arg.h"
00004
00005 namespace rqt_mrta
00006 {
00007 namespace config
00008 {
00009 Args::Args(QObject* parent) : AbstractConfig(parent) {}
00010
00011 Args::~Args()
00012 {
00013 for (size_t index(0); index < args_.count(); index++)
00014 {
00015 if (args_[index])
00016 {
00017 delete args_[index];
00018 args_[index] = NULL;
00019 }
00020 }
00021 args_.clear();
00022 }
00023
00024 Arg* Args::getArg(size_t index) const
00025 {
00026 return index < args_.count() ? args_[index] : NULL;
00027 }
00028
00029 Arg* Args::addArg()
00030 {
00031 Arg* arg = new Arg(this);
00032 args_.append(arg);
00033 connect(arg, SIGNAL(changed()), this, SIGNAL(changed()));
00034 connect(arg, SIGNAL(nameChanged(const QString&)), this,
00035 SLOT(argNameChanged(const QString&)));
00036 connect(arg, SIGNAL(valueChanged(const QString&)), this,
00037 SLOT(argValueChanged(const QString&)));
00038 connect(arg, SIGNAL(defaultValueChanged(const QString&)), this,
00039 SLOT(argDefaultValueChanged(const QString&)));
00040 connect(arg, SIGNAL(destroyed()), this, SLOT(argDestroyed()));
00041 emit added(args_.count() - 1);
00042 emit changed();
00043 return arg;
00044 }
00045
00046 void Args::removeArg(Arg* arg) { removeArg(args_.indexOf(arg)); }
00047
00048 void Args::removeArg(size_t index)
00049 {
00050 if (index >= 0 && index < args_.count())
00051 {
00052 QString name(args_[index]->getName());
00053 args_.remove(index);
00054 emit removed(name);
00055 emit changed();
00056 }
00057 }
00058
00059 void Args::clearArgs()
00060 {
00061 if (!args_.isEmpty())
00062 {
00063 for (size_t index(0); index < args_.count(); index++)
00064 {
00065 if (args_[index])
00066 {
00067 delete args_[index];
00068 args_[index] = NULL;
00069 }
00070 }
00071 args_.clear();
00072 emit cleared();
00073 emit changed();
00074 }
00075 }
00076
00077 bool Args::contains(const QString& name) const
00078 {
00079 for (size_t index(0); index < args_.count(); index++)
00080 {
00081 if (args_[index]->getName() == name)
00082 {
00083 return true;
00084 }
00085 }
00086 return false;
00087 }
00088
00089 size_t Args::count() const { return args_.count(); }
00090
00091 bool Args::isEmpty() const { return args_.isEmpty(); }
00092
00093 QString Args::validate() const
00094 {
00095 QString validation;
00096 for (size_t index(0); index < args_.count(); index++)
00097 {
00098 validation = args_[index]->validate();
00099 if (!validation.isEmpty())
00100 {
00101 break;
00102 }
00103 }
00104 return validation;
00105 }
00106
00107 void Args::save(QSettings& settings) const
00108 {
00109 settings.beginGroup("args");
00110 for (size_t index(0); index < args_.count(); index++)
00111 {
00112 settings.beginGroup("arg_" + QString::number(index));
00113 args_[index]->save(settings);
00114 settings.endGroup();
00115 }
00116 settings.endGroup();
00117 }
00118
00119 void Args::load(QSettings& settings)
00120 {
00121 settings.beginGroup("args");
00122 QStringList groups(settings.childGroups());
00123 size_t index(0);
00124 for (QStringList::iterator it(groups.begin()); it != groups.end(); it++)
00125 {
00126 Arg* arg = index < args_.count() ? arg = args_[index] : addArg();
00127 settings.beginGroup(*it);
00128 arg->load(settings);
00129 settings.endGroup();
00130 ++index;
00131 }
00132 settings.endGroup();
00133 while (index < args_.count())
00134 {
00135 removeArg(index);
00136 }
00137 }
00138
00139 void Args::reset() { clearArgs(); }
00140
00141 void Args::write(QDataStream& stream) const
00142 {
00143 for (size_t index(0); index < args_.count(); index++)
00144 {
00145 args_[index]->write(stream);
00146 }
00147 }
00148
00149 void Args::read(QDataStream& stream)
00150 {
00151 for (size_t index(0); index < args_.count(); index++)
00152 {
00153 args_[index]->read(stream);
00154 }
00155 }
00156
00157 Args& Args::operator=(const Args& config)
00158 {
00159 while (args_.count() < config.args_.count())
00160 {
00161 addArg();
00162 }
00163 while (args_.count() > config.args_.count())
00164 {
00165 removeArg(args_.count() - 1);
00166 }
00167 for (size_t index(0); index < args_.count(); ++index)
00168 {
00169 *args_[index] = *config.args_[index];
00170 }
00171 return *this;
00172 }
00173
00174 QString Args::toLaunch(const QString &prefix) const
00175 {
00176 QString launch;
00177 for (size_t index(0); index < args_.count(); index++)
00178 {
00179 launch += args_[index]->toLaunch(prefix + "\t");
00180 }
00181 return launch;
00182 }
00183
00184 void Args::argNameChanged(const QString& name)
00185 {
00186 int index(args_.indexOf(static_cast<Arg*>(sender())));
00187 if (index != -1)
00188 {
00189 emit argNameChanged(index, name);
00190 emit changed();
00191 }
00192 }
00193
00194 void Args::argValueChanged(const QString& value)
00195 {
00196 int index(args_.indexOf(static_cast<Arg*>(sender())));
00197 if (index != -1)
00198 {
00199 emit argValueChanged(index, value);
00200 emit changed();
00201 }
00202 }
00203
00204 void Args::argDefaultValueChanged(const QString& value)
00205 {
00206 int index(args_.indexOf(static_cast<Arg*>(sender())));
00207 if (index != -1)
00208 {
00209 emit argDefaultValueChanged(index, value);
00210 emit changed();
00211 }
00212 }
00213
00214 void Args::argDestroyed()
00215 {
00216 Arg* arg = static_cast<Arg*>(sender());
00217 int index(args_.indexOf(arg));
00218 if (index >= 0)
00219 {
00220 QString name(arg->getName());
00221 args_.remove(index);
00222 emit removed(name);
00223 emit changed();
00224 }
00225 }
00226 }
00227 }