fix(gui): make cover gallery previews responsive
This commit is contained in:
+146
-2
@@ -4069,7 +4069,7 @@ class GuiTests(TempDirMixin, unittest.TestCase):
|
||||
|
||||
self.assertGreaterEqual(len(dialog.candidates), 4)
|
||||
expected_width = (
|
||||
len(dialog.candidates) * (dialog.THUMBNAIL_SIZE + 16)
|
||||
len(dialog.candidates) * (dialog.candidate_thumbnail_size + 16)
|
||||
+ (len(dialog.candidates) - 1) * dialog.candidate_layout.spacing()
|
||||
)
|
||||
self.assertIs(dialog.candidate_scroll.widget(), dialog.candidate_content)
|
||||
@@ -4078,6 +4078,63 @@ class GuiTests(TempDirMixin, unittest.TestCase):
|
||||
|
||||
self.assert_removed(temp_dir)
|
||||
|
||||
def test_cover_gallery_candidate_images_resize_with_viewport_and_keep_ratio(self):
|
||||
with self.make_temp_dir() as temp_dir:
|
||||
cfg, account, task, canonical = self._cover_gallery_task(temp_dir)
|
||||
self.write_test_image(canonical, width=240, height=120)
|
||||
dialog = CoverGalleryDialog(task, cfg["image_dir"], cfg["db_path"], account=account)
|
||||
self.addCleanup(dialog.close)
|
||||
dialog.show()
|
||||
|
||||
dialog.resize(650, 460)
|
||||
QApplication.processEvents()
|
||||
dialog._candidate_resize_timer.stop()
|
||||
dialog._update_candidate_thumbnail_layout()
|
||||
small_size = dialog.candidate_thumbnail_size
|
||||
thumbnail = dialog.candidate_thumbnails[canonical]
|
||||
small_pixmap = thumbnail.pixmap()
|
||||
|
||||
self.assertEqual(QSize(small_size, small_size), thumbnail.size())
|
||||
self.assertIsNotNone(small_pixmap)
|
||||
self.assertAlmostEqual(2.0, small_pixmap.width() / small_pixmap.height(), places=1)
|
||||
|
||||
dialog.resize(1100, 700)
|
||||
QApplication.processEvents()
|
||||
dialog._candidate_resize_timer.stop()
|
||||
dialog._update_candidate_thumbnail_layout()
|
||||
large_size = dialog.candidate_thumbnail_size
|
||||
large_pixmap = thumbnail.pixmap()
|
||||
|
||||
self.assertGreater(large_size, small_size)
|
||||
self.assertLessEqual(large_size, dialog.MAX_THUMBNAIL_SIZE)
|
||||
self.assertEqual(QSize(large_size, large_size), thumbnail.size())
|
||||
self.assertAlmostEqual(2.0, large_pixmap.width() / large_pixmap.height(), places=1)
|
||||
expected_width = len(dialog.candidates) * (large_size + 16)
|
||||
expected_width += max(0, len(dialog.candidates) - 1) * dialog.candidate_layout.spacing()
|
||||
self.assertGreaterEqual(dialog.candidate_content.minimumWidth(), expected_width)
|
||||
|
||||
self.assert_removed(temp_dir)
|
||||
|
||||
def test_cover_gallery_broken_candidate_keeps_failure_placeholder_after_resize(self):
|
||||
with self.make_temp_dir() as temp_dir:
|
||||
cfg, account, task, canonical = self._cover_gallery_task(temp_dir)
|
||||
with open(canonical, "wb") as fh:
|
||||
fh.write(b"not-an-image")
|
||||
dialog = CoverGalleryDialog(task, cfg["image_dir"], cfg["db_path"], account=account)
|
||||
self.addCleanup(dialog.close)
|
||||
dialog.resize(1000, 680)
|
||||
dialog._update_candidate_thumbnail_layout()
|
||||
|
||||
thumbnail = dialog.candidate_thumbnails[canonical]
|
||||
self.assertTrue(dialog.candidate_images[canonical].isNull())
|
||||
self.assertEqual("图片读取失败", thumbnail.text())
|
||||
self.assertEqual(
|
||||
QSize(dialog.candidate_thumbnail_size, dialog.candidate_thumbnail_size),
|
||||
thumbnail.size(),
|
||||
)
|
||||
|
||||
self.assert_removed(temp_dir)
|
||||
|
||||
def test_cover_gallery_switch_task_resyncs_candidate_content_width(self):
|
||||
with self.make_temp_dir() as temp_dir:
|
||||
cfg, account, tasks, canonicals, archives = self._cover_gallery_task_set(temp_dir, count=2)
|
||||
@@ -4100,7 +4157,7 @@ class GuiTests(TempDirMixin, unittest.TestCase):
|
||||
self.assertTrue(dialog.switch_task(1))
|
||||
|
||||
expected_width = (
|
||||
len(dialog.candidates) * (dialog.THUMBNAIL_SIZE + 16)
|
||||
len(dialog.candidates) * (dialog.candidate_thumbnail_size + 16)
|
||||
+ (len(dialog.candidates) - 1) * dialog.candidate_layout.spacing()
|
||||
)
|
||||
self.assertEqual(tasks[1].id, dialog.task.id)
|
||||
@@ -4111,6 +4168,40 @@ class GuiTests(TempDirMixin, unittest.TestCase):
|
||||
|
||||
self.assert_removed(temp_dir)
|
||||
|
||||
def test_cover_gallery_switch_task_keeps_responsive_thumbnail_size(self):
|
||||
with self.make_temp_dir() as temp_dir:
|
||||
cfg, account, tasks, _canonicals, _archives = self._cover_gallery_task_set(
|
||||
temp_dir,
|
||||
count=2,
|
||||
)
|
||||
dialog = CoverGalleryDialog(
|
||||
tasks[0],
|
||||
cfg["image_dir"],
|
||||
cfg["db_path"],
|
||||
account=account,
|
||||
visible_tasks=tasks,
|
||||
task_index=0,
|
||||
account_by_alias={"alias-a": account},
|
||||
)
|
||||
self.addCleanup(dialog.close)
|
||||
dialog.resize(1100, 700)
|
||||
dialog.show()
|
||||
QApplication.processEvents()
|
||||
dialog._candidate_resize_timer.stop()
|
||||
dialog._update_candidate_thumbnail_layout()
|
||||
responsive_size = dialog.candidate_thumbnail_size
|
||||
|
||||
self.assertGreater(responsive_size, dialog.THUMBNAIL_SIZE)
|
||||
self.assertTrue(dialog.switch_task(1))
|
||||
dialog._candidate_resize_timer.stop()
|
||||
dialog._update_candidate_thumbnail_layout()
|
||||
|
||||
self.assertEqual(responsive_size, dialog.candidate_thumbnail_size)
|
||||
for thumbnail in dialog.candidate_thumbnails.values():
|
||||
self.assertEqual(QSize(responsive_size, responsive_size), thumbnail.size())
|
||||
|
||||
self.assert_removed(temp_dir)
|
||||
|
||||
def test_cover_gallery_save_switches_current_cover(self):
|
||||
with self.make_temp_dir() as temp_dir:
|
||||
cfg = self.make_config(temp_dir)
|
||||
@@ -4265,6 +4356,59 @@ class GuiTests(TempDirMixin, unittest.TestCase):
|
||||
|
||||
self.assert_removed(temp_dir)
|
||||
|
||||
def test_original_image_dialog_switches_between_fit_and_original_size(self):
|
||||
with self.make_temp_dir() as temp_dir:
|
||||
image_path = self.write_test_image(
|
||||
os.path.join(temp_dir, "large-cover.jpg"),
|
||||
width=1200,
|
||||
height=800,
|
||||
)
|
||||
dialog = OriginalImageDialog(image_path)
|
||||
self.addCleanup(dialog.close)
|
||||
dialog.resize(600, 450)
|
||||
dialog.show()
|
||||
QApplication.processEvents()
|
||||
dialog._resize_timer.stop()
|
||||
dialog._update_image_display()
|
||||
|
||||
fit_pixmap = dialog.image_label.pixmap()
|
||||
viewport_size = dialog.scroll.viewport().size()
|
||||
self.assertTrue(dialog.fit_to_window)
|
||||
self.assertEqual("原始尺寸", dialog.display_mode_button.text())
|
||||
self.assertLessEqual(fit_pixmap.width(), viewport_size.width())
|
||||
self.assertLessEqual(fit_pixmap.height(), viewport_size.height())
|
||||
self.assertAlmostEqual(1.5, fit_pixmap.width() / fit_pixmap.height(), places=1)
|
||||
|
||||
dialog.toggle_display_mode()
|
||||
|
||||
original_pixmap = dialog.image_label.pixmap()
|
||||
self.assertFalse(dialog.fit_to_window)
|
||||
self.assertEqual("适应窗口", dialog.display_mode_button.text())
|
||||
self.assertEqual(QSize(1200, 800), original_pixmap.size())
|
||||
self.assertEqual(QSize(1200, 800), dialog.image_label.size())
|
||||
|
||||
dialog.toggle_display_mode()
|
||||
|
||||
self.assertTrue(dialog.fit_to_window)
|
||||
self.assertEqual("原始尺寸", dialog.display_mode_button.text())
|
||||
self.assertLessEqual(dialog.image_label.pixmap().width(), dialog.scroll.viewport().width())
|
||||
self.assertLessEqual(dialog.image_label.pixmap().height(), dialog.scroll.viewport().height())
|
||||
|
||||
self.assert_removed(temp_dir)
|
||||
|
||||
def test_original_image_dialog_keeps_chinese_failure_placeholder(self):
|
||||
with self.make_temp_dir() as temp_dir:
|
||||
dialog = OriginalImageDialog(os.path.join(temp_dir, "missing.jpg"))
|
||||
self.addCleanup(dialog.close)
|
||||
dialog.resize(520, 400)
|
||||
dialog._update_image_display()
|
||||
|
||||
self.assertTrue(dialog.image.isNull())
|
||||
self.assertEqual("图片读取失败", dialog.image_label.text())
|
||||
self.assertFalse(dialog.display_mode_button.isEnabled())
|
||||
|
||||
self.assert_removed(temp_dir)
|
||||
|
||||
def test_cover_gallery_previous_next_switch_visible_tasks_without_resizing(self):
|
||||
with self.make_temp_dir() as temp_dir:
|
||||
cfg, account, tasks, canonicals, _archives = self._cover_gallery_task_set(temp_dir, count=3)
|
||||
|
||||
Reference in New Issue
Block a user