launches.cpp
Go to the documentation of this file.
00001 #include <QDir>
00002 #include <QStringList>
00003 #include "rqt_mrta/config/application/robots.h"
00004 #include "rqt_mrta/config/launches.h"
00005 #include "rqt_mrta/config/launch.h"
00006 #include "utilities/exception.h"
00007 
00008 namespace rqt_mrta
00009 {
00010 namespace config
00011 {
00012 Launches::Launches(QObject* parent) : AbstractConfig(parent) {}
00013 
00014 Launches::~Launches()
00015 {
00016   for (size_t index(0); index < launches_.count(); index++)
00017   {
00018     if (launches_[index])
00019     {
00020       delete launches_[index];
00021       launches_[index] = NULL;
00022     }
00023   }
00024   launches_.clear();
00025 }
00026 
00027 Launch* Launches::getLaunch(size_t index) const
00028 {
00029   return index < launches_.count() ? launches_[index] : NULL;
00030 }
00031 
00032 Launch *Launches::getLaunch(const QString &id) const
00033 {
00034   for (size_t index(0); index < launches_.count(); index)
00035   {
00036     if (launches_[index]->getId() == id)
00037     {
00038       return launches_[index];
00039     }
00040   }
00041   return NULL;
00042 }
00043 
00044 Launch* Launches::addLaunch()
00045 {
00046   Launch* launch = new Launch(this);
00047   launches_.append(launch);
00048   connect(launch, SIGNAL(changed()), this, SIGNAL(changed()));
00049   connect(launch, SIGNAL(idChanged(const QString&)), this,
00050           SLOT(launchIdChanged(const QString&)));
00051   connect(launch, SIGNAL(destroyed()), this, SLOT(launchDestroyed()));
00052   emit added(launches_.count() - 1);
00053   emit changed();
00054   return launch;
00055 }
00056 
00057 void Launches::removeLaunch(Launch* launch)
00058 {
00059   removeLaunch(launches_.indexOf(launch));
00060 }
00061 
00062 void Launches::removeLaunch(size_t index)
00063 {
00064   if (index >= 0 && index < launches_.count())
00065   {
00066     QString name(launches_[index]->getId());
00067     launches_.remove(index);
00068     emit removed(name);
00069     emit changed();
00070   }
00071 }
00072 
00073 void Launches::clearLaunches()
00074 {
00075   if (!launches_.isEmpty())
00076   {
00077     for (size_t index(0); index < launches_.count(); index++)
00078     {
00079       if (launches_[index])
00080       {
00081         delete launches_[index];
00082         launches_[index] = NULL;
00083       }
00084     }
00085     launches_.clear();
00086     emit cleared();
00087     emit changed();
00088   }
00089 }
00090 
00091 bool Launches::contains(const QString& id) const
00092 {
00093   for (size_t index(0); index < launches_.count(); index++)
00094   {
00095     if (launches_[index]->getId() == id)
00096     {
00097       return true;
00098     }
00099   }
00100   return false;
00101 }
00102 
00103 size_t Launches::count() const { return launches_.count(); }
00104 
00105 bool Launches::isEmpty() const { return launches_.isEmpty(); }
00106 
00107 QString Launches::validate() const
00108 {
00109   QString validation;
00110   for (size_t index(0); index < launches_.count(); index++)
00111   {
00112     validation = launches_[index]->validate();
00113     if (!validation.isEmpty())
00114     {
00115       break;
00116     }
00117   }
00118   return validation;
00119 }
00120 
00121 QStringList Launches::willBeGenerated() const
00122 {
00123   QStringList list;
00124   for (size_t index(0); index < launches_.count(); index++)
00125   {
00126     list.append("launch/" + launches_[index]->getId() + ".launch");
00127   }
00128   return list;
00129 }
00130 
00131 void Launches::saveAsLaunch(const QString& package_url) const
00132 {
00133   QDir package(package_url);
00134   if (!package.exists())
00135   {
00136     throw utilities::Exception("Inexistent package.");
00137   }
00138   if (package.cd("launch"))
00139   {
00140     package.cd("..");
00141   }
00142   else if (!package.mkdir("launch"))
00143   {
00144     throw utilities::Exception("Unable to create the launch folder.");
00145   }
00146   for (size_t index(0); index < launches_.count(); index++)
00147   {
00148     /*QString validation(launches_[index]->validate());
00149     if (!validation.isEmpty())
00150     {
00151       ROS_ERROR("%s", validation.toStdString().c_str());
00152       continue;
00153     }*/
00154     launches_[index]->add("package", package.dirName());
00155     launches_[index]->saveAsLaunch(package.path() + "/launch/" +
00156                                    launches_[index]->getId());
00157   }
00158 }
00159 
00160 void Launches::save(QSettings& settings) const
00161 {
00162   settings.beginGroup("launches");
00163   for (size_t index(0); index < launches_.count(); index++)
00164   {
00165     settings.beginGroup("launch_" + QString::number(index));
00166     launches_[index]->save(settings);
00167     settings.endGroup();
00168   }
00169   settings.endGroup();
00170 }
00171 
00172 void Launches::load(QSettings& settings)
00173 {
00174   settings.beginGroup("launches");
00175   QStringList groups(settings.childGroups());
00176   size_t index(0);
00177   for (QStringList::iterator it(groups.begin()); it != groups.end(); it++)
00178   {
00179     Launch* launch =
00180         index < launches_.count() ? launch = launches_[index] : addLaunch();
00181     settings.beginGroup(*it);
00182     launch->load(settings);
00183     settings.endGroup();
00184     ++index;
00185   }
00186   settings.endGroup();
00187   while (index < launches_.count())
00188   {
00189     removeLaunch(index);
00190   }
00191 }
00192 
00193 void Launches::reset() { clearLaunches(); }
00194 
00195 void Launches::write(QDataStream& stream) const
00196 {
00197   for (size_t index(0); index < launches_.count(); index++)
00198   {
00199     launches_[index]->write(stream);
00200   }
00201 }
00202 
00203 void Launches::read(QDataStream& stream)
00204 {
00205   for (size_t index(0); index < launches_.count(); index++)
00206   {
00207     launches_[index]->read(stream);
00208   }
00209 }
00210 
00211 Launches& Launches::operator=(const Launches& config)
00212 {
00213   while (launches_.count() < config.launches_.count())
00214   {
00215     addLaunch();
00216   }
00217   while (launches_.count() > config.launches_.count())
00218   {
00219     removeLaunch(launches_.count() - 1);
00220   }
00221   for (size_t index(0); index < launches_.count(); ++index)
00222   {
00223     *launches_[index] = *config.launches_[index];
00224   }
00225   return *this;
00226 }
00227 
00228 void Launches::setLaunches(const Launches& launches,
00229                            const application::Robots& robots,
00230                            const QString& robots_launch_id)
00231 {
00232   clearLaunches();
00233   Launch* robots_launch = launches.getLaunch(robots_launch_id);
00234   for (size_t i(0); i < launches.count(); i++)
00235   {
00236     Launch* template_launch = launches.getLaunch(i);
00237     bool is_robot_template = robots_launch && template_launch == robots_launch;
00238     size_t count = is_robot_template ? robots.count() : 1;
00239     for (size_t j(0); j < count; j++)
00240     {
00241       Launch* launch = addLaunch();
00242       *launch = *template_launch;
00243       if (is_robot_template)
00244       {
00245         launch->add("robot_id", robots.getRobot(j)->getId());
00246         launch->setId(robots.getRobot(j)->getId() + "_" + launch->getId());
00247       }
00248     }
00249   }
00250 }
00251 
00252 void Launches::launchIdChanged(const QString& id)
00253 {
00254   int index(launches_.indexOf(static_cast<Launch*>(sender())));
00255   if (index != -1)
00256   {
00257     emit launchIdChanged(index, id);
00258     emit changed();
00259   }
00260 }
00261 
00262 void Launches::launchDestroyed()
00263 {
00264   Launch* launch = static_cast<Launch*>(sender());
00265   int index(launches_.indexOf(launch));
00266   if (index >= 0)
00267   {
00268     QString id(launch->getId());
00269     launches_.remove(index);
00270     emit removed(id);
00271     emit changed();
00272   }
00273 }
00274 }
00275 }


rqt_mrta
Author(s): Adriano Henrique Rossette Leite
autogenerated on Thu Jun 6 2019 18:50:52