1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2010 - 2021 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing a spinbox with textual entries. |
|
8 """ |
|
9 |
|
10 from PyQt6.QtWidgets import QSpinBox |
|
11 |
|
12 |
|
13 class E5TextSpinBox(QSpinBox): |
|
14 """ |
|
15 Class implementing a spinbox with textual entries. |
|
16 """ |
|
17 def __init__(self, parent=None): |
|
18 """ |
|
19 Constructor |
|
20 |
|
21 @param parent reference to the parent widget (QWidget) |
|
22 """ |
|
23 super().__init__(parent) |
|
24 |
|
25 self.__items = [] |
|
26 |
|
27 self.setMinimum(0) |
|
28 self.setMaximum(0) |
|
29 |
|
30 def addItem(self, txt, data=None): |
|
31 """ |
|
32 Public method to add an item with item data. |
|
33 |
|
34 @param txt text to be shown (string) |
|
35 @param data associated data |
|
36 """ |
|
37 self.__items.append((txt, data)) |
|
38 self.setMaximum(len(self.__items) - 1) |
|
39 |
|
40 def itemData(self, index): |
|
41 """ |
|
42 Public method to retrieve the data associated with an item. |
|
43 |
|
44 @param index index of the item (integer) |
|
45 @return associated data |
|
46 """ |
|
47 try: |
|
48 return self.__items[index][1] |
|
49 except IndexError: |
|
50 return None |
|
51 |
|
52 def currentIndex(self): |
|
53 """ |
|
54 Public method to retrieve the current index. |
|
55 |
|
56 @return current index (integer) |
|
57 """ |
|
58 return self.value() |
|
59 |
|
60 def textFromValue(self, value): |
|
61 """ |
|
62 Public method to convert a value to text. |
|
63 |
|
64 @param value value to be converted (integer) |
|
65 @return text for the given value (string) |
|
66 """ |
|
67 try: |
|
68 return self.__items[value][0] |
|
69 except IndexError: |
|
70 return "" |
|
71 |
|
72 def valueFromText(self, txt): |
|
73 """ |
|
74 Public method to convert a text to a value. |
|
75 |
|
76 @param txt text to be converted (string) |
|
77 @return value for the given text (integer) |
|
78 """ |
|
79 for index in range(len(self.__items)): |
|
80 if self.__items[index][0] == txt: |
|
81 return index |
|
82 |
|
83 return self.minimum() |
|