openlp/openlp/plugins/bibles/forms/booknameform.py

121 lines
5.1 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
2019-04-13 13:00:22 +00:00
##########################################################################
# OpenLP - Open Source Lyrics Projection #
# ---------------------------------------------------------------------- #
2022-02-06 09:10:17 +00:00
# Copyright (c) 2008-2022 OpenLP Developers #
2019-04-13 13:00:22 +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, either version 3 of the License, or #
# (at your option) any later version. #
# #
# 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, see <https://www.gnu.org/licenses/>. #
##########################################################################
"""
Module implementing BookNameForm.
"""
import logging
import re
2015-11-07 00:49:40 +00:00
from PyQt5 import QtCore
2017-12-28 08:08:12 +00:00
from PyQt5.QtWidgets import QDialog
2017-10-07 07:05:07 +00:00
from openlp.core.common.i18n import translate
from openlp.core.lib.ui import critical_error_message_box
2013-01-01 16:33:41 +00:00
from openlp.plugins.bibles.forms.booknamedialog import Ui_BookNameDialog
from openlp.plugins.bibles.lib import BibleStrings
from openlp.plugins.bibles.lib.db import BiblesResourcesDB
2018-10-02 04:39:42 +00:00
log = logging.getLogger(__name__)
2013-03-14 22:22:18 +00:00
class BookNameForm(QDialog, Ui_BookNameDialog):
"""
2012-04-04 07:26:51 +00:00
Class to manage a dialog which help the user to refer a book name a
to a english book name
"""
2013-08-31 18:17:38 +00:00
log.info('BookNameForm loaded')
2012-04-04 07:26:51 +00:00
2014-03-21 18:23:35 +00:00
def __init__(self, parent=None):
"""
Constructor
"""
super(BookNameForm, self).__init__(parent, QtCore.Qt.WindowSystemMenuHint | QtCore.Qt.WindowTitleHint |
QtCore.Qt.WindowCloseButtonHint)
self.setup_ui(self)
2013-03-14 22:22:18 +00:00
self.custom_signals()
self.book_names = BibleStrings().BookNames
self.book_id = False
2013-03-14 22:22:18 +00:00
def custom_signals(self):
"""
Set up the signals used in the booknameform.
"""
2014-03-21 18:23:35 +00:00
self.old_testament_check_box.stateChanged.connect(self.on_check_box_index_changed)
self.new_testament_check_box.stateChanged.connect(self.on_check_box_index_changed)
self.apocrypha_check_box.stateChanged.connect(self.on_check_box_index_changed)
2014-03-21 18:23:35 +00:00
def on_check_box_index_changed(self, index):
"""
Reload Combobox if CheckBox state has changed
"""
2013-04-18 17:45:14 +00:00
self.reload_combo_box()
2013-04-18 17:45:14 +00:00
def reload_combo_box(self):
"""
Reload the Combobox items
"""
2013-04-18 17:45:14 +00:00
self.corresponding_combo_box.clear()
items = BiblesResourcesDB.get_books()
for item in items:
2013-04-18 17:45:14 +00:00
add_book = True
for book in self.books:
2013-08-31 18:17:38 +00:00
if book.book_reference_id == item['id']:
2013-04-18 17:45:14 +00:00
add_book = False
break
2013-08-31 18:17:38 +00:00
if self.old_testament_check_box.checkState() == QtCore.Qt.Unchecked and item['testament_id'] == 1:
2013-04-18 17:45:14 +00:00
add_book = False
2013-08-31 18:17:38 +00:00
elif self.new_testament_check_box.checkState() == QtCore.Qt.Unchecked and item['testament_id'] == 2:
2013-04-18 17:45:14 +00:00
add_book = False
2013-08-31 18:17:38 +00:00
elif self.apocrypha_check_box.checkState() == QtCore.Qt.Unchecked and item['testament_id'] == 3:
2013-04-18 17:45:14 +00:00
add_book = False
if add_book:
2013-08-31 18:17:38 +00:00
self.corresponding_combo_box.addItem(self.book_names[item['abbreviation']])
2015-11-07 00:49:40 +00:00
def exec(self, name, books, max_books):
self.books = books
2013-04-18 17:45:14 +00:00
log.debug(max_books)
if max_books <= 27:
self.old_testament_check_box.setCheckState(QtCore.Qt.Unchecked)
self.apocrypha_check_box.setCheckState(QtCore.Qt.Unchecked)
elif max_books <= 66:
self.apocrypha_check_box.setCheckState(QtCore.Qt.Unchecked)
self.reload_combo_box()
2013-08-31 18:17:38 +00:00
self.current_book_label.setText(str(name))
2013-04-18 17:45:14 +00:00
self.corresponding_combo_box.setFocus()
2015-11-07 00:49:40 +00:00
return QDialog.exec(self)
2012-04-04 07:26:51 +00:00
def accept(self):
2013-04-18 17:45:14 +00:00
if not self.corresponding_combo_box.currentText():
2013-01-01 16:33:41 +00:00
critical_error_message_box(message=translate('BiblesPlugin.BookNameForm', 'You need to select a book.'))
2013-04-18 17:45:14 +00:00
self.corresponding_combo_box.setFocus()
return False
else:
2013-04-18 17:45:14 +00:00
cor_book = self.corresponding_combo_box.currentText()
2013-08-31 18:17:38 +00:00
for character in '\\.^$*+?{}[]()':
cor_book = cor_book.replace(character, '\\' + character)
books = [key for key in list(self.book_names.keys()) if re.match(cor_book, str(self.book_names[key]))]
2013-08-31 18:17:38 +00:00
books = [_f for _f in map(BiblesResourcesDB.get_book, books) if _f]
if books:
2013-08-31 18:17:38 +00:00
self.book_id = books[0]['id']
return QDialog.accept(self)