Plugins/VcsPlugins/vcsMercurial/HisteditExtension/HgHisteditCommitEditor.py

changeset 5283
06423d65a2b8
child 5291
e93d14b48c34
equal deleted inserted replaced
5282:898b0dda21e1 5283:06423d65a2b8
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2016 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(HgHisteditCommitEditor, self).__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 f = open(self.__fileName, "r")
53 txt = f.read()
54 f.close()
55 except (IOError, OSError) as err:
56 E5MessageBox.critical(
57 self,
58 self.tr("Edit Commit Message"),
59 self.tr("""<p>The file <b>{0}</b> could not be read.</p>"""
60 """<p>Reason: {1}</p>""").format(
61 self.__fileName, str(err)))
62 self.on_buttonBox_rejected()
63 return
64
65 msgLines = []
66 infoLines = []
67 for line in txt.splitlines():
68 if line.startswith("#"):
69 infoLines.append(line[1:].lstrip())
70 elif line.startswith("HG:"):
71 infoLines.append(line[3:].lstrip())
72 else:
73 msgLines.append(line)
74
75 # remove empty lines at end of message
76 for row in range(len(msgLines) - 1, -1, -1):
77 if msgLines[row] == "":
78 del msgLines[row]
79 else:
80 break
81
82 self.messageEdit.setPlainText("\n".join(msgLines))
83 self.infoEdit.setPlainText("\n".join(infoLines))
84
85 @pyqtSlot()
86 def on_buttonBox_accepted(self):
87 """
88 Private slot called by the buttonBox accepted signal.
89 """
90 msg = self.messageEdit.toPlainText()
91 try:
92 f = open(self.__fileName, "w")
93 f.write(msg)
94 f.close()
95 except (IOError, OSError) as err:
96 E5MessageBox.critical(
97 self,
98 self.tr("Edit Commit Message"),
99 self.tr("""<p>The file <b>{0}</b> could not be read.</p>"""
100 """<p>Reason: {1}</p>""").format(
101 self.__fileName, str(err)))
102 self.on_buttonBox_rejected()
103 return
104
105 self.close()
106 QCoreApplication.exit(0)
107
108 @pyqtSlot()
109 def on_buttonBox_rejected(self):
110 """
111 Private slot called by the buttonBox rejected signal.
112 """
113 self.close()
114 QCoreApplication.exit(1)
115
116 @pyqtSlot(str)
117 def on_recentComboBox_activated(self, txt):
118 """
119 Private slot to select a commit message from recent ones.
120
121 @param txt text of the selected entry (string)
122 """
123 if txt:
124 self.messageEdit.setPlainText(txt)

eric ide

mercurial