|
| 1 | +from pathlib import Path |
| 2 | + |
| 3 | +from ament_index_python.packages import get_package_share_directory |
| 4 | +from PyQt6.QtCore import Qt, QTimer |
| 5 | +from PyQt6.QtGui import QColor, QFont, QPainter, QPen, QPixmap |
| 6 | +from PyQt6.QtWidgets import QComboBox, QHBoxLayout, QLabel, QPushButton, QVBoxLayout, QWidget |
| 7 | + |
| 8 | +START_YEAR = 2016 |
| 9 | +END_YEAR = 2025 |
| 10 | + |
| 11 | + |
| 12 | +class AnimationLabel(QLabel): |
| 13 | + def __init__(self, year_list: list[int], interval: int = 2000) -> None: |
| 14 | + super().__init__() |
| 15 | + self.year = START_YEAR |
| 16 | + self.year_list = year_list |
| 17 | + |
| 18 | + map_path = str( |
| 19 | + Path(get_package_share_directory('gui')) / 'images' / 'illinois_river_map_0.png' |
| 20 | + ) |
| 21 | + map_pixmap = QPixmap(map_path) |
| 22 | + self.setPixmap( |
| 23 | + map_pixmap.scaled( |
| 24 | + 750, |
| 25 | + 750, |
| 26 | + Qt.AspectRatioMode.KeepAspectRatio, |
| 27 | + Qt.TransformationMode.SmoothTransformation, |
| 28 | + ) |
| 29 | + ) |
| 30 | + |
| 31 | + self.timer = QTimer(self) |
| 32 | + self.timer.timeout.connect(self.update_frame) |
| 33 | + self.timer.start(interval) # in milliseconds |
| 34 | + |
| 35 | + def update_frame(self) -> None: |
| 36 | + region = 0 |
| 37 | + for year in self.year_list: |
| 38 | + if self.year >= year: |
| 39 | + region += 1 |
| 40 | + |
| 41 | + map_path = str( |
| 42 | + Path(get_package_share_directory('gui')) |
| 43 | + / 'images' |
| 44 | + / ('illinois_river_map_' + str(region) + '.png') |
| 45 | + ) |
| 46 | + |
| 47 | + map_pixmap = QPixmap(map_path) |
| 48 | + |
| 49 | + painter = QPainter(map_pixmap) |
| 50 | + pen = QPen(QColor('black')) |
| 51 | + painter.setFont(QFont('Arial', 50)) |
| 52 | + painter.setPen(pen) |
| 53 | + painter.drawText(20, 60, str(self.year)) |
| 54 | + painter.end() |
| 55 | + self.setPixmap( |
| 56 | + map_pixmap.scaled( |
| 57 | + 750, |
| 58 | + 750, |
| 59 | + Qt.AspectRatioMode.KeepAspectRatio, |
| 60 | + Qt.TransformationMode.SmoothTransformation, |
| 61 | + ) |
| 62 | + ) |
| 63 | + |
| 64 | + self.year += 1 |
| 65 | + self.year = min(self.year, END_YEAR) |
| 66 | + |
| 67 | + |
| 68 | +class CarpAnimation(QWidget): |
| 69 | + def __init__(self) -> None: |
| 70 | + super().__init__() |
| 71 | + |
| 72 | + self.root_layout = QHBoxLayout() |
| 73 | + |
| 74 | + input_layout = QVBoxLayout() |
| 75 | + |
| 76 | + self.dropdown_list: list[QComboBox] = [] |
| 77 | + |
| 78 | + for _ in range(5): |
| 79 | + dropdown = QComboBox() |
| 80 | + # List of years [2016,2025] |
| 81 | + options = list(map(str, range(2016, 2026))) |
| 82 | + options.append('9999') |
| 83 | + dropdown.addItems(options) |
| 84 | + dropdown.setCurrentIndex(len(options) - 1) |
| 85 | + self.dropdown_list.append(dropdown) |
| 86 | + input_layout.addWidget(dropdown) |
| 87 | + |
| 88 | + self.setLayout(self.root_layout) |
| 89 | + |
| 90 | + show_button = QPushButton('Show Animation', None) |
| 91 | + |
| 92 | + input_layout.addWidget(show_button) |
| 93 | + input_layout.addStretch() |
| 94 | + |
| 95 | + self.root_layout.addLayout(input_layout) |
| 96 | + |
| 97 | + show_button.clicked.connect(self.show_animation) |
| 98 | + |
| 99 | + self.animation_label: AnimationLabel | None = None |
| 100 | + |
| 101 | + def show_animation(self) -> None: |
| 102 | + self.root_layout.removeWidget(self.animation_label) |
| 103 | + self.year_list = [int(dropdown.currentText()) for dropdown in self.dropdown_list] |
| 104 | + self.animation_label = AnimationLabel(self.year_list) |
| 105 | + self.root_layout.addWidget(self.animation_label) |
0 commit comments