Skip to content

Commit

Permalink
Rolling namespace in title (#1074)
Browse files Browse the repository at this point in the history
* Update visualization_frame.cpp

* window title format option added

Signed-off-by: Markus Bader <[email protected]>
Co-authored-by: Chris Lalancette <[email protected]>
  • Loading branch information
maxbader and clalancette authored Nov 9, 2023
1 parent a83d819 commit ea2dbb3
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 5 deletions.
14 changes: 14 additions & 0 deletions rviz_common/include/rviz_common/visualization_frame.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,19 @@ class RVIZ_COMMON_PUBLIC VisualizationFrame : public QMainWindow, public WindowM
ros_integration::RosNodeAbstractionIface::WeakPtr rviz_ros_node,
const QString & display_config_file = "");

/// Set the display title format.
/**
* Sets the format of the window title.
* Three replacement tokens are supported:
* - {NAMESPACE} - replace with the namespace this node is in
* - {CONFIG_PATH} - replace with the path (but not the filename) of the configuration file in use.
* - {CONFIG_FILENAME} - replace with the filename (but not the path) of the configuration file in use.
* The default is "RViz[*]" if the default configuration file is in use,
* or "{CONFIG_PATH}/{CONFIG_FILENAME}[*] - RViz" if a custom configuration file is in use.
*/
void
setDisplayTitleFormat(const QString & title_format);

/// Return the visualization manager.
VisualizationManager *
getManager();
Expand Down Expand Up @@ -444,6 +457,7 @@ protected Q_SLOTS:

std::string config_dir_;
std::string persistent_settings_file_;
std::string display_title_format_;
std::string display_config_file_;
std::string default_display_config_file_;
std::string last_config_dir_;
Expand Down
36 changes: 32 additions & 4 deletions rviz_common/src/rviz_common/visualization_frame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include "rviz_common/visualization_frame.hpp"

#include <exception>
#include <filesystem>
#include <fstream>
#include <memory>
#include <string>
Expand Down Expand Up @@ -738,16 +739,43 @@ void VisualizationFrame::setDisplayConfigModified()
}
}

void VisualizationFrame::setDisplayTitleFormat(const QString & title_format)
{
display_title_format_ = title_format.toStdString();
}

void VisualizationFrame::setDisplayConfigFile(const std::string & path)
{
display_config_file_ = path;

std::string title;
if (path == default_display_config_file_) {
title = "RViz[*]";

if (display_title_format_.empty()) {
if (path == default_display_config_file_) {
title = "RViz[*]";
} else {
title = QDir::toNativeSeparators(QString::fromStdString(path)).toStdString() + "[*] - RViz";
}
} else {
title = QDir::toNativeSeparators(QString::fromStdString(path)).toStdString() + "[*] - RViz";
auto find_and_replace_token =
[](std::string & title, const std::string & token, const std::string & replacement)
{
std::size_t found = title.find(token);
if (found != std::string::npos) {
title.replace(found, token.length(), replacement);
}
};
title = display_title_format_;
std::filesystem::path full_filename(path.c_str());
find_and_replace_token(
title, "{NAMESPACE}",
rviz_ros_node_.lock()->get_raw_node()->get_namespace());
find_and_replace_token(title, "{CONFIG_PATH}", full_filename.parent_path().string());
find_and_replace_token(title, "{CONFIG_FILENAME}", full_filename.filename().string());
if (title.find("[*]") == std::string::npos) {
title.append("[*]");
}
}

setWindowTitle(QString::fromStdString(title));
}

Expand Down
16 changes: 15 additions & 1 deletion rviz_common/src/rviz_common/visualizer_app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,13 @@ bool VisualizerApp::init(int argc, char ** argv)
parser.setApplicationDescription("3D visualization tool for ROS2");
parser.addHelpOption();

QCommandLineOption display_title_format_option(
QStringList() << "t" << "display-title-format",
"A display title format like ",
"\"{NAMESPACE} - {CONFIG_PATH}/{CONFIG_FILENAME} - RViz2\" ",
"display_title_format");
parser.addOption(display_title_format_option);

QCommandLineOption display_config_option(
QStringList() << "d" << "display-config",
"A display config file (.rviz) to load",
Expand All @@ -105,7 +112,7 @@ bool VisualizerApp::init(int argc, char ** argv)
"A custom splash-screen image to display", "splash_path");
parser.addOption(splash_screen_option);

QString display_config, fixed_frame, splash_path, help_path;
QString display_config, fixed_frame, splash_path, help_path, display_title_format;
bool enable_ogre_log;

if (app_) {parser.process(*app_);}
Expand All @@ -123,6 +130,10 @@ bool VisualizerApp::init(int argc, char ** argv)
splash_path = parser.value(splash_screen_option);
}

if (parser.isSet(display_title_format_option)) {
display_title_format = parser.value(display_title_format_option);
}

if (enable_ogre_log) {
rviz_rendering::OgreLogging::get()->useLogFileAndStandardOut();
rviz_rendering::OgreLogging::get()->configureLogging();
Expand All @@ -133,6 +144,9 @@ bool VisualizerApp::init(int argc, char ** argv)
node_ = ros_client_abstraction_->init(argc, argv, "rviz", false /* anonymous_name */);

frame_ = new VisualizationFrame(node_);

frame_->setDisplayTitleFormat(display_title_format);

frame_->setApp(this->app_);

if (!help_path.isEmpty()) {
Expand Down

0 comments on commit ea2dbb3

Please sign in to comment.