forked from fra589/cn5X
-
Notifications
You must be signed in to change notification settings - Fork 0
/
qweditmask.py
154 lines (119 loc) · 5.21 KB
/
qweditmask.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# -*- coding: utf-8 -*-
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' '
' Copyright 2018 Gauthier Brière (gauthier.briere "at" gmail.com) '
' '
' This file is part of cn5X++ '
' '
' cn5X++ is free software: you can redistribute it and/or modify it '
' under the terms of the GNU General Public License as published by '
' the Free Software Foundation, either version 3 of the License, or '
' (at your option) any later version. '
' '
' cn5X++ is distributed in the hope that it will be useful, but '
' WITHOUT ANY WARRANTY; without even the implied warranty of '
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the '
' GNU General Public License for more details. '
' '
' You should have received a copy of the GNU General Public License '
' along with this program. If not, see <http://www.gnu.org/licenses/>. '
' '
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
import sys
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtCore import Qt, pyqtSignal, pyqtSlot
from PyQt5.QtGui import QIntValidator
class qwEditMask(QtWidgets.QWidget):
''' Widget personalise construisant un masque binaire (pour 6 axes) avec 6 cases a cocher
'''
valueChanged = pyqtSignal(int) # signal emis a chaque changement de valeur
def __init__(self, parent=None):
super().__init__(parent)
self.__changeEnCours = False
self.__nbAxes = 6
# Creation cadre exterieur
self.frame = QtWidgets.QFrame()
self.frame.setMinimumSize(QtCore.QSize(127, 19))
self.frame.setMaximumSize(QtCore.QSize(127, 19))
self.frame.setObjectName("frame")
self.horizontalLayout = QtWidgets.QHBoxLayout(self.frame)
self.horizontalLayout.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout.setSpacing(2)
self.horizontalLayout.setObjectName("horizontalLayout")
# creation des 6 checkBoxes
self.chk = []
for i in range(6):
self.chk.append(QtWidgets.QCheckBox(self.frame))
self.chk[i].setLayoutDirection(QtCore.Qt.RightToLeft)
self.chk[i].setText("")
self.chk[i].setObjectName("chk{}".format(i))
self.horizontalLayout.addWidget(self.chk[i])
self.chk[i].stateChanged.connect(self.chkStateChange)
# Creation zone de texte
self.lneMask = QtWidgets.QLineEdit(self.frame)
self.lneMask.setMinimumSize(QtCore.QSize(31, 19))
self.lneMask.setMaximumSize(QtCore.QSize(31, 19))
self.lneMask.setAlignment(QtCore.Qt.AlignRight|QtCore.Qt.AlignTrailing|QtCore.Qt.AlignVCenter)
self.lneMask.setObjectName("lneMask")
self.lneMask.setText("0")
validator = QIntValidator(0, 63, self)
self.lneMask.setValidator(validator)
self.horizontalLayout.addWidget(self.lneMask)
self.lneMask.textChanged.connect(self.lneTextChanged)
self.setLayout(self.horizontalLayout)
@pyqtSlot(int)
def chkStateChange(self, value):
if not self.__changeEnCours:
self.__changeEnCours = True
newVal = 0
for i in range(6):
if self.chk[i].isChecked():
newVal += 2**i
self.lneMask.setText(format(newVal))
self.valueChanged.emit(newVal)
self.__changeEnCours = False
@pyqtSlot(str)
def lneTextChanged(self, txt: str):
if not self.__changeEnCours:
self.__changeEnCours = True
try:
newVal = int(txt)
except ValueError:
self.lneMask.setText("0")
newVal = 0
except:
print("Unexpected error:", sys.exc_info()[0])
for i in range(6):
if newVal & 2**i:
self.chk[i].setCheckState(Qt.Checked)
else:
self.chk[i].setCheckState(Qt.Unchecked)
self.valueChanged.emit(newVal)
self.__changeEnCours = False
@pyqtSlot()
def getValue(self):
''' Renvoie la valeur du masque '''
return int(self.lneMask.text())
@pyqtSlot(int)
def setValue(self, val: int):
''' affecte une nouvelle valeur au masque '''
self.lneMask.setText(format(val))
# Definie la propriete pour permettre la configuration par Designer
value = QtCore.pyqtProperty(int, fget=getValue, fset=setValue)
@pyqtSlot()
def getNbAxes(self):
''' Renvoie le nombre d'axes geres '''
return self.__nbAxes
@pyqtSlot(int)
def setNbAxes(self, val: int):
''' Affecte le nombre d'axes geres (valeur entre 3 et 6) '''
if val < 3 or val > 6:
raise RuntimeError(self.tr("Le nombre d'axes doit etre compris entre 3 et 6 !"))
self.__nbAxes = val
for i in range(6):
if i < val:
self.chk[i].setEnabled(True)
else:
self.chk[i].setEnabled(False)
# Definie la propriete pour permettre la configuration par Designer
nbAxes = QtCore.pyqtProperty(int, fget=getNbAxes, fset=setNbAxes)