Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python向けのlinter/formatterをruffで統一する #662

Merged
merged 9 commits into from
Dec 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,12 @@ repos:
# hooks:
# - id: isort

- repo: https://github.com/psf/black
rev: 24.10.0
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.8.4
hooks:
- id: black
args: [--line-length=99, --skip-string-normalization]
- id: ruff
args: [--fix]
- id: ruff-format

- repo: https://github.com/pre-commit/mirrors-clang-format
rev: v17.0.6
Expand Down
2 changes: 1 addition & 1 deletion consai_ros2/consai_visualizer/package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<exec_depend>rqt_py_common</exec_depend>

<test_depend>ament_lint_auto</test_depend>
<test_depend>ament_lint_common</test_depend>
<test_depend>crane_lint_common</test_depend>

<export>
<rqt_gui plugin="${prefix}/plugin.xml"/>
Expand Down
79 changes: 53 additions & 26 deletions consai_ros2/consai_visualizer/src/consai_visualizer/field_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@


class FieldWidget(QWidget):

def __init__(self, parent=None):
super(FieldWidget, self).__init__(parent)

Expand All @@ -62,7 +61,9 @@ def __init__(self, parent=None):
self._draw_area_size = QSizeF(self.rect().size()) # 描画領域サイズ
self._scale_field_to_draw = 1.0 # フィールド領域から描画領域に縮小するスケール
self._do_rotate_draw_area = False # 描画領域を90度回転するフラグ
self._mouse_clicked_point = QPointF(0.0, 0.0) # マウスでクリックした描画領域の座標
self._mouse_clicked_point = QPointF(
0.0, 0.0
) # マウスでクリックした描画領域の座標
self._mouse_current_point = QPointF(0.0, 0.0) # マウスカーソルの現在座標
self._mouse_drag_offset = QPointF(0.0, 0.0) # マウスでドラッグした距離
self._previous_update_time = datetime.datetime.now() # 前回の描画時刻
Expand All @@ -77,7 +78,9 @@ def set_invert(self, param):
self._invert = param

def set_visualizer_objects(self, msg):
self._visualizer_objects.setdefault(msg.z_order, {})[(msg.layer, msg.sub_layer)] = msg
self._visualizer_objects.setdefault(msg.z_order, {})[
(msg.layer, msg.sub_layer)
] = msg

def set_active_layers(self, layers: list[tuple[str, str]]):
self._active_layers = layers
Expand Down Expand Up @@ -167,7 +170,7 @@ def paintEvent(self, event):
painter = QPainter(self)

# 背景色をセット
painter.setBrush(QColor('darkgreen'))
painter.setBrush(QColor("darkgreen"))
painter.drawRect(self.rect())

painter.save()
Expand All @@ -184,7 +187,7 @@ def paintEvent(self, event):
if self._do_rotate_draw_area is True:
painter.rotate(-90)

draw_caption = ('caption', 'caption') in self._active_layers
draw_caption = ("caption", "caption") in self._active_layers
self._draw_objects_on_transformed_area(painter, draw_caption)
self._draw_visualizer_info_on_transformed_area(painter)

Expand Down Expand Up @@ -228,13 +231,19 @@ def _resize_draw_area(self):
else:
# 描画回転にヒステリシスをもたせる
if self._do_rotate_draw_area is True:
self._draw_area_size = QSizeF(widget_height, widget_height * field_h_per_w)
self._draw_area_size = QSizeF(
widget_height, widget_height * field_h_per_w
)
else:
self._draw_area_size = QSizeF(widget_width, widget_width * field_h_per_w)
self._draw_area_size = QSizeF(
widget_width, widget_width * field_h_per_w
)

self._scale_field_to_draw = self._draw_area_size.width() / field_full_width

def _draw_text(self, painter: QPainter, pos: QPointF, text: str, font_size: int = 10):
def _draw_text(
self, painter: QPainter, pos: QPointF, text: str, font_size: int = 10
):
# 回転を考慮したテキスト描画関数
painter.save()
font = painter.font()
Expand All @@ -261,7 +270,9 @@ def _to_qcolor(self, color: VisColor):
output.setAlphaF(color.alpha)
return output

def _draw_objects_on_transformed_area(self, painter: QPainter, draw_caption: bool = False):
def _draw_objects_on_transformed_area(
self, painter: QPainter, draw_caption: bool = False
):
# 描画領域の移動や拡大を考慮した座標系でオブジェクトを描画する
for z_order in sorted(self._visualizer_objects):
for active_layer in self._active_layers:
Expand Down Expand Up @@ -312,13 +323,15 @@ def _draw_visualizer_info_on_transformed_area(self, painter: QPainter):
drag_line.p2.x = current_point.x()
drag_line.p2.y = current_point.y()
drag_line.size = 4
drag_line.color.name = 'lightsalmon'
drag_line.caption = 'dist: {:.1f} : {:.1f}, theta: {:.1f}'.format(
drag_line.color.name = "lightsalmon"
drag_line.caption = "dist: {:.1f} : {:.1f}, theta: {:.1f}".format(
distance.x(), distance.y(), theta_deg
)
self._draw_shape_line(painter, drag_line, True)

def _draw_objects_on_window_area(self, painter: QPainter, draw_caption: bool = False):
def _draw_objects_on_window_area(
self, painter: QPainter, draw_caption: bool = False
):
# ウィンドウ領域にオブジェクトを描画する
for z_order in sorted(self._visualizer_objects):
for active_layer in self._active_layers:
Expand All @@ -335,25 +348,27 @@ def _draw_visualizer_info_on_window_area(self, painter: QPainter):
# フレームレートを描画
time_diff = datetime.datetime.now() - self._previous_update_time
self._frame_rate_buffer.append(1.0 / time_diff.total_seconds())
average_frame_rate = sum(self._frame_rate_buffer) / self._frame_rate_buffer.maxlen
average_frame_rate = (
sum(self._frame_rate_buffer) / self._frame_rate_buffer.maxlen
)
self._previous_update_time = datetime.datetime.now()
annotation = ShapeAnnotation()
annotation.text = 'FPS: {:.1f}'.format(average_frame_rate)
annotation.text = "FPS: {:.1f}".format(average_frame_rate)
annotation.normalized_x = 0.0
annotation.normalized_y = 0.95
annotation.normalized_width = 0.1
annotation.normalized_height = 0.05
annotation.color.name = 'white'
annotation.color.name = "white"
self._draw_shape_annotation(painter, annotation)

# カーソル位置を描画
cursor_pos = self._convert_draw_to_field_pos(self._mouse_current_point)
if self._invert:
annotation.text = 'inv'
annotation.color.name = 'lightcoral'
annotation.text = "inv"
annotation.color.name = "lightcoral"
else:
annotation.text = 'pos'
annotation.text += ' {:.2f} : {:.2f}'.format(cursor_pos.x(), cursor_pos.y())
annotation.text = "pos"
annotation.text += " {:.2f} : {:.2f}".format(cursor_pos.x(), cursor_pos.y())
annotation.normalized_x = 0.1
self._draw_shape_annotation(painter, annotation)

Expand All @@ -365,15 +380,17 @@ def _draw_shape_annotation(
TARGET_WIDTH = shape.normalized_width * self.width()
TARGET_HEIGHT = shape.normalized_height * self.height()

if shape.text == '':
if shape.text == "":
return

painter.save()

# TARGET_HEIGHTに合わせてフォントサイズを変更する
font = painter.font()
font_metrics = QFontMetrics(font)
height_fit_point_size = font.pointSizeF() * TARGET_HEIGHT / font_metrics.height()
height_fit_point_size = (
font.pointSizeF() * TARGET_HEIGHT / font_metrics.height()
)
font.setPointSizeF(height_fit_point_size)
font_metrics = QFontMetrics(font)

Expand All @@ -396,15 +413,19 @@ def _draw_shape_annotation(

painter.restore()

def _draw_shape_point(self, painter: QPainter, shape: ShapePoint, draw_caption: bool = False):
def _draw_shape_point(
self, painter: QPainter, shape: ShapePoint, draw_caption: bool = False
):
painter.setPen(QPen(self._to_qcolor(shape.color), shape.size))
point = self._convert_field_to_draw_point(shape.x, shape.y)
painter.drawPoint(point)

if draw_caption:
self._draw_text(painter, point, shape.caption)

def _draw_shape_line(self, painter: QPainter, shape: ShapeLine, draw_caption: bool = False):
def _draw_shape_line(
self, painter: QPainter, shape: ShapeLine, draw_caption: bool = False
):
painter.setPen(QPen(self._to_qcolor(shape.color), shape.size))
p1 = self._convert_field_to_draw_point(shape.p1.x, shape.p1.y)
p2 = self._convert_field_to_draw_point(shape.p2.x, shape.p2.y)
Expand All @@ -417,7 +438,9 @@ def _draw_shape_line(self, painter: QPainter, shape: ShapeLine, draw_caption: bo
)
self._draw_text(painter, p_mid, shape.caption)

def _draw_shape_arc(self, painter: QPainter, shape: ShapeArc, draw_caption: bool = False):
def _draw_shape_arc(
self, painter: QPainter, shape: ShapeArc, draw_caption: bool = False
):
painter.setPen(QPen(self._to_qcolor(shape.color), shape.size))

top_left = self._convert_field_to_draw_point(
Expand Down Expand Up @@ -476,7 +499,9 @@ def _draw_shape_circle(
)
self._draw_text(painter, bottom, shape.caption)

def _draw_shape_tube(self, painter: QPainter, shape: ShapeTube, draw_caption: bool = False):
def _draw_shape_tube(
self, painter: QPainter, shape: ShapeTube, draw_caption: bool = False
):
painter.setPen(QPen(self._to_qcolor(shape.line_color), shape.line_size))
painter.setBrush(self._to_qcolor(shape.fill_color))

Expand Down Expand Up @@ -524,7 +549,9 @@ def _draw_shape_tube(self, painter: QPainter, shape: ShapeTube, draw_caption: bo
if draw_caption:
self._draw_text(painter, bottom_left, shape.caption)

def _draw_shape_robot(self, painter: QPainter, shape: ShapeRobot, draw_caption: bool = False):
def _draw_shape_robot(
self, painter: QPainter, shape: ShapeRobot, draw_caption: bool = False
):
painter.setPen(QPen(self._to_qcolor(shape.line_color), shape.line_size))
painter.setBrush(self._to_qcolor(shape.fill_color))

Expand Down
Loading
Loading