1# -*- coding: utf-8 -*-
2
3# Copyright (c) 2019 - 2021 Detlev Offenbach <detlev@die-offenbachs.de>
4#
5
6"""
7Module implementing the sub-style definition dialog.
8"""
9
10from PyQt5.QtCore import pyqtSlot
11from PyQt5.QtWidgets import QDialog, QDialogButtonBox
12
13from E5Gui import E5MessageBox
14
15from .Ui_SubstyleDefinitionDialog import Ui_SubstyleDefinitionDialog
16
17
18class SubstyleDefinitionDialog(QDialog, Ui_SubstyleDefinitionDialog):
19    """
20    Class implementing the sub-style definition dialog.
21    """
22    def __init__(self, lexer, style, substyle, parent=None):
23        """
24        Constructor
25
26        @param lexer reference to the lexer object
27        @type PreferencesLexer
28        @param style style number
29        @type int
30        @param substyle sub-style number
31        @type int
32        @param parent reference to the parent widget
33        @type QWidget
34        """
35        super().__init__(parent)
36        self.setupUi(self)
37
38        self.__lexer = lexer
39        self.__style = style
40        self.__substyle = substyle
41
42        self.header.setText(self.tr("<h3>{0} - {1}</h3>").format(
43            self.__lexer.language(), self.__lexer.description(self.__style)))
44        if self.__substyle >= 0:
45            # it's an edit operation
46            self.descriptionEdit.setText(
47                self.__lexer.description(self.__style, self.__substyle))
48            self.wordsEdit.setPlainText(
49                self.__lexer.words(self.__style, self.__substyle))
50
51    def __updateOk(self):
52        """
53        Private slot to update the state of the OK button.
54        """
55        self.buttonBox.button(QDialogButtonBox.StandardButton.Ok).setEnabled(
56            bool(self.descriptionEdit.text().strip()) and
57            bool(self.wordsEdit.toPlainText().strip())
58        )
59
60    @pyqtSlot(str)
61    def on_descriptionEdit_textChanged(self, txt):
62        """
63        Private slot handling changes of the description.
64
65        @param txt text of the description
66        @type str
67        """
68        self.__updateOk()
69
70    @pyqtSlot()
71    def on_wordsEdit_textChanged(self):
72        """
73        Private slot handling changes of the word list.
74        """
75        self.__updateOk()
76
77    @pyqtSlot()
78    def on_resetButton_clicked(self):
79        """
80        Private slot to reset the dialog contents.
81        """
82        ok = E5MessageBox.yesNo(
83            self,
84            self.tr("Reset Sub-Style Data"),
85            self.tr("""Shall the entered sub-style data be reset?"""))
86        if ok:
87            if self.__substyle >= 0:
88                self.descriptionEdit.setText(
89                    self.__lexer.description(self.__style, self.__substyle))
90                self.wordsEdit.setPlainText(
91                    self.__lexer.words(self.__style, self.__substyle))
92            else:
93                self.descriptionEdit.clear()
94                self.wordsEdit.clear()
95
96    @pyqtSlot()
97    def on_defaultButton_clicked(self):
98        """
99        Private slot to set the dialog contents to default values.
100        """
101        filled = (
102            bool(self.descriptionEdit.text().strip()) or
103            bool(self.wordsEdit.toPlainText().strip())
104        )
105        ok = (
106            E5MessageBox.yesNo(
107                self,
108                self.tr("Set Sub-Style Data to Default"),
109                self.tr("""Shall the sub-style data be set to default"""
110                        """ values?"""))
111            if filled else
112            True
113        )
114        if ok:
115            if self.__substyle >= 0:
116                self.descriptionEdit.setText(self.__lexer.defaultDescription(
117                    self.__style, self.__substyle))
118                self.wordsEdit.setPlainText(self.__lexer.defaultWords(
119                    self.__style, self.__substyle))
120            else:
121                self.descriptionEdit.clear()
122                self.wordsEdit.clear()
123
124    def getData(self):
125        """
126        Public method to get the entered data.
127
128        @return tuple containing the sub-style description and words list.
129        @rtype tuple of (str, str)
130        """
131        return (
132            self.descriptionEdit.text().strip(),
133            self.wordsEdit.toPlainText().strip(),
134        )
135