|
| 1 | +// Copyright (c) 2025, Open Source Robotics Foundation, Inc. |
| 2 | +// All rights reserved. |
| 3 | +// |
| 4 | +// Redistribution and use in source and binary forms, with or without |
| 5 | +// modification, are permitted provided that the following conditions are met: |
| 6 | +// |
| 7 | +// * Redistributions of source code must retain the above copyright |
| 8 | +// notice, this list of conditions and the following disclaimer. |
| 9 | +// |
| 10 | +// * Redistributions in binary form must reproduce the above copyright |
| 11 | +// notice, this list of conditions and the following disclaimer in the |
| 12 | +// documentation and/or other materials provided with the distribution. |
| 13 | +// |
| 14 | +// * Neither the name of the copyright holder nor the names of its |
| 15 | +// contributors may be used to endorse or promote products derived from |
| 16 | +// this software without specific prior written permission. |
| 17 | +// |
| 18 | +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 19 | +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 20 | +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 21 | +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 22 | +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 23 | +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 24 | +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 25 | +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 26 | +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 27 | +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 28 | +// POSSIBILITY OF SUCH DAMAGE. |
| 29 | + |
| 30 | +#include <map> |
| 31 | +#include <memory> |
| 32 | +#include <string> |
| 33 | +#include <vector> |
| 34 | + |
| 35 | +#include <QApplication> // NOLINT: cpplint can't handle Qt imports |
| 36 | +#include <QObject> // NOLINT: cpplint can't handle Qt imports |
| 37 | +#include <QRegExp> // NOLINT: cpplint can't handle Qt imports |
| 38 | +#include <QString> // NOLINT: cpplint can't handle Qt imports |
| 39 | +#include <QStringList> // NOLINT: cpplint can't handle Qt imports |
| 40 | + |
| 41 | +#include "rviz_common/properties/ros_action_property.hpp" |
| 42 | +#include "rviz_common/ros_integration/ros_node_abstraction_iface.hpp" |
| 43 | + |
| 44 | +namespace rviz_common |
| 45 | +{ |
| 46 | +namespace properties |
| 47 | +{ |
| 48 | + |
| 49 | +RosActionProperty::RosActionProperty( |
| 50 | + const QString & name, |
| 51 | + const QString & default_value, |
| 52 | + const QString & action_type, |
| 53 | + const QString & description, |
| 54 | + Property * parent, |
| 55 | + const char * changed_slot, |
| 56 | + QObject * receiver) |
| 57 | +: EditableEnumProperty(name, default_value, description, parent, changed_slot, receiver), |
| 58 | + rviz_ros_node_(), |
| 59 | + action_type_(action_type) |
| 60 | +{ |
| 61 | + connect( |
| 62 | + this, SIGNAL(requestOptions(EditableEnumProperty*)), |
| 63 | + this, SLOT(fillActionList())); |
| 64 | +} |
| 65 | + |
| 66 | +void RosActionProperty::initialize(ros_integration::RosNodeAbstractionIface::WeakPtr rviz_ros_node) |
| 67 | +{ |
| 68 | + rviz_ros_node_ = rviz_ros_node; |
| 69 | +} |
| 70 | + |
| 71 | +void RosActionProperty::setActionType(const QString & action_type) |
| 72 | +{ |
| 73 | + action_type_ = action_type; |
| 74 | +} |
| 75 | + |
| 76 | +QString RosActionProperty::getActionType() const |
| 77 | +{ |
| 78 | + return action_type_; |
| 79 | +} |
| 80 | + |
| 81 | +QString RosActionProperty::getAction() const |
| 82 | +{ |
| 83 | + return getValue().toString(); |
| 84 | +} |
| 85 | + |
| 86 | +std::string RosActionProperty::getActionStd() const |
| 87 | +{ |
| 88 | + return getValue().toString().toStdString(); |
| 89 | +} |
| 90 | + |
| 91 | +bool RosActionProperty::isEmpty() const |
| 92 | +{ |
| 93 | + return getActionStd().empty(); |
| 94 | +} |
| 95 | + |
| 96 | +void RosActionProperty::fillActionList() |
| 97 | +{ |
| 98 | + QApplication::setOverrideCursor(QCursor(Qt::WaitCursor)); |
| 99 | + clearOptions(); |
| 100 | + |
| 101 | + std::string std_action_type = action_type_.toStdString() + "_FeedbackMessage"; |
| 102 | + std::string suffix = "/_action/feedback"; |
| 103 | + std::map<std::string, std::vector<std::string>> published_topics = |
| 104 | + rviz_ros_node_.lock()->get_topic_names_and_types(); |
| 105 | + |
| 106 | + for (const auto & topic : published_topics) { |
| 107 | + // Only add topics whose type matches. |
| 108 | + for (const auto & type : topic.second) { |
| 109 | + if (type == std_action_type) { |
| 110 | + auto action = topic.first.substr(0, topic.first.size() - suffix.size()); |
| 111 | + addOptionStd(action); |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + sortOptions(); |
| 116 | + QApplication::restoreOverrideCursor(); |
| 117 | +} |
| 118 | + |
| 119 | +RosFilteredActionProperty::RosFilteredActionProperty( |
| 120 | + const QString & name, |
| 121 | + const QString & default_value, |
| 122 | + const QString & action_type, |
| 123 | + const QString & description, |
| 124 | + const QRegExp & filter, |
| 125 | + Property * parent, |
| 126 | + const char * changed_slot, |
| 127 | + QObject * receiver) |
| 128 | +: RosActionProperty(name, default_value, action_type, description, parent, changed_slot, receiver) |
| 129 | + , filter_(filter) |
| 130 | + , filter_enabled_(true) |
| 131 | +{ |
| 132 | +} |
| 133 | + |
| 134 | +void RosFilteredActionProperty::enableFilter(bool enabled) |
| 135 | +{ |
| 136 | + filter_enabled_ = enabled; |
| 137 | + fillActionList(); |
| 138 | +} |
| 139 | + |
| 140 | +QRegExp RosFilteredActionProperty::filter() const |
| 141 | +{ |
| 142 | + return filter_; |
| 143 | +} |
| 144 | + |
| 145 | +void RosFilteredActionProperty::fillActionList() |
| 146 | +{ |
| 147 | + QStringList filtered_strings_; |
| 148 | + |
| 149 | + // Obtain list of available actions |
| 150 | + RosActionProperty::fillActionList(); |
| 151 | + // Apply filter |
| 152 | + if (filter_enabled_) { |
| 153 | + strings_ = strings_.filter(filter_); |
| 154 | + } |
| 155 | +} |
| 156 | +} // end namespace properties |
| 157 | +} // end namespace rviz_common |
0 commit comments