Skip to content

Commit

Permalink
Merge pull request #11 from gergondet/topic/SupportTableElement
Browse files Browse the repository at this point in the history
[GUI] Support Table element
  • Loading branch information
gergondet authored Jul 7, 2020
2 parents 5de3a97 + cec9c3f commit 4572f2d
Show file tree
Hide file tree
Showing 5 changed files with 170 additions and 3 deletions.
5 changes: 4 additions & 1 deletion mc_rtc_rviz_panel/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -95,6 +97,7 @@ set(SOURCE_FILES
Schema.cpp
SchemaArrayInput.cpp
SchemaWidget.cpp
TableWidget.cpp
${MOC_FILES}
${QRC_FILES})

Expand Down
41 changes: 40 additions & 1 deletion mc_rtc_rviz_panel/src/Panel.cpp
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -27,6 +27,7 @@
#include "NumberSliderWidget.h"
#include "PlotTabWidget.h"
#include "SchemaWidget.h"
#include "TableWidget.h"

#include <boost/filesystem.hpp>
namespace bfs = boost::filesystem;
Expand Down Expand Up @@ -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<std::string> &)), this,
SLOT(got_table_start(const WidgetId &, const std::vector<std::string> &)));
connect(this, SIGNAL(signal_table_row(const WidgetId &, const std::vector<std::string> &)), this,
SLOT(got_table_row(const WidgetId &, const std::vector<std::string> &)));
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)));
Expand Down Expand Up @@ -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<std::string> & header)
{
Q_EMIT signal_table_start(id, header);
}

void Panel::table_row(const WidgetId & id, const std::vector<std::string> & 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);
Expand Down Expand Up @@ -789,6 +810,24 @@ void Panel::got_schema(const WidgetId & id, const std::string & schema)
get_widget<SchemaWidget>(id, schema, data_);
}

void Panel::got_table_start(const WidgetId & id, const std::vector<std::string> & header)
{
auto & w = get_widget<TableWidget>(id);
w.header(header);
}

void Panel::got_table_row(const WidgetId & id, const std::vector<std::string> & data)
{
auto & w = get_widget<TableWidget>(id);
w.row(data);
}

void Panel::got_table_end(const WidgetId & id)
{
auto & w = get_widget<TableWidget>(id);
w.finalize();
}

void Panel::got_form(const WidgetId & id)
{
auto & form = get_widget<FormWidget>(id);
Expand Down
14 changes: 13 additions & 1 deletion mc_rtc_rviz_panel/src/Panel.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2019 CNRS-UM LIRMM, CNRS-AIST JRL
* Copyright 2016-2020 CNRS-UM LIRMM, CNRS-AIST JRL
*/

#pragma once
Expand Down Expand Up @@ -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<std::string> & header) override;

void table_row(const WidgetId & id, const std::vector<std::string> & 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;
Expand Down Expand Up @@ -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<std::string> & header);
void got_table_row(const WidgetId & id, const std::vector<std::string> & 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);
Expand Down Expand Up @@ -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<std::string> & header);
void signal_table_row(const WidgetId & id, const std::vector<std::string> & 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);
Expand Down
80 changes: 80 additions & 0 deletions mc_rtc_rviz_panel/src/TableWidget.cpp
Original file line number Diff line number Diff line change
@@ -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<std::string> & 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<std::string> & 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<QLabel *>(table_->cellWidget(row, column));
if(!item)
{
item = new QLabel();
table_->setCellWidget(row, column, item);
}
item->setText(text.c_str());
}

} // namespace mc_rtc_rviz
33 changes: 33 additions & 0 deletions mc_rtc_rviz_panel/src/TableWidget.h
Original file line number Diff line number Diff line change
@@ -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<std::string> & header);

void row(const std::vector<std::string> & data);

void finalize();

private:
std::vector<std::string> header_ = {};
int rowCount_ = 0;
QHBoxLayout * layout_ = nullptr;
QTableWidget * table_ = nullptr;

void updateItem(int row, int column, const std::string & text);
};

} // namespace mc_rtc_rviz

0 comments on commit 4572f2d

Please sign in to comment.