class KeyWidget: def __init__(self, desc, scale, shift_x=0, shift_y=0): self.active = False self.masked = False self.pressed = False self.desc = desc self.text = "" self.mask_text = "" self.tooltip = "" self.color = None self.scale = 0 self.rotation_angle = desc.rotation_angle self.has2 = desc.width2 != desc.width or desc.height2 != desc.height or desc.x2 != 0 or desc.y2 != 0 self.update_position(scale, shift_x, shift_y) def update_position(self, scale, shift_x=0, shift_y=0): if self.scale != scale or self.shift_x != shift_x or self.shift_y != shift_y: self.scale = scale size = self.scale * (KEY_SIZE_RATIO + KEY_SPACING_RATIO) spacing = self.scale * KEY_SPACING_RATIO self.rotation_x = size * self.desc.rotation_x self.rotation_y = size * self.desc.rotation_y self.shift_x = shift_x self.shift_y = shift_y self.x = size * self.desc.x self.y = size * self.desc.y self.w = size * self.desc.width - spacing self.h = size * self.desc.height - spacing self.rect = QRect(self.x, self.y, self.w, self.h) self.x2 = self.x + size * self.desc.x2 self.y2 = self.y + size * self.desc.y2 self.w2 = size * self.desc.width2 - spacing self.h2 = size * self.desc.height2 - spacing self.rect2 = QRect(self.x2, self.y2, self.w2, self.h2) self.bbox = self.calculate_bbox(self.rect) self.bbox2 = self.calculate_bbox(self.rect2) self.polygon = QPolygonF(self.bbox + [self.bbox[0]]) self.polygon2 = QPolygonF(self.bbox2 + [self.bbox2[0]]) self.polygon = self.polygon.united(self.polygon2) self.draw_path = self.calculate_draw_path() self.draw_path2 = self.calculate_draw_path2() # calculate areas where the inner keycode will be located # nonmask = outer (e.g. Rsft_T) # mask = inner (e.g. KC_A) self.nonmask_rect = QRectF( int(self.x), int(self.y), int(self.w), int(self.h * (1 - KEYBOARD_WIDGET_MASK_HEIGHT))) self.mask_rect = QRectF( int(self.x + KEYBOARD_WIDGET_MASK_PADDING), int(self.y) + int(self.h * (1 - KEYBOARD_WIDGET_MASK_HEIGHT)), int(self.w - 2 * KEYBOARD_WIDGET_MASK_PADDING), int(self.h * KEYBOARD_WIDGET_MASK_HEIGHT - KEYBOARD_WIDGET_MASK_PADDING)) self.mask_bbox = self.calculate_bbox(self.mask_rect) self.mask_polygon = QPolygonF(self.mask_bbox + [self.mask_bbox[0]]) def calculate_bbox(self, rect): x1 = rect.topLeft().x() y1 = rect.topLeft().y() x2 = rect.bottomRight().x() y2 = rect.bottomRight().y() points = [(x1, y1), (x1, y2), (x2, y2), (x2, y1)] bbox = [] for p in points: t = QTransform() t.translate(self.shift_x, self.shift_y) t.translate(self.rotation_x, self.rotation_y) t.rotate(self.rotation_angle) t.translate(-self.rotation_x, -self.rotation_y) p = t.map(QPointF(p[0], p[1])) bbox.append(p) return bbox def calculate_draw_path(self): path = QPainterPath() corner = int(self.h / KEY_ROUNDNESS) if (self.w > self.h) else int( self.w / KEY_ROUNDNESS) path.addRoundedRect(int(self.x), int(self.y), int(self.w), int(self.h), corner, corner) # second part only considered if different from first if self.has2: path2 = QPainterPath() path2.addRoundedRect(int(self.x2), int(self.y2), int(self.w2), int(self.h2), corner, corner) path = path.united(path2) return path def calculate_draw_path2(self): return QPainterPath() def setText(self, text): self.text = text def setMaskText(self, text): self.mask_text = text def setToolTip(self, tooltip): self.tooltip = tooltip def setActive(self, active): self.active = active def setPressed(self, pressed): self.pressed = pressed def setColor(self, color): self.color = color def __repr__(self): qualifiers = ["KeyboardWidget"] if self.desc.row is not None: qualifiers.append("matrix:{},{}".format(self.desc.row, self.desc.col)) if self.desc.layout_index != -1: qualifiers.append("layout:{},{}".format(self.desc.layout_index, self.desc.layout_option)) return " ".join(qualifiers)