1# -*- coding: utf-8 -*-
2
3# Copyright (c) 2011 - 2021 Detlev Offenbach <detlev@die-offenbachs.de>
4#
5
6"""
7Module implementing a dialog to enter data for the Mercurial export command.
8"""
9
10import os
11
12from PyQt5.QtCore import pyqtSlot, QDir
13from PyQt5.QtWidgets import QDialog, QDialogButtonBox
14
15from E5Gui.E5PathPicker import E5PathPickerModes
16
17from .Ui_HgExportDialog import Ui_HgExportDialog
18
19
20class HgExportDialog(QDialog, Ui_HgExportDialog):
21    """
22    Class implementing a dialog to enter data for the Mercurial export command.
23    """
24    def __init__(self, bookmarksList, bookmarkAvailable, parent=None):
25        """
26        Constructor
27
28        @param bookmarksList list of defined bookmarks
29        @type list of str
30        @param bookmarkAvailable flag indicating the availability of the
31            "--bookmark" option
32        @type bool
33        @param parent reference to the parent widget
34        @type QWidget
35        """
36        super().__init__(parent)
37        self.setupUi(self)
38
39        self.directoryPicker.setMode(E5PathPickerModes.DirectoryMode)
40
41        self.buttonBox.button(
42            QDialogButtonBox.StandardButton.Ok).setEnabled(False)
43
44        # set default values for directory and pattern
45        self.patternEdit.setText("%b_%r_%h_%n_of_%N.diff")
46        self.directoryPicker.setText(QDir.tempPath())
47
48        self.bookmarkCombo.addItem("")
49        self.bookmarkCombo.addItems(sorted(bookmarksList))
50        self.bookmarkCombo.setEnabled(bookmarkAvailable)
51
52    def __updateOK(self):
53        """
54        Private slot to update the OK button.
55        """
56        enabled = True
57
58        if (
59            self.directoryPicker.text() == "" or
60            self.patternEdit.text() == "" or
61            (self.changesetsEdit.toPlainText() == "" and
62             self.bookmarkCombo.currentText() == "")
63        ):
64            enabled = False
65
66        self.buttonBox.button(
67            QDialogButtonBox.StandardButton.Ok).setEnabled(enabled)
68
69    @pyqtSlot(str)
70    def on_directoryPicker_textChanged(self, txt):
71        """
72        Private slot to react on changes of the export directory edit.
73
74        @param txt contents of the line edit (string)
75        """
76        self.__updateOK()
77
78    @pyqtSlot(str)
79    def on_patternEdit_textChanged(self, txt):
80        """
81        Private slot to react on changes of the export file name pattern edit.
82
83        @param txt contents of the line edit (string)
84        """
85        self.__updateOK()
86
87    @pyqtSlot()
88    def on_changesetsEdit_textChanged(self):
89        """
90        Private slot to react on changes of the changesets edit.
91        """
92        self.__updateOK()
93
94    def getParameters(self):
95        """
96        Public method to retrieve the export data.
97
98        @return tuple naming the output file name, the list of revisions to
99            export, the name of a bookmarked branch and flags indicating to
100            compare against the second parent, to treat all files as text,
101            to omit dates in the diff headers and to use the git extended
102            diff format
103        @rtype tuple of (str, list of str, str, bool, bool, bool, bool)
104        """
105        return (
106            os.path.join(
107                self.directoryPicker.text(),
108                self.patternEdit.text()),
109            self.changesetsEdit.toPlainText().splitlines(),
110            self.bookmarkCombo.currentText(),
111            self.switchParentCheckBox.isChecked(),
112            self.textCheckBox.isChecked(),
113            self.datesCheckBox.isChecked(),
114            self.gitCheckBox.isChecked()
115        )
116