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