1# -*- coding: utf-8 -*-
2
3# Copyright (c) 2003 - 2021 Detlev Offenbach <detlev@die-offenbachs.de>
4#
5
6"""
7Module implementing a dialog to enter the data for a merge operation.
8"""
9
10import re
11
12from PyQt5.QtWidgets import QDialog, QDialogButtonBox
13
14from .Ui_SvnMergeDialog import Ui_SvnMergeDialog
15
16
17class SvnMergeDialog(QDialog, Ui_SvnMergeDialog):
18    """
19    Class implementing a dialog to enter the data for a merge operation.
20    """
21    def __init__(self, mergelist1, mergelist2, targetlist, force=False,
22                 parent=None):
23        """
24        Constructor
25
26        @param mergelist1 list of previously entered URLs/revisions
27            (list of strings)
28        @param mergelist2 list of previously entered URLs/revisions
29            (list of strings)
30        @param targetlist list of previously entered targets (list of strings)
31        @param force flag indicating a forced merge (boolean)
32        @param parent parent widget (QWidget)
33        """
34        super().__init__(parent)
35        self.setupUi(self)
36
37        self.forceCheckBox.setChecked(force)
38
39        self.okButton = self.buttonBox.button(
40            QDialogButtonBox.StandardButton.Ok)
41        self.okButton.setEnabled(False)
42
43        self.rx_url = re.compile('(?:file:|svn:|svn+ssh:|http:|https:)//.+')
44        self.rx_rev = re.compile('\\d+')
45
46        self.tag1Combo.clear()
47        self.tag1Combo.addItems(mergelist1)
48        self.tag2Combo.clear()
49        self.tag2Combo.addItems(mergelist2)
50        self.targetCombo.clear()
51        self.targetCombo.addItems(targetlist)
52
53        msh = self.minimumSizeHint()
54        self.resize(max(self.width(), msh.width()), msh.height())
55
56    def __enableOkButton(self):
57        """
58        Private method used to enable/disable the OK-button.
59        """
60        self.okButton.setDisabled(
61            self.tag1Combo.currentText() == "" or
62            self.tag2Combo.currentText() == "" or
63            not (
64                (bool(self.rx_url.fullmatch(self.tag1Combo.currentText())) and
65                 bool(self.rx_url.fullmatch(self.tag2Combo.currentText()))) or
66                (bool(self.rx_rev.fullmatch(self.tag1Combo.currentText())) and
67                 bool(self.rx_rev.fullmatch(self.tag2Combo.currentText())))
68            )
69        )
70
71    def on_tag1Combo_editTextChanged(self, text):
72        """
73        Private slot to handle the tag1Combo editTextChanged signal.
74
75        @param text text of the combo (string)
76        """
77        self.__enableOkButton()
78
79    def on_tag2Combo_editTextChanged(self, text):
80        """
81        Private slot to handle the tag2Combo editTextChanged signal.
82
83        @param text text of the combo (string)
84        """
85        self.__enableOkButton()
86
87    def getParameters(self):
88        """
89        Public method to retrieve the merge data.
90
91        @return tuple naming two tag names or two revisions, a target and
92            a flag indicating a forced merge (string, string, string, boolean)
93        """
94        return (self.tag1Combo.currentText(),
95                self.tag2Combo.currentText(),
96                self.targetCombo.currentText(),
97                self.forceCheckBox.isChecked())
98