1# -*- coding: utf-8 -*-
2
3# Copyright (c) 2014 - 2021 Detlev Offenbach <detlev@die-offenbachs.de>
4#
5
6"""
7Module implementing a dialog to enter the data for the creation of an archive.
8"""
9
10import os
11
12from PyQt5.QtCore import pyqtSlot
13from PyQt5.QtWidgets import QDialog, QDialogButtonBox
14
15from E5Gui import E5FileDialog
16
17from .Ui_GitArchiveDataDialog import Ui_GitArchiveDataDialog
18
19import UI.PixmapCache
20import Utilities
21
22
23class GitArchiveDataDialog(QDialog, Ui_GitArchiveDataDialog):
24    """
25    Class implementing a dialog to enter the data for the creation of an
26    archive.
27    """
28    def __init__(self, tagsList, branchesList, formatsList, parent=None):
29        """
30        Constructor
31
32        @param tagsList list of tags (list of strings)
33        @param branchesList list of branches (list of strings)
34        @param formatsList list of archive formats (list of strings)
35        @param parent reference to the parent widget (QWidget)
36        """
37        super().__init__(parent)
38        self.setupUi(self)
39
40        self.fileButton.setIcon(UI.PixmapCache.getIcon("open"))
41
42        self.buttonBox.button(
43            QDialogButtonBox.StandardButton.Ok).setEnabled(False)
44
45        self.tagCombo.addItems(sorted(tagsList))
46        self.branchCombo.addItems(["master"] + sorted(branchesList))
47        self.formatComboBox.addItems(sorted(formatsList))
48        self.formatComboBox.setCurrentIndex(
49            self.formatComboBox.findText("zip"))
50
51        msh = self.minimumSizeHint()
52        self.resize(max(self.width(), msh.width()), msh.height())
53
54    def __updateOK(self):
55        """
56        Private slot to update the OK button.
57        """
58        enabled = True
59        if self.revButton.isChecked():
60            enabled = self.revEdit.text() != ""
61        elif self.tagButton.isChecked():
62            enabled = self.tagCombo.currentText() != ""
63        elif self.branchButton.isChecked():
64            enabled = self.branchCombo.currentText() != ""
65
66        enabled &= bool(self.fileEdit.text())
67
68        self.buttonBox.button(
69            QDialogButtonBox.StandardButton.Ok).setEnabled(enabled)
70
71    @pyqtSlot(str)
72    def on_fileEdit_textChanged(self, txt):
73        """
74        Private slot to handle changes of the file edit.
75
76        @param txt text of the edit (string)
77        """
78        self.__updateOK()
79
80    @pyqtSlot()
81    def on_fileButton_clicked(self):
82        """
83        Private slot to select a file via a file selection dialog.
84        """
85        fileName = E5FileDialog.getSaveFileName(
86            self,
87            self.tr("Select Archive File"),
88            Utilities.fromNativeSeparators(self.fileEdit.text()),
89            "")
90
91        if fileName:
92            root, ext = os.path.splitext(fileName)
93            if not ext:
94                ext = "." + self.formatComboBox.currentText()
95            fileName = root + ext
96            self.fileEdit.setText(Utilities.toNativeSeparators(fileName))
97
98    @pyqtSlot(bool)
99    def on_revButton_toggled(self, checked):
100        """
101        Private slot to handle changes of the rev select button.
102
103        @param checked state of the button (boolean)
104        """
105        self.__updateOK()
106
107    @pyqtSlot(str)
108    def on_revEdit_textChanged(self, txt):
109        """
110        Private slot to handle changes of the rev edit.
111
112        @param txt text of the edit (string)
113        """
114        self.__updateOK()
115
116    @pyqtSlot(bool)
117    def on_tagButton_toggled(self, checked):
118        """
119        Private slot to handle changes of the Tag select button.
120
121        @param checked state of the button (boolean)
122        """
123        self.__updateOK()
124
125    @pyqtSlot(str)
126    def on_tagCombo_editTextChanged(self, txt):
127        """
128        Private slot to handle changes of the Tag combo.
129
130        @param txt text of the combo (string)
131        """
132        self.__updateOK()
133
134    @pyqtSlot(bool)
135    def on_branchButton_toggled(self, checked):
136        """
137        Private slot to handle changes of the Branch select button.
138
139        @param checked state of the button (boolean)
140        """
141        self.__updateOK()
142
143    @pyqtSlot(str)
144    def on_branchCombo_editTextChanged(self, txt):
145        """
146        Private slot to handle changes of the Branch combo.
147
148        @param txt text of the combo (string)
149        """
150        self.__updateOK()
151
152    def getData(self):
153        """
154        Public method to retrieve the entered data.
155
156        @return tuple of selected revision (string), archive format (string),
157            archive file (string) and prefix (string)
158        """
159        if self.revButton.isChecked():
160            rev = self.revEdit.text()
161        elif self.tagButton.isChecked():
162            rev = self.tagCombo.currentText()
163        elif self.branchButton.isChecked():
164            rev = self.branchCombo.currentText()
165        else:
166            rev = "HEAD"
167
168        return (rev, self.formatComboBox.currentText(),
169                Utilities.toNativeSeparators(self.fileEdit.text()),
170                self.prefixEdit.text()
171                )
172