src/eric7/UI/NumbersWidget.py

branch
eric7
changeset 10433
328f3ec4b77a
parent 10069
435cc5875135
child 10439
21c28b0f9e41
equal deleted inserted replaced
10432:2fe91fe443dd 10433:328f3ec4b77a
23 23
24 def __init__(self, parent=None): 24 def __init__(self, parent=None):
25 """ 25 """
26 Constructor 26 Constructor
27 27
28 @param parent reference to the parent widget (QWidget) 28 @param parent reference to the parent widget
29 @type QWidget
29 """ 30 """
30 super().__init__(parent) 31 super().__init__(parent)
31 32
32 self.__bits = 0 33 self.__bits = 0
33 self.__value = 0 34 self.__value = 0
34 35
35 def rowCount(self, parent): # noqa: U100 36 def rowCount(self, parent): # noqa: U100
36 """ 37 """
37 Public method to get the number of rows of the model. 38 Public method to get the number of rows of the model.
38 39
39 @param parent parent index (QModelIndex) 40 @param parent parent index
40 @return number of columns (integer) 41 @type QModelIndex
42 @return number of columns
43 @rtype int
41 """ 44 """
42 return 1 45 return 1
43 46
44 def columnCount(self, parent): # noqa: U100 47 def columnCount(self, parent): # noqa: U100
45 """ 48 """
46 Public method to get the number of columns of the model. 49 Public method to get the number of columns of the model.
47 50
48 @param parent parent index (QModelIndex) 51 @param parent parent index
49 @return number of columns (integer) 52 @type QModelIndex
53 @return number of columns
54 @rtype int
50 """ 55 """
51 return self.__bits 56 return self.__bits
52 57
53 def data(self, index, role=Qt.ItemDataRole.DisplayRole): 58 def data(self, index, role=Qt.ItemDataRole.DisplayRole):
54 """ 59 """
55 Public method to get data from the model. 60 Public method to get data from the model.
56 61
57 @param index index to get data for (QModelIndex) 62 @param index index to get data for
58 @param role role of the data to retrieve (integer) 63 @type QModelIndex
64 @param role role of the data to retrieve
65 @type int
59 @return requested data 66 @return requested data
67 @rtype Any
60 """ 68 """
61 if role == Qt.ItemDataRole.CheckStateRole: 69 if role == Qt.ItemDataRole.CheckStateRole:
62 if (self.__value >> (self.__bits - index.column() - 1)) & 1: 70 if (self.__value >> (self.__bits - index.column() - 1)) & 1:
63 return Qt.CheckState.Checked 71 return Qt.CheckState.Checked
64 else: 72 else:
71 79
72 def flags(self, index): # noqa: U100 80 def flags(self, index): # noqa: U100
73 """ 81 """
74 Public method to get flags from the model. 82 Public method to get flags from the model.
75 83
76 @param index index to get flags for (QModelIndex) 84 @param index index to get flags for
77 @return flags (Qt.ItemFlags) 85 @type QModelIndex
86 @return flags
87 @rtype Qt.ItemFlags
78 """ 88 """
79 return ( 89 return (
80 Qt.ItemFlag.ItemIsUserCheckable 90 Qt.ItemFlag.ItemIsUserCheckable
81 | Qt.ItemFlag.ItemIsEnabled 91 | Qt.ItemFlag.ItemIsEnabled
82 | Qt.ItemFlag.ItemIsSelectable 92 | Qt.ItemFlag.ItemIsSelectable
84 94
85 def headerData(self, section, orientation, role=Qt.ItemDataRole.DisplayRole): 95 def headerData(self, section, orientation, role=Qt.ItemDataRole.DisplayRole):
86 """ 96 """
87 Public method to get header data from the model. 97 Public method to get header data from the model.
88 98
89 @param section section number (integer) 99 @param section section number
90 @param orientation orientation (Qt.Orientation) 100 @type int
91 @param role role of the data to retrieve (Qt.ItemDataRole) 101 @param orientation orientation
102 @type Qt.Orientation
103 @param role role of the data to retrieve
104 @type Qt.ItemDataRole
92 @return requested data 105 @return requested data
106 @rtype Any
93 """ 107 """
94 if ( 108 if (
95 orientation == Qt.Orientation.Horizontal 109 orientation == Qt.Orientation.Horizontal
96 and role == Qt.ItemDataRole.DisplayRole 110 and role == Qt.ItemDataRole.DisplayRole
97 ): 111 ):
101 115
102 def setBits(self, bits): 116 def setBits(self, bits):
103 """ 117 """
104 Public slot to set the number of bits. 118 Public slot to set the number of bits.
105 119
106 @param bits number of bits to show (integer) 120 @param bits number of bits to show
121 @type int
107 """ 122 """
108 self.beginResetModel() 123 self.beginResetModel()
109 self.__bits = bits 124 self.__bits = bits
110 self.endResetModel() 125 self.endResetModel()
111 126
112 def setValue(self, value): 127 def setValue(self, value):
113 """ 128 """
114 Public slot to set the value to show. 129 Public slot to set the value to show.
115 130
116 @param value value to show (integer) 131 @param value value to show
132 @type int
117 """ 133 """
118 self.beginResetModel() 134 self.beginResetModel()
119 self.__value = value 135 self.__value = value
120 self.endResetModel() 136 self.endResetModel()
121 137
122 def setBitsAndValue(self, bits, value): 138 def setBitsAndValue(self, bits, value):
123 """ 139 """
124 Public slot to set the number of bits and the value to show. 140 Public slot to set the number of bits and the value to show.
125 141
126 @param bits number of bits to show (integer) 142 @param bits number of bits to show
127 @param value value to show (integer) 143 @type int
144 @param value value to show
145 @type int
128 """ 146 """
129 self.__bits = bits 147 self.__bits = bits
130 self.__value = value 148 self.__value = value
131 self.beginResetModel() 149 self.beginResetModel()
132 self.endResetModel() 150 self.endResetModel()
133 151
134 def getValue(self): 152 def getValue(self):
135 """ 153 """
136 Public slot to get the current value. 154 Public slot to get the current value.
137 155
138 @return current value of the model (integer) 156 @return current value of the model
157 @rtype int
139 """ 158 """
140 return self.__value 159 return self.__value
141 160
142 def setData(self, index, value, role=Qt.ItemDataRole.EditRole): 161 def setData(self, index, value, role=Qt.ItemDataRole.EditRole):
143 """ 162 """
144 Public method to set the data of a node cell. 163 Public method to set the data of a node cell.
145 164
146 @param index index of the node cell (QModelIndex) 165 @param index index of the node cell
166 @type QModelIndex
147 @param value value to be set 167 @param value value to be set
148 @param role role of the data (integer) 168 @type Any
149 @return flag indicating success (boolean) 169 @param role role of the data
170 @type int
171 @return flag indicating success
172 @rtype boolean)
150 """ 173 """
151 if role == Qt.ItemDataRole.CheckStateRole: 174 if role == Qt.ItemDataRole.CheckStateRole:
152 if Qt.CheckState(value) == Qt.CheckState.Checked: 175 if Qt.CheckState(value) == Qt.CheckState.Checked:
153 self.__value |= 1 << self.__bits - index.column() - 1 176 self.__value |= 1 << self.__bits - index.column() - 1
154 else: 177 else:
171 194
172 def __init__(self, parent=None): 195 def __init__(self, parent=None):
173 """ 196 """
174 Constructor 197 Constructor
175 198
176 @param parent reference to the parent widget (QWidget) 199 @param parent reference to the parent widget
200 @type QWidget
177 """ 201 """
178 super().__init__(parent) 202 super().__init__(parent)
179 self.setupUi(self) 203 self.setupUi(self)
180 204
181 self.setWindowIcon(EricPixmapCache.getIcon("eric")) 205 self.setWindowIcon(EricPixmapCache.getIcon("eric"))
220 244
221 def __formatNumbers(self, numberFormat): 245 def __formatNumbers(self, numberFormat):
222 """ 246 """
223 Private method to format the various number inputs. 247 Private method to format the various number inputs.
224 248
225 @param numberFormat number format indicator (integer) 249 @param numberFormat number format indicator
250 @type int
226 """ 251 """
227 self.__block(True) 252 self.__block(True)
228 253
229 self.binEdit.setStyleSheet("") 254 self.binEdit.setStyleSheet("")
230 self.octEdit.setStyleSheet("") 255 self.octEdit.setStyleSheet("")
279 304
280 def __block(self, b): 305 def __block(self, b):
281 """ 306 """
282 Private slot to block some signals. 307 Private slot to block some signals.
283 308
284 @param b flah indicating the blocking state (boolean) 309 @param b flah indicating the blocking state
310 @type bool
285 """ 311 """
286 self.hexEdit.blockSignals(b) 312 self.hexEdit.blockSignals(b)
287 self.decEdit.blockSignals(b) 313 self.decEdit.blockSignals(b)
288 self.octEdit.blockSignals(b) 314 self.octEdit.blockSignals(b)
289 self.binEdit.blockSignals(b) 315 self.binEdit.blockSignals(b)
292 @pyqtSlot(int) 318 @pyqtSlot(int)
293 def on_sizeBox_valueChanged(self, value): 319 def on_sizeBox_valueChanged(self, value):
294 """ 320 """
295 Private slot handling a change of the bit size. 321 Private slot handling a change of the bit size.
296 322
297 @param value selected bit size (integer) 323 @param value selected bit size
324 @type int
298 """ 325 """
299 self.__formatNumbers(10) 326 self.__formatNumbers(10)
300 327
301 @pyqtSlot() 328 @pyqtSlot()
302 def on_byteOrderButton_clicked(self): 329 def on_byteOrderButton_clicked(self):
332 @pyqtSlot(str) 359 @pyqtSlot(str)
333 def on_binEdit_textChanged(self, txt): 360 def on_binEdit_textChanged(self, txt):
334 """ 361 """
335 Private slot to handle input of a binary number. 362 Private slot to handle input of a binary number.
336 363
337 @param txt text entered (string) 364 @param txt text entered
365 @type str
338 """ 366 """
339 try: 367 try:
340 self.__input = int(txt, 2) 368 self.__input = int(txt, 2)
341 self.__inputValid = True 369 self.__inputValid = True
342 except ValueError: 370 except ValueError:
357 @pyqtSlot(QModelIndex, QModelIndex) 385 @pyqtSlot(QModelIndex, QModelIndex)
358 def __binModelDataChanged(self, start, end): 386 def __binModelDataChanged(self, start, end):
359 """ 387 """
360 Private slot to handle a change of the binary model value by the user. 388 Private slot to handle a change of the binary model value by the user.
361 389
362 @param start start index (QModelIndex) 390 @param start start index
363 @param end end index (QModelIndex) 391 @type QModelIndex
392 @param end end index
393 @type QModelIndex
364 """ 394 """
365 val = self.__model.getValue() 395 val = self.__model.getValue()
366 bytesIn = self.sizeBox.itemData(self.sizeBox.currentIndex()) // 8 396 bytesIn = self.sizeBox.itemData(self.sizeBox.currentIndex()) // 8
367 num = "{0:0{1}b}".format(val, bytesIn * 8) 397 num = "{0:0{1}b}".format(val, bytesIn * 8)
368 self.binEdit.setText(num) 398 self.binEdit.setText(num)
382 @pyqtSlot(str) 412 @pyqtSlot(str)
383 def on_octEdit_textChanged(self, txt): 413 def on_octEdit_textChanged(self, txt):
384 """ 414 """
385 Private slot to handle input of an octal number. 415 Private slot to handle input of an octal number.
386 416
387 @param txt text entered (string) 417 @param txt text entered
418 @type str
388 """ 419 """
389 try: 420 try:
390 self.__input = int(txt, 8) 421 self.__input = int(txt, 8)
391 self.__inputValid = True 422 self.__inputValid = True
392 except ValueError: 423 except ValueError:
419 @pyqtSlot(str) 450 @pyqtSlot(str)
420 def on_decEdit_textChanged(self, txt): 451 def on_decEdit_textChanged(self, txt):
421 """ 452 """
422 Private slot to handle input of a decimal number. 453 Private slot to handle input of a decimal number.
423 454
424 @param txt text entered (string) 455 @param txt text entered
456 @type str
425 """ 457 """
426 try: 458 try:
427 self.__input = int(txt, 10) 459 self.__input = int(txt, 10)
428 self.__inputValid = True 460 self.__inputValid = True
429 except ValueError: 461 except ValueError:
456 @pyqtSlot(str) 488 @pyqtSlot(str)
457 def on_hexEdit_textChanged(self, txt): 489 def on_hexEdit_textChanged(self, txt):
458 """ 490 """
459 Private slot to handle input of a hexadecimal number. 491 Private slot to handle input of a hexadecimal number.
460 492
461 @param txt text entered (string) 493 @param txt text entered
494 @type str
462 """ 495 """
463 try: 496 try:
464 self.__input = int(txt, 16) 497 self.__input = int(txt, 16)
465 self.__inputValid = True 498 self.__inputValid = True
466 except ValueError: 499 except ValueError:

eric ide

mercurial