1
0
mirror of https://gitlab.com/openlp/openlp.git synced 2024-10-31 16:54:40 +00:00
openlp/openlp/plugins/songs/forms/songimportform.py

515 lines
26 KiB
Python
Raw Normal View History

2010-04-02 12:23:40 +00:00
# -*- coding: utf-8 -*-
2013-01-05 22:17:30 +00:00
# vim: autoindent shiftwidth=4 expandtab textwidth=120 tabstop=4 softtabstop=4
2010-04-02 12:23:40 +00:00
###############################################################################
# OpenLP - Open Source Lyrics Projection #
# --------------------------------------------------------------------------- #
2016-12-31 11:01:36 +00:00
# Copyright (c) 2008-2017 OpenLP Developers #
2010-04-02 12:23:40 +00:00
# --------------------------------------------------------------------------- #
# This program is free software; you can redistribute it and/or modify it #
# under the terms of the GNU General Public License as published by the Free #
# Software Foundation; version 2 of the License. #
# #
# This program is distributed in the hope that it will be useful, but WITHOUT #
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or #
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for #
# more details. #
# #
# You should have received a copy of the GNU General Public License along #
# with this program; if not, write to the Free Software Foundation, Inc., 59 #
# Temple Place, Suite 330, Boston, MA 02111-1307 USA #
###############################################################################
2011-01-13 17:55:29 +00:00
"""
The song import functions for OpenLP.
"""
import codecs
2010-04-02 12:23:40 +00:00
import logging
2010-08-20 19:40:07 +00:00
import os
2010-04-02 12:23:40 +00:00
2015-11-07 00:49:40 +00:00
from PyQt5 import QtCore, QtWidgets
2010-04-02 12:23:40 +00:00
2014-03-16 21:25:23 +00:00
from openlp.core.common import RegistryProperties, Settings, UiStrings, translate
2017-08-07 20:50:01 +00:00
from openlp.core.common.path import path_to_str, str_to_path
from openlp.core.lib.ui import critical_error_message_box
2017-08-07 20:50:01 +00:00
from openlp.core.ui.lib.filedialog import FileDialog
2016-04-22 18:25:57 +00:00
from openlp.core.ui.lib.wizard import OpenLPWizard, WizardStrings
from openlp.plugins.songs.lib.importer import SongFormat, SongFormatSelect
2010-04-02 12:23:40 +00:00
log = logging.getLogger(__name__)
2014-03-16 21:25:23 +00:00
class SongImportForm(OpenLPWizard, RegistryProperties):
2010-04-02 12:23:40 +00:00
"""
2010-08-08 14:38:51 +00:00
This is the Song Import Wizard, which allows easy importing of Songs
into OpenLP from other formats like OpenLyrics, OpenSong and CCLI.
2010-04-02 12:23:40 +00:00
"""
2015-11-07 00:49:40 +00:00
completeChanged = QtCore.pyqtSignal()
2013-08-31 18:17:38 +00:00
log.info('SongImportForm loaded')
2010-04-02 12:23:40 +00:00
def __init__(self, parent, plugin):
2010-04-02 12:23:40 +00:00
"""
Instantiate the wizard, and run any extra setup we need to.
2014-03-04 18:49:30 +00:00
:param parent: The QWidget-derived parent of the wizard.
:param plugin: The songs plugin.
2010-04-02 12:23:40 +00:00
"""
super(SongImportForm, self).__init__(parent, plugin, 'songImportWizard', ':/wizards/wizard_song.bmp')
self.clipboard = self.main_window.clipboard
2011-01-13 17:55:29 +00:00
def setupUi(self, image):
"""
Set up the song wizard UI.
"""
2013-03-14 22:22:18 +00:00
self.format_widgets = dict([(song_format, {}) for song_format in SongFormat.get_format_list()])
2013-07-18 19:49:44 +00:00
super(SongImportForm, self).setupUi(image)
2013-03-07 08:05:43 +00:00
self.current_format = SongFormat.OpenLyrics
self.format_stack.setCurrentIndex(self.current_format)
2014-03-04 18:49:30 +00:00
self.format_combo_box.currentIndexChanged.connect(self.on_current_index_changed)
2014-03-04 18:49:30 +00:00
def on_current_index_changed(self, index):
"""
Called when the format combo box's index changed.
"""
2013-03-07 08:05:43 +00:00
self.current_format = index
self.format_stack.setCurrentIndex(index)
2015-11-07 00:49:40 +00:00
self.source_page.completeChanged.emit()
2011-01-13 17:55:29 +00:00
2013-03-14 22:22:18 +00:00
def custom_init(self):
2011-01-13 17:55:29 +00:00
"""
Song wizard specific initialisation.
"""
2013-03-14 22:22:18 +00:00
for song_format in SongFormat.get_format_list():
2013-08-31 18:17:38 +00:00
if not SongFormat.get(song_format, 'availability'):
self.format_widgets[song_format]['disabled_widget'].setVisible(True)
self.format_widgets[song_format]['import_widget'].setVisible(False)
2011-01-13 17:55:29 +00:00
2013-03-14 22:22:18 +00:00
def custom_signals(self):
2011-01-13 17:55:29 +00:00
"""
Song wizard specific signals.
"""
2013-03-14 22:22:18 +00:00
for song_format in SongFormat.get_format_list():
2013-08-31 18:17:38 +00:00
select_mode = SongFormat.get(song_format, 'selectMode')
if select_mode == SongFormatSelect.MultipleFiles:
2013-08-31 18:17:38 +00:00
self.format_widgets[song_format]['addButton'].clicked.connect(self.on_add_button_clicked)
2014-03-04 18:49:30 +00:00
self.format_widgets[song_format]['removeButton'].clicked.connect(self.on_remove_button_clicked)
else:
2013-08-31 18:17:38 +00:00
self.format_widgets[song_format]['browseButton'].clicked.connect(self.on_browse_button_clicked)
2014-03-21 21:38:08 +00:00
self.format_widgets[song_format]['file_path_edit'].textChanged.\
connect(self.on_filepath_edit_text_changed)
2010-04-02 12:23:40 +00:00
2013-03-14 22:22:18 +00:00
def add_custom_pages(self):
2010-04-02 12:23:40 +00:00
"""
2011-01-13 17:55:29 +00:00
Add song wizard specific pages.
2010-04-02 12:23:40 +00:00
"""
2011-01-13 17:55:29 +00:00
# Source Page
2013-03-07 08:05:43 +00:00
self.source_page = SongImportSourcePage()
2013-08-31 18:17:38 +00:00
self.source_page.setObjectName('source_page')
2015-11-07 00:49:40 +00:00
self.source_layout = QtWidgets.QVBoxLayout(self.source_page)
2013-08-31 18:17:38 +00:00
self.source_layout.setObjectName('source_layout')
2015-11-07 00:49:40 +00:00
self.format_layout = QtWidgets.QFormLayout()
2013-08-31 18:17:38 +00:00
self.format_layout.setObjectName('format_layout')
2015-11-07 00:49:40 +00:00
self.format_label = QtWidgets.QLabel(self.source_page)
2013-08-31 18:17:38 +00:00
self.format_label.setObjectName('format_label')
2015-11-07 00:49:40 +00:00
self.format_combo_box = QtWidgets.QComboBox(self.source_page)
2013-08-31 18:17:38 +00:00
self.format_combo_box.setObjectName('format_combo_box')
2013-03-07 08:05:43 +00:00
self.format_layout.addRow(self.format_label, self.format_combo_box)
2015-11-07 00:49:40 +00:00
self.format_spacer = QtWidgets.QSpacerItem(10, 0, QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Minimum)
self.format_layout.setItem(1, QtWidgets.QFormLayout.LabelRole, self.format_spacer)
2013-03-07 08:05:43 +00:00
self.source_layout.addLayout(self.format_layout)
self.format_h_spacing = self.format_layout.horizontalSpacing()
self.format_v_spacing = self.format_layout.verticalSpacing()
self.format_layout.setVerticalSpacing(0)
2015-11-07 00:49:40 +00:00
self.stack_spacer = QtWidgets.QSpacerItem(10, 0, QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Expanding)
self.format_stack = QtWidgets.QStackedLayout()
2013-08-31 18:17:38 +00:00
self.format_stack.setObjectName('format_stack')
2013-03-07 08:05:43 +00:00
self.disablable_formats = []
for self.current_format in SongFormat.get_format_list():
2014-03-04 18:49:30 +00:00
self.add_file_select_item()
2013-03-07 08:05:43 +00:00
self.source_layout.addLayout(self.format_stack)
self.addPage(self.source_page)
2011-01-13 17:55:29 +00:00
def retranslateUi(self):
2010-12-08 22:55:28 +00:00
"""
2011-01-13 17:55:29 +00:00
Song wizard localisation.
2010-12-08 22:55:28 +00:00
"""
2013-01-05 22:17:30 +00:00
self.setWindowTitle(translate('SongsPlugin.ImportWizardForm', 'Song Import Wizard'))
self.title_label.setText(
WizardStrings.HeaderStyle.format(text=translate('OpenLP.Ui', 'Welcome to the Song Import Wizard')))
2014-03-04 18:49:30 +00:00
self.information_label.setText(
translate('SongsPlugin.ImportWizardForm',
'This wizard will help you to import songs from a variety of formats. Click the next button '
'below to start the process by selecting a format to import from.'))
2013-03-07 08:05:43 +00:00
self.source_page.setTitle(WizardStrings.ImportSelect)
self.source_page.setSubTitle(WizardStrings.ImportSelectLong)
self.format_label.setText(WizardStrings.FormatLabel)
2014-03-04 18:49:30 +00:00
for format_list in SongFormat.get_format_list():
2012-06-07 12:30:41 +00:00
format_name, custom_combo_text, description_text, select_mode = \
2014-03-04 18:49:30 +00:00
SongFormat.get(format_list, 'name', 'comboBoxText', 'descriptionText', 'selectMode')
2013-01-05 22:17:30 +00:00
combo_box_text = (custom_combo_text if custom_combo_text else format_name)
2014-03-04 18:49:30 +00:00
self.format_combo_box.setItemText(format_list, combo_box_text)
if description_text is not None:
2014-03-04 18:49:30 +00:00
self.format_widgets[format_list]['description_label'].setText(description_text)
if select_mode == SongFormatSelect.MultipleFiles:
2014-03-04 18:49:30 +00:00
self.format_widgets[format_list]['addButton'].setText(
translate('SongsPlugin.ImportWizardForm', 'Add Files...'))
2014-03-04 18:49:30 +00:00
self.format_widgets[format_list]['removeButton'].setText(
translate('SongsPlugin.ImportWizardForm', 'Remove File(s)'))
else:
2014-03-04 18:49:30 +00:00
self.format_widgets[format_list]['browseButton'].setText(UiStrings().Browse)
f_label = 'Filename:'
if select_mode == SongFormatSelect.SingleFolder:
2012-05-31 13:58:10 +00:00
f_label = 'Folder:'
2014-03-21 21:38:08 +00:00
self.format_widgets[format_list]['filepathLabel'].setText(
translate('SongsPlugin.ImportWizardForm', f_label))
2014-03-04 18:49:30 +00:00
for format_list in self.disablable_formats:
self.format_widgets[format_list]['disabled_label'].setText(SongFormat.get(format_list, 'disabledLabelText'))
2013-03-07 08:05:43 +00:00
self.progress_page.setTitle(WizardStrings.Importing)
self.progress_page.setSubTitle(
2013-01-05 22:17:30 +00:00
translate('SongsPlugin.ImportWizardForm', 'Please wait while your songs are imported.'))
2013-03-07 08:05:43 +00:00
self.progress_label.setText(WizardStrings.Ready)
self.progress_bar.setFormat(WizardStrings.PercentSymbolFormat)
self.error_copy_to_button.setText(translate('SongsPlugin.ImportWizardForm', 'Copy'))
self.error_save_to_button.setText(translate('SongsPlugin.ImportWizardForm', 'Save to File'))
2011-01-13 17:55:29 +00:00
# Align all QFormLayouts towards each other.
2013-08-31 18:17:38 +00:00
formats = [f for f in SongFormat.get_format_list() if 'filepathLabel' in self.format_widgets[f]]
labels = [self.format_widgets[f]['filepathLabel'] for f in formats]
# Get max width of all labels
2013-03-07 08:05:43 +00:00
max_label_width = max(self.format_label.minimumSizeHint().width(),
2014-03-04 18:49:30 +00:00
max([label.minimumSizeHint().width() for label in labels]))
2015-11-07 00:49:40 +00:00
self.format_spacer.changeSize(max_label_width, 0, QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)
2013-08-31 18:17:38 +00:00
spacers = [self.format_widgets[f]['filepathSpacer'] for f in formats]
for index, spacer in enumerate(spacers):
spacer.changeSize(
max_label_width - labels[index].minimumSizeHint().width(), 0,
2015-11-07 00:49:40 +00:00
QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)
# Align descriptionLabels with rest of layout
2014-03-04 18:49:30 +00:00
for format_list in SongFormat.get_format_list():
if SongFormat.get(format_list, 'descriptionText') is not None:
self.format_widgets[format_list]['descriptionSpacer'].changeSize(
2015-11-07 00:49:40 +00:00
max_label_width + self.format_h_spacing, 0, QtWidgets.QSizePolicy.Fixed,
QtWidgets.QSizePolicy.Fixed)
2010-12-08 22:55:28 +00:00
2013-03-07 08:05:43 +00:00
def custom_page_changed(self, page_id):
"""
2012-06-01 07:32:58 +00:00
Called when changing to a page other than the progress page.
"""
2013-03-07 08:05:43 +00:00
if self.page(page_id) == self.source_page:
2014-03-04 18:49:30 +00:00
self.on_current_index_changed(self.format_stack.currentIndex())
2010-04-02 12:23:40 +00:00
def validateCurrentPage(self):
"""
2014-03-04 18:49:30 +00:00
Re-implement the validateCurrentPage() method. Validate the current page before moving on to the next page.
2014-03-05 18:58:22 +00:00
Provide each song format class with a chance to validate its input by overriding is_valid_source().
2010-04-02 12:23:40 +00:00
"""
2015-11-07 00:49:40 +00:00
completeChanged = QtCore.pyqtSignal()
2013-03-07 08:05:43 +00:00
if self.currentPage() == self.welcome_page:
return True
2013-03-07 08:05:43 +00:00
elif self.currentPage() == self.source_page:
this_format = self.current_format
2013-08-31 18:17:38 +00:00
Settings().setValue('songs/last import type', this_format)
select_mode, class_, error_msg = SongFormat.get(this_format, 'selectMode', 'class', 'invalidSourceMsg')
if select_mode == SongFormatSelect.MultipleFiles:
2013-08-31 18:17:38 +00:00
import_source = self.get_list_of_files(self.format_widgets[this_format]['file_list_widget'])
error_title = UiStrings().IFSp
2013-08-31 18:17:38 +00:00
focus_button = self.format_widgets[this_format]['addButton']
else:
2013-08-31 18:17:38 +00:00
import_source = self.format_widgets[this_format]['file_path_edit'].text()
2013-01-05 22:17:30 +00:00
error_title = (UiStrings().IFSs if select_mode == SongFormatSelect.SingleFile else UiStrings().IFdSs)
2013-08-31 18:17:38 +00:00
focus_button = self.format_widgets[this_format]['browseButton']
2014-03-05 18:58:22 +00:00
if not class_.is_valid_source(import_source):
critical_error_message_box(error_title, error_msg)
focus_button.setFocus()
return False
return True
2013-03-07 08:05:43 +00:00
elif self.currentPage() == self.progress_page:
return True
2010-04-02 12:23:40 +00:00
2013-08-31 18:17:38 +00:00
def get_files(self, title, listbox, filters=''):
2010-12-21 13:40:41 +00:00
"""
Opens a QFileDialog and writes the filenames to the given listbox.
2014-05-02 06:42:17 +00:00
:param title: The title of the dialog (str).
2014-03-04 18:49:30 +00:00
:param listbox: A listbox (QListWidget).
2014-05-02 06:42:17 +00:00
:param filters: The file extension filters. It should contain the file descriptions as well as the file
extensions. For example::
2015-09-08 19:13:59 +00:00
2014-05-02 06:42:17 +00:00
'SongBeamer Files (*.sng)'
2010-12-21 13:40:41 +00:00
"""
if filters:
2013-08-31 18:17:38 +00:00
filters += ';;'
filters += '{text} (*)'.format(text=UiStrings().AllFiles)
2017-08-07 20:50:01 +00:00
file_paths, selected_filter = FileDialog.getOpenFileNames(
self, title, Settings().value(self.plugin.settings_section + '/last directory import'), filters)
2017-08-07 20:50:01 +00:00
if file_paths:
file_names = [path_to_str(file_path) for file_path in file_paths]
2014-03-04 18:49:30 +00:00
listbox.addItems(file_names)
Settings().setValue(self.plugin.settings_section + '/last directory import', file_paths[0].parent)
2010-08-08 14:38:51 +00:00
2014-03-04 18:49:30 +00:00
def get_list_of_files(self, list_box):
2011-01-13 17:55:29 +00:00
"""
2014-03-04 18:49:30 +00:00
Return a list of file from the list_box
:param list_box: The source list box
2011-01-13 17:55:29 +00:00
"""
2014-03-04 18:49:30 +00:00
return [list_box.item(i).text() for i in range(list_box.count())]
2013-03-07 08:05:43 +00:00
def remove_selected_items(self, list_box):
2011-01-13 17:55:29 +00:00
"""
2013-03-07 08:05:43 +00:00
Remove selected list_box items
2014-03-04 18:49:30 +00:00
:param list_box: the source list box
2011-01-13 17:55:29 +00:00
"""
2013-03-07 08:05:43 +00:00
for item in list_box.selectedItems():
item = list_box.takeItem(list_box.row(item))
2010-08-08 14:38:51 +00:00
del item
2013-03-07 08:05:43 +00:00
def on_browse_button_clicked(self):
"""
Browse for files or a directory.
"""
2013-03-07 08:05:43 +00:00
this_format = self.current_format
2013-08-31 18:17:38 +00:00
select_mode, format_name, ext_filter = SongFormat.get(this_format, 'selectMode', 'name', 'filter')
file_path_edit = self.format_widgets[this_format]['file_path_edit']
if select_mode == SongFormatSelect.SingleFile:
self.get_file_name(WizardStrings.OpenTypeFile.format(file_type=format_name),
file_path_edit, 'last directory import', ext_filter)
elif select_mode == SongFormatSelect.SingleFolder:
self.get_folder(
WizardStrings.OpenTypeFolder.format(folder_name=format_name), file_path_edit, 'last directory import')
2013-03-07 08:05:43 +00:00
def on_add_button_clicked(self):
"""
Add a file or directory.
"""
2013-03-07 08:05:43 +00:00
this_format = self.current_format
select_mode, format_name, ext_filter, custom_title = \
2013-08-31 18:17:38 +00:00
SongFormat.get(this_format, 'selectMode', 'name', 'filter', 'getFilesTitle')
title = custom_title if custom_title else WizardStrings.OpenTypeFile.format(file_type=format_name)
if select_mode == SongFormatSelect.MultipleFiles:
2013-08-31 18:17:38 +00:00
self.get_files(title, self.format_widgets[this_format]['file_list_widget'], ext_filter)
2015-11-07 00:49:40 +00:00
self.source_page.completeChanged.emit()
2014-03-04 18:49:30 +00:00
def on_remove_button_clicked(self):
"""
Remove a file from the list.
"""
2013-08-31 18:17:38 +00:00
self.remove_selected_items(self.format_widgets[self.current_format]['file_list_widget'])
2015-11-07 00:49:40 +00:00
self.source_page.completeChanged.emit()
2014-03-04 18:49:30 +00:00
def on_filepath_edit_text_changed(self):
"""
Called when the content of the Filename/Folder edit box changes.
"""
2015-11-07 00:49:40 +00:00
self.source_page.completeChanged.emit()
2011-02-14 12:20:30 +00:00
2014-04-02 07:04:12 +00:00
def set_defaults(self):
2011-01-13 17:55:29 +00:00
"""
Set default form values for the song import wizard.
"""
self.restart()
2013-03-07 08:05:43 +00:00
self.finish_button.setVisible(False)
self.cancel_button.setVisible(True)
2013-08-31 18:17:38 +00:00
last_import_type = Settings().value('songs/last import type')
2013-03-07 08:05:43 +00:00
if last_import_type < 0 or last_import_type >= self.format_combo_box.count():
last_import_type = 0
2013-03-07 08:05:43 +00:00
self.format_combo_box.setCurrentIndex(last_import_type)
2014-03-04 18:49:30 +00:00
for format_list in SongFormat.get_format_list():
select_mode = SongFormat.get(format_list, 'selectMode')
if select_mode == SongFormatSelect.MultipleFiles:
2014-03-04 18:49:30 +00:00
self.format_widgets[format_list]['file_list_widget'].clear()
else:
2014-03-04 18:49:30 +00:00
self.format_widgets[format_list]['file_path_edit'].setText('')
2013-03-07 08:05:43 +00:00
self.error_report_text_edit.clear()
self.error_report_text_edit.setHidden(True)
self.error_copy_to_button.setHidden(True)
self.error_save_to_button.setHidden(True)
2010-04-02 12:23:40 +00:00
def pre_wizard(self):
2011-01-13 17:55:29 +00:00
"""
Perform pre import tasks
"""
2013-07-18 19:49:44 +00:00
super(SongImportForm, self).pre_wizard()
2013-03-07 08:05:43 +00:00
self.progress_label.setText(WizardStrings.StartingImport)
2013-02-03 19:23:12 +00:00
self.application.process_events()
2010-04-02 12:23:40 +00:00
2014-03-04 18:49:30 +00:00
def perform_wizard(self):
"""
2014-03-06 20:40:08 +00:00
Perform the actual import. This method pulls in the correct importer class, and then runs the ``do_import``
2014-03-04 18:49:30 +00:00
method of the importer to do the actual importing.
"""
2013-03-07 08:05:43 +00:00
source_format = self.current_format
2013-08-31 18:17:38 +00:00
select_mode = SongFormat.get(source_format, 'selectMode')
if select_mode == SongFormatSelect.SingleFile:
2014-03-04 18:49:30 +00:00
importer = self.plugin.import_songs(source_format,
filename=self.format_widgets[source_format]['file_path_edit'].text())
elif select_mode == SongFormatSelect.SingleFolder:
2014-03-04 18:49:30 +00:00
importer = self.plugin.import_songs(source_format,
folder=self.format_widgets[source_format]['file_path_edit'].text())
else:
2014-03-04 18:49:30 +00:00
importer = self.plugin.import_songs(
source_format,
2013-08-31 18:17:38 +00:00
filenames=self.get_list_of_files(self.format_widgets[source_format]['file_list_widget']))
2014-03-06 20:40:08 +00:00
importer.do_import()
2013-03-07 08:05:43 +00:00
self.progress_label.setText(WizardStrings.FinishedImport)
2010-04-02 12:23:40 +00:00
2013-03-07 08:40:12 +00:00
def on_error_copy_to_button_clicked(self):
"""
Copy the error report to the clipboard.
"""
2013-03-07 08:05:43 +00:00
self.clipboard.setText(self.error_report_text_edit.toPlainText())
2013-03-07 08:40:12 +00:00
def on_error_save_to_button_clicked(self):
"""
Save the error report to a file.
:rtype: None
"""
file_path, filter_used = FileDialog.getSaveFileName(
2014-03-04 18:49:30 +00:00
self, Settings().value(self.plugin.settings_section + '/last directory import'))
if not file_path:
return
with file_path.open('w', encoding='utf-8') as report_file:
report_file.write(self.error_report_text_edit.toPlainText())
2014-03-04 18:49:30 +00:00
def add_file_select_item(self):
"""
Add a file selection page.
"""
2013-03-07 08:05:43 +00:00
this_format = self.current_format
2013-01-05 22:17:30 +00:00
prefix, can_disable, description_text, select_mode = \
2013-08-31 18:17:38 +00:00
SongFormat.get(this_format, 'prefix', 'canDisable', 'descriptionText', 'selectMode')
2015-11-07 00:49:40 +00:00
page = QtWidgets.QWidget()
2013-08-31 18:17:38 +00:00
page.setObjectName(prefix + 'Page')
2011-01-13 17:55:29 +00:00
if can_disable:
2014-03-04 18:49:30 +00:00
import_widget = self.disablable_widget(page, prefix)
2011-01-13 17:55:29 +00:00
else:
2014-03-04 18:49:30 +00:00
import_widget = page
2015-11-07 00:49:40 +00:00
import_layout = QtWidgets.QVBoxLayout(import_widget)
import_layout.setContentsMargins(0, 0, 0, 0)
2014-03-04 18:49:30 +00:00
import_layout.setObjectName(prefix + 'ImportLayout')
if description_text is not None:
2015-11-07 00:49:40 +00:00
description_layout = QtWidgets.QHBoxLayout()
2014-03-04 18:49:30 +00:00
description_layout.setObjectName(prefix + 'DescriptionLayout')
2015-11-07 00:49:40 +00:00
description_spacer = QtWidgets.QSpacerItem(0, 0, QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)
2014-03-04 18:49:30 +00:00
description_layout.addSpacerItem(description_spacer)
2015-11-07 00:49:40 +00:00
description_label = QtWidgets.QLabel(import_widget)
2013-03-07 08:05:43 +00:00
description_label.setWordWrap(True)
description_label.setOpenExternalLinks(True)
2013-08-31 18:17:38 +00:00
description_label.setObjectName(prefix + '_description_label')
2014-03-04 18:49:30 +00:00
description_layout.addWidget(description_label)
import_layout.addLayout(description_layout)
2013-08-31 18:17:38 +00:00
self.format_widgets[this_format]['description_label'] = description_label
2014-03-04 18:49:30 +00:00
self.format_widgets[this_format]['descriptionSpacer'] = description_spacer
2013-01-05 22:17:30 +00:00
if select_mode == SongFormatSelect.SingleFile or select_mode == SongFormatSelect.SingleFolder:
2015-11-07 00:49:40 +00:00
file_path_layout = QtWidgets.QHBoxLayout()
2013-08-31 18:17:38 +00:00
file_path_layout.setObjectName(prefix + '_file_path_layout')
2013-03-07 08:05:43 +00:00
file_path_layout.setContentsMargins(0, self.format_v_spacing, 0, 0)
2015-11-07 00:49:40 +00:00
file_path_label = QtWidgets.QLabel(import_widget)
2014-03-04 18:49:30 +00:00
file_path_label.setObjectName(prefix + 'FilepathLabel')
file_path_layout.addWidget(file_path_label)
2015-11-07 00:49:40 +00:00
file_path_spacer = QtWidgets.QSpacerItem(0, 0, QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)
2014-03-04 18:49:30 +00:00
file_path_layout.addSpacerItem(file_path_spacer)
2015-11-07 00:49:40 +00:00
file_path_edit = QtWidgets.QLineEdit(import_widget)
2013-08-31 18:17:38 +00:00
file_path_edit.setObjectName(prefix + '_file_path_edit')
2013-03-07 08:05:43 +00:00
file_path_layout.addWidget(file_path_edit)
2015-11-07 00:49:40 +00:00
browse_button = QtWidgets.QToolButton(import_widget)
2014-03-04 18:49:30 +00:00
browse_button.setIcon(self.open_icon)
browse_button.setObjectName(prefix + 'BrowseButton')
file_path_layout.addWidget(browse_button)
import_layout.addLayout(file_path_layout)
import_layout.addSpacerItem(self.stack_spacer)
self.format_widgets[this_format]['filepathLabel'] = file_path_label
self.format_widgets[this_format]['filepathSpacer'] = file_path_spacer
2013-08-31 18:17:38 +00:00
self.format_widgets[this_format]['file_path_layout'] = file_path_layout
self.format_widgets[this_format]['file_path_edit'] = file_path_edit
2014-03-04 18:49:30 +00:00
self.format_widgets[this_format]['browseButton'] = browse_button
elif select_mode == SongFormatSelect.MultipleFiles:
2015-11-07 00:49:40 +00:00
file_list_widget = QtWidgets.QListWidget(import_widget)
file_list_widget.setSelectionMode(QtWidgets.QAbstractItemView.ExtendedSelection)
2014-03-04 18:49:30 +00:00
file_list_widget.setObjectName(prefix + 'FileListWidget')
import_layout.addWidget(file_list_widget)
2015-11-07 00:49:40 +00:00
button_layout = QtWidgets.QHBoxLayout()
2013-08-31 18:17:38 +00:00
button_layout.setObjectName(prefix + '_button_layout')
2015-11-07 00:49:40 +00:00
add_button = QtWidgets.QPushButton(import_widget)
2014-03-04 18:49:30 +00:00
add_button.setIcon(self.open_icon)
add_button.setObjectName(prefix + 'AddButton')
button_layout.addWidget(add_button)
2013-03-07 08:05:43 +00:00
button_layout.addStretch()
2015-11-07 00:49:40 +00:00
remove_button = QtWidgets.QPushButton(import_widget)
2014-03-04 18:49:30 +00:00
remove_button.setIcon(self.delete_icon)
remove_button.setObjectName(prefix + 'RemoveButton')
button_layout.addWidget(remove_button)
import_layout.addLayout(button_layout)
self.format_widgets[this_format]['file_list_widget'] = file_list_widget
2013-08-31 18:17:38 +00:00
self.format_widgets[this_format]['button_layout'] = button_layout
2014-03-04 18:49:30 +00:00
self.format_widgets[this_format]['addButton'] = add_button
self.format_widgets[this_format]['removeButton'] = remove_button
2013-03-07 08:05:43 +00:00
self.format_stack.addWidget(page)
2013-08-31 18:17:38 +00:00
self.format_widgets[this_format]['page'] = page
2014-03-04 18:49:30 +00:00
self.format_widgets[this_format]['importLayout'] = import_layout
2013-08-31 18:17:38 +00:00
self.format_combo_box.addItem('')
2011-01-13 17:55:29 +00:00
2014-03-04 18:49:30 +00:00
def disablable_widget(self, page, prefix):
"""
Disable a widget.
"""
2013-03-07 08:05:43 +00:00
this_format = self.current_format
self.disablable_formats.append(this_format)
2015-11-07 00:49:40 +00:00
layout = QtWidgets.QVBoxLayout(page)
layout.setContentsMargins(0, 0, 0, 0)
2011-01-13 17:55:29 +00:00
layout.setSpacing(0)
2013-08-31 18:17:38 +00:00
layout.setObjectName(prefix + '_layout')
2015-11-07 00:49:40 +00:00
disabled_widget = QtWidgets.QWidget(page)
2013-03-07 08:05:43 +00:00
disabled_widget.setVisible(False)
2013-08-31 18:17:38 +00:00
disabled_widget.setObjectName(prefix + '_disabled_widget')
2015-11-07 00:49:40 +00:00
disabled_layout = QtWidgets.QVBoxLayout(disabled_widget)
disabled_layout.setContentsMargins(0, 0, 0, 0)
2013-08-31 18:17:38 +00:00
disabled_layout.setObjectName(prefix + '_disabled_layout')
2015-11-07 00:49:40 +00:00
disabled_label = QtWidgets.QLabel(disabled_widget)
2013-03-07 08:05:43 +00:00
disabled_label.setWordWrap(True)
2013-08-31 18:17:38 +00:00
disabled_label.setObjectName(prefix + '_disabled_label')
2013-03-07 08:05:43 +00:00
disabled_layout.addWidget(disabled_label)
disabled_layout.addSpacerItem(self.stack_spacer)
layout.addWidget(disabled_widget)
2015-11-07 00:49:40 +00:00
import_widget = QtWidgets.QWidget(page)
2013-08-31 18:17:38 +00:00
import_widget.setObjectName(prefix + '_import_widget')
2013-03-07 08:05:43 +00:00
layout.addWidget(import_widget)
2013-08-31 18:17:38 +00:00
self.format_widgets[this_format]['layout'] = layout
self.format_widgets[this_format]['disabled_widget'] = disabled_widget
self.format_widgets[this_format]['disabled_layout'] = disabled_layout
self.format_widgets[this_format]['disabled_label'] = disabled_label
self.format_widgets[this_format]['import_widget'] = import_widget
2013-03-07 08:05:43 +00:00
return import_widget
2012-06-15 16:03:46 +00:00
2015-11-07 00:49:40 +00:00
class SongImportSourcePage(QtWidgets.QWizardPage):
"""
2012-06-06 09:14:53 +00:00
Subclass of QtGui.QWizardPage to override isComplete() for Source Page.
"""
def isComplete(self):
"""
2012-06-06 09:14:53 +00:00
Return True if:
* an available format is selected, and
* if MultipleFiles mode, at least one file is selected
* or if SingleFile mode, the specified file exists
* or if SingleFolder mode, the specified folder exists
When this method returns True, the wizard's Next button is enabled.
"""
wizard = self.wizard()
2013-03-07 08:05:43 +00:00
this_format = wizard.current_format
2013-08-31 18:17:38 +00:00
select_mode, format_available = SongFormat.get(this_format, 'selectMode', 'availability')
if format_available:
if select_mode == SongFormatSelect.MultipleFiles:
2013-08-31 18:17:38 +00:00
if wizard.format_widgets[this_format]['file_list_widget'].count() > 0:
return True
else:
2014-03-04 18:49:30 +00:00
file_path = str(wizard.format_widgets[this_format]['file_path_edit'].text())
if file_path:
if select_mode == SongFormatSelect.SingleFile and os.path.isfile(file_path):
2012-06-05 09:16:24 +00:00
return True
2014-03-04 18:49:30 +00:00
elif select_mode == SongFormatSelect.SingleFolder and os.path.isdir(file_path):
2012-06-05 09:16:24 +00:00
return True
2012-05-31 12:45:00 +00:00
return False