1# -*- coding: utf-8 -*-
2
3# Copyright (c) 2014 - 2021 Detlev Offenbach <detlev@die-offenbachs.de>
4#
5
6"""
7Module implementing a dialog to select the data for pushing a branch.
8"""
9
10from PyQt5.QtCore import pyqtSlot
11from PyQt5.QtWidgets import QDialog, QDialogButtonBox
12
13from .Ui_GitBranchPushDialog import Ui_GitBranchPushDialog
14
15
16class GitBranchPushDialog(QDialog, Ui_GitBranchPushDialog):
17    """
18    Class implementing a dialog to select the data for pushing a branch.
19    """
20    def __init__(self, branches, remotes, delete=False, parent=None):
21        """
22        Constructor
23
24        @param branches list of branch names (list of string)
25        @param remotes list of remote names (list of string)
26        @param delete flag indicating a delete branch action (boolean)
27        @param parent reference to the parent widget (QWidget)
28        """
29        super().__init__(parent)
30        self.setupUi(self)
31
32        self.__okButton = self.buttonBox.button(
33            QDialogButtonBox.StandardButton.Ok)
34
35        self.__allBranches = self.tr("<all branches>")
36
37        if "origin" in remotes:
38            self.remoteComboBox.addItem("origin")
39            remotes.remove("origin")
40        self.remoteComboBox.addItems(sorted(remotes))
41
42        if delete:
43            self.branchComboBox.addItem("")
44        else:
45            self.branchComboBox.addItem(self.__allBranches)
46        if "master" in branches:
47            if not delete:
48                self.branchComboBox.addItem("master")
49            branches.remove("master")
50        self.branchComboBox.addItems(sorted(branches))
51
52        if delete:
53            self.__okButton.setEnabled(False)
54            self.branchComboBox.setEditable(True)
55
56    @pyqtSlot(str)
57    def on_branchComboBox_editTextChanged(self, txt):
58        """
59        Private slot to handle a change of the branch name.
60
61        @param txt branch name (string)
62        """
63        self.__okButton.setEnabled(bool(txt))
64
65    def getData(self):
66        """
67        Public method to get the selected data.
68
69        @return tuple of selected branch name, remote name and a flag
70            indicating all branches (tuple of two strings and a boolean)
71        """
72        return (self.branchComboBox.currentText(),
73                self.remoteComboBox.currentText(),
74                self.branchComboBox.currentText() == self.__allBranches)
75