ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

Saving rviz plugin configuration

asked 2021-02-16 21:55:52 -0500

standmit gravatar image

updated 2021-02-18 06:31:37 -0500

gvdhoorn gravatar image

Hello! I made an rviz panel for scaling the scene. I override save and load methods and expect saving and loading scale on saving and loading .rviz file. But it doesn't happen: .rviz file doesn't contains scale and scale is always has defualt value on startup. What is wrong?

header:

#pragma once

#include <rviz/panel.h>

class QLineEdit;

namespace orbiterros {
namespace rviz_plugin {

class ScalePanel : public rviz::Panel {
    Q_OBJECT

    public:
        ScalePanel(QWidget* parent = nullptr);

        virtual void load(const rviz::Config& config);
        virtual void save(rviz::Config config);

        static const char panel_name[];

    public Q_SLOTS:
        void setWorldScale(const float& scale);

    protected Q_SLOTS:
        void updateWorldScale();

    protected:
        QLineEdit* world_scale_editor_;
        float actual_scale_;

        static const QString world_scale_property_name;
};

}
}

.cpp:

#include "scale_panel.h"
#include <QHBoxLayout>
#include <QLabel>
#include <QLineEdit>
#include <QValidator>
#include <rviz/visualization_manager.h>
#include <OGRE/OgreSceneManager.h>
#include <QLocale>

namespace orbiterros {
namespace rviz_plugin {

constexpr char ScalePanel::panel_name[] = "scale_panel";
const QString ScalePanel::world_scale_property_name("World scale");

ScalePanel::ScalePanel(QWidget* parent):
        rviz::Panel(parent),
        world_scale_editor_(new QLineEdit),
        actual_scale_(1.0)
{
    QHBoxLayout* layout = new QHBoxLayout;
    layout->addWidget(new QLabel(world_scale_property_name));
    world_scale_editor_->setText(QVariant(actual_scale_).toString());
    world_scale_editor_->setValidator(new QDoubleValidator(1e-15, 1e3, 7));
    layout->addWidget(world_scale_editor_);
    setLayout(layout);
    bool success = connect(world_scale_editor_, SIGNAL(editingFinished()), this, SLOT(updateWorldScale()));
    Q_ASSERT(success);
}

void ScalePanel::updateWorldScale() {
    if (not world_scale_editor_->hasAcceptableInput()) {
        ROS_DEBUG_NAMED(panel_name, "Invalid scale value (validator)");
        return;
    }

    bool ok;
    const float new_scale = QLocale(QLocale::German).toFloat(world_scale_editor_->text(), &ok);
    if (not ok) {
        ROS_DEBUG_NAMED(panel_name, "Invalid scale value (conversion)");
        return;
    }

    setWorldScale(new_scale);
}

void ScalePanel::setWorldScale(const float& scale) {
    if (scale < 1e-15 or scale > 1e3) {
        ROS_DEBUG_NAMED(panel_name, "Invalid scale value (value)");
        return;
    }

    this->vis_manager_->getSceneManager()->getRootSceneNode()->setScale(scale, scale, scale);
    ROS_DEBUG_NAMED(panel_name, "Set world scale to %f", scale);

    Q_EMIT configChanged();
}

void ScalePanel::save(rviz::Config config) {
    rviz::Panel::save(config);
    config.mapSetValue(world_scale_property_name, actual_scale_);
}

void ScalePanel::load(const rviz::Config& config) {
    rviz::Panel::load(config);
    QVariant scale;
    if (config.mapGetValue(world_scale_property_name, &scale)) {
        world_scale_editor_->setText(scale.toString());
        updateWorldScale();
    }
}

}
}

#include <pluginlib/class_list_macros.h>
PLUGINLIB_EXPORT_CLASS(orbiterros::rviz_plugin::ScalePanel, rviz::Panel)

Code on Github

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2021-02-18 05:53:44 -0500

mgruhler gravatar image

Your function signature is the following:

virtual void save(rviz::Config config);

Whereas the function signature you want to override is

virtual void save(rviz::Config config) const;

See also the docs page (you haven't specified which ROS distro you are using, but it is the same for at least noetic, melodic and kinetic).

Thus, you are not overriding the function, but introduce a new one. You could safeguard against that by using the override keyword available in C++11.

edit flag offensive delete link more

Comments

Thank you!

standmit gravatar image standmit  ( 2021-02-18 21:32:50 -0500 )edit

Question Tools

2 followers

Stats

Asked: 2021-02-16 21:55:52 -0500

Seen: 237 times

Last updated: Feb 18 '21