|
| 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_service_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 | +RosServiceProperty::RosServiceProperty( |
| 50 | + const QString & name, |
| 51 | + const QString & default_value, |
| 52 | + const QString & service_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 | + service_type_(service_type) |
| 60 | +{ |
| 61 | + connect( |
| 62 | + this, SIGNAL(requestOptions(EditableEnumProperty*)), |
| 63 | + this, SLOT(fillServiceList())); |
| 64 | +} |
| 65 | + |
| 66 | +void RosServiceProperty::initialize(ros_integration::RosNodeAbstractionIface::WeakPtr rviz_ros_node) |
| 67 | +{ |
| 68 | + rviz_ros_node_ = rviz_ros_node; |
| 69 | +} |
| 70 | + |
| 71 | +void RosServiceProperty::setServiceType(const QString & service_type) |
| 72 | +{ |
| 73 | + service_type_ = service_type; |
| 74 | +} |
| 75 | + |
| 76 | +QString RosServiceProperty::getServiceType() const |
| 77 | +{ |
| 78 | + return service_type_; |
| 79 | +} |
| 80 | + |
| 81 | +QString RosServiceProperty::getService() const |
| 82 | +{ |
| 83 | + return getValue().toString(); |
| 84 | +} |
| 85 | + |
| 86 | +std::string RosServiceProperty::getServiceStd() const |
| 87 | +{ |
| 88 | + return getValue().toString().toStdString(); |
| 89 | +} |
| 90 | + |
| 91 | +bool RosServiceProperty::isEmpty() const |
| 92 | +{ |
| 93 | + return getServiceStd().empty(); |
| 94 | +} |
| 95 | + |
| 96 | +void RosServiceProperty::fillServiceList() |
| 97 | +{ |
| 98 | + QApplication::setOverrideCursor(QCursor(Qt::WaitCursor)); |
| 99 | + clearOptions(); |
| 100 | + |
| 101 | + std::string std_service_type = service_type_.toStdString(); |
| 102 | + std::map<std::string, std::vector<std::string>> service_servers = |
| 103 | + rviz_ros_node_.lock()->get_service_names_and_types(); |
| 104 | + |
| 105 | + for (const auto & service : service_servers) { |
| 106 | + // Only add service whose type matches. |
| 107 | + for (const auto & type : service.second) { |
| 108 | + if (type == std_service_type) { |
| 109 | + addOptionStd(service.first); |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + sortOptions(); |
| 114 | + QApplication::restoreOverrideCursor(); |
| 115 | +} |
| 116 | + |
| 117 | +RosFilteredServiceProperty::RosFilteredServiceProperty( |
| 118 | + const QString & name, |
| 119 | + const QString & default_value, |
| 120 | + const QString & service_type, |
| 121 | + const QString & description, |
| 122 | + const QRegExp & filter, |
| 123 | + Property * parent, |
| 124 | + const char * changed_slot, |
| 125 | + QObject * receiver) |
| 126 | +: RosServiceProperty(name, default_value, service_type, description, parent, changed_slot, receiver) |
| 127 | + , filter_(filter) |
| 128 | + , filter_enabled_(true) |
| 129 | +{ |
| 130 | +} |
| 131 | + |
| 132 | +void RosFilteredServiceProperty::enableFilter(bool enabled) |
| 133 | +{ |
| 134 | + filter_enabled_ = enabled; |
| 135 | + fillServiceList(); |
| 136 | +} |
| 137 | + |
| 138 | +QRegExp RosFilteredServiceProperty::filter() const |
| 139 | +{ |
| 140 | + return filter_; |
| 141 | +} |
| 142 | + |
| 143 | +void RosFilteredServiceProperty::fillServiceList() |
| 144 | +{ |
| 145 | + QStringList filtered_strings_; |
| 146 | + |
| 147 | + // Obtain list of available services |
| 148 | + RosServiceProperty::fillServiceList(); |
| 149 | + // Apply filter |
| 150 | + if (filter_enabled_) { |
| 151 | + strings_ = strings_.filter(filter_); |
| 152 | + } |
| 153 | +} |
| 154 | +} // end namespace properties |
| 155 | +} // end namespace rviz_common |
0 commit comments