Browse Source

moved more stuff around for better thread-safety

I can not get it to crash anymore It's become slow though. Still, better slow than crashy, right?
master
Christian Voigt 1 year ago
parent
commit
082d857c9e
  1. 46
      world_regions_widget/layout_region_label_collection.py
  2. 24
      world_regions_widget/object_region_label.py

46
world_regions_widget/layout_region_label_collection.py

@ -18,34 +18,34 @@ from tools import (
class RegionLabelHelper(QObject):
update_region_raw_data_signal = pyqtSignal(np.ndarray)
update_region_collection_raw_data_signal = pyqtSignal(str, np.ndarray)
toggle_is_marked_signal = pyqtSignal(RegionLabel)
toggle_is_selected_signal = pyqtSignal(RegionLabel)
region_label_set_raw_data_array = pyqtSignal(str, np.ndarray)
class WorkerUpdateLabelCollection(QThread):
finished = pyqtSignal()
progress = pyqtSignal(int)
def __init__(self, blockshaped_map_data, label_collection):
def __init__(self, blockshaped_map_data, region_label_helper):
super(QThread, self).__init__()
self.blockshaped_map_data = blockshaped_map_data
self.regions_per_row = blockshaped_map_data.shape[0]
self.offset = int(32 / 2) - int(self.blockshaped_map_data.shape[0] / 2)
self.label_collection = label_collection
self.region_label_helper = region_label_helper
def run(self):
progress_percentage = (100 / self.regions_per_row) / self.regions_per_row
for y in range(0, self.regions_per_row):
for x in range(0, self.regions_per_row):
current_progress = int((y * (progress_percentage * self.regions_per_row)) + (x * progress_percentage))
self.progress.emit(current_progress)
grid_slot = create_region_string_from_grid_index(x=x + self.offset, y=y + self.offset)
region_label = self.label_collection.get(grid_slot)
region_label.set_raw_data_array(self.blockshaped_map_data[y][x].copy())
region_label.mark_as_region()
self.progress.emit(
int((y * (progress_percentage * self.regions_per_row)) + (x * progress_percentage))
)
self.region_label_helper.region_label_set_raw_data_array.emit(
create_region_string_from_grid_index(x=x + self.offset, y=y + self.offset),
self.blockshaped_map_data[y][x].copy()
)
self.progress.emit(100)
self.finished.emit()
@ -66,6 +66,8 @@ class RegionLabelCollection(QGridLayout):
world_file_helper=None
):
super(RegionLabelCollection, self).__init__()
self.thread = None
self.label_collection = {}
self.zoom_adjusted_label_size = zoom_adjusted_label_size
@ -95,7 +97,9 @@ class RegionLabelCollection(QGridLayout):
self.world_file_helper.load_world_file_signal.connect(self.load_world_file_slot)
self.world_file_helper.save_world_file_signal.connect(self.save_world_file_slot)
self.region_label_helper.update_region_raw_data_signal.connect(self.change_region_label_region_raw_data_slot)
self.region_label_helper.update_region_collection_raw_data_signal.connect(
self.change_region_label_region_raw_data_slot
)
self.region_label_helper.toggle_is_marked_signal.connect(self.change_region_label_toggle_marked_slot)
self.region_label_helper.toggle_is_selected_signal.connect(self.change_region_label_toggle_selected_slot)
@ -108,10 +112,8 @@ class RegionLabelCollection(QGridLayout):
self.currently_hovered_label = region_label
region_label.update_graphics_effect()
def change_region_label_region_raw_data_slot(self, raw_region_data: np.ndarray):
index_x, index_y = create_grid_index_from_region_string(
self.currently_hovered_label.region_string
)
def change_region_label_region_raw_data_slot(self, region_string: str, raw_region_data: np.ndarray):
index_x, index_y = create_grid_index_from_region_string(region_string)
offset = int(32 / 2) - int(self.blockshaped_map_data.shape[0] / 2)
self.blockshaped_map_data[index_y - offset][index_x - offset] = raw_region_data
@ -193,7 +195,7 @@ class RegionLabelCollection(QGridLayout):
self.thread = QThread()
self.worker = WorkerUpdateLabelCollection(
self.blockshaped_map_data.copy(), self.label_collection.copy()
self.blockshaped_map_data, self.region_label_helper
)
self.worker.moveToThread(self.thread)
self.thread.started.connect(self.worker.run)
@ -201,17 +203,15 @@ class RegionLabelCollection(QGridLayout):
self.worker.finished.connect(self.worker.deleteLater)
self.thread.finished.connect(self.thread.deleteLater)
self.worker.progress.connect(self.update_label_collection_with_raw_map_data_progress)
self.thread.finished.connect(self.update_label_collection_with_raw_map_data_done)
self.thread.start()
self.thread.finished.connect(
lambda: self.update_label_collection_with_raw_map_data_done()
)
def update_label_collection_with_raw_map_data_progress(self, percentage):
self.progress_bar_helper.progress_bar_percentage_signal.emit(percentage)
def update_label_collection_with_raw_map_data_done(self):
self.progress_bar_helper.progress_bar_percentage_signal.emit(0)
self.thread = None
def load_world_file_slot(self, file_name):
length = int(math.sqrt(pathlib.Path(file_name).stat().st_size / 2))
@ -219,9 +219,7 @@ class RegionLabelCollection(QGridLayout):
with open(file_name, 'r') as raw_file:
raw_data_array = np.fromfile(raw_file, dtype=np.uint16)
self.update_label_collection_with_raw_map_data(
raw_data_array, length
)
self.update_label_collection_with_raw_map_data(raw_data_array, length)
def save_world_file_slot(self, file_name):
map_data = rearrange(self.blockshaped_map_data, 'x y dx dy -> (x dx) (y dy)')

24
world_regions_widget/object_region_label.py

@ -60,6 +60,7 @@ class RegionLabel(QLabel):
self.action_save_region.triggered.connect(self.save_region_as)
self.action_replace_region.triggered.connect(self.load_region_from_file)
self.customContextMenuRequested.connect(self.on_context_menu)
self.region_label_helper.region_label_set_raw_data_array.connect(self.set_raw_data_array)
def init_ui(self):
self.setStatusTip(self.region_string)
@ -118,14 +119,12 @@ class RegionLabel(QLabel):
self.setPixmap(pixmap)
""" getter + setter"""
def set_raw_data_array(self, raw_data_array: np.array = None) -> None:
self.raw_data_array = raw_data_array
chunk = QImage(self.raw_data_array.copy(), 512, 512, QImage.Format_Grayscale16)
self.set_label_pixmap(chunk)
def mark_as_region(self) -> None:
self.is_region = True
def set_raw_data_array(self, region_string: str, raw_data_array: np.array = None) -> None:
if region_string == self.region_string:
self.raw_data_array = raw_data_array
chunk = QImage(self.raw_data_array.copy(), 512, 512, QImage.Format_Grayscale16)
self.set_label_pixmap(chunk)
self.is_region = True
def set_adjacent_marked_region_labels(self, adjacent_marked_region_labels) -> None:
self.adjacent_marked_region_labels = adjacent_marked_region_labels
@ -192,11 +191,10 @@ class RegionLabel(QLabel):
with open(file_name, 'r') as raw_file:
raw_data_array = np.fromfile(raw_file, dtype=np.uint16)
self.set_raw_data_array(np.flipud(np.reshape(raw_data_array, (length, length))))
chunk = QImage(self.raw_data_array.copy(), 512, 512, QImage.Format_Grayscale16)
self.set_label_pixmap(chunk)
self.region_label_helper.update_region_raw_data_signal.emit(self.raw_data_array)
self.set_raw_data_array(self.region_string, np.flipud(np.reshape(raw_data_array, (length, length))))
self.region_label_helper.update_region_collection_raw_data_signal.emit(
self.region_string, self.raw_data_array
)
""" altering class functions """
def mouse_any_click_event(self, event):

Loading…
Cancel
Save