00001 #include "rqt_mrta/config/application/robots.h" 00002 #include "rqt_mrta/config/application/robot.h" 00003 #include "rqt_mrta/config/application/tasks.h" 00004 #include "rqt_mrta/config/application/task.h" 00005 #include "rqt_mrta/robot_tree_widget.h" 00006 00007 #include <ros/console.h> 00008 00009 Q_DECLARE_METATYPE(rqt_mrta::config::application::Robots*) 00010 Q_DECLARE_METATYPE(rqt_mrta::config::application::Robot*) 00011 Q_DECLARE_METATYPE(rqt_mrta::config::application::Tasks*) 00012 Q_DECLARE_METATYPE(rqt_mrta::config::application::Task*) 00013 00014 namespace rqt_mrta 00015 { 00016 RobotTreeWidget::RobotTreeWidget(QWidget* parent) 00017 : QTreeWidget(parent), config_(NULL), current_type_(None) 00018 { 00019 current_.robot_ = NULL; 00020 setColumnCount(1); 00021 headerItem()->setText(0, ""); 00022 connect(this, SIGNAL(currentItemChanged(QTreeWidgetItem*, QTreeWidgetItem*)), 00023 this, SLOT(currentItemChanged(QTreeWidgetItem*, QTreeWidgetItem*))); 00024 } 00025 00026 RobotTreeWidget::~RobotTreeWidget() { config_ = NULL; } 00027 00028 RobotsConfig* RobotTreeWidget::getConfig() const { return config_; } 00029 00030 void RobotTreeWidget::setConfig(RobotsConfig* config) 00031 { 00032 if (config_ != config) 00033 { 00034 if (config_) 00035 { 00036 disconnect(config_, SIGNAL(changed()), this, SLOT(robotsConfigChanged())); 00037 disconnect(config_, SIGNAL(robotIdChanged(size_t, const QString&)), this, 00038 SLOT(configRobotIdChanged(size_t, const QString&))); 00039 disconnect(config_, SIGNAL(added(size_t)), this, 00040 SLOT(configRobotAdded(size_t))); 00041 disconnect(config_, SIGNAL(removed(const QString&)), this, 00042 SLOT(configRobotRemoved(const QString&))); 00043 disconnect(config_, SIGNAL(cleared()), this, SLOT(configRobotsCleared())); 00044 disconnect(config_, SIGNAL(taskIdChanged(size_t, size_t, const QString&)), 00045 this, 00046 SLOT(configTaskIdChanged(size_t, size_t, const QString&))); 00047 disconnect(config_, SIGNAL(taskAdded(size_t, size_t)), this, 00048 SLOT(configTaskAdded(size_t, size_t))); 00049 disconnect(config_, SIGNAL(taskRemoved(size_t, const QString&)), this, 00050 SLOT(configTaskRemoved(size_t, const QString&))); 00051 disconnect(config_, SIGNAL(tasksCleared(size_t)), this, 00052 SLOT(configTasksCleared(size_t))); 00053 clear(); 00054 } 00055 config_ = config; 00056 if (config_) 00057 { 00058 connect(config_, SIGNAL(changed()), this, SLOT(robotsConfigChanged())); 00059 connect(config_, SIGNAL(robotIdChanged(size_t, const QString&)), this, 00060 SLOT(configRobotIdChanged(size_t, const QString&))); 00061 connect(config_, SIGNAL(added(size_t)), this, 00062 SLOT(configRobotAdded(size_t))); 00063 connect(config_, SIGNAL(removed(const QString&)), this, 00064 SLOT(configRobotRemoved(const QString&))); 00065 connect(config_, SIGNAL(cleared()), this, SLOT(configRobotsCleared())); 00066 connect(config_, SIGNAL(taskIdChanged(size_t, size_t, const QString&)), 00067 this, SLOT(configTaskIdChanged(size_t, size_t, const QString&))); 00068 connect(config_, SIGNAL(taskAdded(size_t, size_t)), this, 00069 SLOT(configTaskAdded(size_t, size_t))); 00070 connect(config_, SIGNAL(taskRemoved(size_t, const QString&)), this, 00071 SLOT(configTaskRemoved(size_t, const QString&))); 00072 connect(config_, SIGNAL(tasksCleared(size_t)), this, 00073 SLOT(configTasksCleared(size_t))); 00074 for (size_t i(0); i < config_->count(); i++) 00075 { 00076 configRobotAdded(i); 00077 } 00078 } 00079 } 00080 } 00081 00082 QStringList RobotTreeWidget::getTasks() const 00083 { 00084 QStringList tasks; 00085 for (size_t i(0); i < config_->count(); i++) 00086 { 00087 RobotConfig* robot = config_->getRobot(i); 00088 for (size_t j(0); j < robot->getTasks()->count(); j++) 00089 { 00090 QString task_id(robot->getTasks()->getTask(j)->getId()); 00091 if (!tasks.contains(task_id)) 00092 { 00093 tasks.append(task_id); 00094 } 00095 } 00096 } 00097 return tasks; 00098 } 00099 00100 QString RobotTreeWidget::getCurrentId() const 00101 { 00102 return current_type_ == Robot 00103 ? current_.robot_->getId() 00104 : current_type_ == Task ? current_.task_->getId() : ""; 00105 } 00106 00107 void RobotTreeWidget::setCurrentId(const QString& id) 00108 { 00109 if (current_type_ == Robot) 00110 { 00111 current_.robot_->setId(id); 00112 } 00113 else if (current_type_ == Task) 00114 { 00115 current_.task_->setId(id); 00116 } 00117 } 00118 00119 void RobotTreeWidget::addRobot() 00120 { 00121 if (config_) 00122 { 00123 RobotConfig* robot = config_->addRobot(); 00124 int i(0); 00125 while (config_->contains("robot" + QString::number(++i))); 00126 robot->setId("robot" + QString::number(i)); 00127 } 00128 } 00129 00130 void RobotTreeWidget::addRobot(RobotConfig* robot) 00131 { 00132 QTreeWidgetItem* root = invisibleRootItem(); 00133 QTreeWidgetItem* item = new QTreeWidgetItem(root); 00134 item->setText(0, robot->getId()); 00135 item->setData(0, Qt::UserRole, QVariant::fromValue<RobotConfig*>(robot)); 00136 root->addChild(item); 00137 emit robotAdded(robot->getId()); 00138 emit changed(); 00139 for (size_t j(0); j < robot->getTasks()->count(); j++) 00140 { 00141 addTask(robot->getTasks()->getTask(j), item); 00142 } 00143 setCurrentItem(item); 00144 } 00145 00146 void RobotTreeWidget::addTask() 00147 { 00148 QTreeWidgetItem* current = currentItem(); 00149 if (current) 00150 { 00151 QTreeWidgetItem* parent = current->parent(); 00152 QTreeWidgetItem* robot_item = !parent ? current : parent; 00153 RobotConfig* robot = 00154 robot_item->data(0, Qt::UserRole).value<RobotConfig*>(); 00155 TaskConfig* task = robot->getTasks()->addTask(); 00156 int i(0); 00157 while (robot->getTasks()->contains("task" + QString::number(++i))); 00158 task->setId("task" + QString::number(i)); 00159 } 00160 } 00161 00162 void RobotTreeWidget::addTask(TaskConfig* task, QTreeWidgetItem* parent) 00163 { 00164 QTreeWidgetItem* item = new QTreeWidgetItem(parent); 00165 item->setText(0, task->getId()); 00166 item->setData(0, Qt::UserRole, QVariant::fromValue<TaskConfig*>(task)); 00167 parent->addChild(item); 00168 setCurrentItem(item); 00169 emit taskAdded(task->getId()); 00170 emit changed(); 00171 } 00172 00173 void RobotTreeWidget::robotsConfigChanged() { emit changed(); } 00174 00175 void RobotTreeWidget::configRobotIdChanged(size_t robot_index, 00176 const QString& robot_id) 00177 { 00178 QTreeWidgetItem* item = invisibleRootItem()->child(robot_index); 00179 item->setText(0, robot_id); 00180 emit changed(); 00181 } 00182 00183 void RobotTreeWidget::configRobotAdded(size_t index) 00184 { 00185 addRobot(config_->getRobot(index)); 00186 } 00187 00188 void RobotTreeWidget::configRobotRemoved(const QString& robot_id) {} 00189 00190 void RobotTreeWidget::configRobotsCleared() { clear(); } 00191 00192 void RobotTreeWidget::configTaskIdChanged(size_t robot_index, size_t task_index, 00193 const QString& task_id) 00194 { 00195 QTreeWidgetItem* item = 00196 invisibleRootItem()->child(robot_index)->child(task_index); 00197 item->setText(0, task_id); 00198 emit changed(); 00199 } 00200 00201 void RobotTreeWidget::configTaskAdded(size_t robot_index, size_t task_index) 00202 { 00203 TaskConfig* task = 00204 config_->getRobot(robot_index)->getTasks()->getTask(task_index); 00205 addTask(task, invisibleRootItem()->child(robot_index)); 00206 } 00207 00208 void RobotTreeWidget::configTaskRemoved(size_t robot_index, 00209 const QString& task_id) 00210 { 00211 } 00212 00213 void RobotTreeWidget::configTasksCleared(size_t robot_index) {} 00214 00215 QString RobotTreeWidget::validate() const 00216 { 00217 return config_ ? config_->validate() : ""; 00218 } 00219 00220 void RobotTreeWidget::clear() 00221 { 00222 QTreeWidget::clear(); 00223 current_type_ = None; 00224 current_.robot_ = NULL; 00225 emit selected(None, ""); 00226 emit changed(); 00227 } 00228 00229 void RobotTreeWidget::currentItemChanged(QTreeWidgetItem* current, 00230 QTreeWidgetItem* previous) 00231 { 00232 if (!current) 00233 { 00234 current_type_ = None; 00235 current_.robot_ = NULL; 00236 emit selected(None, ""); 00237 return; 00238 } 00239 else if (!current->parent()) 00240 { 00241 current_type_ = Robot; 00242 current_.robot_ = current->data(0, Qt::UserRole).value<RobotConfig*>(); 00243 emit robotSelected(current_.robot_->getId()); 00244 emit selected(Robot, current_.robot_->getId()); 00245 } 00246 else if (!current->parent()->parent()) 00247 { 00248 current_type_ = Task; 00249 current_.task_ = current->data(0, Qt::UserRole).value<TaskConfig*>(); 00250 emit taskSelected(current_.task_->getId()); 00251 emit selected(Task, current_.task_->getId()); 00252 } 00253 } 00254 }