00001 #include "rqt_mrta/config/config.h"
00002 #include "rqt_mrta/config/param.h"
00003 #include "rqt_mrta/config/params.h"
00004 #include "rqt_mrta/config/param_interface.h"
00005 #include "rqt_mrta/param_tree_widget.h"
00006
00007 Q_DECLARE_METATYPE(rqt_mrta::config::ParamInterface*)
00008
00009 namespace rqt_mrta
00010 {
00011 ParamTreeWidget::ParamTreeWidget(QWidget* parent)
00012 : QTreeWidget(parent), config_(NULL)
00013 {
00014 setColumnCount(2);
00015 headerItem()->setText(0, "Name");
00016 headerItem()->setText(1, "Value");
00017 connect(this, SIGNAL(currentItemChanged(QTreeWidgetItem*, QTreeWidgetItem*)),
00018 this, SLOT(currentItemChanged(QTreeWidgetItem*, QTreeWidgetItem*)));
00019 connect(this, SIGNAL(itemDoubleClicked(QTreeWidgetItem*, int)), this,
00020 SLOT(itemDoubleClicked(QTreeWidgetItem*, int)));
00021 }
00022
00023 ParamTreeWidget::~ParamTreeWidget() {}
00024
00025 config::Config* ParamTreeWidget::getConfig() const { return config_; }
00026
00027 void ParamTreeWidget::setConfig(config::Config* config)
00028 {
00029 if (config != config_)
00030 {
00031 if (config_)
00032 {
00033 disconnect(config_, SIGNAL(changed()), this, SIGNAL(changed()));
00034 disconnect(config_, SIGNAL(idChanged(const QString&)), this,
00035 SLOT(configIdChanged(const QString&)));
00036 disconnect(config_, SIGNAL(added(const QString&)), this,
00037 SLOT(configAdded(const QString&)));
00038 disconnect(config_, SIGNAL(removed(const QString&)), this,
00039 SLOT(configRemoved(const QString&)));
00040 disconnect(config_, SIGNAL(cleared(const QString&)), this,
00041 SLOT(configCleared(const QString&)));
00042 disconnect(config_, SIGNAL(nameChanged(const QString&, const QString&)),
00043 this, SLOT(configNameChanged(const QString&, const QString&)));
00044 disconnect(config_, SIGNAL(valueChanged(const QString&, const QVariant&)),
00045 this,
00046 SLOT(configValueChanged(const QString&, const QVariant&)));
00047 disconnect(config_,
00048 SIGNAL(toolTipChanged(const QString&, const QString&)), this,
00049 SLOT(configToolTipChanged(const QString&, const QString&)));
00050
00051
00052 }
00053 config_ = config;
00054 if (config_)
00055 {
00056 connect(config_, SIGNAL(changed()), this, SIGNAL(changed()));
00057 connect(config_, SIGNAL(idChanged(const QString&)), this,
00058 SLOT(configIdChanged(const QString&)));
00059 connect(config_, SIGNAL(added(const QString&)), this,
00060 SLOT(configAdded(const QString&)));
00061 connect(config_, SIGNAL(removed(const QString&)), this,
00062 SLOT(configRemoved(const QString&)));
00063 connect(config_, SIGNAL(cleared(const QString&)), this,
00064 SLOT(configCleared(const QString&)));
00065 connect(config_, SIGNAL(nameChanged(const QString&, const QString&)),
00066 this, SLOT(configNameChanged(const QString&, const QString&)));
00067 connect(config_, SIGNAL(valueChanged(const QString&, const QVariant&)),
00068 this, SLOT(configValueChanged(const QString&, const QVariant&)));
00069 connect(config_, SIGNAL(toolTipChanged(const QString&, const QString&)),
00070 this, SLOT(configToolTipChanged(const QString&, const QString&)));
00071
00072 for (size_t index(0); index < config_->count(); index++)
00073 {
00074 config::ParamInterface* param = config_->getChild(index);
00075 connect(param, SIGNAL(destroyed()), this, SLOT(paramDestroyed()));
00076 if (param->isParam())
00077 {
00078 addParam(static_cast<ParamConfig*>(param), invisibleRootItem());
00079 }
00080 else
00081 {
00082 addParam(static_cast<ParamsConfig*>(param), invisibleRootItem());
00083 }
00084 }
00085 setCurrentItem(invisibleRootItem());
00086 }
00087 }
00088 }
00089
00090 QString ParamTreeWidget::validate(QTreeWidgetItem* parent) const
00091 {
00092 if (!parent)
00093 {
00094 parent = invisibleRootItem();
00095 }
00096 QString validation;
00097 for (size_t index(0); index < parent->childCount(); index++)
00098 {
00099 QTreeWidgetItem* item = parent->child(index);
00100 config::ParamInterface* param =
00101 item->data(0, Qt::UserRole).value<config::ParamInterface*>();
00102 validation = param->validate();
00103 if (!validation.isEmpty())
00104 {
00105 return validation;
00106 }
00107 }
00108 return validation;
00109 }
00110
00111 void ParamTreeWidget::addParam(ParamConfig* param, QTreeWidgetItem* parent)
00112 {
00113 QTreeWidgetItem* item = new QTreeWidgetItem(parent);
00114 item->setText(0, param->getName() + (param->isMandatory() ? " *" : ""));
00115 item->setData(0, Qt::UserRole,
00116 QVariant::fromValue<config::ParamInterface*>(param));
00117 item->setText(1, param->getDefaultValue().toString());
00118 item->setToolTip(0, param->getToolTip());
00119 item->setToolTip(1, param->getToolTip());
00120 parent->addChild(item);
00121 emit paramAdded(param->getFullName());
00122 setCurrentItem(item);
00123 }
00124
00125 void ParamTreeWidget::addParam(ParamsConfig* params, QTreeWidgetItem* parent)
00126 {
00127 QTreeWidgetItem* item = new QTreeWidgetItem(parent);
00128 item->setExpanded(true);
00129 item->setText(0, params->getName());
00130 item->setData(0, Qt::UserRole,
00131 QVariant::fromValue<config::ParamInterface*>(params));
00132 parent->addChild(item);
00133 emit paramAdded(params->getFullName());
00134 for (size_t index(0); index < params->count(); index++)
00135 {
00136 config::ParamInterface* param = params->getChild(index);
00137 if (param->isParam())
00138 {
00139 addParam(static_cast<ParamConfig*>(param), item);
00140 }
00141 else
00142 {
00143 addParam(static_cast<ParamsConfig*>(param), item);
00144 }
00145 }
00146 setCurrentItem(item);
00147 }
00148
00149 QTreeWidgetItem* ParamTreeWidget::getItem(const QString& full_name,
00150 QTreeWidgetItem* parent) const
00151 {
00152 if (!parent)
00153 {
00154 parent = invisibleRootItem();
00155 }
00156 QStringList names(full_name.split("/"));
00157 QString name(names.first());
00158 names.removeFirst();
00159 for (size_t index(0); index < parent->childCount(); index++)
00160 {
00161 QTreeWidgetItem* item = parent->child(index);
00162 if (item->text(0) == name)
00163 {
00164 return names.isEmpty() ? item : getItem(names.join("/"), item);
00165 }
00166 }
00167 return NULL;
00168 }
00169
00170 void ParamTreeWidget::currentItemChanged(QTreeWidgetItem* current,
00171 QTreeWidgetItem* previous)
00172 {
00173 if (previous && current && previous != current && previous->childCount() == 0)
00174 {
00175 config::ParamInterface* param =
00176 previous->data(0, Qt::UserRole).value<config::ParamInterface*>();
00177 if (param->isParam())
00178 {
00179 ParamConfig* p = static_cast<ParamConfig*>(param);
00180 p->setValue(previous->text(1));
00181 }
00182 }
00183 }
00184
00185 void ParamTreeWidget::itemDoubleClicked(QTreeWidgetItem* item, int column)
00186 {
00187 if (column == 0)
00188 {
00189 item->setFlags(item->flags() & ~Qt::ItemIsEditable);
00190 }
00191 if (column == 1 && item->childCount() == 0)
00192 {
00193 item->setFlags(item->flags() | Qt::ItemIsEditable);
00194 }
00195 }
00196
00197 void ParamTreeWidget::configIdChanged(const QString& config_id) {}
00198
00199 void ParamTreeWidget::configAdded(const QString& full_name)
00200 {
00201 config::ParamInterface* param = config_->getParam(full_name);
00202 QStringList parent_name(full_name.split("/"));
00203 parent_name.removeLast();
00204 QTreeWidgetItem* parent = getItem(parent_name.join("/"));
00205 if (param->isParam())
00206 {
00207 addParam(static_cast<ParamConfig*>(param), parent);
00208 }
00209 else
00210 {
00211 addParam(static_cast<ParamsConfig*>(param), parent);
00212 }
00213 }
00214
00215 void ParamTreeWidget::configRemoved(const QString& full_name)
00216 {
00217 QTreeWidgetItem* item = getItem(full_name);
00218 if (item)
00219 {
00220 delete item;
00221 }
00222 }
00223
00224 void ParamTreeWidget::configCleared(const QString& full_name) {}
00225
00226 void ParamTreeWidget::configNameChanged(const QString& previous_name,
00227 const QString& name)
00228 {
00229 QTreeWidgetItem* item = getItem(previous_name);
00230 if (item)
00231 {
00232 item->setText(0, name);
00233 }
00234 }
00235
00236 void ParamTreeWidget::configValueChanged(const QString& name,
00237 const QVariant& value)
00238 {
00239 QTreeWidgetItem* item = getItem(name);
00240 if (item)
00241 {
00242 item->setText(1, value.toString());
00243 }
00244 }
00245
00246 void ParamTreeWidget::configToolTipChanged(const QString& name,
00247 const QString& tool_tip)
00248 {
00249 QTreeWidgetItem* item = getItem(name);
00250 if (item)
00251 {
00252 item->setToolTip(0, tool_tip);
00253 item->setToolTip(1, tool_tip);
00254 }
00255 }
00256
00257 void ParamTreeWidget::paramDestroyed()
00258 {
00259 ROS_INFO_STREAM("[ParamTreeWidget::paramDestroyed]");
00260 }
00261 }