Skip to content

Commit

Permalink
Make arrange method for importing images configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
rbreu committed May 26, 2024
1 parent 2b92406 commit 95b024e
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 4 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ Added
The confirmation dialog can be disalbed in:
Settings -> Miscellaneous -> Confirm when closing an unsaved file
* Add option to arrange by filename (Arrange -> Square (by filename))
* Added a setting to choose the default arrange method on importing
images in batch.
(Settings -> Settings -> Images & Items -> Default Arrange Method).

Fixed
-----
Expand Down
5 changes: 5 additions & 0 deletions beeref/config/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,11 @@ class BeeSettings(QtCore.QSettings):
'cast': int,
'validate': lambda x: 0 <= x <= 200,
},
'Items/arrange_default': {
'default': 'optimal',
'validate': lambda x: x in (
'optimal', 'horizontal', 'vertical', 'square'),
},
'Items/image_allocation_limit': {
'default': 256,
'cast': int,
Expand Down
14 changes: 13 additions & 1 deletion beeref/scene.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@
# You should have received a copy of the GNU General Public License
# along with BeeRef. If not, see <https://www.gnu.org/licenses/>.

from queue import Queue
from functools import partial
import logging
import math
from queue import Queue

from PyQt6 import QtCore, QtWidgets, QtGui
from PyQt6.QtCore import Qt
Expand Down Expand Up @@ -178,6 +179,17 @@ def normalize_size(self):
self.undo_stack.push(
commands.NormalizeItems(items, scale_factors))

def arrange_default(self):
default = self.settings.valueOrDefault('Items/arrange_default')
MAPPING = {
'optimal': self.arrange_optimal,
'horizontal': self.arrange,
'vertical': partial(self.arrange, vertical=True),
'square': self.arrange_square,
}

MAPPING[default]()

def arrange(self, vertical=False):
"""Arrange items in a line (horizontally or vertically)."""

Expand Down
2 changes: 1 addition & 1 deletion beeref/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,7 @@ def on_insert_images_finished(self, new_scene, filename, errors):
'Problem loading images',
msg + IMG_LOADING_ERROR_MSG + errornames)
self.scene.add_queued_items()
self.scene.arrange_optimal()
self.scene.arrange_default()
self.undo_stack.endMacro()
if new_scene:
self.on_action_fit_scene()
Expand Down
18 changes: 16 additions & 2 deletions beeref/widgets/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,19 @@ def convert_value_from_qt(self, value):
return value == Qt.CheckState.Checked


class ArrangeDefaultWidget(RadioGroup):
TITLE = 'Default Arrange Method:'
HELPTEXT = ('How images are arranged when inserted in batch')
KEY = 'Items/arrange_default'
OPTIONS = (
('optimal', 'Optimal', 'Arrange Optimal'),
('horizontal', 'Horizontal (by filename)',
'Arrange Horizontal (by filename)'),
('vertical', 'Vertical (by filename)',
'Arrange Vertical (by filename)'),
('square', 'Square (by filename)', 'Arrannge Square (by filename)'))


class ImageStorageFormatWidget(RadioGroup):
TITLE = 'Image Storage Format:'
HELPTEXT = ('How images are stored inside bee files.'
Expand Down Expand Up @@ -191,8 +204,9 @@ def __init__(self, parent):
items_layout = QtWidgets.QGridLayout()
items.setLayout(items_layout)
items_layout.addWidget(ImageStorageFormatWidget(), 0, 0)
items_layout.addWidget(ArrangeGapWidget(), 0, 1)
items_layout.addWidget(AllocationLimitWidget(), 1, 0)
items_layout.addWidget(AllocationLimitWidget(), 0, 1)
items_layout.addWidget(ArrangeGapWidget(), 1, 0)
items_layout.addWidget(ArrangeDefaultWidget(), 1, 1)
tabs.addTab(items, '&Images && Items')

layout = QtWidgets.QVBoxLayout()
Expand Down
15 changes: 15 additions & 0 deletions tests/test_scene.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import math
from unittest.mock import patch, MagicMock

import pytest
from pytest import approx

from PyQt6 import QtCore, QtGui, QtWidgets
Expand Down Expand Up @@ -249,6 +250,20 @@ def test_normalize_size_when_no_items(view):
view.scene.cancel_crop_mode.assert_called_once_with()


@pytest.mark.parametrize('value,expected_func,expected_kwargs',
[('optimal', 'arrange_optimal', {}),
('horizontal', 'arrange', {}),
('vertical', 'arrange', {'vertical': True}),
('square', 'arrange_square', {})])
def test_arrange_default(
value, expected_func, expected_kwargs, settings, view):
settings.setValue('Items/arrange_default', value)
setattr(view.scene, expected_func, MagicMock())
view.scene.arrange_default()
getattr(view.scene, expected_func).assert_called_once_with(
**expected_kwargs)


def test_arrange_horizontal(view):
item1 = BeePixmapItem(QtGui.QImage())
item1.filename = 'foo.png'
Expand Down

0 comments on commit 95b024e

Please sign in to comment.