Skip to content

Commit

Permalink
fixes image import
Browse files Browse the repository at this point in the history
  • Loading branch information
w4ffl35 committed Oct 18, 2023
1 parent 8d1eb9d commit f77077f
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 117 deletions.
163 changes: 116 additions & 47 deletions src/airunner/widgets/canvas_plus/canvas_plus_widget.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import math
from functools import partial

from PIL import Image
from PIL.ImageQt import ImageQt, QImage
from PyQt6.QtCore import Qt, QPoint, QPointF, QThread, QRect, QRectF
from PyQt6.QtGui import QBrush, QColor, QPen, QPixmap, QPainter
from PyQt6.QtWidgets import QGraphicsScene, QGraphicsView, QGraphicsItem, QGraphicsPixmapItem
from PyQt6.QtCore import Qt, QPoint, QPointF, QRect
from PyQt6.QtGui import QBrush, QColor, QPen, QPixmap, QPainter, QCursor
from PyQt6.QtWidgets import QGraphicsScene, QGraphicsItem, QGraphicsPixmapItem, QGraphicsLineItem

from airunner.aihandler.settings_manager import SettingsManager
from airunner.data.models import Layer, LayerImage
from airunner.cursors.circle_brush import CircleCursor
from airunner.data.db import session
from airunner.data.models import Layer, LayerImage, CanvasSettings, ActiveGridSettings, GridSettings
from airunner.utils import get_session, save_session
from airunner.widgets.base_widget import BaseWidget
from airunner.widgets.canvas_plus.templates.canvas_plus_ui import Ui_canvas
Expand Down Expand Up @@ -185,14 +188,6 @@ def mouseReleaseEvent(self, event):
self.settings_manager.set_value("active_grid_settings.pos_y", pos.y())


class CanvasView(QGraphicsView):
def enable_select_tool(self):
self.setDragMode(QGraphicsView.DragMode.RubberBandDrag)

def disable_select_tool(self):
self.setDragMode(QGraphicsView.DragMode.NoDrag)


class CanvasPlusWidget(BaseWidget):
widget_class_ = Ui_canvas
scene = None
Expand All @@ -203,27 +198,69 @@ class CanvasPlusWidget(BaseWidget):
line_color = None
canvas_color = None
layers = {}
images = {}
active_grid_area = None
active_grid_area_pivot_point = QPoint(0, 0)
active_grid_area_position = QPoint(0, 0)
last_pos = QPoint(0, 0)

@property
def active_grid_area_selected(self):
return self.settings_manager.current_tool == "active_grid_area"

@property
def select_selected(self):
return self.settings_manager.current_tool == "select"

@property
def eraser_selected(self):
return self.settings_manager.current_tool == "eraser"

@property
def brush_selected(self):
return self.settings_manager.current_tool == "brush"

@property
def move_selected(self):
return self.settings_manager.current_tool == "move"

@property
def brush_size(self):
return self.settings_manager.brush_settings.size

@property
def active_grid_area_rect(self):
rect = QRect(
self.settings_manager.active_grid_settings.pos_x,
self.settings_manager.active_grid_settings.pos_y,
self.settings_manager.working_width,
self.settings_manager.working_height
self.active_grid_settings.pos_x,
self.active_grid_settings.pos_y,
self.active_grid_settings.width,
self.active_grid_settings.height
)

# apply self.pos_x and self.pox_y to the rect
rect.translate(self.last_pos.x(), self.last_pos.y())
rect.translate(self.canvas_settings.pos_x, self.canvas_settings.pos_y)

return rect

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.canvas_settings = session.query(CanvasSettings).first()
self.active_grid_settings = session.query(ActiveGridSettings).first()
self.ui.central_widget.resizeEvent = self.resizeEvent
self.app.add_image_to_canvas_signal.connect(self.handle_add_image_to_canvas)
self.app.image_data.connect(self.handle_image_data)

def handle_image_data(self, data):
image = data["image"]
image_data = data["data"]
# add image to the canvas
pixmap = QPixmap.fromImage(ImageQt(image))
image = DraggablePixmap(self, pixmap)
self.scene.addItem(image)
# image.setPos(QPointF(
# self.canvas_settings.pos_x + data.pos_x,
# self.canvas_settings.pos_y + data.pos_y
# ))

def handle_mouse_event(self, original_mouse_event, event):
if event.buttons() == Qt.MouseButton.MiddleButton:
Expand All @@ -243,7 +280,7 @@ def initialize(self):
# Create a QGraphicsScene object
self.scene = QGraphicsScene(self)

self.view = CanvasView(self.ui.central_widget)
self.view = self.ui.canvas_container
original_mouse_event = self.view.mouseMoveEvent
self.view.mouseMoveEvent = partial(self.handle_mouse_event, original_mouse_event)
#self.view.setDragMode(QGraphicsView.DragMode.RubberBandDrag)
Expand All @@ -263,9 +300,6 @@ def initialize(self):
self.scene.setBackgroundBrush(QBrush(self.canvas_color))
self.view.setScene(self.scene)

# Add the QGraphicsView object
self.ui.canvas_container.layout().addWidget(self.view)

# Set the size of the QGraphicsScene object to match the size of the QGraphicsView object
self.set_scene_rect()

Expand Down Expand Up @@ -296,11 +330,12 @@ def draw_layers(self):
pixmap = QPixmap.fromImage(ImageQt(image))
image = LayerImageItem(self, pixmap, layer_image)
self.layers[layer.id].append(image)
self.scene.addItem(image)
if layer_image.visible and not layer.hidden:
self.scene.addItem(image)
pos = QPoint(layer_image.pos_x, layer_image.pos_y)
image.setPos(QPointF(
self.last_pos.x() + pos.x(),
self.last_pos.y() + pos.y()
self.canvas_settings.pos_x + pos.x(),
self.canvas_settings.pos_y + pos.y()
))

def handle_changed_signal(self, key, value):
Expand All @@ -312,14 +347,20 @@ def handle_changed_signal(self, key, value):
self.do_draw()
elif key == "current_section_shapegif":
self.do_draw()
elif key == "layer_image_data.visible":
self.do_draw()
elif key == "layer_data.hidden":
self.do_draw()
elif key == "grid_settings.show_grid":
# remove lines from scene
for item in self.scene.items():
if isinstance(item, QGraphicsLineItem):
self.scene.removeItem(item)
self.do_draw()

def set_scene_rect(self):
self.scene.setSceneRect(0, 0, self.view_size.width(), self.view_size.height())


images = {}


def draw_lines(self):
width_cells = math.ceil(self.view_size.width() / self.cell_size)
height_cells = math.ceil(self.view_size.height() / self.cell_size)
Expand All @@ -330,21 +371,20 @@ def draw_lines(self):
Qt.PenStyle.SolidLine
)

# vertical lines
h = self.view_size.height() + abs(self.last_pos.y()) % self.cell_size
y = 0
for i in range(width_cells):
x = i * self.cell_size + self.last_pos.x() % self.cell_size
self.scene.addLine(x, y, x, h, pen)

# # horizontal lines
w = self.view_size.width() + abs(self.last_pos.x()) % self.cell_size
x = 0
for i in range(height_cells):
y = i * self.cell_size + self.last_pos.y() % self.cell_size
self.scene.addLine(x, y, w, y, pen)

active_grid_area = None
if self.settings_manager.grid_settings.show_grid:
# vertical lines
h = self.view_size.height() + abs(self.canvas_settings.pos_y) % self.cell_size
y = 0
for i in range(width_cells):
x = i * self.cell_size + self.canvas_settings.pos_x % self.cell_size
self.scene.addLine(x, y, x, h, pen)

# # horizontal lines
w = self.view_size.width() + abs(self.canvas_settings.pos_x) % self.cell_size
x = 0
for i in range(height_cells):
y = i * self.cell_size + self.canvas_settings.pos_y % self.cell_size
self.scene.addLine(x, y, w, y, pen)

def draw_active_grid_area_container(self):
"""
Expand All @@ -367,14 +407,43 @@ def action_button_clicked_focus(self):
self.last_pos = QPoint(0, 0)
self.do_draw()

active_grid_area_position = QPoint(0, 0)

def do_draw(self):
self.view_size = self.view.viewport().size()
self.set_scene_rect()
self.draw_lines()
self.draw_layers()
self.draw_active_grid_area_container()
self.ui.canvas_position.setText(
f"X {-self.last_pos.x(): 05d} Y {self.last_pos.y(): 05d}"
)
f"X {-self.canvas_settings.pos_x: 05d} Y {self.canvas_settings.pos_y: 05d}"
)

def update_cursor(self):
# if self.is_canvas_drag_mode:
# # show as grab cursor
# self.canvas_container.setCursor(Qt.CursorShape.ClosedHandCursor)
if self.move_selected:
self.ui.canvas_container.setCursor(Qt.CursorShape.OpenHandCursor)
elif self.active_grid_area_selected:
self.ui.canvas_container.setCursor(Qt.CursorShape.DragMoveCursor)
elif self.brush_selected or self.eraser_selected:
self.ui.canvas_container.setCursor(
CircleCursor(
Qt.GlobalColor.white,
Qt.GlobalColor.transparent,
self.brush_size
)
)
else:
self.ui.canvas_container.setCursor(QCursor(Qt.CursorShape.ArrowCursor))


def load_image(self, image_path):
image = Image.open(image_path)

if self.app.settings_manager.resize_on_paste:
image.thumbnail((self.settings_manager.working_width,
self.settings_manager.working_height), Image.ANTIALIAS)

pixmap = QPixmap.fromImage(ImageQt(image))
image = DraggablePixmap(self, pixmap)
self.scene.addItem(image)
56 changes: 4 additions & 52 deletions src/airunner/widgets/canvas_plus/templates/canvas_plus.ui
Original file line number Diff line number Diff line change
Expand Up @@ -62,24 +62,9 @@
<string notr="true"/>
</property>
<layout class="QGridLayout" name="gridLayout_2">
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>3</number>
</property>
<property name="horizontalSpacing">
<number>0</number>
</property>
<property name="verticalSpacing">
<number>3</number>
</property>
<item row="0" column="0">
<widget class="QGraphicsView" name="canvas_container"/>
</item>
<item row="1" column="0">
<widget class="QLabel" name="canvas_position">
<property name="sizePolicy">
Expand All @@ -94,7 +79,7 @@
</font>
</property>
<property name="styleSheet">
<string notr="true">padding-right: 10px</string>
<string notr="true"/>
</property>
<property name="text">
<string>TextLabel</string>
Expand All @@ -104,39 +89,6 @@
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QFrame" name="canvas_container">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="styleSheet">
<string notr="true"/>
</property>
<property name="frameShape">
<enum>QFrame::NoFrame</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Plain</enum>
</property>
<layout class="QGridLayout" name="gridLayout_3">
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
</layout>
</widget>
</item>
</layout>
</widget>
</item>
Expand Down
22 changes: 4 additions & 18 deletions src/airunner/widgets/canvas_plus/templates/canvas_plus_ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ def setupUi(self, canvas):
self.central_widget.setStyleSheet("")
self.central_widget.setObjectName("central_widget")
self.gridLayout_2 = QtWidgets.QGridLayout(self.central_widget)
self.gridLayout_2.setContentsMargins(0, 0, 0, 3)
self.gridLayout_2.setHorizontalSpacing(0)
self.gridLayout_2.setVerticalSpacing(3)
self.gridLayout_2.setObjectName("gridLayout_2")
self.canvas_container = QtWidgets.QGraphicsView(parent=self.central_widget)
self.canvas_container.setObjectName("canvas_container")
self.gridLayout_2.addWidget(self.canvas_container, 0, 0, 1, 1)
self.canvas_position = QtWidgets.QLabel(parent=self.central_widget)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Policy.Preferred, QtWidgets.QSizePolicy.Policy.Maximum)
sizePolicy.setHorizontalStretch(0)
Expand All @@ -47,24 +47,10 @@ def setupUi(self, canvas):
font = QtGui.QFont()
font.setPointSize(8)
self.canvas_position.setFont(font)
self.canvas_position.setStyleSheet("padding-right: 10px")
self.canvas_position.setStyleSheet("")
self.canvas_position.setAlignment(QtCore.Qt.AlignmentFlag.AlignRight|QtCore.Qt.AlignmentFlag.AlignTrailing|QtCore.Qt.AlignmentFlag.AlignVCenter)
self.canvas_position.setObjectName("canvas_position")
self.gridLayout_2.addWidget(self.canvas_position, 1, 0, 1, 1)
self.canvas_container = QtWidgets.QFrame(parent=self.central_widget)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Policy.Preferred, QtWidgets.QSizePolicy.Policy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.canvas_container.sizePolicy().hasHeightForWidth())
self.canvas_container.setSizePolicy(sizePolicy)
self.canvas_container.setStyleSheet("")
self.canvas_container.setFrameShape(QtWidgets.QFrame.Shape.NoFrame)
self.canvas_container.setFrameShadow(QtWidgets.QFrame.Shadow.Plain)
self.canvas_container.setObjectName("canvas_container")
self.gridLayout_3 = QtWidgets.QGridLayout(self.canvas_container)
self.gridLayout_3.setContentsMargins(0, 0, 0, 0)
self.gridLayout_3.setObjectName("gridLayout_3")
self.gridLayout_2.addWidget(self.canvas_container, 0, 0, 1, 1)
self.gridLayout.addWidget(self.central_widget, 0, 0, 1, 1)

self.retranslateUi(canvas)
Expand Down

0 comments on commit f77077f

Please sign in to comment.