Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

736 add a duplicate feature for plugins #737

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions mapviz/include/mapviz/config_item.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,13 @@ namespace mapviz
Q_SIGNALS:
void UpdateSizeHint();
void ToggledDraw(QListWidgetItem* plugin, bool visible);
void DuplicateRequest(QListWidgetItem* plugin);
void RemoveRequest(QListWidgetItem* plugin);

public Q_SLOTS:
void Hide();
void EditName();
void Duplicate();
void Remove();
void ToggleDraw(bool toggled);

Expand All @@ -82,6 +84,7 @@ namespace mapviz
QString name_;
QString type_;
QAction* edit_name_action_;
QAction* duplicate_item_action_;
QAction* remove_item_action_;
bool visible_;
};
Expand Down
2 changes: 2 additions & 0 deletions mapviz/include/mapviz/mapviz.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ namespace mapviz
void SelectNewDisplay();
void RemoveDisplay();
void RemoveDisplay(QListWidgetItem* item);
void DuplicateDisplay();
void DuplicateDisplay(QListWidgetItem *item);
void ReorderDisplays();
void FixedFrameSelected(const QString& text);
void TargetFrameSelected(const QString& text);
Expand Down
8 changes: 8 additions & 0 deletions mapviz/src/config_item.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,12 @@ namespace mapviz
ui_.setupUi(this);

edit_name_action_ = new QAction("Edit Name", this);
duplicate_item_action_ = new QAction("Duplicate", this);
remove_item_action_ = new QAction("Remove", this);
remove_item_action_->setIcon(QIcon(":/images/remove-icon-th.png"));

connect(edit_name_action_, SIGNAL(triggered()), this, SLOT(EditName()));
connect(duplicate_item_action_, SIGNAL(triggered()), this, SLOT(Duplicate()));
connect(remove_item_action_, SIGNAL(triggered()), this, SLOT(Remove()));
}

Expand All @@ -71,6 +73,7 @@ namespace mapviz
{
QMenu menu(this);
menu.addAction(edit_name_action_);
menu.addAction(duplicate_item_action_);
menu.addAction(remove_item_action_);
menu.exec(event->globalPos());
}
Expand Down Expand Up @@ -109,6 +112,11 @@ namespace mapviz
}
}

void ConfigItem::Duplicate()
{
Q_EMIT DuplicateRequest(item_);
}

void ConfigItem::Remove()
{
Q_EMIT RemoveRequest(item_);
Expand Down
61 changes: 61 additions & 0 deletions mapviz/src/mapviz.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1188,6 +1188,7 @@ MapvizPluginPtr Mapviz::CreateNewDisplay(
item->setSizeHint(config_item->sizeHint());
connect(config_item, SIGNAL(UpdateSizeHint()), this, SLOT(UpdateSizeHints()));
connect(config_item, SIGNAL(ToggledDraw(QListWidgetItem*, bool)), this, SLOT(ToggleShowPlugin(QListWidgetItem*, bool)));
connect(config_item, SIGNAL(DuplicateRequest(QListWidgetItem*)), this, SLOT(DuplicateDisplay(QListWidgetItem*)));
connect(config_item, SIGNAL(RemoveRequest(QListWidgetItem*)), this, SLOT(RemoveDisplay(QListWidgetItem*)));
connect(plugin.get(), SIGNAL(VisibleChanged(bool)), config_item, SLOT(ToggleDraw(bool)));
connect(plugin.get(), SIGNAL(SizeChanged()), this, SLOT(UpdateSizeHints()));
Expand Down Expand Up @@ -1499,6 +1500,66 @@ void Mapviz::RemoveDisplay(QListWidgetItem* item)
}
}

void Mapviz::DuplicateDisplay()
{
QListWidgetItem* item = ui_.configs->item(ui_.configs->currentRow());
if (item != nullptr)
{
DuplicateDisplay(item);
}
}

void Mapviz::DuplicateDisplay(QListWidgetItem* item)
{
ROS_INFO("Duplicating active display... ");
// - Get plugin associated with QListWidgetItem
if (plugins_.count(item) != 1)
{
ROS_ERROR("Item attempted to duplicate is not a plugin.");
return;
}
MapvizPluginPtr target_plugin = plugins_[item];
ConfigItem* target_config_item = static_cast<ConfigItem*>(ui_.configs->itemWidget(item));

// - Save plugin config to a temporary string via an emitter
YAML::Emitter out;
out << YAML::BeginMap;
out << YAML::Key << "type" << YAML::Value << target_plugin->Type();
out << YAML::Key << "name" << YAML::Value << target_config_item->Name().toStdString();
out << YAML::Key << "config" << YAML::Value;
out << YAML::BeginMap;
out << YAML::Key << "visible" << YAML::Value << target_plugin->Visible();
out << YAML::Key << "collapsed" << YAML::Value << target_config_item->Collapsed();
target_plugin->SaveConfig(out, "");
out << YAML::EndMap;
out << YAML::EndMap;

// - Create the new display via existing MapvizPlugin::LoadConfig interface
YAML::Node temp_node;
swri_yaml_util::LoadString(out.c_str(), temp_node);
YAML::Node temp_config_node = temp_node["config"];
if (!temp_config_node)
{
ROS_ERROR("Cannot duplicate plugin of type %s. Invalid config.",
target_plugin->Type().c_str());
return;
}
try
{
MapvizPluginPtr duplicate_plugin = CreateNewDisplay(
target_config_item->Name().toStdString(),
target_plugin->Type(),
target_plugin->Visible(),
target_config_item->Collapsed());
duplicate_plugin->LoadConfig(temp_config_node, "");
duplicate_plugin->DrawIcon();
}
catch (const pluginlib::LibraryLoadException& e)
{
ROS_ERROR("%s", e.what());
}
}

void Mapviz::ClearDisplays()
{
while (ui_.configs->count() > 0)
Expand Down