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

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

eric ide

mercurial