def get_songbook_key(text): """ Get the key to sort by :param text: the text tuple to be processed. """ return get_natural_key('{0} {1} {2}'.format( text[1], text[0], text[2]))
def init_on_load(self): """ Precompute a natural sorting, locale aware sorting key. Song sorting is performance sensitive operation. To get maximum speed lets precompute the sorting key. """ self.sort_key = get_natural_key(self.title)
def dnd_move_internal(self, target): """ Handle drag-and-drop moving of images within the media manager :param target: This contains the QTreeWidget that is the target of the DnD action """ items_to_move = self.list_view.selectedItems() # Determine group to move images to target_group = target if target_group is not None and isinstance( target_group.data(0, QtCore.Qt.UserRole), ImageFilenames): target_group = target.parent() # Move to toplevel if target_group is None: target_group = self.list_view.invisibleRootItem() target_group.setData(0, QtCore.Qt.UserRole, ImageGroups()) target_group.data(0, QtCore.Qt.UserRole).id = 0 # Move images in the treeview items_to_save = [] for item in items_to_move: if isinstance(item.data(0, QtCore.Qt.UserRole), ImageFilenames): if isinstance(item.parent(), QtWidgets.QTreeWidgetItem): item.parent().removeChild(item) else: self.list_view.invisibleRootItem().removeChild(item) target_group.addChild(item) item.setSelected(True) item_data = item.data(0, QtCore.Qt.UserRole) item_data.group_id = target_group.data(0, QtCore.Qt.UserRole).id items_to_save.append(item_data) target_group.setExpanded(True) # Update the group ID's of the images in the database self.manager.save_objects(items_to_save) # Sort the target group group_items = [] image_items = [] for item in target_group.takeChildren(): if isinstance(item.data(0, QtCore.Qt.UserRole), ImageGroups): group_items.append(item) if isinstance(item.data(0, QtCore.Qt.UserRole), ImageFilenames): image_items.append(item) group_items.sort(key=lambda item: get_natural_key(item.text(0))) target_group.addChildren(group_items) image_items.sort(key=lambda item: get_natural_key(item.text(0))) target_group.addChildren(image_items)
def load_list(self, media, target_group=None): """ Load the media list :param media: The media :param target_group: """ media.sort(key=lambda file_path: get_natural_key(os.path.split(str(file_path))[1])) file_name = translate('MediaPlugin.MediaItem', 'Live Stream') item_name = QtWidgets.QListWidgetItem(file_name) item_name.setIcon(UiIcons().video) item_name.setData(QtCore.Qt.UserRole, UiStrings().LiveStream) item_name.setToolTip(translate('MediaPlugin.MediaItem', 'Show Live Stream')) self.list_view.addItem(item_name) for track in media: track_str = str(track) track_info = QtCore.QFileInfo(track_str) item_name = None # Dont add the live stream in when reloading the UI. if track_str == UiStrings().LiveStream: continue elif track_str.startswith('optical:'): # Handle optical based item (file_name, title, audio_track, subtitle_track, start, end, clip_name) = parse_optical_path(track_str) item_name = QtWidgets.QListWidgetItem(clip_name) item_name.setIcon(UiIcons().optical) item_name.setData(QtCore.Qt.UserRole, track) item_name.setToolTip('{name}@{start}-{end}'.format(name=file_name, start=format_milliseconds(start), end=format_milliseconds(end))) elif not os.path.exists(track): # File doesn't exist, mark as error. file_name = os.path.split(track_str)[1] item_name = QtWidgets.QListWidgetItem(file_name) item_name.setIcon(UiIcons().error) item_name.setData(QtCore.Qt.UserRole, track) item_name.setToolTip(track_str) elif track_info.isFile(): # Normal media file handling. file_name = os.path.split(track_str)[1] item_name = QtWidgets.QListWidgetItem(file_name) search = file_name.split('.')[-1].lower() if search in AUDIO_EXT: item_name.setIcon(UiIcons().audio) else: item_name.setIcon(UiIcons().video) item_name.setData(QtCore.Qt.UserRole, track) item_name.setToolTip(track_str) if item_name: self.list_view.addItem(item_name)
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 get_list(self, media_type=MediaType.Audio): """ Get the list of media, optional select media type. :param media_type: Type to get, defaults to audio. :return: The media list """ media_file_paths = Settings().value(self.settings_section + '/media files') media_file_paths.sort(key=lambda file_path: get_natural_key(os.path.split(str(file_path))[1])) if media_type == MediaType.Audio: extension = AUDIO_EXT else: extension = VIDEO_EXT extension = [x[1:] for x in extension] media = [x for x in media_file_paths if os.path.splitext(x)[1] in extension] return media
def load_list(self, media, target_group=None): """ Load the media list :param media: The media :param target_group: """ media.sort(key=lambda file_name: get_natural_key( os.path.split(str(file_name))[1])) for track in media: track_info = QtCore.QFileInfo(track) item_name = None if track.startswith('optical:'): # Handle optical based item (file_name, title, audio_track, subtitle_track, start, end, clip_name) = parse_optical_path(track) item_name = QtWidgets.QListWidgetItem(clip_name) item_name.setIcon(UiIcons().optical) item_name.setData(QtCore.Qt.UserRole, track) item_name.setToolTip('{name}@{start}-{end}'.format( name=file_name, start=format_milliseconds(start), end=format_milliseconds(end))) elif not os.path.exists(track): # File doesn't exist, mark as error. file_name = os.path.split(str(track))[1] item_name = QtWidgets.QListWidgetItem(file_name) item_name.setIcon(UiIcons().error) item_name.setData(QtCore.Qt.UserRole, track) item_name.setToolTip(track) elif track_info.isFile(): # Normal media file handling. file_name = os.path.split(str(track))[1] item_name = QtWidgets.QListWidgetItem(file_name) search = file_name.split('.')[-1].lower() if '*.{text}'.format( text=search ) in self.media_controller.audio_extensions_list: item_name.setIcon(UiIcons().audio) else: item_name.setIcon(UiIcons().video) item_name.setData(QtCore.Qt.UserRole, track) item_name.setToolTip(track) if item_name: self.list_view.addItem(item_name)
def get_list(self, media_type=MediaType.Audio): """ Get the list of media, optional select media type. :param media_type: Type to get, defaults to audio. :return: The media list """ media_file_paths = Settings().value(self.settings_section + '/media files') media_file_paths.sort( key=lambda file_path: get_natural_key(file_path.name)) if media_type == MediaType.Audio: extension = self.media_controller.audio_extensions_list else: extension = self.media_controller.video_extensions_list extension = [x[1:] for x in extension] media = [x for x in media_file_paths if x.suffix in extension] return media
def fill_groups_combobox(self, combobox, parent_group_id=0, prefix=''): """ Recursively add groups to the combobox in the 'Add group' dialog. :param combobox: The QComboBox to add the options to. :param parent_group_id: The ID of the group that will be added. :param prefix: A string containing the prefix that will be added in front of the groupname for each level of the tree. """ if parent_group_id == 0: combobox.clear() combobox.top_level_group_added = False image_groups = self.manager.get_all_objects( ImageGroups, ImageGroups.parent_id == parent_group_id) image_groups.sort( key=lambda group_object: get_natural_key(group_object.group_name)) for image_group in image_groups: combobox.addItem(prefix + image_group.group_name, image_group.id) self.fill_groups_combobox(combobox, image_group.id, prefix + ' ')
def add_sub_groups(self, group_list, parent_group_id): """ Recursively add subgroups to the given parent group in a QTreeWidget. :param group_list: The List object that contains all QTreeWidgetItems. :param parent_group_id: The ID of the group that will be added recursively. """ image_groups = self.manager.get_all_objects( ImageGroups, ImageGroups.parent_id == parent_group_id) image_groups.sort( key=lambda group_object: get_natural_key(group_object.group_name)) folder_icon = UiIcons().group for image_group in image_groups: group = QtWidgets.QTreeWidgetItem() group.setText(0, image_group.group_name) group.setData(0, QtCore.Qt.UserRole, image_group) group.setIcon(0, folder_icon) if parent_group_id == 0: self.list_view.addTopLevelItem(group) else: group_list[parent_group_id].addChild(group) group_list[image_group.id] = group self.add_sub_groups(group_list, image_group.id)
def get_book_key(book): """Get the key to sort by""" return get_natural_key(book.name)
def get_topic_key(topic): """Get the key to sort by""" return get_natural_key(topic.name)
def get_author_key(author): """Get the key to sort by""" return get_natural_key(author.display_name)
def __lt__(self, other): return get_natural_key(self.title) < get_natural_key(other.title)
def __eq__(self, other): return get_natural_key(self.title) == get_natural_key(other.title)
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 get_songbook_key(text_array): """ Get the key to sort by :param text_array: the result text to be processed. """ return get_natural_key(text_array[1]), get_natural_key(text_array[0]), get_natural_key(text_array[2])
def get_cclinumber_key(song): """Get the key to sort by""" return get_natural_key(song.ccli_number), song.sort_key
def get_theme_key(song): """Get the key to sort by""" return get_natural_key(song.theme_name), song.sort_key