在线功能合并

pull/1/head
wubw1656 2023-10-07 21:06:49 +08:00
parent 86d0b5519f
commit 48d6c63740
2 changed files with 107 additions and 122 deletions

216
librviz_tutorial/src/myviz.cpp Normal file → Executable file
View File

@ -4,7 +4,7 @@
#include <QLabel>
#include<QPlainTextEdit>
#include<QPushButton>
#include<QPushButton>
#include<QCheckBox>
#include <QVBoxLayout>
#include <QHBoxLayout>
@ -39,38 +39,14 @@
#include "rviz/selection/selection_manager.h"
#include "rviz/panel.h"
#include<QDebug>
#include<QKeyEvent>
#include <QMutex>
#include<QTimer>
#include<QObject>
#include <QMetaEnum>
#include <QRegExp>
#include <cmath>
#include<QMessageBox>
#include<QCoreApplication>
#include <QProgressBar>
#include <rosbag/bag.h>
#include <rosbag/view.h>
#include <rosbag/query.h>
#include<string>
#include<QFileDialog>
#include "rviz/properties/property_tree_widget.h"
#include "rviz/selection/selection_manager.h"
#include "rviz/panel.h"
#include "rviz/visualization_manager.h"
#include"rviz/tool_manager.h"
#include"rviz/tool_manager.h"
#include "rviz/render_panel.h"
#include "rviz/display.h"
#include "myviz.h"
#include "../tools/color.h"
#include "qthread.h"
#include "qthread.h"
// BEGIN_TUTORIAL
// Constructor for MyViz. This does most of the work of the class.
MyViz::MyViz( QWidget* parent)
@ -84,8 +60,9 @@ MyViz::MyViz( QWidget* parent)
// 全局变量存放处
// 颜色名称可添加待设置rgb类型设置颜色
color_name = {"white","red","green","black","blue","yellow","pink","purple","skyblue","lightgreen","orange",
color_name = {"white","red","green","black","blue","yellow","pink","purple","skyblue","lightgreen","orange",
color_name = {
"white","red","green","black","blue","yellow",
"pink","purple","skyblue","lightgreen","orange",
};
// 颜色路径名
color_icon_filename = QString("../Rviz/librviz_ws/src/librviz_tutorial/sources/images/%1.png");
@ -117,19 +94,18 @@ MyViz::MyViz( QWidget* parent)
controls_layout->addWidget( login_button, ctl_index, 0);
controls_layout->addWidget( quit_login_button, ctl_index++, 1);
controls_layout->addWidget( move_camera_button, ctl_index, 0);
controls_layout->addWidget( selection_button, ctl_index++ ,1);
// offline 布局
// QPushButton setting!////////////////////////////////////////////////////////////////////////////////////////////////
QPushButton* play = new QPushButton("play", this);
QPushButton* pause = new QPushButton("pause", this);
QPushButton* but_save = new QPushButton("save", this);// buttonn: save one frame
QPushButton* but_saveMul = new QPushButton("saveMul", this);// button: save multiple frames
QPushButton* but_file = new QPushButton("file", this);// button: select file
QPushButton* play = new QPushButton("播放", this);
QPushButton* pause = new QPushButton("暂停", this);
QPushButton* but_save = new QPushButton("保存一帧", this);// buttonn: save one frame
QPushButton* but_saveMul = new QPushButton("保存多帧", this);// button: save multiple frames
QPushButton* but_file = new QPushButton("打开文件", this);// button: select file
// QLabel setting!/////////////////////////////////////////////////////////////////////////////////////////////////////
QLabel* label_speed = new QLabel("speed:");//speed combobox
QLabel* label_speed = new QLabel("播放速度:");//speed combobox
QLabel* label_split = new QLabel("");//speed combobox
// QSlider setting!////////////////////////////////////////////////////////////////////////////////////////////////////
@ -144,11 +120,11 @@ MyViz::MyViz( QWidget* parent)
but_file->setEnabled(false);
// setting button size!////////////////////////////////////////////////////////////////////////////////////////////////
play->setFixedSize(100, 30);
pause->setFixedSize(100, 30);
but_save->setFixedSize(100, 30);
but_saveMul->setFixedSize(100, 30);
but_file->setFixedSize(100, 30);
play->setFixedSize(120, 30);
pause->setFixedSize(120, 30);
but_save->setFixedSize(120, 30);
but_saveMul->setFixedSize(120, 30);
but_file->setFixedSize(120, 30);
for(int i = 0; i < speed_name.size(); i++)
speed_combobox->addItem(speed_name[i] + "x");
@ -159,6 +135,9 @@ MyViz::MyViz( QWidget* parent)
controls_layout->addWidget( but_save, ctl_index++, 1 );
controls_layout->addWidget( but_saveMul, ctl_index++, 0 );
controls_layout->addWidget( move_camera_button, ctl_index, 0);
controls_layout->addWidget( selection_button, ctl_index++ ,1);
controls_layout->addWidget(label_speed,ctl_index,0);
controls_layout->addWidget(speed_combobox,ctl_index++,1);
@ -169,26 +148,29 @@ MyViz::MyViz( QWidget* parent)
controls_layout->setSpacing(5);
// 是否选择显示坐标轴
QLabel* label_axes = new QLabel("Display Axes:");
QComboBox * axesBox = new QComboBox();
axesBox->addItem("no");
axesBox->addItem("yes");
QLabel* label_axes = new QLabel("参考坐标系:");
QCheckBox *axes_checkbox = new QCheckBox("", this);
// QComboBox * axesBox = new QComboBox();
// axesBox->addItem("不显示");
// axesBox->addItem("显示");
// 选择圆圈范围
QLabel* label_circle = new QLabel("Circle Scale(m): ");
QSpinBox* circel_r_text=new QSpinBox();
QLabel* label_circle = new QLabel("参考圆(m): ");
QLineEdit* circleLineEdit = new QLineEdit();
circleLineEdit->setPlaceholderText("请输入数字");
circleLineEdit->setFixedWidth(120);
controls_layout->addWidget(label_axes, ctl_index,0);
controls_layout->addWidget(axesBox, ctl_index++,1);
controls_layout->addWidget(axes_checkbox, ctl_index++,1);
controls_layout->addWidget(label_circle, ctl_index,0);
controls_layout->addWidget(circel_r_text, ctl_index++,1);
controls_layout->addWidget(circleLineEdit, ctl_index++,1);
// 第二个layout
QGridLayout* display_layout = new QGridLayout();
int display_index = 0;
//QGridLayout* display_layout = new QGridLayout();
//int display_index = 0;
// Tree 全局变量存放地方
Tree_Display(display_layout, display_index++);
Tree_Display(controls_layout, ctl_index++);
// 待解锁子组件设置父组件,方便寻找,登陆后解锁可编辑属性
@ -196,6 +178,8 @@ MyViz::MyViz( QWidget* parent)
login_button->setParent(this);
quit_login_button->setObjectName("quit_login_button");
quit_login_button->setParent(this);
quit_login_button->setEnabled(false);
but_file->setObjectName("open_file");
but_file->setParent(this);
@ -216,9 +200,10 @@ MyViz::MyViz( QWidget* parent)
connect(move_camera_button, SIGNAL(clicked()), this, SLOT( slot_move_camera()));
// 增加坐标变换
connect(axesBox, SIGNAL(currentIndexChanged(int)), this, SLOT(AxesDisplayChanged(int)));
// connect(axesBox, SIGNAL(currentIndexChanged(int)), this, SLOT(AxesDisplayChanged(int)));
connect(axes_checkbox, SIGNAL(stateChanged(int)), this, SLOT(AxesDisplayChanged(int)));
// 增加圆圈变换
connect(circel_r_text,SIGNAL(valueChanged(int)),this,SLOT(CircleDisplayChanged(int)));
connect(circleLineEdit, SIGNAL(textChanged(const QString&)), this, SLOT(CircleDisplayChanged(const QString&)));
//off
connect(progress,&QSlider::valueChanged,this,[=](){// 变动会触发
if(!ismoved){
@ -325,10 +310,6 @@ MyViz::MyViz( QWidget* parent)
render_panel_ = new rviz::RenderPanel(); // RVIZ在QT上显示的类
QHBoxLayout* main_layout = new QHBoxLayout; // 主要layout QV垂直QH水平
// QSplitter *splitter = new QSplitter();
// main_layout->addWidget(splitter);
// splitter->addWidget(render_panel_);
// Next we initialize the main RViz classes.
//
// The VisualizationManager is the container for Display objects,
@ -350,12 +331,13 @@ MyViz::MyViz( QWidget* parent)
// qDebug()<< cur_fixed_frame;
// 创建select panel
QLabel *select_label = new QLabel("Select Display:");
QLabel *select_label = new QLabel("坐标点显示:");
tree_widget_ = new rviz::PropertyTreeWidget();
tree_widget_->setModel(manager_->getSelectionManager()->getPropertyModel());
display_layout->addWidget(select_label, display_index++, 0);
display_layout->addWidget(tree_widget_, display_index++, 0);
ctl_index++;
controls_layout->addWidget(select_label, ctl_index++, 0);
// controls_layout->addWidget(tree_widget_, ctl_index++, 0);
controls_layout->addWidget(tree_widget_, ctl_index++, 0, 1, 2);
// 设置网格某一行伸缩比例,放最后一行
// controls_layout->setRowStretch(--ctl_index, 6);
@ -367,8 +349,6 @@ MyViz::MyViz( QWidget* parent)
// cloud = manager_->createDisplay( "rviz/PointCloud2", "point cloud", true );
// ROS_ASSERT( cloud != NULL );
laser_ = manager_->createDisplay( "rviz/LaserScan", "Qlaser", true );
ROS_ASSERT( laser_ );
laser_ = manager_->createDisplay( "rviz/LaserScan", "Qlaser", true );
ROS_ASSERT( laser_ );
// 新增雷达
@ -398,7 +378,7 @@ MyViz::MyViz( QWidget* parent)
// Set the top-level layout for this MyViz widget.
main_layout->addLayout( controls_layout );
main_layout->addLayout( display_layout );
//main_layout->addLayout( controls_layout );
main_layout->addWidget( render_panel_,2); // 存放rviz的位置, stretch 拉伸系数
setLayout( main_layout );
@ -422,10 +402,10 @@ void MyViz::Tree_Display(QGridLayout* controls_layout, int index){
global->setExpanded(true);
// fix frame
QTreeWidgetItem *fixed_frame = new QTreeWidgetItem(global, QStringList("Fixed Frame"));
QTreeWidgetItem *fixed_frame = new QTreeWidgetItem(global, QStringList("固定参考系"));
QLineEdit* fixed_frame_text = new QLineEdit();
fixed_frame_text->setText("laser");
fixed_frame_text->setText("world");
fixed_frame_text->setStyleSheet("background:transparent;border-width:0;border-style:inset");
// 初始化固定坐标系
cur_fixed_frame = fixed_frame_text->text();
@ -434,7 +414,7 @@ void MyViz::Tree_Display(QGridLayout* controls_layout, int index){
// 将其他控件(非TreeWidgetItem)放入treeItem中
menu->setItemWidget(fixed_frame, 1, fixed_frame_text); // 将container 放到 Item ,数字为列数
QTreeWidgetItem *bg_color = new QTreeWidgetItem(global, QStringList("BackGround Color"));
QTreeWidgetItem *bg_color = new QTreeWidgetItem(global, QStringList("背景颜色"));
QLineEdit* bg_color_text = new QLineEdit();
bg_color_text->setText("48;48;48");
bg_color_text->setStyleSheet("border-width:0;border-style:outset");
@ -444,23 +424,20 @@ void MyViz::Tree_Display(QGridLayout* controls_layout, int index){
// global->addChild(fixed_frame);
// global->setTextAlignment(1, 2); // (列数,对齐方式)
// grid - 一层树
QTreeWidgetItem * grid = new QTreeWidgetItem(menu, QStringList("Grid"));
QTreeWidgetItem * grid = new QTreeWidgetItem(menu, QStringList("栅格"));
// 二层
QTreeWidgetItem *grid_color = new QTreeWidgetItem(grid, QStringList("Color"));
QTreeWidgetItem *grid_color = new QTreeWidgetItem(grid, QStringList("颜色"));
QComboBox * grid_color_cbox = new QComboBox();
for(int i =0 ; i < color_name.size(); ++i){
QString color_path = color_icon_filename.arg(color_name[i]);
QIcon color_icon(color_path);
grid_color_cbox->addItem(color_icon, color_name[i]);
QString color_path = color_icon_filename.arg(color_name[i]);
QIcon color_icon(color_path);
grid_color_cbox->addItem(color_icon, color_name[i]);
}
connect(grid_color_cbox, SIGNAL(currentIndexChanged(int)), this, SLOT(GridColorChanged(int)));
menu->setItemWidget(grid_color, 1, grid_color_cbox);
// grid cell size
QTreeWidgetItem *grid_cell = new QTreeWidgetItem(grid, QStringList("Cell Size"));
QTreeWidgetItem *grid_cell = new QTreeWidgetItem(grid, QStringList("网格大小"));
QSlider* cell_size_slider = new QSlider( Qt::Horizontal );
cell_size_slider->setMinimum( 1 );
cell_size_slider->setMaximum( 100 );
@ -469,7 +446,7 @@ void MyViz::Tree_Display(QGridLayout* controls_layout, int index){
menu->setItemWidget(grid_cell, 1, cell_size_slider);
// grid Line Thickness
QTreeWidgetItem *grid_thickness = new QTreeWidgetItem(grid, QStringList("Line Thickness"));
QTreeWidgetItem *grid_thickness = new QTreeWidgetItem(grid, QStringList("线条粗细"));
QSlider* thickness_slider = new QSlider( Qt::Horizontal );
thickness_slider->setMinimum( 1 );
thickness_slider->setMaximum( 100 );
@ -478,9 +455,9 @@ void MyViz::Tree_Display(QGridLayout* controls_layout, int index){
menu->setItemWidget(grid_thickness, 1, thickness_slider);
// laser - 一层树
QTreeWidgetItem * laser = new QTreeWidgetItem(menu, QStringList("Laser"));
QTreeWidgetItem * laser = new QTreeWidgetItem(menu, QStringList("雷达"));
// 二层
QTreeWidgetItem *laser_topic = new QTreeWidgetItem(laser, QStringList("Topic "));
QTreeWidgetItem *laser_topic = new QTreeWidgetItem(laser, QStringList("话题"));
// Topic_Laser 输入雷达
QLineEdit* laser_topic_text = new QLineEdit();
@ -493,13 +470,13 @@ void MyViz::Tree_Display(QGridLayout* controls_layout, int index){
menu->setItemWidget(laser_topic, 1, laser_topic_text);
// Laser size
QTreeWidgetItem *laser_size = new QTreeWidgetItem(laser, QStringList("Size "));
QTreeWidgetItem *laser_size = new QTreeWidgetItem(laser, QStringList("大小"));
QSpinBox* laser_size_text=new QSpinBox();
connect(laser_size_text,SIGNAL(valueChanged(int)),this,SLOT(setLaserSize(int)));
menu->setItemWidget(laser_size, 1, laser_size_text);
// Laser color
QTreeWidgetItem *laser_color = new QTreeWidgetItem(laser, QStringList("Color "));
QTreeWidgetItem *laser_color = new QTreeWidgetItem(laser, QStringList("颜色"));
QComboBox * laser_color_comboBox = new QComboBox();
for(int i =0 ; i < color_name.size(); ++i){
QString color_path = color_icon_filename.arg(color_name[i]);
@ -513,55 +490,70 @@ void MyViz::Tree_Display(QGridLayout* controls_layout, int index){
connect(laser_color_comboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(LaserColorChanged(int)));
menu->setItemWidget(laser_color, 1, laser_color_comboBox);
controls_layout->addWidget(menu, index, 0 );
//controls_layout->addWidget(menu, index, 0 );
controls_layout->addWidget(menu, index, 0, 1, 2);
}
void MyViz::quit_login_button_clicked(){
landed = 0;
// 退出设置登录按钮可用
QWidget * login_button = this->findChild<QWidget*>("login_button");
if (login_button) {
// 找到了指定的子组件 在这里可以对子组件进行操作
login_button->setEnabled(true);
landed = 0;
}else{
qDebug() << "login_button组件未找到" ;
}
// 退出设置退出按钮不可用
QWidget * quit_button = this->findChild<QWidget*>("quit_login_button");
if (quit_button) {
quit_button->setEnabled(false);
}else{
qDebug() << "quit_login_button组件未找到" ;
}
// 退出设置其他组件不可用
if(dlg != NULL){
for (int i=0; i< dlg->setting_name.size(); i++){
QWidget *foundChild = this->findChild<QWidget*>(dlg->setting_name[i]);
if (foundChild) {
// 找到了指定的子组件 在这里可以对子组件进行操作
foundChild->setEnabled(false);
}else{
qDebug()<<"" << i+1 <<"组件未找到" ;
}
QWidget *foundChild = this->findChild<QWidget*>(dlg->setting_name[i]);
if (foundChild) {
// 找到了指定的子组件 在这里可以对子组件进行操作
foundChild->setEnabled(false);
}else{
qDebug()<<"" << i+1 <<"组件未找到" ;
}
}
}
}
void MyViz::login_button_clicked(){
dlg->setWindowTitle(tr("登录"));
dlg->setWindowTitle(tr("登录"));
// dlg->setFixedSize(400, 300);
// dlg->open();// 非模态级别对话框
if(dlg->exec() == QDialog::Accepted){
// 登录成功后
qDebug() << "login accepted";
QWidget * login_button = this->findChild<QWidget*>("login_button");
if (login_button) {
// 找到了指定的子组件 在这里可以对子组件进行操作
login_button->setEnabled(false);
landed = 1;
}else{
qDebug()<<"login_button组件未找到" ;
}
if(dlg->exec() == QDialog::Accepted){
// 登录成功后
landed = 1;
qDebug() << "login accepted";
// 登录设置其他组件和退出可用
QWidget * quit_button = this->findChild<QWidget*>("quit_login_button");
if (quit_button) {
quit_button->setEnabled(true);
}else{
// 登录失败
qDebug() << "login refused";
qDebug() << "quit_login_button组件未找到" ;
}
// 登录设置登录不可用
QWidget * login_button = this->findChild<QWidget*>("login_button");
if (login_button) {
login_button->setEnabled(false);
}else{
qDebug()<<"login_button组件未找到" ;
}
}else{
// 登录失败
qDebug() << "login refused";
}
}
void MyViz::PlayCircle(int index){
void MyViz::PlayCircle(double index){
int argc =0;
char** argv;
ros::init(argc, argv, "basic_shapes");
@ -645,27 +637,29 @@ void MyViz::PlayCircle(int index){
}
void MyViz::CircleDisplayChanged(int r){
if(r == 0){
void MyViz::CircleDisplayChanged(const QString& text){
double index = text.toDouble();
if(index <= 0){
marker_->setValue(false);
}else
{
marker_->setValue(true);
std::thread ThreadCircle(&MyViz::PlayCircle, this, r);// 新开线程执行操作
std::thread ThreadCircle(&MyViz::PlayCircle, this, index);// 新开线程执行操作
ThreadCircle.detach();// 执行完成后自动回收资源
}
}
void MyViz::AxesDisplayChanged(int index)
void MyViz::AxesDisplayChanged(int stage)
{
switch (index)
// 0未被选中1被部分选中2 被选中
switch (stage)
{
case 0: // "yes" 选项被选择
case 0: // 未被选中
if (axes_display != NULL)
axes_display->setValue(false);
break;
case 1: // "no" 选项被选择
case 2: // 被选择
if (axes_display != NULL)
axes_display->setValue(true);
break;

View File

@ -70,9 +70,7 @@ public:
virtual ~MyViz();
void subCallback(const std_msgs::String& msg);
void pubThread();
void PlayCircle(int index); // 圆圈线程执行函数
rosbag::View *view;
void PlayCircle(int index); // 圆圈线程执行函数
void PlayCircle(double index); // 圆圈线程执行函数
rosbag::View *view;
private Q_SLOTS: // QT信号和槽
void setThickness( int thickness_percent );
@ -87,9 +85,7 @@ private Q_SLOTS: // QT信号和槽
void setFixedFrame(const QString &FixedFrame);
void setBackgroundColor(const QString& bg_color); // 背景颜色
void AxesDisplayChanged(int index); // axe
void CircleDisplayChanged(int index); // circle
void AxesDisplayChanged(int index); // axe
void CircleDisplayChanged(int index); // circle
void CircleDisplayChanged(const QString& text); // 圆圈变化
void Tree_Display(QGridLayout* layout, int index);
void login_button_clicked();
@ -106,17 +102,12 @@ private:
rviz::VisualizationManager* manager_; //
rviz::RenderPanel* render_panel_;
rviz::ToolManager* tool_manager_;
rviz::ToolManager* tool_manager_;
rviz::Display* grid_;
rviz::Display* cloud;
rviz::Display* laser_;
rviz::Display* axes_display;
rviz::Display* marker_;
rviz::PropertyTreeWidget* tree_widget_ ;
rviz::Display* laser_;
rviz::Display* axes_display;
rviz::Display* marker_;
rviz::PropertyTreeWidget* tree_widget_ ;
ros::NodeHandle nh;
ros::Subscriber sub;
ros::Publisher pub;