def _set_opacity_with_hex_edge(self, input_opacity): """ Set the opacity of the stored QPixmap image and display it """ # Store the opacity self.stored_opacity = input_opacity # Create and initialize the QImage from the stored QPixmap button_image = self.stored_pixmap # Resize the button image to scale button_image = button_image.scaled( data.QSize( math.ceil(button_image.size().width() * self.scale[0]), math.ceil(button_image.size().height() * self.scale[1]), ), transformMode=data.Qt.SmoothTransformation ) # Scale the hex image hex_image = self.stored_hex scaled_size = data.QSize( math.ceil(hex_image.size().width() * self.scale[0]), math.ceil(hex_image.size().height() * self.scale[1]), ) image = data.QImage( scaled_size, data.QImage.Format_ARGB32_Premultiplied, ) image.fill(data.Qt.transparent) # image.fill(data.theme.Context_Menu_Background) # Create and initialize the QPainter that will manipulate the QImage button_painter = data.QPainter(image) button_painter.setCompositionMode( data.QPainter.CompositionMode_SourceOver ) button_painter.setOpacity(input_opacity) # Resize the hex image to scale hex_image = hex_image.scaled( data.QSize( math.ceil(hex_image.size().width() * self.scale[0]), math.ceil(hex_image.size().height() * self.scale[1]), ), transformMode=data.Qt.SmoothTransformation ) # Adjust inner button positioning according to the scale button_painter.drawPixmap(0, 0, hex_image) x_scaled = math.ceil(self.scale[0] * self.INNER_IMAGE_OFFSET[0]) y_scaled = math.ceil(self.scale[1] * self.INNER_IMAGE_OFFSET[1]) button_painter.drawPixmap(x_scaled, y_scaled, button_image) button_painter.end() # Display the manipulated image self.setPixmap(data.QPixmap.fromImage(image)) # Set the button mask, which sets the button area to the shape of # the button image instead of a rectangle self.setMask(hex_image.mask())
def _set_extra_button_opacity(self, input_opacity): """Set the opacity of the extra button""" #Store the opacity self.extra_button_stored_opacity = input_opacity #Create and initialize the QImage from the stored QPixmap button_image = self.extra_button_stored_pixmap image = data.QImage(button_image.size(), data.QImage.Format_ARGB32_Premultiplied) image.fill(data.Qt.transparent) #Create and initialize the QPainter that will manipulate the QImage button_painter = data.QPainter(image) button_painter.setOpacity(input_opacity) button_painter.drawPixmap(0, 0, button_image) button_painter.end() #Display the manipulated image self.extra_button.setPixmap(data.QPixmap.fromImage(image))
def create_base_image(): hex_image = data.QImage( functions.create_size(width, height), data.QImage.Format_ARGB32_Premultiplied) hex_image.fill(data.Qt.transparent) qpainter = data.QPainter(hex_image) qpainter.setRenderHints(data.QPainter.Antialiasing | data.QPainter.TextAntialiasing | data.QPainter.SmoothPixmapTransform) hb = components.HexBuilder( qpainter, (width / 2, height / 2), self.DEFAULT_SIZE, self.scale_factor, fill_color=data.theme.Settings_Hex_Background, line_width=2, line_color=data.QColor(64, 64, 64), ) hb.create_grid(False) qpainter.end() return data.QPixmap.fromImage(hex_image)
def set_color(self, color): if not(color in self.valid_colors): raise Exception("Invalid EnlargeButton color selected!") elif color == "red": selected_image = self.hex_image_red elif color == "green": selected_image = self.hex_image_green elif color == "default": selected_image = None self.current_color = color scaled_size = data.QSize( self.hex_image.size().width(), self.hex_image.size().height(), ) image = data.QImage( scaled_size, data.QImage.Format_ARGB32_Premultiplied, ) image.fill(data.Qt.transparent) button_painter = data.QPainter(image) button_painter.setCompositionMode( data.QPainter.CompositionMode_SourceOver ) button_painter.setOpacity(1.0) # Adjust inner button positioning according to the scale x = (self.hex_image.width() - self.button_image.width()) / 2 y = (self.hex_image.height() - self.button_image.height()) / 2 button_painter.drawPixmap(0, 0, self.hex_image) if selected_image != None: button_painter.drawPixmap( (5 * (self.scale_factor - 1.0)), (5 * (self.scale_factor - 1.0)), selected_image ) button_painter.drawPixmap(x, y, self.button_image) button_painter.end() # Set the image as the pixmap self.setPixmap(data.QPixmap.fromImage(image))
def draw(self, opacity): image = data.QImage(self.pixmap.size(), data.QImage.Format_ARGB32_Premultiplied) image.fill(data.Qt.transparent) painter = data.QPainter(image) painter.setOpacity(opacity) painter.drawPixmap(0, 0, self.pixmap) if opacity < 0.5: painter.setPen(data.theme.Font.Default) else: painter.setPen(data.QColor(255, 255, 255)) painter.setFont( data.QFont('Segoe UI', int(16 * self.scale), data.QFont.Bold)) painter.setOpacity(1.0) painter.drawText(self.pixmap.rect(), data.Qt.AlignCenter, self.text) painter.end() # Display the manipulated image self.setPixmap(data.QPixmap.fromImage(image)) # Set the button mask, which sets the button area to the shape of # the button image instead of a rectangle self.setMask(self.pixmap.mask())
def create_color_image(color): hex_image = data.QImage( data.QSize(width, height), data.QImage.Format_ARGB32_Premultiplied ) hex_image.fill(data.Qt.transparent) qpainter = data.QPainter(hex_image) qpainter.setRenderHints( data.QPainter.Antialiasing | data.QPainter.TextAntialiasing | data.QPainter.SmoothPixmapTransform ) hb = components.HexBuilder( qpainter, (width/2, height/2), self.DEFAULT_SIZE, self.scale_factor, fill_color=color, line_width=0, line_color=data.QColor(0,0,0), ) hb.create_grid(False) qpainter.end() return data.QPixmap.fromImage(hex_image)
def init_layout(self, text, dialog_type): # Setup the image # First create the background image using the hex builder back_image = data.QImage(functions.create_size(246, 211), data.QImage.Format_ARGB32_Premultiplied) back_image.fill(data.Qt.transparent) painter = data.QPainter(back_image) painter.setRenderHints(data.QPainter.Antialiasing | data.QPainter.TextAntialiasing | data.QPainter.SmoothPixmapTransform) hex_builder = components.HexBuilder( painter, (123, 28), 30, 1.0, fill_color=data.theme.YesNoDialog_Background, line_width=3, line_color=data.theme.YesNoDialog_Edge, ) hex_builder.create_grid( False, 2, 2, 3, 4, 0, 5, 3, (3, True), 5, 0, 0, 4, 3 # OkDialog ) painter.end() original_dialog_image = data.QPixmap.fromImage(back_image) # Now add the images according to the type of dialog dialog_image = original_dialog_image.scaled( original_dialog_image.size() * self.scale, transformMode=data.Qt.SmoothTransformation) self.image = data.QLabel(self) self.image.setPixmap(dialog_image) self.image.setGeometry( 0, 0, int(dialog_image.rect().width() * self.scale), int(dialog_image.rect().height() * self.scale), ) self.image.setScaledContents(True) # Set the dialog mask to match the image mask self.setMask(dialog_image.mask()) # Setup the image behind the label if dialog_type != None: if dialog_type == "question": type_pixmap = data.QPixmap( os.path.join(data.resources_directory, "various/dialog-question.png")) elif dialog_type == "warning": type_pixmap = data.QPixmap( os.path.join(data.resources_directory, "various/dialog-warning.png")) elif dialog_type == "error": type_pixmap = data.QPixmap( os.path.join(data.resources_directory, "various/dialog-error.png")) else: raise Exception("Wrong dialog type!") image = data.QImage(type_pixmap.size(), data.QImage.Format_ARGB32_Premultiplied) image.fill(data.Qt.transparent) painter = data.QPainter(image) painter.setOpacity(0.2) painter.drawPixmap(0, 0, type_pixmap) painter.end() type_pixmap = data.QPixmap.fromImage(image) type_pixmap = type_pixmap.scaled( type_pixmap.size() * 2.0 * self.scale, transformMode=data.Qt.SmoothTransformation) self.type_label = data.QLabel(self) self.type_label.setPixmap(type_pixmap) type_label_rect = functions.create_rect( (self.image.rect().width() - type_pixmap.rect().width()) / 2 * self.scale, (self.image.rect().height() - type_pixmap.rect().height()) / 2 * self.scale, type_pixmap.rect().width() * self.scale, type_pixmap.rect().height() * self.scale, ) self.type_label.setGeometry(type_label_rect) # Setup the text label self.text = text self.label = data.QLabel(self) self.label.setFont( data.QFont('Segoe UI', int(12 * self.scale), data.QFont.Bold)) self.label.setWordWrap(True) self.label.setAlignment(data.Qt.AlignCenter) self.label.setStyleSheet('color: rgb({}, {}, {})'.format( data.theme.Font.Default.red(), data.theme.Font.Default.green(), data.theme.Font.Default.blue(), )) self.label.setText(text) width_diff = self.image.rect().width() - original_dialog_image.width() height_diff = self.image.rect().height( ) - original_dialog_image.height() x_offset = 20 * (self.scale - 1.0) y_offset = 60 * (self.scale - 1.0) label_rect = functions.create_rect( dialog_image.rect().x() + 20 + x_offset, dialog_image.rect().y() + 60 + y_offset, dialog_image.rect().width() - (40 * self.scale), dialog_image.rect().height() - (120 * self.scale), ) self.label.setGeometry(label_rect) # Shrink text if needed for i in range(10): label_width = label_rect.width() label_height = label_rect.height() font_metrics = data.QFontMetrics(self.label.font()) bounding_rectangle = font_metrics.boundingRect( functions.create_rect(0, 0, label_width, label_height), self.label.alignment() | data.Qt.TextWordWrap, text) if (bounding_rectangle.width() > label_width or bounding_rectangle.height() > label_height): self.label.setFont( data.QFont('Segoe UI', int((12 - i) * self.scale), data.QFont.Bold)) else: break # Setup the buttons self.button_no = self.Button( self, os.path.join(data.resources_directory, "various/hex-red.png"), "OK", data.QMessageBox.No, self.scale) x_offset = 93 * (self.scale - 1.0) y_offset = 158 * (self.scale - 1.0) self.button_no.setGeometry(int(93 + x_offset), int(158 + y_offset), int(59 * self.scale), int(50 * self.scale)) self.button_no.on_signal.connect(self.update_state_off) self.button_no.off_signal.connect(self.update_state_reset) # Setup the layout self.layout = data.QGridLayout() self.layout.setSpacing(0) self.layout.setContentsMargins(data.QMargins(0, 0, 0, 0)) self.layout.addWidget(self.image) self.setLayout(self.layout) # Setup tranparency and borders if data.on_rpi == True: self.image.setStyleSheet("border:0;" + "background-color:white;") else: self.image.setAttribute(data.Qt.WA_TranslucentBackground) self.image.setStyleSheet("border:0;" + "background-color:transparent;") self.setAttribute(data.Qt.WA_TranslucentBackground) self.setStyleSheet("border:0;" + "background-color:transparent;") self.setGeometry(dialog_image.rect()) self.center() self.setWindowFlags(data.Qt.FramelessWindowHint)
def __init__(self, name, picture, scale_factor=1.0, function=None, key_combination=None, starting_position=None, end_position=None, parent=None): super().__init__(parent) self.name = name self.scale_factor = scale_factor self.key_combination = key_combination self.animating = False self.starting_position = starting_position self.end_position = end_position self.disable() self.properties = {} # Set default font self.setFont(data.get_current_font()) # Button image self.button_image = data.QPixmap( os.path.join(data.resources_directory, picture)) adjusted_size = self.DEFAULT_SIZE * self.scale_factor self.button_image = self.button_image.scaled( functions.create_size( math.ceil(adjusted_size), math.ceil(adjusted_size), ), transformMode=data.Qt.SmoothTransformation) width, height = components.HexBuilder.get_single_hex_size( adjusted_size, 2) def create_base_image(): hex_image = data.QImage( functions.create_size(width, height), data.QImage.Format_ARGB32_Premultiplied) hex_image.fill(data.Qt.transparent) qpainter = data.QPainter(hex_image) qpainter.setRenderHints(data.QPainter.Antialiasing | data.QPainter.TextAntialiasing | data.QPainter.SmoothPixmapTransform) hb = components.HexBuilder( qpainter, (width / 2, height / 2), self.DEFAULT_SIZE, self.scale_factor, fill_color=data.theme.Settings_Hex_Background, line_width=2, line_color=data.QColor(64, 64, 64), ) hb.create_grid(False) qpainter.end() return data.QPixmap.fromImage(hex_image) self.hex_image = create_base_image() self.hex_image = self.hex_image.scaled( functions.create_size( math.ceil(self.hex_image.size().width() * self.scale_factor), math.ceil(self.hex_image.size().height() * self.scale_factor), ), transformMode=data.Qt.SmoothTransformation) # Green hex background def create_color_image(color): hex_image = data.QImage( functions.create_size(width, height), data.QImage.Format_ARGB32_Premultiplied) hex_image.fill(data.Qt.transparent) qpainter = data.QPainter(hex_image) qpainter.setRenderHints(data.QPainter.Antialiasing | data.QPainter.TextAntialiasing | data.QPainter.SmoothPixmapTransform) hb = components.HexBuilder( qpainter, (width / 2, height / 2), self.DEFAULT_SIZE, self.scale_factor, fill_color=color, line_width=0, line_color=data.QColor(0, 0, 0), ) hb.create_grid(False) qpainter.end() return data.QPixmap.fromImage(hex_image) self.hex_image_green = create_color_image(data.QColor( 138, 226, 52)) self.hex_image_green = self.hex_image_green.scaled( functions.create_size( math.ceil(self.hex_image_green.size().width() * self.scale_factor) - 1, math.ceil(self.hex_image_green.size().height() * self.scale_factor) - 1, ), transformMode=data.Qt.SmoothTransformation) # Red hex background self.hex_image_red = create_color_image(data.QColor(239, 41, 41)) self.hex_image_red = self.hex_image_red.scaled( functions.create_size( math.ceil(self.hex_image_red.size().width() * self.scale_factor) - 1, math.ceil(self.hex_image_red.size().height() * self.scale_factor) - 1, ), transformMode=data.Qt.SmoothTransformation) scaled_size = functions.create_size( self.hex_image.size().width(), self.hex_image.size().height(), ) image = data.QImage( scaled_size, data.QImage.Format_ARGB32_Premultiplied, ) image.fill(data.Qt.transparent) button_painter = data.QPainter(image) button_painter.setCompositionMode( data.QPainter.CompositionMode_SourceOver) button_painter.setOpacity(1.0) # Adjust inner button positioning according to the scale x = (self.hex_image.width() - self.button_image.width()) / 2 y = (self.hex_image.height() - self.button_image.height()) / 2 button_painter.drawPixmap(0, 0, self.hex_image) button_painter.drawPixmap(x, y, self.button_image) button_painter.end() # Set properites self.setParent(parent) self.setPixmap(data.QPixmap.fromImage(image)) self.setGeometry(0, 0, int(image.width() * self.scale_factor), int(image.height() * self.scale_factor)) self.setMask(self.hex_image.mask()) self.setScaledContents(True) # Set the non-enlarged dimensions self.original_size = (self.geometry().width(), self.geometry().height()) # Set the initial color state self.current_color = "default"
def create_background_image(in_scale=1.0): """ Dinamically create the settings manipulator's background image """ # Check if the QPixmap has been created already if SettingsGuiManipulator.settings_background_image == None: scale = in_scale edge_length = 27 scaled_edge_diff = (edge_length - (edge_length * scale)) / edge_length back_color = data.theme.Settings_Background edge_color = data.QColor(data.theme.Settings_Hex_Edge) SettingsGuiManipulator.theme_name = data.theme.name def add_offset(offset): x_add = 64.0 y_add = 20.0 return (offset[0] + x_add, offset[1] + y_add) settings_background_image = data.QImage( functions.create_size(*SettingsGuiManipulator.DEFAULT_SIZE), data.QImage.Format_ARGB32_Premultiplied) settings_background_image.fill(data.Qt.transparent) # settings_background_image.fill(data.QColor(255,255,255)) qpainter = data.QPainter(settings_background_image) qpainter.setRenderHints(data.QPainter.Antialiasing | data.QPainter.TextAntialiasing | data.QPainter.SmoothPixmapTransform) # Corner options x = edge_length + 205 y = 1.8 * edge_length + 30 offset = (x, y) hb = components.HexBuilder( qpainter, offset, edge_length, scale, fill_color=back_color, line_width=2, line_color=edge_color, ) grid_list = [ (3, True), (4, True), (4, True), (4, True), (5, True), (1, True), ] hb.create_grid(*grid_list) # Label field last_step = hb.get_last_position() hb = components.HexBuilder( qpainter, offset, edge_length, scale, fill_color=data.theme.Settings_Label_Background, line_width=3, line_color=data.QColor(146, 146, 146), ) hb.set_first_position(last_step) hb.create_grid(5, 5, 0, 2, 0, 2, 3, 1, 0, 2, 3, 4) # Editor buttons offset = (90, 280) offset = add_offset(offset) row_length = 6 editor_button_count = 30 hb = components.HexBuilder( qpainter, offset, edge_length, scale, fill_color=back_color, line_width=2, line_color=edge_color, ) grid_list = hb.generate_square_grid_list(row_length, editor_button_count) hb.create_grid(*grid_list) # Editor edge hb.next_step_move(3) first_edge_hex_position = hb.get_last_position() hb = components.HexBuilder( qpainter, first_edge_hex_position, edge_length, scale, fill_color=back_color, line_width=2, line_color=edge_color, ) grid_list = [ (4, True), (5, True), (4, True), (5, True), (4, True), (5, True), (0, True), (0, True), (0, True), (0, True), (0, True), (1, True), (1, True), (2, True), (1, True), (2, True), (1, True), (2, True), (3, True), (3, True), (3, True), (3, True), (3, True), ] hb.create_grid(*grid_list) # General buttons offset = (offset[0] + (8 * hb.horizontal_step), offset[1] - (6 * hb.vertical_step)) row_length = 7 general_button_count = 56 hb = components.HexBuilder( qpainter, offset, edge_length, scale, fill_color=back_color, line_width=2, line_color=edge_color, ) grid_list = hb.generate_square_grid_list(row_length, general_button_count) hb.create_grid(*grid_list) # General edge hb.next_step_move(3) first_edge_hex_position = hb.get_last_position() hb = components.HexBuilder( qpainter, first_edge_hex_position, edge_length, scale, fill_color=back_color, line_width=2, line_color=edge_color, ) grid_list = [ (1, True), (2, True), (1, True), (2, True), (1, True), (2, True), (1, True), (0, True), (0, True), (0, True), (0, True), (0, True), (0, True), (0, True), (0, True), (5, True), (5, True), (4, True), (5, True), (4, True), (5, True), (4, True), (4, True), (3, True), (3, True), (3, True), (3, True), (3, True), (3, True), (3, True), (3, True), ] hb.create_grid(*grid_list) qpainter.end() SettingsGuiManipulator.settings_background_image = data.QPixmap.fromImage( settings_background_image) return SettingsGuiManipulator.settings_background_image