File indexing completed on 2025-02-02 04:22:25

0001 """
0002 SPDX-FileCopyrightText: 2017 Wolthera van Hövell tot Westerflier <griffinvalley@gmail.com>
0003 
0004 This file is part of the Comics Project Management Tools(CPMT).
0005 
0006 SPDX-License-Identifier: GPL-3.0-or-later
0007 """
0008 
0009 """
0010 A dialog for editing the general project settings.
0011 """
0012 import os
0013 from PyQt5.QtWidgets import QWidget, QDialog, QDialogButtonBox, QHBoxLayout, QFormLayout, QPushButton, QLabel, QLineEdit, QToolButton, QFrame, QAction, QFileDialog, QComboBox, QSizePolicy
0014 from PyQt5.QtCore import QDir, Qt, pyqtSignal
0015 from krita import *
0016 
0017 """
0018 A Widget that contains both a qlabel and a button for selecting a path.
0019 """
0020 
0021 
0022 class path_select(QWidget):
0023     projectUrl = ""
0024     question = i18n("Which folder?")
0025 
0026     """
0027     emits when a new directory has been chosen.
0028     """
0029     locationChanged = pyqtSignal()
0030     """
0031     Initialise the widget.
0032     @param question is the question asked when selecting a directory.
0033     @param project url is the url to which the label is relative.
0034     """
0035 
0036     def __init__(self, parent=None, flags=None, question=str(), projectUrl=None):
0037         super(path_select, self).__init__(parent)
0038         self.setLayout(QHBoxLayout())
0039         self.location = QLabel()
0040         self.button = QToolButton()  # Until we have a proper icon
0041         self.layout().addWidget(self.location)
0042         self.layout().addWidget(self.button)
0043         self.layout().setContentsMargins(0, 0, 0, 0)
0044         self.setSizePolicy(QSizePolicy.MinimumExpanding, QSizePolicy.Minimum)
0045         self.location.setFrameStyle(QFrame.StyledPanel | QFrame.Sunken)
0046         self.button.setSizePolicy(QSizePolicy.Minimum, QSizePolicy.Minimum)
0047         self.location.setSizePolicy(QSizePolicy.MinimumExpanding, QSizePolicy.Minimum)
0048         self.location.setAlignment(Qt.AlignRight)
0049         self.location.setLineWidth(1)
0050         if projectUrl is None:
0051             self.projectUrl = QDir.homePath()
0052         else:
0053             self.projectUrl = projectUrl
0054         self.question = question
0055         self.action_change_folder = QAction(i18n("Change Folder"), self)
0056         self.action_change_folder.setIcon(Application.icon("folder"))
0057         self.action_change_folder.triggered.connect(self.slot_change_location)
0058         self.button.setDefaultAction(self.action_change_folder)
0059 
0060     """
0061     pops up a directory chooser widget, and when a directory is chosen a locationChanged signal is emitted.
0062     """
0063 
0064     def slot_change_location(self):
0065         location = QFileDialog.getExistingDirectory(caption=self.question, directory=self.projectUrl)
0066         if location is not None and location.isspace() is False and len(location) > 0:
0067             location = os.path.relpath(location, self.projectUrl)
0068             self.location.setText(location)
0069             self.locationChanged.emit()
0070     """
0071     Set the location.
0072     @param path - the location relative to the projectUrl.
0073     """
0074 
0075     def setLocation(self, path=str()):
0076         self.location.setText(path)
0077     """
0078     Get the location.
0079     @returns a string with the location relative to the projectUrl.
0080     """
0081 
0082     def getLocation(self):
0083         return str(self.location.text())
0084 
0085 
0086 """
0087 Dialog for editing basic proect details like the project name, default template,
0088 template location, etc.
0089 """
0090 
0091 
0092 class comics_project_details_editor(QDialog):
0093     configGroup = "ComicsProjectManagementTools"
0094     """
0095     Initialise the editor.
0096     @param projectUrl - The directory to which all paths are relative.
0097     """
0098 
0099     def __init__(self, projectUrl=str()):
0100         super().__init__()
0101         self.projectUrl = projectUrl
0102         layout = QFormLayout()
0103         self.setLayout(layout)
0104         self.setWindowTitle(i18n("Comic Project Settings"))
0105         buttons = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel)
0106 
0107         buttons.accepted.connect(self.accept)
0108         buttons.rejected.connect(self.reject)
0109         self.lnProjectName = QLineEdit()
0110         self.lnProjectConcept = QLineEdit()
0111         self.cmb_defaultTemplate = QComboBox()
0112 
0113         self.pagesLocation = path_select(question=i18n("Where should the pages go?"), projectUrl=self.projectUrl)
0114         self.exportLocation = path_select(question=i18n("Where should the export go?"), projectUrl=self.projectUrl)
0115         self.templateLocation = path_select(question=i18n("Where are the templates?"), projectUrl=self.projectUrl)
0116         self.translationLocation = path_select(question=i18n("Where are the translations?"), projectUrl=self.projectUrl)
0117         self.keyLocation = path_select(question=i18n("Where are the extra auto-completion keys located?"))
0118         self.keyLocation.setToolTip(i18n("The location for extra autocompletion keys in the metadata editor. Point this at a folder containing key_characters/key_format/key_genre/key_rating/key_author_roles/key_other with inside txt files (csv for rating) containing the extra auto-completion keys, each on a new line. This path is stored in the Krita configuration, and not the project configuration."))
0119         self.templateLocation.locationChanged.connect(self.refill_templates)
0120 
0121         layout.addRow(i18n("Project name:"), self.lnProjectName)
0122         layout.addRow(i18n("Project concept:"), self.lnProjectConcept)
0123         layout.addRow(i18n("Pages folder:"), self.pagesLocation)
0124         layout.addRow(i18n("Export folder:"), self.exportLocation)
0125         layout.addRow(i18n("Template folder:"), self.templateLocation)
0126         layout.addRow(i18n("Translation folder:"), self.translationLocation)
0127         layout.addRow(i18n("Default template:"), self.cmb_defaultTemplate)
0128         layout.addRow(i18n("Extra keys folder:"), self.keyLocation)
0129 
0130         self.layout().addWidget(buttons)
0131 
0132     """
0133     Fill the templates doc with the kra files found in the templates directory.
0134     Might want to extend this to other files as well, as they basically get resaved anyway...
0135     """
0136 
0137     def refill_templates(self):
0138         self.cmb_defaultTemplate.clear()
0139         templateLocation = os.path.join(self.projectUrl, self.templateLocation.getLocation())
0140         for entry in os.scandir(templateLocation):
0141             if entry.name.endswith('.kra') and entry.is_file():
0142                 name = os.path.relpath(entry.path, templateLocation)
0143                 self.cmb_defaultTemplate.addItem(name)
0144 
0145     """
0146     Load the UI values from the config dictionary given.
0147     """
0148 
0149     def setConfig(self, config, projectUrl):
0150 
0151         self.projectUrl = projectUrl
0152         if "projectName"in config.keys():
0153             self.lnProjectName.setText(config["projectName"])
0154         if "concept"in config.keys():
0155             self.lnProjectConcept.setText(config["concept"])
0156         if "pagesLocation" in config.keys():
0157             self.pagesLocation.setLocation(config.get("pagesLocation", "pages"))
0158             self.exportLocation.setLocation(config.get("exportLocation", "export"))
0159             self.templateLocation.setLocation(config.get("templateLocation", "templates"))
0160             self.translationLocation.setLocation(config.get("translationLocation", "translations"))
0161             self.refill_templates()
0162         self.keyLocation.setLocation(Application.readSetting(self.configGroup, "extraKeysLocation", str()))
0163 
0164     """
0165     Store the GUI values into the config dictionary given.
0166     
0167     @return the config diactionary filled with new values.
0168     """
0169 
0170     def getConfig(self, config):
0171         config["projectName"] = self.lnProjectName.text()
0172         config["concept"] = self.lnProjectConcept.text()
0173         config["pagesLocation"] = self.pagesLocation.getLocation()
0174         config["exportLocation"] = self.exportLocation.getLocation()
0175         config["templateLocation"] = self.templateLocation.getLocation()
0176         config["translationLocation"] = self.translationLocation.getLocation()
0177         config["singlePageTemplate"] = os.path.join(self.templateLocation.getLocation(), self.cmb_defaultTemplate.currentText())
0178         Application.writeSetting(self.configGroup, "extraKeysLocation", self.keyLocation.getLocation())
0179         return config