From 38932f0782d9dd149beface3d05a796fc9e90272 Mon Sep 17 00:00:00 2001 From: Timmy Welch Date: Fri, 6 Dec 2024 22:02:32 -0800 Subject: [PATCH] Add language to ComicTagger --- comictaggerlib/crediteditorwindow.py | 64 ++++++----- comictaggerlib/settingswindow.py | 2 +- comictaggerlib/taggerwindow.py | 69 ++++++------ comictaggerlib/ui/TemplateHelp.ui | 2 +- comictaggerlib/ui/crediteditorwindow.ui | 134 ++++++++++++------------ comictaggerlib/ui/taggerwindow.ui | 9 +- 6 files changed, 144 insertions(+), 136 deletions(-) diff --git a/comictaggerlib/crediteditorwindow.py b/comictaggerlib/crediteditorwindow.py index 27c6fde..3144933 100644 --- a/comictaggerlib/crediteditorwindow.py +++ b/comictaggerlib/crediteditorwindow.py @@ -17,10 +17,13 @@ from __future__ import annotations import logging -from typing import Any +import operator +import natsort from PyQt5 import QtWidgets, uic +from comicapi import utils +from comicapi.genericmetadata import Credit from comictaggerlib.ui import ui_path logger = logging.getLogger(__name__) @@ -30,7 +33,7 @@ class CreditEditorWindow(QtWidgets.QDialog): ModeEdit = 0 ModeNew = 1 - def __init__(self, parent: QtWidgets.QWidget, mode: int, role: str, name: str, primary: bool) -> None: + def __init__(self, parent: QtWidgets.QWidget, mode: int, credit: Credit) -> None: super().__init__(parent) with (ui_path / "crediteditorwindow.ui").open(encoding="utf-8") as uifile: @@ -45,54 +48,47 @@ class CreditEditorWindow(QtWidgets.QDialog): # Add the entries to the role combobox self.cbRole.addItem("") - self.cbRole.addItem("Writer") self.cbRole.addItem("Artist") - self.cbRole.addItem("Penciller") - self.cbRole.addItem("Inker") self.cbRole.addItem("Colorist") - self.cbRole.addItem("Letterer") self.cbRole.addItem("Cover Artist") self.cbRole.addItem("Editor") - self.cbRole.addItem("Other") + self.cbRole.addItem("Inker") + self.cbRole.addItem("Letterer") + self.cbRole.addItem("Penciller") self.cbRole.addItem("Plotter") self.cbRole.addItem("Scripter") + self.cbRole.addItem("Translator") + self.cbRole.addItem("Writer") + self.cbRole.addItem("Other") - self.leName.setText(name) + self.cbLanguage.addItem("", "") + for f in natsort.humansorted(utils.languages().items(), operator.itemgetter(1)): + self.cbLanguage.addItem(f[1], f[0]) - if role is not None and role != "": - i = self.cbRole.findText(role) + self.leName.setText(credit.person) + + if credit.role is not None and credit.role != "": + i = self.cbRole.findText(credit.role) if i == -1: - self.cbRole.setEditText(role) + self.cbRole.setEditText(credit.role) else: self.cbRole.setCurrentIndex(i) - self.cbPrimary.setChecked(primary) + if credit.language != "": + i = self.cbLanguage.findText(credit.language) + if i == -1: + self.cbLanguage.setEditText(credit.language) + else: + self.cbLanguage.setCurrentIndex(i) - self.cbRole.currentIndexChanged.connect(self.role_changed) - self.cbRole.editTextChanged.connect(self.role_changed) + self.cbPrimary.setChecked(credit.primary) - self.update_primary_button() - - def update_primary_button(self) -> None: - enabled = self.current_role_can_be_primary() - self.cbPrimary.setEnabled(enabled) - - def current_role_can_be_primary(self) -> bool: - role = self.cbRole.currentText() - if role.casefold() in ("artist", "writer"): - return True - - return False - - def role_changed(self, s: Any) -> None: - self.update_primary_button() - - def get_credits(self) -> tuple[str, str, bool]: + def get_credit(self) -> Credit: primary = self.current_role_can_be_primary() and self.cbPrimary.isChecked() - return self.cbRole.currentText(), self.leName.text(), primary + return Credit(self.leName.text(), self.cbRole.currentText(), primary, self.cbLanguage.currentText()) def accept(self) -> None: - if self.cbRole.currentText() == "" or self.leName.text() == "": - QtWidgets.QMessageBox.warning(self, "Whoops", "You need to enter both role and name for a credit.") + if self.leName.text() == "": + QtWidgets.QMessageBox.warning(self, "Whoops", "You need to enter a name for a credit.") else: QtWidgets.QDialog.accept(self) diff --git a/comictaggerlib/settingswindow.py b/comictaggerlib/settingswindow.py index d862c05..cb13f98 100644 --- a/comictaggerlib/settingswindow.py +++ b/comictaggerlib/settingswindow.py @@ -105,7 +105,7 @@ Accepts the following variables: {characters} (string) {teams} (string) {locations} (string) -{credits} (list of dict({'role': string, 'person': string, 'primary': boolean})) +{credits} (list of dict({'role': string, 'person': string, 'primary': boolean, 'language': str})) {writer} (string) {penciller} (string) {inker} (string) diff --git a/comictaggerlib/taggerwindow.py b/comictaggerlib/taggerwindow.py index eedbf26..675222e 100644 --- a/comictaggerlib/taggerwindow.py +++ b/comictaggerlib/taggerwindow.py @@ -38,7 +38,7 @@ import comictaggerlib.ui from comicapi import utils from comicapi.comicarchive import ComicArchive, tags from comicapi.filenameparser import FileNameParser -from comicapi.genericmetadata import GenericMetadata +from comicapi.genericmetadata import Credit, GenericMetadata from comicapi.issuestring import IssueString from comictaggerlib import ctsettings, ctversion from comictaggerlib.applicationlogwindow import ApplicationLogWindow, QTextEditLogger @@ -126,7 +126,8 @@ class TaggerWindow(QtWidgets.QMainWindow): "teams": self.teTeams, "locations": self.teLocations, "credits": (self.twCredits, self.btnAddCredit, self.btnEditCredit, self.btnRemoveCredit), - "credits.person": 2, + "credits.person": 3, + "credits.language": 2, "credits.role": 1, "credits.primary": 0, "tags": self.teTags, @@ -874,30 +875,34 @@ class TaggerWindow(QtWidgets.QMainWindow): if self.is_dupe_credit(credit.role.title(), credit.person): continue - self.add_new_credit_entry(row, credit.role.title(), credit.person, credit.primary) + self.add_new_credit_entry(row, credit) self.twCredits.setSortingEnabled(True) self.update_credit_colors() - def add_new_credit_entry(self, row: int, role: str, name: str, primary_flag: bool = False) -> None: + def add_new_credit_entry(self, row: int, credit: Credit) -> None: self.twCredits.insertRow(row) - item_text = role - item = QtWidgets.QTableWidgetItem(item_text) + item = QtWidgets.QTableWidgetItem(credit.role) item.setFlags(QtCore.Qt.ItemFlag.ItemIsSelectable | QtCore.Qt.ItemFlag.ItemIsEnabled) - item.setData(QtCore.Qt.ItemDataRole.ToolTipRole, item_text) + item.setData(QtCore.Qt.ItemDataRole.ToolTipRole, credit.role) self.twCredits.setItem(row, 1, item) - item_text = name - item = QtWidgets.QTableWidgetItem(item_text) - item.setData(QtCore.Qt.ItemDataRole.ToolTipRole, item_text) + language = utils.get_language_from_iso(credit.language) or credit.language + item = QtWidgets.QTableWidgetItem(language) + item.setData(QtCore.Qt.ItemDataRole.ToolTipRole, credit.language) item.setFlags(QtCore.Qt.ItemFlag.ItemIsSelectable | QtCore.Qt.ItemFlag.ItemIsEnabled) self.twCredits.setItem(row, 2, item) + item = QtWidgets.QTableWidgetItem(credit.person) + item.setData(QtCore.Qt.ItemDataRole.ToolTipRole, credit.person) + item.setFlags(QtCore.Qt.ItemFlag.ItemIsSelectable | QtCore.Qt.ItemFlag.ItemIsEnabled) + self.twCredits.setItem(row, 3, item) + item = QtWidgets.QTableWidgetItem("") item.setFlags(QtCore.Qt.ItemFlag.ItemIsSelectable | QtCore.Qt.ItemFlag.ItemIsEnabled) self.twCredits.setItem(row, 0, item) - self.update_credit_primary_flag(row, primary_flag) + self.update_credit_primary_flag(row, credit.primary) def is_dupe_credit(self, role: str, name: str) -> bool: for r in range(self.twCredits.rowCount()): @@ -958,10 +963,11 @@ class TaggerWindow(QtWidgets.QMainWindow): md.credits = [] for row in range(self.twCredits.rowCount()): role = self.twCredits.item(row, 1).text() - name = self.twCredits.item(row, 2).text() + lang = self.twCredits.item(row, 2).text() + name = self.twCredits.item(row, 3).text() primary_flag = self.twCredits.item(row, 0).text() != "" - md.add_credit(name, role, bool(primary_flag)) + md.add_credit(name, role, bool(primary_flag), lang) md.pages = self.page_list_editor.get_page_list() @@ -1251,35 +1257,35 @@ class TaggerWindow(QtWidgets.QMainWindow): self.twCredits.item(row, 0).setText("Yes") def modify_credits(self, edit: bool) -> None: + row = self.twCredits.rowCount() + old = Credit() if edit: row = self.twCredits.currentRow() - role = self.twCredits.item(row, 1).text() - name = self.twCredits.item(row, 2).text() - primary = self.twCredits.item(row, 0).text() != "" - else: - row = self.twCredits.rowCount() - role = "" - name = "" - primary = False + old = Credit( + self.twCredits.item(row, 3).text(), + self.twCredits.item(row, 1).text(), + self.twCredits.item(row, 0).text() != "", + self.twCredits.item(row, 2).text(), + ) - editor = CreditEditorWindow(self, CreditEditorWindow.ModeEdit, role, name, primary) + editor = CreditEditorWindow(self, CreditEditorWindow.ModeEdit, old) editor.setModal(True) editor.exec() if editor.result(): - new_role, new_name, new_primary = editor.get_credits() + new = editor.get_credit() - if new_name == name and new_role == role and new_primary == primary: + if new == old: # nothing has changed, just quit return # name and role is the same, but primary flag changed - if new_name == name and new_role == role: - self.update_credit_primary_flag(row, new_primary) + if new.person == old.person and new.role == old.role: + self.update_credit_primary_flag(row, new.primary) return # check for dupes ok_to_mod = True - if self.is_dupe_credit(new_role, new_name): + if self.is_dupe_credit(new.role, new.person): # delete the dupe credit from list qmsg = QtWidgets.QMessageBox() qmsg.setText("Duplicate Credit!") @@ -1301,13 +1307,14 @@ class TaggerWindow(QtWidgets.QMainWindow): if ok_to_mod: # modify it if edit: - self.twCredits.item(row, 1).setText(new_role) - self.twCredits.item(row, 2).setText(new_name) - self.update_credit_primary_flag(row, new_primary) + self.twCredits.item(row, 1).setText(new.role) + self.twCredits.item(row, 3).setText(new.person) + self.twCredits.item(row, 2).setText(new.language) + self.update_credit_primary_flag(row, new.primary) else: # add new entry row = self.twCredits.rowCount() - self.add_new_credit_entry(row, new_role, new_name, new_primary) + self.add_new_credit_entry(row, new) self.update_credit_colors() self.set_dirty_flag() diff --git a/comictaggerlib/ui/TemplateHelp.ui b/comictaggerlib/ui/TemplateHelp.ui index 5ecc5b3..0ca877e 100644 --- a/comictaggerlib/ui/TemplateHelp.ui +++ b/comictaggerlib/ui/TemplateHelp.ui @@ -97,7 +97,7 @@ tr:nth-child(even) { <tr><td>{characters}</td><td>string</td></tr> <tr><td>{teams}</td><td>string</td></tr> <tr><td>{locations}</td><td>string</td></tr> - <tr><td>{credits}</td><td>list of dict({'role': string, 'person': string, 'primary': boolean})</td></tr> + <tr><td>{credits}</td><td>list of dict({'role': string, 'person': string, 'primary': boolean, 'language': str})</td></tr> <tr><td>{writer}</td><td>(string)</td></tr> <tr><td>{penciller}</td><td>(string)</td></tr> <tr><td>{inker}</td><td>(string)</td></tr> diff --git a/comictaggerlib/ui/crediteditorwindow.ui b/comictaggerlib/ui/crediteditorwindow.ui index 1744f4c..8f14f8f 100644 --- a/comictaggerlib/ui/crediteditorwindow.ui +++ b/comictaggerlib/ui/crediteditorwindow.ui @@ -2,80 +2,80 @@ dialogCreditEditor - - - 0 - 0 - 400 - 196 - - Modify Credit false - - - - 180 - 140 - 191 - 30 - - - - Qt::Horizontal - - - QDialogButtonBox::Cancel|QDialogButtonBox::Ok - - - - - - 30 - 30 - 341 - 91 - - - - - - - Role - - - - - - - true - - - - - - - - - - Name - - - - - - - Primary - - - - - + + + + + + + Role + + + + + + + true + + + + + + + + + + Name + + + + + + + Primary + + + + + + + true + + + + + + + Language + + + + + + + + + Qt::Horizontal + + + QDialogButtonBox::Cancel|QDialogButtonBox::Ok + + + + + + cbRole + leName + cbLanguage + cbPrimary + diff --git a/comictaggerlib/ui/taggerwindow.ui b/comictaggerlib/ui/taggerwindow.ui index 29ffd21..4a8c5d3 100644 --- a/comictaggerlib/ui/taggerwindow.ui +++ b/comictaggerlib/ui/taggerwindow.ui @@ -919,7 +919,7 @@ Source 0 - 3 + 4 2 @@ -937,7 +937,12 @@ Source - Credit + Role + + + + + Language