Skip to content

Commit

Permalink
Use an rclpy context manager. (#312)
Browse files Browse the repository at this point in the history
This makes the code cleaner and ensures we cleanup no
matter how we exit.

Signed-off-by: Chris Lalancette <clalancette@gmail.com>
  • Loading branch information
clalancette authored Aug 6, 2024
1 parent c465dfb commit 6ed041c
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 56 deletions.
100 changes: 51 additions & 49 deletions rqt_py_common/src/rqt_py_common/topic_completer.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,52 +95,54 @@ def update_topics(self, node):
QTreeView, QVBoxLayout, QWidget

import rclpy
rclpy.init()
topic_completer_node = rclpy.create_node()

app = QApplication(sys.argv)
mw = QMainWindow()
widget = QWidget(mw)
layout = QVBoxLayout(widget)

edit = QLineEdit()
edit_completer = TopicCompleter(edit)
edit_completer.update_topics(topic_completer_node)
# edit_completer.setCompletionMode(QCompleter.InlineCompletion)
edit.setCompleter(edit_completer)

combo = QComboBox()
combo.setEditable(True)
combo_completer = TopicCompleter(combo)
combo_completer.update_topics(topic_completer_node)

# combo_completer.setCompletionMode(QCompleter.InlineCompletion)
combo.lineEdit().setCompleter(combo_completer)

model_tree = QTreeView()
model_tree.setModel(combo_completer.model())
model_tree.expandAll()
for column in range(combo_completer.model().columnCount()):
model_tree.resizeColumnToContents(column)

completion_tree = QTreeView()
completion_tree.setModel(combo_completer.completionModel())
completion_tree.expandAll()
for column in range(combo_completer.completionModel().columnCount()):
completion_tree.resizeColumnToContents(column)

layout.addWidget(model_tree)
layout.addWidget(completion_tree)
layout.addWidget(edit)
layout.addWidget(combo)
layout.setStretchFactor(model_tree, 1)
widget.setLayout(layout)
mw.setCentralWidget(widget)

mw.move(300, 0)
mw.resize(800, 900)
mw.show()
app.exec_()

topic_completer_node.destroy_node()
rclpy.shutdown()
from rclpy.executors import ExternalShutdownException

try:
with rclpy.init():
topic_completer_node = rclpy.create_node()

app = QApplication(sys.argv)
mw = QMainWindow()
widget = QWidget(mw)
layout = QVBoxLayout(widget)

edit = QLineEdit()
edit_completer = TopicCompleter(edit)
edit_completer.update_topics(topic_completer_node)
# edit_completer.setCompletionMode(QCompleter.InlineCompletion)
edit.setCompleter(edit_completer)

combo = QComboBox()
combo.setEditable(True)
combo_completer = TopicCompleter(combo)
combo_completer.update_topics(topic_completer_node)

# combo_completer.setCompletionMode(QCompleter.InlineCompletion)
combo.lineEdit().setCompleter(combo_completer)

model_tree = QTreeView()
model_tree.setModel(combo_completer.model())
model_tree.expandAll()
for column in range(combo_completer.model().columnCount()):
model_tree.resizeColumnToContents(column)

completion_tree = QTreeView()
completion_tree.setModel(combo_completer.completionModel())
completion_tree.expandAll()
for column in range(combo_completer.completionModel().columnCount()):
completion_tree.resizeColumnToContents(column)

layout.addWidget(model_tree)
layout.addWidget(completion_tree)
layout.addWidget(edit)
layout.addWidget(combo)
layout.setStretchFactor(model_tree, 1)
widget.setLayout(layout)
mw.setCentralWidget(widget)

mw.move(300, 0)
mw.resize(800, 900)
mw.show()
app.exec_()
except (KeyboardInterrupt, ExternalShutdownException):
pass
16 changes: 9 additions & 7 deletions rqt_py_common/src/rqt_py_common/topic_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

import rclpy

from rqt_py_common.message_helpers import get_message_class


Expand Down Expand Up @@ -78,8 +76,12 @@ def _recursive_create_field_dict(self, field_name, field):

if __name__ == '__main__':
import pprint
rclpy.init()
topic_dict_node = rclpy.create_node('topic_dict')
pprint.pprint(TopicDict(node=topic_dict_node).get_topics())
topic_dict_node.destroy_node()
rclpy.shutdown()
import rclpy
from rclpy.executors import ExternalShutdownException

try:
with rclpy.init():
topic_dict_node = rclpy.create_node('topic_dict')
pprint.pprint(TopicDict(node=topic_dict_node).get_topics())
except (KeyboardInterrupt, ExternalShutdownException):
pass

0 comments on commit 6ed041c

Please sign in to comment.