Browse Source

some more optimizations. trying to remove all cross class variable-passing. still have a few crashes I blame thread-safety for :)

well, that pretty much still sums it up. Moved some functions around to reduce calls.
moved stuff around so they make more sense and are more atomic
unified code-appearance between files a bit
master
Christian Voigt 1 year ago
parent
commit
450b0b25fa
  1. 85
      main.py
  2. 96
      world_regions_widget/__init__.py
  3. 108
      world_regions_widget/layout_region_label_collection.py
  4. 122
      world_regions_widget/object_region_label.py
  5. 13
      world_regions_widget/tools.py

85
main.py

@ -1,7 +1,7 @@
import sys
from PyQt5.QtCore import (
Qt, QSettings, QByteArray, QSize, QObject, pyqtSignal, QPoint
Qt, QSettings, QByteArray, QSize, QObject, pyqtSignal, QPoint, QThread
)
from PyQt5.QtGui import QCursor
from PyQt5.QtWidgets import (
@ -13,11 +13,31 @@ from PyQt5.QtWidgets import (
from cursors import ChraniCursors
from world_regions_widget import WorldRegionsWidget
from tools import shift_is_pressed, control_is_pressed
from region_label import RegionLabel
from object_region_label import RegionLabel
class ProgressBarHelper(QObject):
class ProgressBarHelper(QThread):
progress_bar_percentage_signal = pyqtSignal(int)
finished = pyqtSignal()
progress_bar = QProgressBar
def __init__(self):
super(QThread, self).__init__()
general_progress_bar = QProgressBar()
general_progress_bar.setRange(0, 100)
general_progress_bar.setValue(0)
general_progress_bar.setAlignment(Qt.AlignCenter)
self.progress_bar_percentage_signal.connect(
self.progress_bar_percentage_slot
)
self.progress_bar = general_progress_bar
def set_maximum_size(self, size: QSize):
self.progress_bar.setMaximumSize(size)
def progress_bar_percentage_slot(self, percentage: int):
self.progress_bar.setValue(percentage)
class MouseCursorHelper(QObject):
@ -26,15 +46,27 @@ class MouseCursorHelper(QObject):
center_label_signal = pyqtSignal()
class GuiTest(QMainWindow, ChraniCursors):
class ChraniMapTools(QMainWindow, ChraniCursors):
setting = QSettings
def __init__(self):
super().__init__()
super(ChraniMapTools, self).__init__()
self.settings = QSettings('chrani_map_tools', '0.1.6')
# the common user enjoys a bit of control. so why force window sizes on them, eh?
self.read_window_settings()
self.mouse_cursor_helper = MouseCursorHelper()
self.progress_bar_helper = ProgressBarHelper()
self.init_signals()
self.init_ui()
def init_signals(self):
self.mouse_cursor_helper.mouse_cursor_change_signal.connect(self.mouse_cursor_change_slot)
self.mouse_cursor_helper.hovered_label_signal.connect(self.hovered_label_slot)
self.mouse_cursor_helper.center_label_signal.connect(self.center_label_slot)
def init_ui(self):
application_frame = QWidget()
application_frame_layout = QVBoxLayout()
@ -44,9 +76,6 @@ class GuiTest(QMainWindow, ChraniCursors):
window_layout = QHBoxLayout()
window_layout.setContentsMargins(0, 0, 0, 0)
# the common user enjoys a bit of control. so why force window sizes on them, eh?
self.read_window_settings()
region_view_width = 512
region_view = QWidget()
region_view_layout = QVBoxLayout()
@ -63,22 +92,12 @@ class GuiTest(QMainWindow, ChraniCursors):
staging_view.setMinimumWidth(staging_view_width)
staging_view.setLayout(staging_view_layout)
general_progress_bar = QProgressBar()
general_progress_bar.setRange(0, 100)
general_progress_bar.setValue(0)
general_progress_bar.setAlignment(Qt.AlignCenter)
general_progress_bar.setMaximumWidth(staging_view_width)
mouse_cursor_helper = MouseCursorHelper()
mouse_cursor_helper.mouse_cursor_change_signal.connect(self.mouse_cursor_change_slot)
mouse_cursor_helper.hovered_label_signal.connect(self.hovered_label_slot)
mouse_cursor_helper.center_label_signal.connect(self.center_label_slot)
progress_bar_helper = ProgressBarHelper()
progress_bar_helper.progress_bar_percentage_signal.connect(self.progress_bar_percentage_slot)
self.general_progress_bar = general_progress_bar
self.progress_bar_helper.set_maximum_size(QSize(staging_view_width, 12))
self.statusBar().addPermanentWidget(self.progress_bar_helper.progress_bar)
world_regions_widget = WorldRegionsWidget(
progress_bar_helper=progress_bar_helper, mouse_cursor_helper=mouse_cursor_helper
progress_bar_helper=self.progress_bar_helper,
mouse_cursor_helper=self.mouse_cursor_helper
)
region_view_layout.addWidget(
world_regions_widget
@ -92,9 +111,6 @@ class GuiTest(QMainWindow, ChraniCursors):
application_frame_layout.addWidget(window)
application_frame.setLayout(application_frame_layout)
self.statusBar().addPermanentWidget(general_progress_bar)
self.statusBar()
self.setCentralWidget(application_frame)
def center_label_slot(self):
@ -103,9 +119,6 @@ class GuiTest(QMainWindow, ChraniCursors):
int(self.currently_hovered_label.height() / 2)
)))
def progress_bar_percentage_slot(self, percentage: int):
self.general_progress_bar.setValue(percentage)
def hovered_label_slot(self, region_label: RegionLabel):
self.currently_hovered_label = region_label
@ -160,22 +173,22 @@ class GuiTest(QMainWindow, ChraniCursors):
self.store_window_settings()
# This here function will print out silent exceptions. Nothing worse than the IDE going to console without any clue
# about whats wrong
def exception_hook(exctype, value, traceback):
def my_exception_hook(exctype, value, traceback):
print(exctype, value, traceback)
sys.excepthook(exctype, value, traceback)
sys._excepthook(exctype, value, traceback)
sys.exit(1)
sys._excepthook = sys.excepthook # Back-up the reference to the exception hook
sys.excepthook = my_exception_hook
def main():
app = QApplication(sys.argv)
gt = GuiTest()
gt = ChraniMapTools()
gt.show()
sys.exit(app.exec())
app.exec()
if __name__ == '__main__':
sys.excepthook = exception_hook
main()

96
world_regions_widget/__init__.py

@ -1,7 +1,3 @@
import math
import pathlib
import numpy as np
from PyQt5.QtCore import (
Qt, QPoint, QObject, pyqtSignal
)
@ -14,32 +10,23 @@ from PyQt5.QtWidgets import (
QFileDialog
)
from einops import rearrange
from region_label import RegionLabel
from .region_label_collection import RegionLabelCollection
from object_region_label import RegionLabel
from layout_region_label_collection import RegionLabelCollection
from tools import (
create_grid_index_from_region_string,
shift_is_pressed, control_is_pressed
)
class RegionLabelHelper(QObject):
change_region_label_region_raw_data_signal = pyqtSignal(np.ndarray)
change_region_label_toggle_marked_signal = pyqtSignal(RegionLabel)
change_region_label_toggle_selected_signal = pyqtSignal(RegionLabel)
class WorldFileHelper(QObject):
load_world_file_signal = pyqtSignal(str)
save_world_file_signal = pyqtSignal(str)
class WorldRegionsWidget(QScrollArea):
root_dir = str
min_zoom_factor = float
max_zoom_factor = float
region_size = int
regions_per_row = int
world_loaded = bool
world_label_zoom_factor = float
world_label_region_size = float
@ -49,14 +36,11 @@ class WorldRegionsWidget(QScrollArea):
action_open_world_file = QAction
action_save_world_file = QAction
world_regions_image_raw_data = np.array
def __init__(
self, zoom_factor=1.0, min_zoom_factor=1.0, max_zoom_factor=32.0, region_size=16,
progress_bar_helper=None, mouse_cursor_helper=None
):
super().__init__()
self.world_loaded = False
super(WorldRegionsWidget, self).__init__()
self.min_zoom_factor = min_zoom_factor
self.max_zoom_factor = max_zoom_factor
@ -66,30 +50,32 @@ class WorldRegionsWidget(QScrollArea):
self.drag_in_progress = False
self.drag_start = QPoint(0, 0)
region_label_helper = RegionLabelHelper()
self.progress_bar_helper = progress_bar_helper
self.mouse_cursor_helper = mouse_cursor_helper
self.world_file_helper = WorldFileHelper()
self.init_actions()
self.init_signals()
self.init_ui()
def init_ui(self):
self.region_label_collection = RegionLabelCollection(
zoom_adjusted_label_size=self.world_label_zoom_factor * self.world_label_region_size,
progress_bar_helper=progress_bar_helper, mouse_cursor_helper=mouse_cursor_helper,
region_label_helper=region_label_helper
progress_bar_helper=self.progress_bar_helper, mouse_cursor_helper=self.mouse_cursor_helper,
world_file_helper=self.world_file_helper
)
self.progress_bar_helper = progress_bar_helper
self.mouse_cursor_helper = mouse_cursor_helper
self.region_label_helper = region_label_helper
self.region_label_collection.setSpacing(0)
self.region_label_collection.setContentsMargins(0, 0, 0, 0)
region_label_collection_wrapper_widget = QWidget()
region_label_collection_wrapper_widget.setLayout(self.region_label_collection)
mouse_cursor_helper.hovered_label_signal.connect(self.hovered_label_slot)
self.setAlignment(Qt.AlignCenter)
self.setFrameShape(QFrame.NoFrame)
self.setWidget(region_label_collection_wrapper_widget)
self.init_actions()
def init_signals(self):
self.mouse_cursor_helper.hovered_label_signal.connect(self.hovered_label_slot)
self.action_open_world_file.triggered.connect(self.open_world_file_dialogue)
self.action_save_world_file.triggered.connect(self.save_world_file_dialogue)
def hovered_label_slot(self, region_label: RegionLabel):
self.currently_hovered_label = region_label
@ -99,17 +85,15 @@ class WorldRegionsWidget(QScrollArea):
open_world_file.setShortcut(
QKeySequence(Qt.ControlModifier | Qt.Key_O)
)
open_world_file.triggered.connect(self.open_world_file_dialogue)
self.addAction(open_world_file)
self.action_open_world_file = open_world_file
self.addAction(self.action_open_world_file)
save_world_file = QAction()
save_world_file.setShortcut(
QKeySequence(Qt.ControlModifier | Qt.Key_S)
)
save_world_file.triggered.connect(self.save_world_file_dialogue)
self.addAction(save_world_file)
self.action_save_world_file = save_world_file
self.addAction(self.action_save_world_file)
def action_zoom_in(self):
self.world_label_zoom_factor += self.min_zoom_factor
@ -166,7 +150,7 @@ class WorldRegionsWidget(QScrollArea):
else:
self.drag_in_progress = False
super().mousePressEvent(mouse_event)
super(WorldRegionsWidget, self).mousePressEvent(mouse_event)
def mouseReleaseEvent(self, mouse_event):
if mouse_event.button() == Qt.LeftButton:
@ -175,7 +159,7 @@ class WorldRegionsWidget(QScrollArea):
if mouse_event.button() == Qt.RightButton:
self.mouse_cursor_helper.mouse_cursor_change_signal.emit(None)
super().mouseReleaseEvent(mouse_event)
super(WorldRegionsWidget, self).mouseReleaseEvent(mouse_event)
def mouseMoveEvent(self, mouse_event):
if self.drag_in_progress:
@ -189,7 +173,7 @@ class WorldRegionsWidget(QScrollArea):
mouse_event.x(), mouse_event.y()
)
super().mouseMoveEvent(mouse_event)
super(WorldRegionsWidget, self).mouseMoveEvent(mouse_event)
def wheelEvent(self, mouse_event):
if mouse_event.angleDelta().y() > 0:
@ -208,12 +192,12 @@ class WorldRegionsWidget(QScrollArea):
]):
self.mouse_cursor_helper.mouse_cursor_change_signal.emit(key_event)
super().keyPressEvent(key_event)
super(WorldRegionsWidget, self).keyPressEvent(key_event)
def keyReleaseEvent(self, key_event):
self.mouse_cursor_helper.mouse_cursor_change_signal.emit(key_event)
super().keyReleaseEvent(key_event)
super(WorldRegionsWidget, self).keyReleaseEvent(key_event)
# file operations
def open_world_file_dialogue(self):
@ -223,34 +207,12 @@ class WorldRegionsWidget(QScrollArea):
)[0]
if file_name:
self.load_world_file(file_name)
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)
with open(file_name, 'r') as raw_file:
raw_data_array = np.fromfile(raw_file, dtype=np.uint16)
self.world_regions_image_raw_data = raw_data_array
self.region_label_collection.update_label_collection_with_raw_map_data(
raw_data_array, length
)
self.load_world_file_done(file_name)
def load_world_file_done(self, file_name):
self.world_loaded = True
self.world_file_helper.load_world_file_signal.emit(file_name)
def save_world_file_dialogue(self):
map_data = rearrange(self.region_label_collection.blockshaped_map_data, 'x y dx dy -> (x dx) (y dy)')
options = QFileDialog.Options()
file_name, _ = QFileDialog.getSaveFileName(
self, caption='save the world', filter='RAW files (*.raw)', options=options
)
if file_name:
with open(file_name, 'w+b') as raw_file:
raw_file.write(
bytearray(np.flipud(map_data))
)
self.world_file_helper.save_world_file_signal.emit(file_name)

108
world_regions_widget/region_label_collection.py → world_regions_widget/layout_region_label_collection.py

@ -1,22 +1,28 @@
import math
import pathlib
import numpy as np
from PyQt5.QtCore import (
QThread, pyqtSignal
)
from PyQt5.QtGui import (
QImage
QThread, pyqtSignal, QObject
)
from PyQt5.QtWidgets import (
QGridLayout
)
from einops import rearrange
from .region_label import RegionLabel
from object_region_label import RegionLabel
from tools import (
create_grid_index_from_region_string, create_region_string_from_grid_index
)
class RegionLabelHelper(QObject):
update_region_raw_data_signal = pyqtSignal(np.ndarray)
toggle_is_marked_signal = pyqtSignal(RegionLabel)
toggle_is_selected_signal = pyqtSignal(RegionLabel)
class WorkerUpdateLabelCollection(QThread):
finished = pyqtSignal()
progress = pyqtSignal(int)
@ -32,23 +38,17 @@ class WorkerUpdateLabelCollection(QThread):
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):
self.progress.emit(int((y * (progress_percentage * self.regions_per_row)) + (x * progress_percentage)))
self.queue_update_label_at(x, y, self.blockshaped_map_data[y][x].copy(), self.offset)
current_progress = int((y * (progress_percentage * self.regions_per_row)) + (x * progress_percentage))
self.progress.emit(current_progress)
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 = create_region_string_from_grid_index(x=x + offset, y=y + offset)
region_label = self.label_collection.get(grid_slot)
grid_slot = create_region_string_from_grid_index(x=x + self.offset, y=y + self.offset)
region_label = self.label_collection.get(grid_slot)
self.finished_updating_label_at(region_label, chunk, chunk_raw_data)
region_label.set_raw_data_array(self.blockshaped_map_data[y][x].copy())
region_label.mark_as_region()
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()
self.progress.emit(100)
self.finished.emit()
class RegionLabelCollection(QGridLayout):
@ -63,29 +63,43 @@ class RegionLabelCollection(QGridLayout):
def __init__(
self, zoom_adjusted_label_size=None,
progress_bar_helper=None, mouse_cursor_helper=None,
region_label_helper=None
world_file_helper=None
):
super().__init__()
super(RegionLabelCollection, self).__init__()
self.label_collection = {}
self.zoom_adjusted_label_size = zoom_adjusted_label_size
self.progress_bar_helper = progress_bar_helper
self.mouse_cursor_helper = mouse_cursor_helper
self.region_label_helper = region_label_helper
mouse_cursor_helper.hovered_label_signal.connect(self.hovered_label_slot)
region_label_helper.change_region_label_region_raw_data_signal.connect(
self.change_region_label_region_raw_data_slot
)
self.world_file_helper = world_file_helper
self.region_label_helper = RegionLabelHelper()
region_label_helper.change_region_label_toggle_marked_signal.connect(
self.change_region_label_toggle_marked_slot
)
self.init_signals()
self.init_ui()
region_label_helper.change_region_label_toggle_selected_signal.connect(
self.change_region_label_toggle_selected_slot
)
def init_ui(self):
self.setSpacing(0)
self.setContentsMargins(0, 0, 0, 0)
self.create_label_collection(zoom_adjusted_label_size)
for y in range(0, 32):
for x in range(0, 32):
self.create_label(
x=x, y=y,
dimensions=self.zoom_adjusted_label_size, mouse_cursor_helper=self.mouse_cursor_helper,
region_label_helper=self.region_label_helper
)
def init_signals(self):
self.mouse_cursor_helper.hovered_label_signal.connect(self.hovered_label_slot)
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)
def hovered_label_slot(self, region_label):
self.region_label_helper.update_region_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)
def hovered_label_slot(self, region_label: RegionLabel):
for index, label in self.label_collection.items():
if region_label is not label:
label.remove_graphics_effects()
@ -148,15 +162,6 @@ class RegionLabelCollection(QGridLayout):
return adjacent_marked_region_labels
def create_label_collection(self, label_size):
for y in range(0, 32):
for x in range(0, 32):
self.create_label(
x=x, y=y,
dimensions=label_size, mouse_cursor_helper=self.mouse_cursor_helper,
region_label_helper=self.region_label_helper
)
def create_label(self, x=None, y=None, dimensions=None, mouse_cursor_helper=None, region_label_helper=None):
label = RegionLabel(
x=x, y=y,
@ -207,3 +212,20 @@ class RegionLabelCollection(QGridLayout):
def update_label_collection_with_raw_map_data_done(self):
self.progress_bar_helper.progress_bar_percentage_signal.emit(0)
def load_world_file_slot(self, file_name):
length = int(math.sqrt(pathlib.Path(file_name).stat().st_size / 2))
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
)
def save_world_file_slot(self, file_name):
map_data = rearrange(self.blockshaped_map_data, 'x y dx dy -> (x dx) (y dy)')
with open(file_name, 'w+b') as raw_file:
raw_file.write(
bytearray(np.flipud(map_data))
)

122
world_regions_widget/region_label.py → world_regions_widget/object_region_label.py

@ -13,7 +13,7 @@ from PyQt5.QtWidgets import (
)
from tools import (
create_region_string_from_grid_index, toggle_open_in_context_menu
create_region_string_from_grid_index, toggle_open_in_context_menu, get_color_by_coordinates
)
@ -31,11 +31,13 @@ class RegionLabel(QLabel):
adjacent_marked_region_labels = dict
action_save_region = QAction
action_replace_region = QAction
def __init__(
self,
x=None, y=None, dimensions=QSize(512, 512),
mouse_cursor_helper=None,
region_label_helper=None
self,
x=None, y=None, dimensions=QSize(512, 512),
mouse_cursor_helper=None, region_label_helper=None
):
super(RegionLabel, self).__init__()
self.x, self.y = x, y
@ -43,8 +45,6 @@ class RegionLabel(QLabel):
self.mouse_cursor_helper = mouse_cursor_helper
self.region_label_helper = region_label_helper
pixmap = QPixmap(dimensions)
self.is_marked = False
self.is_region = False
self.context_menu_is_shown = False
@ -52,42 +52,37 @@ class RegionLabel(QLabel):
self.region_string = create_region_string_from_grid_index(x=x, y=y)
self.setStatusTip(self.region_string)
pixmap.fill(self.get_color_by_coordinates(x, y))
self.setScaledContents(True)
self.setPixmap(pixmap)
self.init_actions()
self.init_signals()
self.init_ui()
self.init_context_menu()
def init_signals(self):
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)
def remove_label_pixmap(self):
pixmap = QPixmap(self.dimensions)
pixmap.fill(self.get_color_by_coordinates(self.x, self.y))
self.setPixmap(pixmap)
def init_ui(self):
self.setStatusTip(self.region_string)
def set_label_pixmap(self, chunk):
pixmap = QPixmap(self.dimensions)
pixmap.fill(QColor("transparent"))
painter = QPainter(pixmap)
painter.drawPixmap(0, 0, self.dimensions.width(), self.dimensions.height(), QPixmap.fromImage(chunk))
painter.end()
pixmap.fill(
get_color_by_coordinates(self.x, self.y)
)
self.setScaledContents(True)
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
self.init_context_menu()
def mark_as_region(self) -> None:
self.is_region = True
def init_actions(self):
action_save_region = QAction("save region as...", self)
self.action_save_region = action_save_region
def set_adjacent_marked_region_labels(self, adjacent_marked_region_labels) -> None:
self.adjacent_marked_region_labels = adjacent_marked_region_labels
action_replace_region = QAction("replace region with file...", self)
self.action_replace_region = action_replace_region
""" context menu """
def init_context_menu(self):
self.setContextMenuPolicy(Qt.CustomContextMenu)
self.customContextMenuRequested.connect(self.on_context_menu)
menu = QMenu(self)
label = QLabel(self.region_string)
@ -97,15 +92,9 @@ class RegionLabel(QLabel):
menu.addAction(widget_action)
menu.addSeparator()
action_save_region = QAction("save region as...", self)
action_save_region.triggered.connect(self.save_region_as)
menu.addAction(action_save_region)
action_replace_region = QAction("replace region with file...", self)
action_replace_region.triggered.connect(self.load_region_from_file)
menu.addAction(action_replace_region)
menu.addAction(self.action_save_region)
menu.addAction(self.action_replace_region)
menu.closeEvent = self.on_context_menu_close
self.context_menu = menu
def on_context_menu(self, point):
@ -114,21 +103,36 @@ class RegionLabel(QLabel):
toggle_open_in_context_menu(self)
self.context_menu.exec_(self.mapToGlobal(point))
def on_context_menu_close(self, point):
toggle_open_in_context_menu(self)
def remove_label_pixmap(self):
pixmap = QPixmap(self.dimensions)
pixmap.fill(get_color_by_coordinates(self.x, self.y))
self.setPixmap(pixmap)
""" visual stuff"""
@staticmethod
def get_color_by_coordinates(x=0, y=0) -> QColor:
# if used in a grid, this is basically creating a gradient
return QColor(
int((((x + 1) * 4) + ((y + 1) * 4)) / 2 - 1),
int((((x + 1) * 4) + ((y + 1) * 4)) / 2 - 1),
int((((x + 1) * 4) + ((y + 1) * 4)) / 2 - 1)
)
def set_label_pixmap(self, chunk):
pixmap = QPixmap(self.dimensions)
pixmap.fill(QColor("transparent"))
painter = QPainter(pixmap)
painter.drawPixmap(0, 0, self.dimensions.width(), self.dimensions.height(), QPixmap.fromImage(chunk))
painter.end()
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_adjacent_marked_region_labels(self, adjacent_marked_region_labels) -> None:
self.adjacent_marked_region_labels = adjacent_marked_region_labels
""" visual stuff"""
def update_style_effects(self):
style = None
style = ""
if self.is_marked:
style = "color: #ffffff; "
for direction in ["top", "right", "bottom", "left"]:
@ -192,37 +196,37 @@ class RegionLabel(QLabel):
chunk = QImage(self.raw_data_array.copy(), 512, 512, QImage.Format_Grayscale16)
self.set_label_pixmap(chunk)
self.region_label_helper.change_region_label_region_raw_data_signal.emit(self.raw_data_array)
self.region_label_helper.update_region_raw_data_signal.emit(self.raw_data_array)
""" altering class functions """
def mouse_any_click_event(self, event):
modifiers = QApplication.keyboardModifiers()
if modifiers == Qt.ControlModifier and self.is_region:
if event.buttons() == Qt.LeftButton:
self.region_label_helper.change_region_label_toggle_marked_signal.emit(self)
self.region_label_helper.toggle_is_marked_signal.emit(self)
elif self.is_region:
if event.buttons() == Qt.LeftButton:
self.region_label_helper.change_region_label_toggle_selected_signal.emit(self)
self.region_label_helper.toggle_is_selected_signal.emit(self)
def mousePressEvent(self, event):
self.mouse_any_click_event(event)
super().mousePressEvent(event)
super(RegionLabel, self).mousePressEvent(event)
def mouseDoubleClickEvent(self, event):
if self.is_region:
self.region_label_helper.change_region_label_toggle_marked_signal.emit(self)
super().mouseDoubleClickEvent(event)
self.region_label_helper.toggle_is_marked_signal.emit(self)
super(RegionLabel, self).mouseDoubleClickEvent(event)
def enterEvent(self, event) -> None:
self.mouse_cursor_helper.hovered_label_signal.emit(self)
self.mouse_cursor_helper.mouse_cursor_change_signal.emit(None)
super().enterEvent(event)
super(RegionLabel, self).enterEvent(event)
def leaveEvent(self, event) -> None:
self.mouse_cursor_helper.mouse_cursor_change_signal.emit(None)
super().leaveEvent(event)
super(RegionLabel, self).leaveEvent(event)
def wheelEvent(self, event):
self.mouse_cursor_helper.hovered_label_signal.emit(self)

13
world_regions_widget/tools.py

@ -1,4 +1,6 @@
from PyQt5.QtGui import QGuiApplication
from PyQt5.QtGui import (
QGuiApplication, QColor
)
from PyQt5.QtCore import Qt
@ -45,3 +47,12 @@ def toggle_open_in_context_menu(region_label=None):
if region_label is not None:
region_label.context_menu_is_shown = not region_label.context_menu_is_shown
region_label.update_graphics_effect()
def get_color_by_coordinates(x=0, y=0) -> QColor:
# if used in a grid, this is basically creating a gradient
return QColor(
int((((x + 1) * 4) + ((y + 1) * 4)) / 2 - 1),
int((((x + 1) * 4) + ((y + 1) * 4)) / 2 - 1),
int((((x + 1) * 4) + ((y + 1) * 4)) / 2 - 1)
)

Loading…
Cancel
Save