diff --git a/mc_rtc_rviz_panel/src/CMakeLists.txt b/mc_rtc_rviz_panel/src/CMakeLists.txt index 9eb0576..eedcd8d 100644 --- a/mc_rtc_rviz_panel/src/CMakeLists.txt +++ b/mc_rtc_rviz_panel/src/CMakeLists.txt @@ -17,7 +17,9 @@ set(MOC_HEADERS PlotWidget.h PlotTabWidget.h SchemaArrayInput.h - SchemaWidget.h) + SchemaWidget.h + TableWidget.h +) if(NOT ${DISABLE_ROS}) set(ROS_MOC_HEADERS @@ -95,6 +97,7 @@ set(SOURCE_FILES Schema.cpp SchemaArrayInput.cpp SchemaWidget.cpp + TableWidget.cpp ${MOC_FILES} ${QRC_FILES}) diff --git a/mc_rtc_rviz_panel/src/Panel.cpp b/mc_rtc_rviz_panel/src/Panel.cpp index 2900930..9a1c735 100644 --- a/mc_rtc_rviz_panel/src/Panel.cpp +++ b/mc_rtc_rviz_panel/src/Panel.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2016-2019 CNRS-UM LIRMM, CNRS-AIST JRL + * Copyright 2016-2020 CNRS-UM LIRMM, CNRS-AIST JRL */ #include "Panel.h" @@ -27,6 +27,7 @@ #include "NumberSliderWidget.h" #include "PlotTabWidget.h" #include "SchemaWidget.h" +#include "TableWidget.h" #include namespace bfs = boost::filesystem; @@ -243,6 +244,11 @@ Panel::Panel(QWidget * parent) SLOT(got_xytheta(const WidgetId &, const WidgetId &, bool, const Eigen::Vector3d &, double))); connect(this, SIGNAL(signal_schema(const WidgetId &, const std::string &)), this, SLOT(got_schema(const WidgetId &, const std::string &))); + connect(this, SIGNAL(signal_table_start(const WidgetId &, const std::vector &)), this, + SLOT(got_table_start(const WidgetId &, const std::vector &))); + connect(this, SIGNAL(signal_table_row(const WidgetId &, const std::vector &)), this, + SLOT(got_table_row(const WidgetId &, const std::vector &))); + connect(this, SIGNAL(signal_table_end(const WidgetId &)), this, SLOT(got_table_end(const WidgetId &))); connect(this, SIGNAL(signal_form(const WidgetId &)), this, SLOT(got_form(const WidgetId &))); connect(this, SIGNAL(signal_form_checkbox(const WidgetId &, const std::string &, bool, bool)), this, SLOT(got_form_checkbox(const WidgetId &, const std::string &, bool, bool))); @@ -479,6 +485,21 @@ void Panel::schema(const WidgetId & id, const std::string & schema) Q_EMIT signal_schema(id, schema); } +void Panel::table_start(const WidgetId & id, const std::vector & header) +{ + Q_EMIT signal_table_start(id, header); +} + +void Panel::table_row(const WidgetId & id, const std::vector & data) +{ + Q_EMIT signal_table_row(id, data); +} + +void Panel::table_end(const WidgetId & id) +{ + Q_EMIT signal_table_end(id); +} + void Panel::form(const WidgetId & id) { Q_EMIT signal_form(id); @@ -789,6 +810,24 @@ void Panel::got_schema(const WidgetId & id, const std::string & schema) get_widget(id, schema, data_); } +void Panel::got_table_start(const WidgetId & id, const std::vector & header) +{ + auto & w = get_widget(id); + w.header(header); +} + +void Panel::got_table_row(const WidgetId & id, const std::vector & data) +{ + auto & w = get_widget(id); + w.row(data); +} + +void Panel::got_table_end(const WidgetId & id) +{ + auto & w = get_widget(id); + w.finalize(); +} + void Panel::got_form(const WidgetId & id) { auto & form = get_widget(id); diff --git a/mc_rtc_rviz_panel/src/Panel.h b/mc_rtc_rviz_panel/src/Panel.h index d618571..cd6ecf1 100644 --- a/mc_rtc_rviz_panel/src/Panel.h +++ b/mc_rtc_rviz_panel/src/Panel.h @@ -1,5 +1,5 @@ /* - * Copyright 2016-2019 CNRS-UM LIRMM, CNRS-AIST JRL + * Copyright 2016-2020 CNRS-UM LIRMM, CNRS-AIST JRL */ #pragma once @@ -137,6 +137,12 @@ class Panel : public CategoryWidget, public mc_control::ControllerClient void schema(const WidgetId & id, const std::string & schema) override; + void table_start(const WidgetId & id, const std::vector & header) override; + + void table_row(const WidgetId & id, const std::vector & data) override; + + void table_end(const WidgetId & id) override; + void form(const WidgetId & id) override; void form_checkbox(const WidgetId & formId, const std::string & name, bool required, bool def) override; @@ -279,6 +285,9 @@ private slots: const mc_rtc::gui::ArrowConfig & config, bool ro); void got_schema(const WidgetId & id, const std::string & schema); + void got_table_start(const WidgetId & id, const std::vector & header); + void got_table_row(const WidgetId & id, const std::vector & data); + void got_table_end(const WidgetId & id); void got_form(const WidgetId & id); void got_form_checkbox(const WidgetId & formId, const std::string & name, bool required, bool def); void got_form_integer_input(const WidgetId & formId, const std::string & name, bool required, int def); @@ -373,6 +382,9 @@ private slots: const Eigen::Vector3d & vec, double altitude); void signal_schema(const WidgetId & id, const std::string & schema); + void signal_table_start(const WidgetId & id, const std::vector & header); + void signal_table_row(const WidgetId & id, const std::vector & data); + void signal_table_end(const WidgetId & id); void signal_form(const WidgetId & id); void signal_form_checkbox(const WidgetId & formId, const std::string & name, bool required, bool def); void signal_form_integer_input(const WidgetId & formId, const std::string & name, bool required, int def); diff --git a/mc_rtc_rviz_panel/src/TableWidget.cpp b/mc_rtc_rviz_panel/src/TableWidget.cpp new file mode 100644 index 0000000..5ab6e81 --- /dev/null +++ b/mc_rtc_rviz_panel/src/TableWidget.cpp @@ -0,0 +1,80 @@ +/* + * Copyright 2016-2020 CNRS-UM LIRMM, CNRS-AIST JRL + */ + +#include "TableWidget.h" + +namespace mc_rtc_rviz +{ + +TableWidget::TableWidget(const ClientWidgetParam & params) : ClientWidget(params) +{ + layout_ = new QHBoxLayout(this); + table_ = new QTableWidget(); + table_->verticalHeader()->hide(); +#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0) + table_->horizontalHeader()->setResizeMode(QHeaderView::Stretch); +#else + table_->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch); +#endif + layout_->addWidget(table_); +} + +void TableWidget::header(const std::vector & header) +{ + rowCount_ = 0; + if(header_ == header) + { + return; + } + header_ = header; + table_->setColumnCount(header_.size()); + for(size_t j = 0; j < header_.size(); ++j) + { + auto item = table_->horizontalHeaderItem(j); + if(!item) + { + item = new QTableWidgetItem(); + table_->setHorizontalHeaderItem(j, item); + } + item->setText(header_[j].c_str()); + } +} + +void TableWidget::row(const std::vector & data) +{ + for(size_t j = 0; j < data.size(); ++j) + { + updateItem(rowCount_, j, data[j]); + } + rowCount_ += 1; +} + +void TableWidget::finalize() +{ + while(table_->rowCount() > rowCount_) + { + table_->removeRow(table_->rowCount() - 1); + } +} + +void TableWidget::updateItem(int row, int column, const std::string & text) +{ + if(table_->rowCount() < row + 1) + { + table_->setRowCount(row + 1); + } + if(table_->columnCount() < column + 1) + { + table_->setColumnCount(column + 1); + } + auto item = dynamic_cast(table_->cellWidget(row, column)); + if(!item) + { + item = new QLabel(); + table_->setCellWidget(row, column, item); + } + item->setText(text.c_str()); +} + +} // namespace mc_rtc_rviz diff --git a/mc_rtc_rviz_panel/src/TableWidget.h b/mc_rtc_rviz_panel/src/TableWidget.h new file mode 100644 index 0000000..8d36c92 --- /dev/null +++ b/mc_rtc_rviz_panel/src/TableWidget.h @@ -0,0 +1,33 @@ +/* + * Copyright 2016-2020 CNRS-UM LIRMM, CNRS-AIST JRL + */ + +#pragma once + +#include "ClientWidget.h" + +namespace mc_rtc_rviz +{ + +struct TableWidget : public ClientWidget +{ + Q_OBJECT +public: + TableWidget(const ClientWidgetParam & params); + + void header(const std::vector & header); + + void row(const std::vector & data); + + void finalize(); + +private: + std::vector header_ = {}; + int rowCount_ = 0; + QHBoxLayout * layout_ = nullptr; + QTableWidget * table_ = nullptr; + + void updateItem(int row, int column, const std::string & text); +}; + +} // namespace mc_rtc_rviz