Browse Source

added icon for future plotting option (visible when holding shift over a valid region)

added option to save a map back to a file (CTRL+S)
move main image creation into label_collection, as the whole system needs to be reworked
master
Christian Voigt 1 year ago
parent
commit
1d566e18e7
  1. 48
      world_regions_widget/__init__.py
  2. BIN
      world_regions_widget/cursors/plot_region.png
  3. 3
      world_regions_widget/region_label.py
  4. 8
      world_regions_widget/region_label_collection.py

48
world_regions_widget/__init__.py

@ -32,6 +32,8 @@ class WorldRegionsWidget(QScrollArea):
region_label_collection = RegionLabelCollection
action_open_world_file = QAction
action_save_world_file = QAction
world_regions_image = QImage
world_regions_image_raw_data = np.array
@ -40,6 +42,7 @@ class WorldRegionsWidget(QScrollArea):
cursor_add_region = QCursor
cursor_remove_region = QCursor
cursor_hover_region = QCursor
cursor_plot_region = QCursor
def __init__(self, zoom_factor=1.0, min_zoom_factor=0.5, max_zoom_factor=24.0, region_size=48):
super().__init__()
@ -78,6 +81,14 @@ class WorldRegionsWidget(QScrollArea):
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.action_save_world_file = save_world_file
self.addAction(self.action_save_world_file)
def init_cursors(self):
self.root_dir = os.path.dirname(os.path.abspath(__file__))
@ -93,6 +104,9 @@ class WorldRegionsWidget(QScrollArea):
self.cursor_remove_region = QCursor(QPixmap(
os.path.join(self.root_dir, 'cursors/remove_region.png')
).scaled(cursor_size, Qt.KeepAspectRatio), -1, -1)
self.cursor_plot_region = QCursor(QPixmap(
os.path.join(self.root_dir, 'cursors/plot_region.png')
).scaled(cursor_size, Qt.KeepAspectRatio), -1, -1)
self.setCursor(self.cursor_hover_region)
@ -160,6 +174,16 @@ class WorldRegionsWidget(QScrollArea):
self.setCursor(self.cursor_remove_region)
else:
self.setCursor(self.cursor_add_region)
elif modifiers == Qt.ShiftModifier or (event is not None and event.key() == 16777248):
try:
currently_hovered_region_is_loaded = self.currently_hovered_region.is_region
except AttributeError:
currently_hovered_region_is_loaded = None
if currently_hovered_region_is_loaded:
self.setCursor(self.cursor_plot_region)
else:
self.setCursor(self.cursor_hover_region)
else:
self.setCursor(self.cursor_hover_region)
@ -167,6 +191,9 @@ class WorldRegionsWidget(QScrollArea):
def mousePressEvent(self, event):
if event.button() == Qt.LeftButton:
self.trigger_cursor_change()
modifiers = QApplication.keyboardModifiers()
if modifiers == Qt.ShiftModifier and self.currently_hovered_region.is_region:
pass
if event.button() == Qt.RightButton:
self.drag_in_progress = True
@ -212,7 +239,7 @@ class WorldRegionsWidget(QScrollArea):
# super().wheelEvent(event)
def keyPressEvent(self, event):
if event.key() in (16777249, 16777250):
if event.key() in (16777249, 16777250, 16777248):
self.trigger_cursor_change(event)
super().keyPressEvent(event)
@ -239,9 +266,18 @@ class WorldRegionsWidget(QScrollArea):
self.world_regions_image_raw_data = raw_data_array
map_image = QImage(raw_data_array, length, length, QImage.Format_Grayscale16)
self.world_regions_image = map_image
self.region_label_collection.update_label_collection_with_loaded_image(
map_image, offset=offset
self.region_label_collection.update_label_collection_with_raw_map_data(
raw_data_array, length, offset=offset
)
def save_world_file_dialogue(self):
options = QFileDialog.Options()
file_name, _ = QFileDialog.getSaveFileName(
self, caption='QFileDialog.getSaveFileName()', filter='RAW files (*.raw)', options=options
)
if file_name:
with open(file_name, 'w+b') as raw_file:
raw_file.write(
bytearray(self.world_regions_image_raw_data)
)

BIN
world_regions_widget/cursors/plot_region.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.0 KiB

3
world_regions_widget/region_label.py

@ -1,3 +1,5 @@
import numpy as np
from PyQt5.QtCore import Qt
from PyQt5.QtGui import (
QPixmap, QColor, QImage
@ -20,6 +22,7 @@ class RegionLabel(QLabel):
adjacent_marked_region_labels = dict
image = QImage
raw_data_array = np.array
def __init__(self, collection, x=None, y=None):
super(RegionLabel, self).__init__()

8
world_regions_widget/region_label_collection.py

@ -1,4 +1,4 @@
from PyQt5.QtGui import QPixmap, QColor, QPainter
from PyQt5.QtGui import QPixmap, QColor, QPainter, QImage
from PyQt5.QtWidgets import (
QGridLayout
)
@ -91,7 +91,9 @@ class RegionLabelCollection(QGridLayout):
if label is not empty:
label.parent().adjustSize()
def update_label_collection_with_loaded_image(self, image, offset=0):
def update_label_collection_with_raw_map_data(self, raw_map_data, length, offset=0):
image = QImage(raw_map_data, length, length, QImage.Format_Grayscale16)
length = image.width()
regions_per_row = int(length / 512)
flipped_image = image.mirrored()
@ -99,7 +101,7 @@ class RegionLabelCollection(QGridLayout):
for x in range(0, regions_per_row):
for y in range(0, regions_per_row):
chunk = flipped_image.copy(x * 512, y * 512, 512, 512)
grid_slot = str(y + offset) + "-" + str(x + offset)
grid_slot = "{}-{}".format(y + offset, x + offset)
region_label = self.label_collection.get(grid_slot)
region_label.set_image(chunk)
region_label.mark_as_region()

Loading…
Cancel
Save