def controller_text(self, var): """ Perform an action on the slide controller. """ log.debug("controller_text var = %s" % var) current_item = self.live_controller.service_item data = [] if current_item: for index, frame in enumerate(current_item.get_frames()): item = {} # Handle text (songs, custom, bibles) if current_item.is_text(): if frame['verseTag']: item['tag'] = str(frame['verseTag']) else: item['tag'] = str(index + 1) item['text'] = str(frame['text']) item['html'] = str(frame['html']) # Handle images, unless a custom thumbnail is given or if thumbnails is disabled elif current_item.is_image() and not frame.get( 'image', '') and Settings().value('remotes/thumbnails'): item['tag'] = str(index + 1) thumbnail_path = os.path.join('images', 'thumbnails', frame['title']) full_thumbnail_path = os.path.join( AppLocation.get_data_path(), thumbnail_path) # Create thumbnail if it doesn't exists if not os.path.exists(full_thumbnail_path): create_thumb(current_item.get_frame_path(index), full_thumbnail_path, False) item['img'] = urllib.request.pathname2url(os.path.sep + thumbnail_path) item['text'] = str(frame['title']) item['html'] = str(frame['title']) else: # Handle presentation etc. item['tag'] = str(index + 1) if current_item.is_capable( ItemCapabilities.HasDisplayTitle): item['title'] = str(frame['display_title']) if current_item.is_capable(ItemCapabilities.HasNotes): item['slide_notes'] = str(frame['notes']) if current_item.is_capable(ItemCapabilities.HasThumbnails) and \ Settings().value('remotes/thumbnails'): # If the file is under our app directory tree send the portion after the match data_path = AppLocation.get_data_path() if frame['image'][0:len(data_path)] == data_path: item['img'] = urllib.request.pathname2url( frame['image'][len(data_path):]) item['text'] = str(frame['title']) item['html'] = str(frame['title']) item['selected'] = (self.live_controller.selected_row == index) data.append(item) json_data = {'results': {'slides': data}} if current_item: json_data['results'][ 'item'] = self.live_controller.service_item.unique_identifier self.do_json_header() return json.dumps(json_data).encode()
def convert_thumbnail(self, file, idx): """ Convert the slide image the application made to a scaled 360px height .png image. """ if self.check_thumbnails(): return if os.path.isfile(file): thumb_path = self.get_thumbnail_path(idx, False) create_thumb(file, thumb_path, False, QtCore.QSize(-1, 360))
def convert_thumbnail(self, file, idx): """ Convert the slide image the application made to a standard 320x240 .png image. """ if self.check_thumbnails(): return if os.path.isfile(file): thumb_path = self.get_thumbnail_path(idx, False) create_thumb(file, thumb_path, False, QtCore.QSize(320, 240))
def test_create_thumb_empty_img(self): """ Test the create_thumb() function with a size of only height specified. """ # GIVEN: An image to create a thumb of. image_path = os.path.join(TEST_PATH, 'church.jpg') thumb_path = os.path.join(TEST_PATH, 'church_thumb.jpg') thumb_size = QtCore.QSize(-1, 100) expected_size_1 = QtCore.QSize(88, 88) expected_size_2 = QtCore.QSize(100, 100) # Remove the thumb so that the test actually tests if the thumb will be created. Maybe it was not deleted in the # last test. try: os.remove(thumb_path) except: pass # Only continue when the thumb does not exist. self.assertFalse( os.path.exists(thumb_path), 'Test was not run, because the thumb already exists.') # WHEN: Create the thumb. with patch('openlp.core.lib.QtGui.QImageReader.size') as mocked_size: mocked_size.return_value = QtCore.QSize(0, 0) icon = create_thumb(image_path, thumb_path, size=None) # THEN: Check if the thumb was created with aspect ratio of 1. self.assertTrue(os.path.exists(thumb_path), 'Test was not ran, because the thumb already exists') self.assertIsInstance(icon, QtGui.QIcon, 'The icon should be a QIcon') self.assertFalse(icon.isNull(), 'The icon should not be null') self.assertEqual(expected_size_1, QtGui.QImageReader(thumb_path).size(), 'The thumb should have the given size') # WHEN: Create the thumb. with patch('openlp.core.lib.QtGui.QImageReader.size') as mocked_size: mocked_size.return_value = QtCore.QSize(0, 0) icon = create_thumb(image_path, thumb_path, size=thumb_size) # THEN: Check if the thumb was created with aspect ratio of 1. self.assertIsInstance(icon, QtGui.QIcon, 'The icon should be a QIcon') self.assertFalse(icon.isNull(), 'The icon should not be null') self.assertEqual(expected_size_2, QtGui.QImageReader(thumb_path).size(), 'The thumb should have the given size') # Remove the thumb so that the test actually tests if the thumb will be created. try: os.remove(thumb_path) except: pass
def save_preview(self, theme_name, preview_pixmap): """ Save the preview QPixmap object to a file """ sample_path_name = self.theme_path / '{file_name}.png'.format( file_name=theme_name) if sample_path_name.exists(): sample_path_name.unlink() preview_pixmap.save(str(sample_path_name), 'png') thumb_path = self.thumb_path / '{name}.png'.format(name=theme_name) create_thumb(sample_path_name, thumb_path, False)
def controller_text(self, var): """ Perform an action on the slide controller. :param var: variable - not used """ log.debug("controller_text var = {var}".format(var=var)) current_item = self.live_controller.service_item data = [] if current_item: for index, frame in enumerate(current_item.get_frames()): item = {} # Handle text (songs, custom, bibles) if current_item.is_text(): if frame['verseTag']: item['tag'] = str(frame['verseTag']) else: item['tag'] = str(index + 1) item['text'] = str(frame['text']) item['html'] = str(frame['html']) # Handle images, unless a custom thumbnail is given or if thumbnails is disabled elif current_item.is_image() and not frame.get('image', '') and Settings().value('remotes/thumbnails'): item['tag'] = str(index + 1) thumbnail_path = os.path.join('images', 'thumbnails', frame['title']) full_thumbnail_path = os.path.join(AppLocation.get_data_path(), thumbnail_path) # Create thumbnail if it doesn't exists if not os.path.exists(full_thumbnail_path): create_thumb(current_item.get_frame_path(index), full_thumbnail_path, False) item['img'] = urllib.request.pathname2url(os.path.sep + thumbnail_path) item['text'] = str(frame['title']) item['html'] = str(frame['title']) else: # Handle presentation etc. item['tag'] = str(index + 1) if current_item.is_capable(ItemCapabilities.HasDisplayTitle): item['title'] = str(frame['display_title']) if current_item.is_capable(ItemCapabilities.HasNotes): item['slide_notes'] = str(frame['notes']) if current_item.is_capable(ItemCapabilities.HasThumbnails) and \ Settings().value('remotes/thumbnails'): # If the file is under our app directory tree send the portion after the match data_path = AppLocation.get_data_path() if frame['image'][0:len(data_path)] == data_path: item['img'] = urllib.request.pathname2url(frame['image'][len(data_path):]) item['text'] = str(frame['title']) item['html'] = str(frame['title']) item['selected'] = (self.live_controller.selected_row == index) data.append(item) json_data = {'results': {'slides': data}} if current_item: json_data['results']['item'] = self.live_controller.service_item.unique_identifier self.do_json_header() return json.dumps(json_data).encode()
def convert_thumbnail(self, image_path, index): """ Convert the slide image the application made to a scaled 360px height .png image. :param openlp.core.common.path.Path image_path: Path to the image to create a thumb nail of :param int index: The index of the slide to create the thumbnail for. :rtype: None """ if self.check_thumbnails(): return if image_path.is_file(): thumb_path = self.get_thumbnail_path(index, False) create_thumb(image_path, thumb_path, False, QtCore.QSize(-1, 360))
def test_create_thumb_empty_img(self): """ Test the create_thumb() function with a size of only height specified. """ # GIVEN: An image to create a thumb of. image_path = RESOURCE_PATH / 'church.jpg' thumb_path = RESOURCE_PATH / 'church_thumb.jpg' thumb_size = QtCore.QSize(-1, 100) expected_size_1 = QtCore.QSize(88, 88) expected_size_2 = QtCore.QSize(100, 100) # Remove the thumb so that the test actually tests if the thumb will be created. Maybe it was not deleted in the # last test. try: thumb_path.unlink() except: pass # Only continue when the thumb does not exist. assert thumb_path.exists( ) is False, 'Test was not run, because the thumb already exists.' # WHEN: Create the thumb. with patch('openlp.core.lib.QtGui.QImageReader.size') as mocked_size: mocked_size.return_value = QtCore.QSize(0, 0) icon = create_thumb(image_path, thumb_path, size=None) # THEN: Check if the thumb was created with aspect ratio of 1. assert thumb_path.exists( ) is True, 'Test was not ran, because the thumb already exists' assert isinstance(icon, QtGui.QIcon), 'The icon should be a QIcon' assert icon.isNull() is False, 'The icon should not be null' assert expected_size_1 == QtGui.QImageReader( str(thumb_path)).size(), 'The thumb should have the given size' # WHEN: Create the thumb. with patch('openlp.core.lib.QtGui.QImageReader.size') as mocked_size: mocked_size.return_value = QtCore.QSize(0, 0) icon = create_thumb(image_path, thumb_path, size=thumb_size) # THEN: Check if the thumb was created with aspect ratio of 1. assert isinstance(icon, QtGui.QIcon), 'The icon should be a QIcon' assert icon.isNull() is False, 'The icon should not be null' assert expected_size_2 == QtGui.QImageReader( str(thumb_path)).size(), 'The thumb should have the given size' # Remove the thumb so that the test actually tests if the thumb will be created. try: thumb_path.unlink() except: pass
def generate_and_save_image(self, theme_name, theme): """ Generate and save a preview image :param str theme_name: The name of the theme. :param theme: The theme data object. """ frame = self.generate_image(theme) sample_path_name = self.theme_path / '{file_name}.png'.format(file_name=theme_name) if sample_path_name.exists(): sample_path_name.unlink() frame.save(str(sample_path_name), 'png') thumb_path = self.thumb_path / '{name}.png'.format(name=theme_name) create_thumb(sample_path_name, thumb_path, False)
def generate_and_save_image(self, name, theme): """ Generate and save a preview image :param name: The name of the theme. :param theme: The theme data object. """ frame = self.generate_image(theme) sample_path_name = os.path.join(self.path, name + '.png') if os.path.exists(sample_path_name): os.unlink(sample_path_name) frame.save(sample_path_name, 'png') thumb = os.path.join(self.thumb_path, '{name}.png'.format(name=name)) create_thumb(sample_path_name, thumb, False)
def generate_and_save_image(self, name, theme): """ Generate and save a preview image :param name: The name of the theme. :param theme: The theme data object. """ frame = self.generate_image(theme) sample_path_name = os.path.join(self.path, name + '.png') if os.path.exists(sample_path_name): os.unlink(sample_path_name) frame.save(sample_path_name, 'png') thumb = os.path.join(self.thumb_path, '%s.png' % name) create_thumb(sample_path_name, thumb, False)
def load_themes(self): """ Loads the theme lists and triggers updates across the whole system using direct calls or core functions and events for the plugins. The plugins will call back in to get the real list if they want it. """ self._theme_list.clear() self.theme_list_widget.clear() files = AppLocation.get_files(self.settings_section, '/*.json') # Sort the themes by its name considering language specific files.sort(key=lambda file_name: get_locale_key(str(file_name))) # now process the file list of png files for file in files: # check to see file is in theme root directory theme_path = self.theme_path / file if theme_path.exists(): text_name = theme_path.stem if text_name == self.global_theme: name = translate('OpenLP.ThemeManager', '{name} (default)').format(name=text_name) else: name = text_name thumb_path = self.thumb_path / '{name}.png'.format( name=text_name) item_name = QtWidgets.QListWidgetItem(name) if validate_thumb(theme_path, thumb_path): icon = build_icon(thumb_path) else: icon = create_thumb(theme_path, thumb_path) item_name.setIcon(icon) item_name.setData(QtCore.Qt.UserRole, text_name) self.theme_list_widget.addItem(item_name) self._theme_list[text_name] = self._get_theme_data(text_name) self._push_themes()
def load_themes(self): """ Loads the theme lists and triggers updates across the whole system using direct calls or core functions and events for the plugins. The plugins will call back in to get the real list if they want it. """ self.theme_list = [] self.theme_list_widget.clear() files = AppLocation.get_files(self.settings_section, '.png') # Sort the themes by its name considering language specific files.sort(key=lambda file_name: get_locale_key(str(file_name))) # now process the file list of png files for name in files: # check to see file is in theme root directory theme = os.path.join(self.path, name) if os.path.exists(theme): text_name = os.path.splitext(name)[0] if text_name == self.global_theme: name = translate('OpenLP.ThemeManager', '%s (default)') % text_name else: name = text_name thumb = os.path.join(self.thumb_path, '%s.png' % text_name) item_name = QtGui.QListWidgetItem(name) if validate_thumb(theme, thumb): icon = build_icon(thumb) else: icon = create_thumb(theme, thumb) item_name.setIcon(icon) item_name.setData(QtCore.Qt.UserRole, text_name) self.theme_list_widget.addItem(item_name) self.theme_list.append(text_name) self._push_themes()
def create_thumb_with_size_test(self): """ Test the create_thumb() function """ # GIVEN: An image to create a thumb of. image_path = os.path.join(TEST_PATH, 'church.jpg') thumb_path = os.path.join(TEST_PATH, 'church_thumb.jpg') thumb_size = QtCore.QSize(10, 20) # Remove the thumb so that the test actually tests if the thumb will be created. Maybe it was not deleted in the # last test. try: os.remove(thumb_path) except: pass # Only continue when the thumb does not exist. self.assertFalse(os.path.exists(thumb_path), 'Test was not run, because the thumb already exists.') # WHEN: Create the thumb. icon = create_thumb(image_path, thumb_path, size=thumb_size) # THEN: Check if the thumb was created. self.assertTrue(os.path.exists(thumb_path), 'Test was not ran, because the thumb already exists') self.assertIsInstance(icon, QtGui.QIcon, 'The icon should be a QIcon') self.assertFalse(icon.isNull(), 'The icon should not be null') self.assertEqual(thumb_size, QtGui.QImageReader(thumb_path).size(), 'The thumb should have the given size') # Remove the thumb so that the test actually tests if the thumb will be created. try: os.remove(thumb_path) except: pass
def test_create_thumb_height_only(self): """ Test the create_thumb() function with a size of only height specified. """ # GIVEN: An image to create a thumb of. image_path = os.path.join(TEST_PATH, 'church.jpg') thumb_path = os.path.join(TEST_PATH, 'church_thumb.jpg') thumb_size = QtCore.QSize(-1, 100) expected_size = QtCore.QSize(72, 100) # Remove the thumb so that the test actually tests if the thumb will be created. Maybe it was not deleted in the # last test. try: os.remove(thumb_path) except: pass # Only continue when the thumb does not exist. self.assertFalse(os.path.exists(thumb_path), 'Test was not run, because the thumb already exists.') # WHEN: Create the thumb. icon = create_thumb(image_path, thumb_path, size=thumb_size) # THEN: Check if the thumb was created, retaining its aspect ratio. self.assertTrue(os.path.exists(thumb_path), 'Test was not ran, because the thumb already exists') self.assertIsInstance(icon, QtGui.QIcon, 'The icon should be a QIcon') self.assertFalse(icon.isNull(), 'The icon should not be null') self.assertEqual(expected_size, QtGui.QImageReader(thumb_path).size(), 'The thumb should have the given size') # Remove the thumb so that the test actually tests if the thumb will be created. try: os.remove(thumb_path) except: pass
def create_thumb_with_size_test(self): """ Test the create_thumb() function """ # GIVEN: An image to create a thumb of. image_path = os.path.join(TEST_PATH, 'church.jpg') thumb_path = os.path.join(TEST_PATH, 'church_thumb.jpg') thumb_size = QtCore.QSize(10, 20) # Remove the thumb so that the test actually tests if the thumb will be created. Maybe it was not deleted in the # last test. try: os.remove(thumb_path) except: pass # Only continue when the thumb does not exist. assert not os.path.exists(thumb_path), 'Test was not ran, because the thumb already exists.' # WHEN: Create the thumb. icon = create_thumb(image_path, thumb_path, size=thumb_size) # THEN: Check if the thumb was created. assert os.path.exists(thumb_path), 'Test was not ran, because the thumb already exists.' assert isinstance(icon, QtGui.QIcon), 'The icon should be a QIcon.' assert not icon.isNull(), 'The icon should not be null.' assert QtGui.QImageReader(thumb_path).size() == thumb_size, 'The thumb should have the given size.' # Remove the thumb so that the test actually tests if the thumb will be created. try: os.remove(thumb_path) except: pass
def load_themes(self): """ Loads the theme lists and triggers updates across the whole system using direct calls or core functions and events for the plugins. The plugins will call back in to get the real list if they want it. """ self.theme_list = [] self.theme_list_widget.clear() files = AppLocation.get_files(self.settings_section, '.png') # Sort the themes by its name considering language specific files.sort(key=lambda file_name: get_locale_key(str(file_name))) # now process the file list of png files for name in files: # check to see file is in theme root directory theme = os.path.join(self.path, name) if os.path.exists(theme): text_name = os.path.splitext(name)[0] if text_name == self.global_theme: name = translate('OpenLP.ThemeManager', '{name} (default)').format(name=text_name) else: name = text_name thumb = os.path.join(self.thumb_path, '{name}.png'.format(name=text_name)) item_name = QtWidgets.QListWidgetItem(name) if validate_thumb(theme, thumb): icon = build_icon(thumb) else: icon = create_thumb(theme, thumb) item_name.setIcon(icon) item_name.setData(QtCore.Qt.UserRole, text_name) self.theme_list_widget.addItem(item_name) self.theme_list.append(text_name) self._push_themes()
def test_create_thumb_empty_img(self): """ Test the create_thumb() function with a size of only height specified. """ # GIVEN: An image to create a thumb of. image_path = os.path.join(TEST_PATH, 'church.jpg') thumb_path = os.path.join(TEST_PATH, 'church_thumb.jpg') thumb_size = QtCore.QSize(-1, 100) expected_size_1 = QtCore.QSize(88, 88) expected_size_2 = QtCore.QSize(100, 100) # Remove the thumb so that the test actually tests if the thumb will be created. Maybe it was not deleted in the # last test. try: os.remove(thumb_path) except: pass # Only continue when the thumb does not exist. self.assertFalse(os.path.exists(thumb_path), 'Test was not run, because the thumb already exists.') # WHEN: Create the thumb. with patch('openlp.core.lib.QtGui.QImageReader.size') as mocked_size: mocked_size.return_value = QtCore.QSize(0, 0) icon = create_thumb(image_path, thumb_path, size=None) # THEN: Check if the thumb was created with aspect ratio of 1. self.assertTrue(os.path.exists(thumb_path), 'Test was not ran, because the thumb already exists') self.assertIsInstance(icon, QtGui.QIcon, 'The icon should be a QIcon') self.assertFalse(icon.isNull(), 'The icon should not be null') self.assertEqual(expected_size_1, QtGui.QImageReader(thumb_path).size(), 'The thumb should have the given size') # WHEN: Create the thumb. with patch('openlp.core.lib.QtGui.QImageReader.size') as mocked_size: mocked_size.return_value = QtCore.QSize(0, 0) icon = create_thumb(image_path, thumb_path, size=thumb_size) # THEN: Check if the thumb was created with aspect ratio of 1. self.assertIsInstance(icon, QtGui.QIcon, 'The icon should be a QIcon') self.assertFalse(icon.isNull(), 'The icon should not be null') self.assertEqual(expected_size_2, QtGui.QImageReader(thumb_path).size(), 'The thumb should have the given size') # Remove the thumb so that the test actually tests if the thumb will be created. try: os.remove(thumb_path) except: pass
def save_theme(self, theme, image=None, background_override=None): """ Writes the theme to the disk and including the background image and thumbnail if necessary :param Theme theme: The theme data object. :param image: The theme thumbnail. Optionally. :param background_override: Background to use rather than background_source. Optionally. :rtype: None """ name = theme.theme_name theme_pretty = theme.export_theme(self.theme_path) theme_dir = self.theme_path / name create_paths(theme_dir) theme_path = theme_dir / '{file_name}.json'.format(file_name=name) try: theme_path.write_text(theme_pretty) except OSError: self.log_exception('Saving theme to file failed') if theme.background_source and theme.background_filename: background_file = background_override # Use theme source image if override doesn't exist if not background_file or not background_file.exists(): background_file = theme.background_source if self.old_background_image_path and theme.background_filename != self.old_background_image_path: delete_file(self.old_background_image_path) if not background_file.exists(): self.log_warning( 'Background does not exist, retaining cached background') elif background_file != theme.background_filename: try: shutil.copyfile(background_file, theme.background_filename) except OSError: self.log_exception('Failed to save theme image') if image: sample_path_name = self.theme_path / '{file_name}.png'.format( file_name=name) if sample_path_name.exists(): sample_path_name.unlink() image.save(str(sample_path_name), 'png') thumb_path = self.thumb_path / '{name}.png'.format(name=name) create_thumb(sample_path_name, thumb_path, False) else: self.update_preview_images([name])
def load_full_list(self, images, initial_load=False, open_group=None): """ Replace the list of images and groups in the interface. ``images`` A List of ImageFilenames objects that will be used to reload the mediamanager list. ``initial_load`` When set to False, the busy cursor and progressbar will be shown while loading images. ``open_group`` ImageGroups object of the group that must be expanded after reloading the list in the interface. """ if not initial_load: self.application.set_busy_cursor() self.main_window.display_progress_bar(len(images)) self.list_view.clear() # Load the list of groups and add them to the treeView. group_items = {} self.add_sub_groups(group_items, parent_group_id=0) if open_group is not None: self.expand_group(open_group.id) # Sort the images by its filename considering language specific. # characters. images.sort(key=lambda image_object: get_locale_key(os.path.split(str(image_object.filename))[1])) for imageFile in images: log.debug('Loading image: %s', imageFile.filename) filename = os.path.split(imageFile.filename)[1] thumb = os.path.join(self.servicePath, filename) if not os.path.exists(imageFile.filename): icon = build_icon(':/general/general_delete.png') else: if validate_thumb(imageFile.filename, thumb): icon = build_icon(thumb) else: icon = create_thumb(imageFile.filename, thumb) item_name = QtGui.QTreeWidgetItem(filename) item_name.setText(0, filename) item_name.setIcon(0, icon) item_name.setToolTip(0, imageFile.filename) item_name.setData(0, QtCore.Qt.UserRole, imageFile) if imageFile.group_id == 0: self.list_view.addTopLevelItem(item_name) else: group_items[imageFile.group_id].addChild(item_name) if not initial_load: self.main_window.increment_progress_bar() if not initial_load: self.main_window.finished_progress_bar() self.application.set_normal_cursor()
def load_full_list(self, images, initial_load=False, open_group=None): """ Replace the list of images and groups in the interface. :param list[openlp.plugins.images.lib.db.ImageFilenames] images: A List of Image Filenames objects that will be used to reload the mediamanager list. :param initial_load: When set to False, the busy cursor and progressbar will be shown while loading images. :param open_group: ImageGroups object of the group that must be expanded after reloading the list in the interface. """ if not initial_load: self.application.set_busy_cursor() self.main_window.display_progress_bar(len(images)) self.list_view.clear() # Load the list of groups and add them to the treeView. group_items = {} self.add_sub_groups(group_items, parent_group_id=0) if open_group is not None: self.expand_group(open_group.id) # Sort the images by its filename considering language specific. # characters. images.sort(key=lambda image_object: get_natural_key(image_object. file_path.name)) for image in images: log.debug('Loading image: {name}'.format(name=image.file_path)) file_name = image.file_path.name thumbnail_path = self.generate_thumbnail_path(image) if not image.file_path.exists(): icon = UiIcons().delete else: if validate_thumb(image.file_path, thumbnail_path): icon = build_icon(thumbnail_path) else: icon = create_thumb(image.file_path, thumbnail_path) item_name = QtWidgets.QTreeWidgetItem([file_name]) item_name.setText(0, file_name) item_name.setIcon(0, icon) item_name.setToolTip(0, str(image.file_path)) item_name.setData(0, QtCore.Qt.UserRole, image) if image.group_id == 0: self.list_view.addTopLevelItem(item_name) else: group_items[image.group_id].addChild(item_name) if not initial_load: self.main_window.increment_progress_bar() if not initial_load: self.main_window.finished_progress_bar() self.application.set_normal_cursor()
def test_create_thumb_height_only(self): """ Test the create_thumb() function with a size of only height specified. """ # GIVEN: An image to create a thumb of. image_path = RESOURCE_PATH / 'church.jpg' thumb_path = RESOURCE_PATH / 'church_thumb.jpg' thumb_size = QtCore.QSize(-1, 100) expected_size = QtCore.QSize(72, 100) # Remove the thumb so that the test actually tests if the thumb will be created. Maybe it was not deleted in the # last test. try: thumb_path.unlink() except: pass # Only continue when the thumb does not exist. assert thumb_path.exists( ) is False, 'Test was not run, because the thumb already exists.' # WHEN: Create the thumb. icon = create_thumb(image_path, thumb_path, size=thumb_size) # THEN: Check if the thumb was created, retaining its aspect ratio. assert thumb_path.exists( ) is True, 'Test was not ran, because the thumb already exists' assert isinstance(icon, QtGui.QIcon), 'The icon should be a QIcon' assert icon.isNull() is False, 'The icon should not be null' assert expected_size == QtGui.QImageReader( str(thumb_path)).size(), 'The thumb should have the given size' # Remove the thumb so that the test actually tests if the thumb will be created. try: thumb_path.unlink() except: pass
def load_list(self, file_paths, target_group=None, initial_load=False): """ Add presentations into the media manager. This is called both on initial load of the plugin to populate with existing files, and when the user adds new files via the media manager. :param list[pathlib.Path] file_paths: List of file paths to add to the media manager. """ current_paths = self.get_file_list() titles = [file_path.name for file_path in current_paths] self.application.set_busy_cursor() if not initial_load: self.main_window.display_progress_bar(len(file_paths)) # Sort the presentations by its filename considering language specific characters. file_paths.sort(key=lambda file_path: get_natural_key(file_path.name)) for file_path in file_paths: if not initial_load: self.main_window.increment_progress_bar() if current_paths.count(file_path) > 0: continue file_name = file_path.name if not file_path.exists(): item_name = QtWidgets.QListWidgetItem(file_name) item_name.setIcon(UiIcons().delete) item_name.setData(QtCore.Qt.UserRole, file_path) item_name.setToolTip(str(file_path)) self.list_view.addItem(item_name) else: if titles.count(file_name) > 0: if not initial_load: critical_error_message_box( translate('PresentationPlugin.MediaItem', 'File Exists'), translate( 'PresentationPlugin.MediaItem', 'A presentation with that filename already exists.' )) continue controller_name = self.find_controller_by_type(file_path) if controller_name: controller = self.controllers[controller_name] doc = controller.add_document(file_path) thumbnail_path = doc.get_thumbnail_folder() / 'icon.png' preview_path = doc.get_thumbnail_path(1, True) if not preview_path and not initial_load: doc.load_presentation() preview_path = doc.get_thumbnail_path(1, True) doc.close_presentation() if not (preview_path and preview_path.exists()): icon = UiIcons().delete else: if validate_thumb(preview_path, thumbnail_path): icon = build_icon(thumbnail_path) else: icon = create_thumb(preview_path, thumbnail_path) else: if initial_load: icon = UiIcons().delete else: critical_error_message_box( UiStrings().UnsupportedFile, translate( 'PresentationPlugin.MediaItem', 'This type of presentation is not supported.')) continue item_name = QtWidgets.QListWidgetItem(file_name) item_name.setData(QtCore.Qt.UserRole, file_path) item_name.setIcon(icon) item_name.setToolTip(str(file_path)) self.list_view.addItem(item_name) if not initial_load: self.main_window.finished_progress_bar() self.application.set_normal_cursor()
def controller_text(request): """ Perform an action on the slide controller. :param request: the http request - not used """ log.debug("controller_text ") live_controller = Registry().get('live_controller') current_item = live_controller.service_item data = [] if current_item: for index, frame in enumerate(current_item.get_frames()): item = {} # Handle text (songs, custom, bibles) if current_item.is_text(): if frame['verseTag']: item['tag'] = str(frame['verseTag']) else: item['tag'] = str(index + 1) item['chords_text'] = str(frame['chords_text']) item['text'] = str(frame['text']) item['html'] = str(frame['html']) # Handle images, unless a custom thumbnail is given or if thumbnails is disabled elif current_item.is_image() and not frame.get( 'image', '') and Settings().value('api/thumbnails'): item['tag'] = str(index + 1) thumbnail_path = os.path.join('images', 'thumbnails', frame['title']) full_thumbnail_path = AppLocation.get_data_path( ) / thumbnail_path # Create thumbnail if it doesn't exists if not full_thumbnail_path.exists(): create_thumb(Path(current_item.get_frame_path(index)), full_thumbnail_path, False) Registry().get('image_manager').add_image( str(full_thumbnail_path), frame['title'], None, 88, 88) item['img'] = urllib.request.pathname2url(os.path.sep + str(thumbnail_path)) item['text'] = str(frame['title']) item['html'] = str(frame['title']) else: # Handle presentation etc. item['tag'] = str(index + 1) if current_item.is_capable(ItemCapabilities.HasDisplayTitle): item['title'] = str(frame['display_title']) if current_item.is_capable(ItemCapabilities.HasNotes): item['slide_notes'] = str(frame['notes']) if current_item.is_capable( ItemCapabilities.HasThumbnails) and Settings().value( 'api/thumbnails'): # If the file is under our app directory tree send the portion after the match data_path = str(AppLocation.get_data_path()) if frame['image'][0:len(data_path)] == data_path: item['img'] = urllib.request.pathname2url( frame['image'][len(data_path):]) Registry().get('image_manager').add_image( frame['image'], frame['title'], None, 88, 88) item['text'] = str(frame['title']) item['html'] = str(frame['title']) item['selected'] = (live_controller.selected_row == index) item['title'] = current_item.title data.append(item) json_data = {'results': {'slides': data}} if current_item: json_data['results'][ 'item'] = live_controller.service_item.unique_identifier return json_data
def load_list(self, files, target_group=None, initial_load=False): """ Add presentations into the media manager. This is called both on initial load of the plugin to populate with existing files, and when the user adds new files via the media manager. """ current_list = self.get_file_list() titles = [os.path.split(file)[1] for file in current_list] self.application.set_busy_cursor() if not initial_load: self.main_window.display_progress_bar(len(files)) # Sort the presentations by its filename considering language specific characters. files.sort(key=lambda filename: get_locale_key(os.path.split(str(filename))[1])) for file in files: if not initial_load: self.main_window.increment_progress_bar() if current_list.count(file) > 0: continue filename = os.path.split(str(file))[1] if not os.path.exists(file): item_name = QtGui.QListWidgetItem(filename) item_name.setIcon(build_icon(ERROR_IMAGE)) item_name.setData(QtCore.Qt.UserRole, file) item_name.setToolTip(file) self.list_view.addItem(item_name) else: if titles.count(filename) > 0: if not initial_load: critical_error_message_box(translate('PresentationPlugin.MediaItem', 'File Exists'), translate('PresentationPlugin.MediaItem', 'A presentation with that filename already exists.') ) continue controller_name = self.findControllerByType(filename) if controller_name: controller = self.controllers[controller_name] doc = controller.add_document(str(file)) thumb = os.path.join(doc.get_thumbnail_folder(), 'icon.png') preview = doc.get_thumbnail_path(1, True) if not preview and not initial_load: doc.load_presentation() preview = doc.get_thumbnail_path(1, True) doc.close_presentation() if not (preview and os.path.exists(preview)): icon = build_icon(':/general/general_delete.png') else: if validate_thumb(preview, thumb): icon = build_icon(thumb) else: icon = create_thumb(preview, thumb) else: if initial_load: icon = build_icon(':/general/general_delete.png') else: critical_error_message_box(UiStrings().UnsupportedFile, translate('PresentationPlugin.MediaItem', 'This type of presentation is not supported.')) continue item_name = QtGui.QListWidgetItem(filename) item_name.setData(QtCore.Qt.UserRole, file) item_name.setIcon(icon) item_name.setToolTip(file) self.list_view.addItem(item_name) if not initial_load: self.main_window.finished_progress_bar() self.application.set_normal_cursor()
def load_list(self, files, target_group=None, initial_load=False): """ Add presentations into the media manager. This is called both on initial load of the plugin to populate with existing files, and when the user adds new files via the media manager. """ current_list = self.get_file_list() titles = [os.path.split(file)[1] for file in current_list] self.application.set_busy_cursor() if not initial_load: self.main_window.display_progress_bar(len(files)) # Sort the presentations by its filename considering language specific characters. files.sort(key=lambda filename: get_locale_key( os.path.split(str(filename))[1])) for file in files: if not initial_load: self.main_window.increment_progress_bar() if current_list.count(file) > 0: continue filename = os.path.split(file)[1] if not os.path.exists(file): item_name = QtWidgets.QListWidgetItem(filename) item_name.setIcon(build_icon(ERROR_IMAGE)) item_name.setData(QtCore.Qt.UserRole, file) item_name.setToolTip(file) self.list_view.addItem(item_name) else: if titles.count(filename) > 0: if not initial_load: critical_error_message_box( translate('PresentationPlugin.MediaItem', 'File Exists'), translate( 'PresentationPlugin.MediaItem', 'A presentation with that filename already exists.' )) continue controller_name = self.find_controller_by_type(filename) if controller_name: controller = self.controllers[controller_name] doc = controller.add_document(file) thumb = os.path.join(doc.get_thumbnail_folder(), 'icon.png') preview = doc.get_thumbnail_path(1, True) if not preview and not initial_load: doc.load_presentation() preview = doc.get_thumbnail_path(1, True) doc.close_presentation() if not (preview and os.path.exists(preview)): icon = build_icon(':/general/general_delete.png') else: if validate_thumb(preview, thumb): icon = build_icon(thumb) else: icon = create_thumb(preview, thumb) else: if initial_load: icon = build_icon(':/general/general_delete.png') else: critical_error_message_box( UiStrings().UnsupportedFile, translate( 'PresentationPlugin.MediaItem', 'This type of presentation is not supported.')) continue item_name = QtWidgets.QListWidgetItem(filename) item_name.setData(QtCore.Qt.UserRole, file) item_name.setIcon(icon) item_name.setToolTip(file) self.list_view.addItem(item_name) if not initial_load: self.main_window.finished_progress_bar() self.application.set_normal_cursor()