Skip to content

Commit

Permalink
added question on rescaling images
Browse files Browse the repository at this point in the history
  • Loading branch information
Chilipp committed Dec 18, 2018
1 parent 7152e38 commit 816284d
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 7 deletions.
3 changes: 3 additions & 0 deletions straditize/widgets/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ class StraditizerWidgets(QWidget, DockMixin):
"""A widget that contains widgets to control a
:class:`straditize.straditizer.Straditizer`"""

#: Boolean that is True if all dialogs should be answered with `Yes`
always_yes = False

#: The QTreeWidget that contains the different widgets for the digitization
tree = None

Expand Down
10 changes: 6 additions & 4 deletions straditize/widgets/image_correction.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ class ImageRotator(StraditizerControlBase, QWidget):
_rotating = False
_ha = False
_va = False
ask = True

def __init__(self, straditizer_widgets, item=None, *args, **kwargs):
super(ImageRotator, self).__init__(*args, **kwargs)
Expand Down Expand Up @@ -144,7 +143,8 @@ def rotate_image(self):
angle = self.angle
if angle is None:
return
answer = QMessageBox.Yes if not self.ask else QMessageBox.question(
sw = self.straditizer_widgets
answer = QMessageBox.Yes if sw.always_yes else QMessageBox.question(
self, 'Restart project?',
'This will close the straditizer and create new figures. '
'Are you sure, you want to continue?')
Expand Down Expand Up @@ -264,8 +264,10 @@ def equalize_axes(self, event=None):
def draw_figure(self):
self.fig.canvas.draw()

def rescale(self, ask=True):
answer = QMessageBox.question(
def rescale(self, ask=None):
if ask is None:
ask = not self.straditizer_widgets.always_yes
answer = QMessageBox.Yes if not ask else QMessageBox.question(
self, 'Restart project?',
'This will close the straditizer and create new figures. '
'Are you sure, you want to continue?')
Expand Down
21 changes: 20 additions & 1 deletion straditize/widgets/menu_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from psyplot_gui.compat.qtcompat import (
with_qt5, QFileDialog, QMenu, QKeySequence, QDialog, QDialogButtonBox,
QLineEdit, QToolButton, QIcon, QCheckBox, QHBoxLayout, QVBoxLayout, QLabel,
QDesktopWidget, QPushButton, QTreeWidgetItem, Qt)
QDesktopWidget, QPushButton, QTreeWidgetItem, Qt, QMessageBox)
from PyQt5 import QtWidgets
from psyplot_gui.common import get_icon
import numpy as np
Expand Down Expand Up @@ -418,6 +418,25 @@ def open_straditizer(self, fname=None, *args, **kwargs):
else:
from PIL import Image
image = Image.open(fname)
w, h = image.size
im_size = w * h
if im_size > 20e6:
recom_frac = 17403188.0 / im_size
answer = (
QMessageBox.Yes
if self.straditizer_widgets.always_yes else
QMessageBox.question(
self.straditizer_widgets, "Large straditizer image",
"This is a rather large image with %1.0f pixels. "
"Shall I reduce it to %1.0f%% of it's size for a "
"better interactive experience?<br>"
"If not, you can rescale it via<br><br>"
"Transform source image &rarr; Rescale image" % (
im_size, 100. * recom_frac)))
if answer == QMessageBox.Yes:
image = image.resize((int(round(w * recom_frac)),
int(round(h * recom_frac))))

stradi = Straditizer(image, *args, **kwargs)
stradi.set_attr('image_file', fname)
self.finish_loading(stradi)
Expand Down
1 change: 1 addition & 0 deletions tests/widgets/_base_testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ def setUpClass(cls):
else:
cls.window = main.mainwindow
cls.straditizer_widgets = get_straditizer_widgets(cls.window)
cls.straditizer_widgets.always_yes = True
cls.straditizer_widgets.switch_to_straditizer_layout()

def setUp(self):
Expand Down
2 changes: 0 additions & 2 deletions tests/widgets/test_image_correction.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ def test_horizontal_rotation(self):
angle = self.rotator.angle
self.assertEqual(angle, -90)
rotated = self.straditizer.image.rotate(angle, expand=True)
self.straditizer_widgets.image_rotator.ask = False
QTest.mouseClick(self.straditizer_widgets.apply_button,
Qt.LeftButton)
self.assertArrayEquals(self.straditizer.image, rotated)
Expand All @@ -35,7 +34,6 @@ def test_vertical_rotation(self):
angle = self.rotator.angle
self.assertEqual(angle, 90)
rotated = self.straditizer.image.rotate(angle, expand=True)
self.straditizer_widgets.image_rotator.ask = False
QTest.mouseClick(self.straditizer_widgets.apply_button,
Qt.LeftButton)
self.assertArrayEquals(self.straditizer.image, rotated)
Expand Down

0 comments on commit 816284d

Please sign in to comment.