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