eric7/Plugins/VcsPlugins/vcsMercurial/HisteditExtension/HgHisteditCommitEditor.py

branch
eric7
changeset 8312
800c432b34c8
parent 8218
7c09585bd960
child 8318
962bce857696
equal deleted inserted replaced
8311:4e8b98454baa 8312:800c432b34c8
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2016 - 2021 Detlev Offenbach <detlev@die-offenbachs.de>
4 #
5
6 """
7 Module implementing a dialog to edit the commit message of a revision.
8 """
9
10 from PyQt5.QtCore import pyqtSlot, QCoreApplication
11 from PyQt5.QtWidgets import QDialog
12
13 from E5Gui import E5MessageBox
14
15 from Ui_HgHisteditCommitEditor import Ui_HgHisteditCommitEditor
16
17 import Preferences
18
19
20 class HgHisteditCommitEditor(QDialog, Ui_HgHisteditCommitEditor):
21 """
22 Class implementing a dialog to edit the commit message of a revision.
23 """
24 def __init__(self, fileName, parent=None):
25 """
26 Constructor
27
28 @param fileName name of the file containing the commit message
29 to be edited
30 @type str
31 @param parent reference to the parent widget
32 @type QWidget
33 """
34 super().__init__(parent)
35 self.setupUi(self)
36
37 self.__fileName = fileName
38 self.__readFile()
39
40 self.recentCommitMessages = Preferences.toList(
41 Preferences.Prefs.settings.value('Mercurial/Commits'))
42 self.recentComboBox.clear()
43 self.recentComboBox.addItem("")
44 self.recentComboBox.addItems(self.recentCommitMessages)
45
46 def __readFile(self):
47 """
48 Private method to read the file containing the commit message and
49 populate the dialog.
50 """
51 try:
52 with open(self.__fileName, "r") as f:
53 txt = f.read()
54 except OSError as err:
55 E5MessageBox.critical(
56 self,
57 self.tr("Edit Commit Message"),
58 self.tr("""<p>The file <b>{0}</b> could not be read.</p>"""
59 """<p>Reason: {1}</p>""").format(
60 self.__fileName, str(err)))
61 self.on_buttonBox_rejected()
62 return
63
64 msgLines = []
65 infoLines = []
66 for line in txt.splitlines():
67 if line.startswith("#"):
68 infoLines.append(line[1:].lstrip())
69 elif line.startswith("HG:"):
70 infoLines.append(line[3:].lstrip())
71 else:
72 msgLines.append(line)
73
74 # remove empty lines at end of message
75 for row in range(len(msgLines) - 1, -1, -1):
76 if msgLines[row] == "":
77 del msgLines[row]
78 else:
79 break
80
81 self.messageEdit.setPlainText("\n".join(msgLines))
82 self.infoEdit.setPlainText("\n".join(infoLines))
83
84 @pyqtSlot()
85 def on_buttonBox_accepted(self):
86 """
87 Private slot called by the buttonBox accepted signal.
88 """
89 msg = self.messageEdit.toPlainText()
90 try:
91 with open(self.__fileName, "w") as f:
92 f.write(msg)
93 except OSError as err:
94 E5MessageBox.critical(
95 self,
96 self.tr("Edit Commit Message"),
97 self.tr("""<p>The file <b>{0}</b> could not be read.</p>"""
98 """<p>Reason: {1}</p>""").format(
99 self.__fileName, str(err)))
100 self.on_buttonBox_rejected()
101 return
102
103 self.close()
104 QCoreApplication.exit(0)
105
106 @pyqtSlot()
107 def on_buttonBox_rejected(self):
108 """
109 Private slot called by the buttonBox rejected signal.
110 """
111 self.close()
112 QCoreApplication.exit(1)
113
114 @pyqtSlot(int)
115 def on_recentComboBox_activated(self, index):
116 """
117 Private slot to select a commit message from recent ones.
118
119 @param index index of the selected entry
120 @type int
121 """
122 txt = self.recentComboBox.itemText(index)
123 if txt:
124 self.messageEdit.setPlainText(txt)

eric ide

mercurial