eric7/EricWidgets/EricTextSpinBox.py

branch
eric7
changeset 8358
144a6b854f70
parent 8356
68ec9c3d4de5
child 8881
54e42bc2437a
diff -r a081458cc57b -r 144a6b854f70 eric7/EricWidgets/EricTextSpinBox.py
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/eric7/EricWidgets/EricTextSpinBox.py	Sat May 22 19:58:24 2021 +0200
@@ -0,0 +1,83 @@
+# -*- coding: utf-8 -*-
+
+# Copyright (c) 2010 - 2021 Detlev Offenbach <detlev@die-offenbachs.de>
+#
+
+"""
+Module implementing a spinbox with textual entries.
+"""
+
+from PyQt6.QtWidgets import QSpinBox
+
+
+class EricTextSpinBox(QSpinBox):
+    """
+    Class implementing a spinbox with textual entries.
+    """
+    def __init__(self, parent=None):
+        """
+        Constructor
+        
+        @param parent reference to the parent widget (QWidget)
+        """
+        super().__init__(parent)
+        
+        self.__items = []
+        
+        self.setMinimum(0)
+        self.setMaximum(0)
+    
+    def addItem(self, txt, data=None):
+        """
+        Public method to add an item with item data.
+        
+        @param txt text to be shown (string)
+        @param data associated data
+        """
+        self.__items.append((txt, data))
+        self.setMaximum(len(self.__items) - 1)
+    
+    def itemData(self, index):
+        """
+        Public method to retrieve the data associated with an item.
+        
+        @param index index of the item (integer)
+        @return associated data
+        """
+        try:
+            return self.__items[index][1]
+        except IndexError:
+            return None
+    
+    def currentIndex(self):
+        """
+        Public method to retrieve the current index.
+        
+        @return current index (integer)
+        """
+        return self.value()
+    
+    def textFromValue(self, value):
+        """
+        Public method to convert a value to text.
+        
+        @param value value to be converted (integer)
+        @return text for the given value (string)
+        """
+        try:
+            return self.__items[value][0]
+        except IndexError:
+            return ""
+    
+    def valueFromText(self, txt):
+        """
+        Public method to convert a text to a value.
+        
+        @param txt text to be converted (string)
+        @return value for the given text (integer)
+        """
+        for index in range(len(self.__items)):
+            if self.__items[index][0] == txt:
+                return index
+        
+        return self.minimum()

eric ide

mercurial