1# -*- coding: utf-8 -*-
2
3# Copyright (c) 2019 - 2021 Detlev Offenbach <detlev@die-offenbachs.de>
4#
5
6"""
7Module implementing a dialog to enter data for a new conda environment.
8"""
9
10from PyQt5.QtCore import pyqtSlot
11from PyQt5.QtWidgets import QDialog, QDialogButtonBox
12
13from E5Gui.E5PathPicker import E5PathPickerModes
14
15from .Ui_CondaNewEnvironmentDataDialog import Ui_CondaNewEnvironmentDataDialog
16
17
18class CondaNewEnvironmentDataDialog(QDialog, Ui_CondaNewEnvironmentDataDialog):
19    """
20    Class implementing a dialog to enter data for a new conda environment.
21    """
22    def __init__(self, title, showRequirements, parent=None):
23        """
24        Constructor
25
26        @param title tirle of the dialog
27        @type str
28        @param showRequirements flag indicating to show the requirements
29            file input widget
30        @type bool
31        @param parent reference to the parent widget
32        @type QWidget
33        """
34        super().__init__(parent)
35        self.setupUi(self)
36
37        self.setWindowTitle(title)
38
39        self.__requirementsMode = showRequirements
40
41        self.requirementsFilePicker.setMode(E5PathPickerModes.OpenFileMode)
42        self.requirementsFilePicker.setFilters(
43            self.tr("Text Files (*.txt);;All Files (*)"))
44
45        self.requirementsLabel.setVisible(showRequirements)
46        self.requirementsFilePicker.setVisible(showRequirements)
47
48        self.__updateOK()
49
50        msh = self.minimumSizeHint()
51        self.resize(max(self.width(), msh.width()), msh.height())
52
53    def __updateOK(self):
54        """
55        Private method to update the enabled state of the OK button.
56        """
57        enable = bool(self.nameEdit.text()) and bool(self.condaNameEdit.text())
58        if self.__requirementsMode:
59            enable &= bool(self.requirementsFilePicker.text())
60
61        self.buttonBox.button(
62            QDialogButtonBox.StandardButton.Ok).setEnabled(enable)
63
64    @pyqtSlot(str)
65    def on_nameEdit_textChanged(self, txt):
66        """
67        Private slot to handle changes of the logical name.
68
69        @param txt current text of the logical name entry
70        @type str
71        """
72        self.__updateOK()
73
74    @pyqtSlot(str)
75    def on_condaNameEdit_textChanged(self, txt):
76        """
77        Private slot to handle changes of the conda name.
78
79        @param txt current text of the conda name entry
80        @type str
81        """
82        self.__updateOK()
83
84    @pyqtSlot(str)
85    def on_requirementsFilePicker_textChanged(self, txt):
86        """
87        Private slot to handle changes of the requirements file name.
88
89        @param txt current text of the requirements file name entry
90        @type str
91        """
92        self.__updateOK()
93
94    def getData(self):
95        """
96        Public method to get the entered data.
97
98        @return tuple with the logical name of the new environment, the conda
99            name and the requirements file name
100        @rtype tuple of (str, str, str)
101        """
102        requirementsFile = (
103            self.requirementsFilePicker.text()
104            if self.__requirementsMode else
105            ""
106        )
107
108        return (
109            self.nameEdit.text(),
110            self.condaNameEdit.text(),
111            requirementsFile
112        )
113