Browse Source

updating the label_collection is now threaded and tied into the progress bar

master
Christian Voigt 1 year ago
parent
commit
d63a97b18d
  1. 11
      main.py
  2. 29
      world_regions_widget/__init__.py
  3. 80
      world_regions_widget/region_label_collection.py

11
main.py

@ -39,11 +39,6 @@ class GuiTest(QMainWindow):
region_view.setStyleSheet("background-color: #cccccc;")
region_view_layout.setContentsMargins(0, 0, 0, 0)
world_regions_widget = WorldRegionsWidget()
region_view_layout.addWidget(
world_regions_widget
)
region_view.setLayout(region_view_layout)
region_view.setMinimumSize(QSize(region_view_width, region_view_width))
@ -60,6 +55,11 @@ class GuiTest(QMainWindow):
general_progress_bar.setAlignment(Qt.AlignCenter)
general_progress_bar.setMaximumWidth(staging_view_width)
world_regions_widget = WorldRegionsWidget(progress_bar=general_progress_bar)
region_view_layout.addWidget(
world_regions_widget
)
window_layout.addWidget(region_view)
window_layout.addWidget(staging_view)
@ -68,6 +68,7 @@ class GuiTest(QMainWindow):
application_frame_layout.addWidget(window)
application_frame.setLayout(application_frame_layout)
self.statusBar().addPermanentWidget(general_progress_bar)
self.statusBar()
self.setCentralWidget(application_frame)

29
world_regions_widget/__init__.py

@ -40,9 +40,10 @@ class WorldRegionsWidget(QScrollArea, ChraniCursors):
world_regions_image_raw_data = np.array
def __init__(self, zoom_factor=1.0, min_zoom_factor=0.5, max_zoom_factor=24.0, region_size=48):
def __init__(self, zoom_factor=1.0, min_zoom_factor=0.5, max_zoom_factor=24.0, region_size=48, progress_bar=None):
super().__init__()
self.world_loaded = False
self.progress_bar = progress_bar
self.min_zoom_factor = min_zoom_factor
self.max_zoom_factor = max_zoom_factor
@ -248,20 +249,26 @@ class WorldRegionsWidget(QScrollArea, ChraniCursors):
)[0]
if file_name:
length = int(math.sqrt(pathlib.Path(file_name).stat().st_size / 2))
self.regions_per_row = int(length / 512)
offset = int(32 / 2) - int(self.regions_per_row / 2)
self.load_world_file(file_name)
with open(file_name, 'r') as raw_file:
raw_data_array = np.fromfile(raw_file, dtype=np.uint16)
def load_world_file(self, file_name):
length = int(math.sqrt(pathlib.Path(file_name).stat().st_size / 2))
self.regions_per_row = int(length / 512)
offset = int(32 / 2) - int(self.regions_per_row / 2)
self.world_regions_image_raw_data = raw_data_array
with open(file_name, 'r') as raw_file:
raw_data_array = np.fromfile(raw_file, dtype=np.uint16)
self.region_label_collection.update_label_collection_with_raw_map_data(
raw_data_array, length, offset=offset
)
self.world_regions_image_raw_data = raw_data_array
self.region_label_collection.update_label_collection_with_raw_map_data(
raw_data_array, length, offset=offset
)
self.load_world_file_done(file_name)
self.world_loaded = True
def load_world_file_done(self, file_name):
self.world_loaded = True
def save_world_file_dialogue(self):
map_data = rearrange(self.region_label_collection.blockshaped_map_data, 'x y dx dy -> (x dx) (y dy)')

80
world_regions_widget/region_label_collection.py

@ -1,7 +1,9 @@
import numpy as np
from PyQt5.QtCore import QPoint
from PyQt5.QtCore import QPoint, QThread, pyqtSignal
from PyQt5.QtGui import QPixmap, QColor, QPainter, QImage, QCursor
from PyQt5.QtGui import (
QImage, QCursor
)
from PyQt5.QtWidgets import (
QGridLayout
)
@ -10,17 +12,55 @@ from einops import rearrange
from .region_label import RegionLabel
class WorkerUpdateLabelCollection(QThread):
finished = pyqtSignal()
progress = pyqtSignal(int)
def __init__(self, blockshaped_map_data, regions_per_row, offset, label_collection):
super(QThread, self).__init__()
self.blockshaped_map_data = blockshaped_map_data
self.regions_per_row = regions_per_row
self.offset = offset
self.label_collection = label_collection
def run(self):
progress_percentage = int(100 / self.regions_per_row)
for y in range(0, self.regions_per_row):
self.progress.emit(int(progress_percentage * y))
for x in range(0, self.regions_per_row):
self.queue_update_label_at(x, y, self.blockshaped_map_data[y][x].copy(), self.offset)
self.progress.emit(100)
self.finished.emit()
def queue_update_label_at(self, x, y, chunk_raw_data, offset=0):
chunk = QImage(chunk_raw_data, 512, 512, QImage.Format_Grayscale16)
grid_slot = RegionLabelCollection().create_region_string_from_grid_index(x=x + offset, y=y + offset)
region_label = self.label_collection.get(grid_slot)
self.finished_updating_label_at(region_label, chunk, chunk_raw_data)
def finished_updating_label_at(self, region_label, chunk, chunk_raw_data):
region_label.set_label_pixmap(chunk)
region_label.set_raw_data_array(chunk_raw_data)
region_label.mark_as_region()
class RegionLabelCollection(QGridLayout):
thread = QThread
worker = WorkerUpdateLabelCollection
label_collection = dict
currently_hovered_label = RegionLabel
blockshaped_map_data = np.array
def __init__(self, world_regions):
def __init__(self, world_regions=None):
super().__init__()
self.world_regions = world_regions
self.label_collection = {}
self.create_label_collection(self.world_regions.zoom_adjusted_label_size)
if world_regions is not None:
self.world_regions = world_regions
self.label_collection = {}
self.create_label_collection(self.world_regions.zoom_adjusted_label_size)
def set_currently_hovered_label(self, region_label):
self.currently_hovered_label = region_label
@ -134,14 +174,24 @@ class RegionLabelCollection(QGridLayout):
raw_map_data = np.flipud(np.reshape(raw_map_data, (length, length)))
self.blockshaped_map_data = rearrange(raw_map_data, '(x dx) (y dy) -> x y dx dy', dx=512, dy=512)
for y in range(0, regions_per_row):
for x in range(0, regions_per_row):
chunk_raw_data = self.blockshaped_map_data[y][x]
chunk = QImage(chunk_raw_data.copy(), 512, 512, QImage.Format_Grayscale16)
self.thread = QThread()
self.worker = WorkerUpdateLabelCollection(
self.blockshaped_map_data, regions_per_row, offset, self.label_collection
)
self.worker.moveToThread(self.thread)
self.thread.started.connect(self.worker.run)
self.worker.finished.connect(self.thread.quit)
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.start()
self.thread.finished.connect(
lambda: self.update_label_collection_with_raw_map_data_done()
)
grid_slot = self.create_region_string_from_grid_index(x=x + offset, y=y + offset)
def update_label_collection_with_raw_map_data_progress(self, percentage):
self.world_regions.progress_bar.setValue(percentage)
region_label = self.label_collection.get(grid_slot)
region_label.set_raw_data_array(chunk_raw_data)
region_label.mark_as_region()
region_label.set_label_pixmap(chunk)
def update_label_collection_with_raw_map_data_done(self):
self.world_regions.progress_bar.setValue(0)

Loading…
Cancel
Save