Skip to content

Commit

Permalink
add emptybutton
Browse files Browse the repository at this point in the history
  • Loading branch information
Ar-Ray-code committed Apr 6, 2024
1 parent 1db03c0 commit 6620462
Show file tree
Hide file tree
Showing 6 changed files with 340 additions and 0 deletions.
40 changes: 40 additions & 0 deletions button_rviz_plugin/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
cmake_minimum_required(VERSION 3.8)
project(button_rviz_plugin)

if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
add_compile_options(-Wall -Wextra -Wpedantic)
endif()

# find dependencies
find_package(ament_cmake REQUIRED)
find_package(rviz_common REQUIRED)
find_package(rviz_rendering REQUIRED)

set(CMAKE_AUTOMOC ON)

add_library(${PROJECT_NAME} SHARED
src/button_panel.cpp
)
ament_target_dependencies(${PROJECT_NAME}
rviz_common
rviz_rendering
)
install(TARGETS
${PROJECT_NAME}
EXPORT ${PROJECT_NAME}
ARCHIVE DESTINATION lib
LIBRARY DESTINATION lib
RUNTIME DESTINATION bin
INCLUDES DESTINATION include
)

pluginlib_export_plugin_description_file(rviz_common plugins_description.xml)

if(BUILD_TESTING)
find_package(ament_lint_auto REQUIRED)
set(ament_cmake_copyright_FOUND TRUE)
set(ament_cmake_cpplint_FOUND TRUE)
ament_lint_auto_find_test_dependencies()
endif()

ament_package()
21 changes: 21 additions & 0 deletions button_rviz_plugin/package.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0"?>
<?xml-model href="http://download.ros.org/schema/package_format3.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?>
<package format="3">
<name>button_rviz_plugin</name>
<version>0.0.0</version>
<description>button rviz plugin for CoRE_viewer</description>
<maintainer email="[email protected]">Ar-Ray-code</maintainer>
<license>MIT</license>

<buildtool_depend>ament_cmake</buildtool_depend>

<depend>rviz_common</depend>
<depend>rviz_rendering</depend>

<test_depend>ament_lint_auto</test_depend>
<test_depend>ament_lint_common</test_depend>

<export>
<build_type>ament_cmake</build_type>
</export>
</package>
5 changes: 5 additions & 0 deletions button_rviz_plugin/plugins_description.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<library path="button_rviz_plugin">
<class name="button_rviz_plugin/EmptyButton" type="button_rviz_plugin::EmptyButton" base_class_type="rviz_common::Panel">
<description>button panel</description>
</class>
</library>
83 changes: 83 additions & 0 deletions button_rviz_plugin/src/button_handler.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// Copyright 2024 StrayedCats.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#pragma once

#include <rclcpp/rclcpp.hpp>
#include <std_msgs/msg/empty.hpp>

namespace button_rviz_plugin
{
class ButtonHandler
{
public:
ButtonHandler(void) {}

void setRosNodePtr(const rclcpp::Node::SharedPtr node_ptr)
{
node_ptr_ = node_ptr;
}

bool initializePublisher(const std::string topic_name)
{
if (topic_name == "") {
return false;
}
button_publisher_ =
node_ptr_->create_publisher<std_msgs::msg::Empty>(topic_name, 10);
return true;
}

void finalizePublisher(void)
{
button_publisher_.reset();
}

void publishButton(bool button)
{
if (button)
{
std_msgs::msg::Empty msg;
button_publisher_->publish(msg);
}
}

std::vector<std::string> getEmptyTopicList(void) const
{
return getTopicList("std_msgs/msg/Empty");
}

private:
std::vector<std::string> getTopicList(const std::string type_name) const
{
std::map<std::string,
std::vector<std::string>> topic_map = node_ptr_->get_topic_names_and_types();

std::vector<std::string> output;
for (auto pair : topic_map) {
for (auto s : pair.second) {
if (s == type_name) {
output.push_back(pair.first);
break;
}
}
}
return output;
}

rclcpp::Node::SharedPtr node_ptr_;
rclcpp::Publisher<std_msgs::msg::Empty>::SharedPtr button_publisher_;
};

} // namespace button_rviz_plugin
136 changes: 136 additions & 0 deletions button_rviz_plugin/src/button_panel.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
// Copyright 2024 StrayedCats.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "button_panel.hpp"

#include <rviz_common/config.hpp>
#include <rviz_common/display_context.hpp>

#include <QPainter>
#include <QMouseEvent>
#include <QSizePolicy>

namespace button_rviz_plugin
{
EmptyButton::EmptyButton(QWidget * parent)
: rviz_common::Panel(parent)
{
QVBoxLayout * layout = new QVBoxLayout;

QHBoxLayout * layout_1st = new QHBoxLayout;
enable_check_ = new QCheckBox("Enable");
layout_1st->addWidget(enable_check_);
topic_combo_ = new QComboBox();
topic_combo_->setEditable(true);
layout_1st->addWidget(topic_combo_);
// layout->addLayout(layout_1st);

// QHBoxLayout * layout_3rd = new QHBoxLayout;
a_button_ = new QPushButton("click");
layout_1st->addWidget(a_button_);
layout->addLayout(layout_1st);

setLayout(layout);

interval_timer_ = new QTimer(this);

connect(interval_timer_, &QTimer::timeout, this, &EmptyButton::onTick);
connect(enable_check_, &QCheckBox::stateChanged, this, &EmptyButton::onCheckChange);
connect(a_button_, &QPushButton::clicked, this, &EmptyButton::onClickA);

interval_timer_->start(100);
}

void EmptyButton::onInitialize()
{
button_handler_.setRosNodePtr(
this->getDisplayContext()->getRosNodeAbstraction().lock()->get_raw_node());
updateTopicList();
}

void EmptyButton::onCheckChange(int state)
{
if (state == Qt::Checked) {
std::string topic_name = topic_combo_->currentText().toStdString();
bool ret = button_handler_.initializePublisher(topic_name);
if (!ret) {
return;
}
is_active_ = true;
} else {
button_handler_.finalizePublisher();
is_active_ = false;
updateTopicList();
}
}

void EmptyButton::onClickA()
{
a_clicked_ = true;
}

void EmptyButton::onTick()
{
if (is_active_) {
button_handler_.publishButton(a_clicked_);
a_clicked_ = false;
}
}

void EmptyButton::save(rviz_common::Config config) const
{
rviz_common::Panel::save(config);
config.mapSetValue("BaseTopic", topic_combo_->currentText());
config.mapSetValue("Checked", enable_check_->isChecked());
}

void EmptyButton::load(const rviz_common::Config & config)
{
rviz_common::Panel::load(config);
QString tmp_text;
bool tmp_bool;
if (config.mapGetString("BaseTopic", &tmp_text)) {
topic_combo_->setCurrentText(tmp_text);
}
if (config.mapGetBool("Checked", &tmp_bool)) {
enable_check_->setChecked(tmp_bool);
}
}

void EmptyButton::updateTopicList(void)
{
std::string previous_topic_name = topic_combo_->currentText().toStdString();
auto topic_list = button_handler_.getEmptyTopicList();
topic_combo_->clear();
int same_topic_index = -1;
for (auto t : topic_list) {
topic_combo_->addItem(t.c_str());
if (t == previous_topic_name) {
same_topic_index = topic_combo_->count() - 1;
}
}

if (previous_topic_name != "") {
if (same_topic_index < 0) {
topic_combo_->addItem(previous_topic_name.c_str());
same_topic_index = topic_combo_->count() - 1;
}
topic_combo_->setCurrentIndex(same_topic_index);
}
}

} // namespace button_rviz_plugin

#include <pluginlib/class_list_macros.hpp>
PLUGINLIB_EXPORT_CLASS(button_rviz_plugin::EmptyButton, rviz_common::Panel)
55 changes: 55 additions & 0 deletions button_rviz_plugin/src/button_panel.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright 2024 StrayedCats.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#pragma once

#include <QtWidgets>
#include <QComboBox>

#ifndef Q_MOC_RUN
#include "button_handler.hpp"
#include <rviz_common/panel.hpp>
#endif

namespace button_rviz_plugin
{

class EmptyButton : public rviz_common::Panel
{
Q_OBJECT

public:
EmptyButton(QWidget * parent = nullptr);
void onInitialize() override;
void load(const rviz_common::Config & config) override;
void save(rviz_common::Config config) const override;

public Q_SLOTS:
void onCheckChange(int state);
void onClickA(void);
void onTick(void);

private:
void updateTopicList(void);

ButtonHandler button_handler_{};
QCheckBox * enable_check_;
QComboBox * topic_combo_;
QPushButton * a_button_;
QTimer * interval_timer_;
bool is_active_{false};
bool a_clicked_{false};
};

} // namespace button_rviz_plugin

0 comments on commit 6620462

Please sign in to comment.