-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
373 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,7 @@ | |
MENU_SEPARATOR, | ||
'save', | ||
'save_as', | ||
'export_scene', | ||
MENU_SEPARATOR, | ||
'quit', | ||
], | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
# This file is part of BeeRef. | ||
# | ||
# BeeRef is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License as published by | ||
# the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# BeeRef is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with BeeRef. If not, see <https://www.gnu.org/licenses/>. | ||
|
||
import logging | ||
|
||
from PyQt6 import QtCore, QtGui | ||
|
||
from .errors import BeeFileIOError | ||
from beeref import constants | ||
|
||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class SceneToPixmapExporter: | ||
"""For exporting the scene to a single image.""" | ||
|
||
MARGIN = 100 | ||
|
||
def __init__(self, scene): | ||
self.scene = scene | ||
self.scene.cancel_crop_mode() | ||
self.scene.set_selected_all_items(False) | ||
# Selection outlines/handles will be rendered to the exported | ||
# image, and they also influence the size of the sceneRect. | ||
# So deselect first. | ||
size = self.scene.sceneRect().size() | ||
if isinstance(size, QtCore.QSizeF): | ||
size = size.toSize() | ||
self.margin = max(size.width(), size.height()) * 0.03 | ||
self.default_size = size.grownBy( | ||
QtCore.QMargins(*([int(self.margin)] * 4))) | ||
logger.debug(f'Default export size: {self.default_size}') | ||
logger.debug(f'Default export margin: {self.margin}') | ||
|
||
def render_to_image(self, size): | ||
logger.debug(f'Final export size: {size}') | ||
margin = self.margin * size.width() / self.default_size.width() | ||
logger.debug(f'Final export margin: {margin}') | ||
|
||
image = QtGui.QImage(size, QtGui.QImage.Format.Format_RGB32) | ||
image.fill(QtGui.QColor(*constants.COLORS['Scene:Canvas'])) | ||
painter = QtGui.QPainter(image) | ||
painter.setViewport(QtCore.QRect( | ||
int(margin), | ||
int(margin), | ||
int(size.width() - 2 * margin), | ||
int(size.height() - 2 * margin))) | ||
self.scene.render(painter) | ||
painter.end() | ||
return image | ||
|
||
def export(self, filename, size): | ||
logger.debug(f'Exporting scene to {filename}') | ||
image = self.render_to_image(size) | ||
if not image.save(filename): | ||
raise BeeFileIOError( | ||
msg=str('Error writing image'), filename=filename) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import os | ||
import stat | ||
from unittest.mock import patch | ||
import pytest | ||
|
||
from PyQt6 import QtGui, QtCore | ||
|
||
from beeref import constants | ||
from beeref.items import BeePixmapItem | ||
from beeref.fileio.errors import BeeFileIOError | ||
from beeref.fileio.export import SceneToPixmapExporter | ||
|
||
|
||
def test_scene_to_pixmap_exporter_default_size_and_margin(view): | ||
item1 = BeePixmapItem( | ||
QtGui.QImage(100, 100, QtGui.QImage.Format.Format_RGB32)) | ||
item1.setPos(QtCore.QPointF(0, 0)) | ||
view.scene.addItem(item1) | ||
|
||
item2 = BeePixmapItem( | ||
QtGui.QImage(100, 100, QtGui.QImage.Format.Format_RGB32)) | ||
item1.setPos(QtCore.QPointF(200, 0)) | ||
view.scene.addItem(item2) | ||
|
||
exporter = SceneToPixmapExporter(view.scene) | ||
assert view.scene.sceneRect().size().toSize() == QtCore.QSize(300, 100) | ||
assert (exporter.margin - 9) < 0.000001 | ||
assert exporter.default_size == QtCore.QSize(318, 118) | ||
|
||
|
||
def test_scene_to_pixmap_exporter_default_size_and_margin_when_selection(view): | ||
item1 = BeePixmapItem( | ||
QtGui.QImage(100, 100, QtGui.QImage.Format.Format_RGB32)) | ||
item1.setPos(QtCore.QPointF(0, 0)) | ||
view.scene.addItem(item1) | ||
|
||
item2 = BeePixmapItem( | ||
QtGui.QImage(100, 100, QtGui.QImage.Format.Format_RGB32)) | ||
item1.setPos(QtCore.QPointF(200, 0)) | ||
view.scene.addItem(item2) | ||
item2.setSelected(True) | ||
|
||
exporter = SceneToPixmapExporter(view.scene) | ||
assert view.scene.sceneRect().size().toSize() == QtCore.QSize(300, 100) | ||
assert (exporter.margin - 9) < 0.000001 | ||
assert exporter.default_size == QtCore.QSize(318, 118) | ||
|
||
|
||
@patch('PyQt6.QtGui.QPainter.setViewport') | ||
def test_scene_to_pixmap_exporter_render_sets_margins(set_mock, view): | ||
item = BeePixmapItem( | ||
QtGui.QImage(1000, 1200, QtGui.QImage.Format.Format_RGB32)) | ||
view.scene.addItem(item) | ||
exporter = SceneToPixmapExporter(view.scene) | ||
assert exporter.margin == 36 | ||
assert exporter.default_size == QtCore.QSize(1072, 1272) | ||
exporter.render_to_image(QtCore.QSize(536, 636)) | ||
|
||
set_mock.assert_called_once_with( | ||
QtCore.QRect(18, 18, 500, 600)) | ||
|
||
|
||
def test_scene_to_pixmap_exporter_render_renders_scene(view): | ||
item_img = QtGui.QImage(1000, 1200, QtGui.QImage.Format.Format_RGB32) | ||
item_img.fill(QtGui.QColor(11, 22, 33)) | ||
item = BeePixmapItem(item_img) | ||
view.scene.addItem(item) | ||
exporter = SceneToPixmapExporter(view.scene) | ||
assert exporter.margin == 36 | ||
assert exporter.default_size == QtCore.QSize(1072, 1272) | ||
image = exporter.render_to_image(QtCore.QSize(536, 636)) | ||
assert image.pixel(1, 1) == QtGui.QColor(*constants.COLORS['Scene:Canvas']) | ||
assert image.pixel(100, 100) == QtGui.QColor(11, 22, 33) | ||
|
||
|
||
def test_scene_to_pixmap_export_writes_image(view, tmpdir): | ||
filename = os.path.join(tmpdir, 'foo.png') | ||
item_img = QtGui.QImage(1000, 1200, QtGui.QImage.Format.Format_RGB32) | ||
item = BeePixmapItem(item_img) | ||
view.scene.addItem(item) | ||
exporter = SceneToPixmapExporter(view.scene) | ||
exporter.export(filename, QtCore.QSize(100, 120)) | ||
|
||
with open(filename, 'rb') as f: | ||
assert f.read().startswith(b'\x89PNG') | ||
|
||
|
||
def test_scene_to_pixmap_export_when_file_not_writeable(view, tmpdir): | ||
filename = os.path.join(tmpdir, 'foo.png') | ||
with open(filename, 'w') as f: | ||
f.write('foo') | ||
os.chmod(filename, stat.S_IREAD) | ||
item_img = QtGui.QImage(1000, 1200, QtGui.QImage.Format.Format_RGB32) | ||
item = BeePixmapItem(item_img) | ||
view.scene.addItem(item) | ||
exporter = SceneToPixmapExporter(view.scene) | ||
|
||
with pytest.raises(BeeFileIOError) as e: | ||
exporter.export(filename, QtCore.QSize(100, 120)) | ||
assert e.filename == filename |
Oops, something went wrong.