Commits

Anonymous committed 57b674d

settings: add genTextEntry function

This function creates a multi-line text edit widget that can be associated to
settings configuration keys.

This type of widget can contain settings whose value spans several lines.

The INI file format that is used to save mercurial's configuration settings does
supports having keys whose value spans more than line. However, it does not
support having empty lines in such a field. This revision takes this into,
account, removing any empty lines from the contents of the text fields before
saving them into the mercurial configuration files.

  • Participants
  • Parent commits bd50adc

Comments (0)

Files changed (1)

tortoisehg/hgqt/settings.py

         self.curvalue = None
         self.setEchoMode(QLineEdit.Password)
         self.setMinimumWidth(ENTRY_WIDTH)
+class TextEntry(QTextEdit):
+    def __init__(self, parent=None, **opts):
+        QTextEdit.__init__(self, parent, toolTip=opts['tooltip'])
+        self.opts = opts
+        self.curvalue = None
+        self.setMinimumWidth(ENTRY_WIDTH)
+
+    ## common APIs for all edit widgets
+    def setValue(self, curvalue):
+        self.curvalue = curvalue
+        if curvalue:
+            self.setPlainText(hglib.tounicode(curvalue))
+        else:
+            self.setPlainText('')
+
+    def value(self):
+        # It is not possible to set a multi-line value with an empty line
+        utext = self.removeEmptyLines(self.toPlainText())
+        return utext and hglib.fromunicode(utext) or None
+
+    def isDirty(self):
+        return self.value() != self.curvalue
+
+    def removeEmptyLines(self, text):
+        if not text:
+            return text
+        rawlines = hglib.fromunicode(text).splitlines()
+        lines = []
+        for line in rawlines:
+            if not line.strip():
+                continue
+            lines.append(line)
+        return os.linesep.join(lines)
+
 
 class FontEntry(QWidget):
     def __init__(self, parent=None, **opts):
 def genPasswordEntry(opts):
     'Generate a password entry box'
     return PasswordEntry(**opts)
+def genTextEntry(opts):
+    'Generate a multi-line text input entry box'
+    return TextEntry(**opts)
 
 def genDefaultCombo(opts, defaults=[]):
     'user must select from a list'