1# -*- coding: utf-8 -*-
2
3# Copyright (c) 2010 - 2021 Detlev Offenbach <detlev@die-offenbachs.de>
4#
5
6"""
7Module implementing a dialog to select a revision.
8"""
9
10from PyQt5.QtCore import pyqtSlot
11from PyQt5.QtWidgets import QDialog, QDialogButtonBox
12
13from .Ui_HgRevisionSelectionDialog import Ui_HgRevisionSelectionDialog
14
15
16class HgRevisionSelectionDialog(QDialog, Ui_HgRevisionSelectionDialog):
17    """
18    Class implementing a dialog to select a revision.
19    """
20    def __init__(self, tagsList, branchesList, bookmarksList=None,
21                 noneLabel="", parent=None):
22        """
23        Constructor
24
25        @param tagsList list of tags (list of strings)
26        @param branchesList list of branches (list of strings)
27        @param bookmarksList list of bookmarks (list of strings)
28        @param noneLabel labeltext for "no revision selected" (string)
29        @param parent parent widget (QWidget)
30        """
31        super().__init__(parent)
32        self.setupUi(self)
33
34        self.buttonBox.button(
35            QDialogButtonBox.StandardButton.Ok).setEnabled(False)
36
37        self.tagCombo.addItems(sorted(tagsList))
38        self.branchCombo.addItems(["default"] + sorted(branchesList))
39        if bookmarksList is not None:
40            self.bookmarkCombo.addItems(sorted(bookmarksList))
41        else:
42            self.bookmarkButton.setHidden(True)
43            self.bookmarkCombo.setHidden(True)
44
45        if noneLabel:
46            self.noneButton.setText(noneLabel)
47
48        msh = self.minimumSizeHint()
49        self.resize(max(self.width(), msh.width()), msh.height())
50
51    def __updateOK(self):
52        """
53        Private slot to update the OK button.
54        """
55        enabled = True
56        if self.idButton.isChecked():
57            enabled = self.idEdit.text() != ""
58        elif self.tagButton.isChecked():
59            enabled = self.tagCombo.currentText() != ""
60        elif self.branchButton.isChecked():
61            enabled = self.branchCombo.currentText() != ""
62        elif self.bookmarkButton.isChecked():
63            enabled = self.bookmarkCombo.currentText() != ""
64
65        self.buttonBox.button(
66            QDialogButtonBox.StandardButton.Ok).setEnabled(enabled)
67
68    @pyqtSlot(bool)
69    def on_idButton_toggled(self, checked):
70        """
71        Private slot to handle changes of the ID select button.
72
73        @param checked state of the button (boolean)
74        """
75        self.__updateOK()
76
77    @pyqtSlot(bool)
78    def on_tagButton_toggled(self, checked):
79        """
80        Private slot to handle changes of the Tag select button.
81
82        @param checked state of the button (boolean)
83        """
84        self.__updateOK()
85
86    @pyqtSlot(bool)
87    def on_branchButton_toggled(self, checked):
88        """
89        Private slot to handle changes of the Branch select button.
90
91        @param checked state of the button (boolean)
92        """
93        self.__updateOK()
94
95    @pyqtSlot(bool)
96    def on_bookmarkButton_toggled(self, checked):
97        """
98        Private slot to handle changes of the Bookmark select button.
99
100        @param checked state of the button (boolean)
101        """
102        self.__updateOK()
103
104    @pyqtSlot(str)
105    def on_idEdit_textChanged(self, txt):
106        """
107        Private slot to handle changes of the ID edit.
108
109        @param txt text of the edit (string)
110        """
111        self.__updateOK()
112
113    @pyqtSlot(str)
114    def on_tagCombo_editTextChanged(self, txt):
115        """
116        Private slot to handle changes of the Tag combo.
117
118        @param txt text of the combo (string)
119        """
120        self.__updateOK()
121
122    @pyqtSlot(str)
123    def on_branchCombo_editTextChanged(self, txt):
124        """
125        Private slot to handle changes of the Branch combo.
126
127        @param txt text of the combo (string)
128        """
129        self.__updateOK()
130
131    @pyqtSlot(str)
132    def on_bookmarkCombo_editTextChanged(self, txt):
133        """
134        Private slot to handle changes of the Bookmark combo.
135
136        @param txt text of the combo (string)
137        """
138        self.__updateOK()
139
140    def getRevision(self, revset=True):
141        """
142        Public method to retrieve the selected revision.
143
144        @param revset flag indicating to get the revision or ID as a
145            revset
146        @type bool
147        @return selected revision
148        @rtype str
149        """
150        if self.numberButton.isChecked():
151            if revset:
152                rev = "rev({0})".format(self.numberSpinBox.value())
153            else:
154                rev = str(self.numberSpinBox.value())
155        elif self.idButton.isChecked():
156            if revset:
157                rev = "id({0})".format(self.idEdit.text())
158            else:
159                rev = self.idEdit.text()
160        elif self.tagButton.isChecked():
161            rev = self.tagCombo.currentText()
162        elif self.branchButton.isChecked():
163            rev = self.branchCombo.currentText()
164        elif self.bookmarkButton.isChecked():
165            rev = self.bookmarkCombo.currentText()
166        elif self.tipButton.isChecked():
167            rev = "tip"
168        else:
169            rev = ""
170
171        return rev
172