def printImage(self): """ Print image. """ # create printer object and print output defined by the platform # the program is being run on. # QPrinter.NativeFormat is the default printer = QPrinter() printer.setOutputFormat(QPrinter.NativeFormat) # Create printer dialog to configure printer print_dialog = QPrintDialog(printer) # if the dialog is accepted by the user, begin printing if (print_dialog.exec_() == QPrintDialog.Accepted): # use QPainter to output a PDF file painter = QPainter() # begin painting device painter.begin(printer) # Set QRect to hold painter's current viewport, which # is the image_label rect = QRect(painter.viewport()) # get the size of image_label and use it to set the size # of the viewport size = QSize(self.image_label.pixmap().size()) size.scale(rect.size(), Qt.KeepAspectRatio) painter.setViewport(rect.x(), rect.y(), size.width(), size.height()) painter.setWindow(self.image_label.pixmap().rect()) # scale the image_label to fit the rect source (0, 0) painter.drawPixmap(0, 0, self.image_label.pixmap()) # end painting painter.end()
def resizeEvent(self, event): """ Docstring. """ newSize = QSize(10, 10) newSize.scale(event.size(), Qt.KeepAspectRatio) # print(newSize * 0.95) self.resize(newSize)
def resizeEvent(self, event): """ Forces the widget to be square upon resize event """ # Create a square base size of 10x10 and scale it to the new size # maintaining aspect ratio. new_size = QSize(10, 10) new_size.scale(event.size(), Qt.KeepAspectRatio) self.resize(new_size)
def printImage(self): printer = QPrinter() printer.setOutputFormat(QPrinter.NativeFormat) # QPrinter.NativeFormat is the default print_dialog = QPrintDialog(printer) # If the dialog is accepted by the user, begin printing if print_dialog.exec_() == QPrintDialog.Accepted: # Use QPainter to output a PDF file painter = QPainter() painter.begin(printer) rect = QRect(painter.viewport()) size = QSize(self.image_label.pixmap().size()) size.scale(rect.size(), Qt.KeepAspectRatio) painter.setViewport(rect.x(), rect.y(), size.width(), size. height()) painter.setWindow(self.image_label.pixmap().rect()) # Scale the image_label to fit the rect source (0, 0) painter.drawPixmap(0, 0, self.image_label.pixmap()) painter.end()
def print_image(self): printer = QPrinter() printer.setOutputFormat(QPrinter.NativeFormat) # Create printer dialog to configure printer print_dialog = QPrintDialog(printer) if print_dialog.exec_() == QPrintDialog.Accepted: # Use QPainter to output a PDF file painter = QPainter() # Begin painting device painter.begin(printer) # Set QRect to hold painter's current viewport, which is the display rect = QRect(painter.viewport()) # Get the size of display and use it to set the size of the viewport size = QSize(self.display.pixmap().size()) size.scale(rect.size(), Qt.KeepAspectRatio) painter.setViewport(rect.x(), rect.y(), size.width(), size.height()) painter.setWindow(self.display.pixmap().rect()) # Scale the image_label to fit the rect source (0, 0) painter.drawPixmap(0, 0, self.display.pixmap()) # End painting painter.end()
def resizeEvent(self, event): # Create a square base size of 10x10 and scale it to the new size # maintaining aspect ratio. new_size = QSize(10, 10) new_size.scale(event.size(), Qt.KeepAspectRatio) self.resize(new_size)
def paintEvent(self, event, *args): """ Custom paint event """ self.mutex.lock() # Paint custom frame image on QWidget painter = QPainter(self) painter.setRenderHints( QPainter.Antialiasing | QPainter.SmoothPixmapTransform | QPainter.TextAntialiasing, True) # Fill background black painter.fillRect(event.rect(), self.palette().window()) if self.current_image: # DRAW FRAME # Calculate new frame image size, maintaining aspect ratio pixSize = self.current_image.size() pixSize.scale(event.rect().size(), Qt.KeepAspectRatio) # Scale image scaledPix = self.current_image.scaled(pixSize, Qt.KeepAspectRatio, Qt.SmoothTransformation) # Calculate center of QWidget and Draw image center = self.centeredViewport(self.width(), self.height()) painter.drawImage(center, scaledPix) if self.transforming_clip: # Draw transform handles on top of video preview # Get framerate fps = get_app().project.get(["fps"]) fps_float = float(fps["num"]) / float(fps["den"]) # Determine frame # of clip start_of_clip = round( float(self.transforming_clip.data["start"]) * fps_float) position_of_clip = ( float(self.transforming_clip.data["position"]) * fps_float) + 1 playhead_position = float( get_app().window.preview_thread.current_frame) clip_frame_number = round(playhead_position - position_of_clip) + start_of_clip + 1 # Get properties of clip at current frame raw_properties = json.loads( self.transforming_clip_object.PropertiesJSON( clip_frame_number)) # Get size of current video player player_width = self.rect().width() player_height = self.rect().height() # Determine original size of clip's reader source_width = self.transforming_clip.data['reader']['width'] source_height = self.transforming_clip.data['reader']['height'] source_size = QSize(source_width, source_height) # Determine scale of clip scale = self.transforming_clip.data['scale'] if scale == openshot.SCALE_FIT: source_size.scale(player_width, player_height, Qt.KeepAspectRatio) elif scale == openshot.SCALE_STRETCH: source_size.scale(player_width, player_height, Qt.IgnoreAspectRatio) elif scale == openshot.SCALE_CROP: width_size = QSize( player_width, round(player_width / (float(source_width) / float(source_height)))) height_size = QSize( round(player_height / (float(source_height) / float(source_width))), player_height) if width_size.width() >= player_width and width_size.height( ) >= player_height: source_size.scale(width_size.width(), width_size.height(), Qt.KeepAspectRatio) else: source_size.scale(height_size.width(), height_size.height(), Qt.KeepAspectRatio) # Get new source width / height (after scaling mode applied) source_width = source_size.width() source_height = source_size.height() # Init X/Y x = 0.0 y = 0.0 # Get scaled source image size (scale_x, scale_y) sx = max(float(raw_properties.get('scale_x').get('value')), 0.001) sy = max(float(raw_properties.get('scale_y').get('value')), 0.001) scaled_source_width = source_width * sx scaled_source_height = source_height * sy # Determine gravity of clip gravity = self.transforming_clip.data['gravity'] if gravity == openshot.GRAVITY_TOP_LEFT: x += self.centeredViewport(self.width(), self.height()).x() # nudge right y += self.centeredViewport(self.width(), self.height()).y() # nudge down elif gravity == openshot.GRAVITY_TOP: x = (player_width - scaled_source_width) / 2.0 # center y += self.centeredViewport(self.width(), self.height()).y() # nudge down elif gravity == openshot.GRAVITY_TOP_RIGHT: x = player_width - scaled_source_width # right x -= self.centeredViewport(self.width(), self.height()).x() # nudge left y += self.centeredViewport(self.width(), self.height()).y() # nudge down elif gravity == openshot.GRAVITY_LEFT: y = (player_height - scaled_source_height) / 2.0 # center x += self.centeredViewport(self.width(), self.height()).x() # nudge right elif gravity == openshot.GRAVITY_CENTER: x = (player_width - scaled_source_width) / 2.0 # center y = (player_height - scaled_source_height) / 2.0 # center elif gravity == openshot.GRAVITY_RIGHT: x = player_width - scaled_source_width # right y = (player_height - scaled_source_height) / 2.0 # center x -= self.centeredViewport(self.width(), self.height()).x() # nudge left elif gravity == openshot.GRAVITY_BOTTOM_LEFT: y = (player_height - scaled_source_height) # bottom x += self.centeredViewport(self.width(), self.height()).x() # nudge right y -= self.centeredViewport(self.width(), self.height()).y() # nudge up elif gravity == openshot.GRAVITY_BOTTOM: x = (player_width - scaled_source_width) / 2.0 # center y = (player_height - scaled_source_height) # bottom y -= self.centeredViewport(self.width(), self.height()).y() # nudge up elif gravity == openshot.GRAVITY_BOTTOM_RIGHT: x = player_width - scaled_source_width # right y = (player_height - scaled_source_height) # bottom x -= self.centeredViewport(self.width(), self.height()).x() # nudge left y -= self.centeredViewport(self.width(), self.height()).y() # nudge up # Track gravity starting coordinate self.gravity_point = QPointF(x, y) # Scale to fit in widget final_size = QSize(source_width, source_height) # Adjust x,y for location x_offset = raw_properties.get('location_x').get('value') y_offset = raw_properties.get('location_y').get('value') x += (scaledPix.width() * x_offset) y += (scaledPix.height() * y_offset) self.transform = QTransform() # Apply translate/move if x or y: self.transform.translate(x, y) # Apply scale if sx or sy: self.transform.scale(sx, sy) # Apply shear shear_x = raw_properties.get('shear_x').get('value') shear_y = raw_properties.get('shear_y').get('value') if shear_x or shear_y: self.transform.shear(shear_x, shear_y) # Apply rotation rotation = raw_properties.get('rotation').get('value') if rotation: origin_x = x - self.centeredViewport( self.width(), self.height()).x() + (scaled_source_width / 2.0) origin_y = y - self.centeredViewport( self.width(), self.height()).y() + (scaled_source_height / 2.0) self.transform.translate(origin_x, origin_y) self.transform.rotate(rotation) self.transform.translate(-origin_x, -origin_y) # Apply transform painter.setTransform(self.transform) # Draw transform corners and cernter origin circle # Corner size cs = 6.0 os = 12.0 # Calculate 4 corners coordinates self.topLeftHandle = QRectF(0.0, 0.0, cs / sx, cs / sy) self.topRightHandle = QRectF(source_width - (cs / sx), 0, cs / sx, cs / sy) self.bottomLeftHandle = QRectF(0.0, source_height - (cs / sy), cs / sx, cs / sy) self.bottomRightHandle = QRectF(source_width - (cs / sx), source_height - (cs / sy), cs / sx, cs / sy) # Draw 4 corners painter.fillRect(self.topLeftHandle, QBrush(QColor("#53a0ed"))) painter.fillRect(self.topRightHandle, QBrush(QColor("#53a0ed"))) painter.fillRect(self.bottomLeftHandle, QBrush(QColor("#53a0ed"))) painter.fillRect(self.bottomRightHandle, QBrush(QColor("#53a0ed"))) # Calculate 4 side coordinates self.topHandle = QRectF( 0.0 + (source_width / 2.0) - (cs / sx / 2.0), 0, cs / sx, cs / sy) self.bottomHandle = QRectF( 0.0 + (source_width / 2.0) - (cs / sx / 2.0), source_height - (cs / sy), cs / sx, cs / sy) self.leftHandle = QRectF(0.0, (source_height / 2.0) - (cs / sy / 2.0), cs / sx, cs / sy) self.rightHandle = QRectF(source_width - (cs / sx), (source_height / 2.0) - (cs / sy / 2.0), cs / sx, cs / sy) # Draw 4 sides (centered) painter.fillRect(self.topHandle, QBrush(QColor("#53a0ed"))) painter.fillRect(self.bottomHandle, QBrush(QColor("#53a0ed"))) painter.fillRect(self.leftHandle, QBrush(QColor("#53a0ed"))) painter.fillRect(self.rightHandle, QBrush(QColor("#53a0ed"))) # Calculate center coordinate self.centerHandle = QRectF((source_width / 2.0) - (os / sx), (source_height / 2.0) - (os / sy), os / sx * 2.0, os / sy * 2.0) # Draw origin painter.setBrush(QColor(83, 160, 237, 122)) painter.setPen(Qt.NoPen) painter.drawEllipse(self.centerHandle) # Draw translucent rectangle self.clipRect = QRectF(0, 0, final_size.width(), final_size.height()) # Remove transform painter.resetTransform() # End painter painter.end() self.mutex.unlock()
def paintEvent(self, event, *args): """ Custom paint event """ self.mutex.lock() # Paint custom frame image on QWidget painter = QPainter(self) painter.setRenderHints(QPainter.Antialiasing | QPainter.SmoothPixmapTransform | QPainter.TextAntialiasing, True) # Fill background black painter.fillRect(event.rect(), self.palette().window()) if self.current_image: # DRAW FRAME # Calculate new frame image size, maintaining aspect ratio pixSize = self.current_image.size() pixSize.scale(event.rect().size(), Qt.KeepAspectRatio) # Scale image scaledPix = self.current_image.scaled(pixSize, Qt.KeepAspectRatio, Qt.SmoothTransformation) # Calculate center of QWidget and Draw image center = self.centeredViewport(self.width(), self.height()) painter.drawImage(center, scaledPix) if self.transforming_clip: # Draw transform handles on top of video preview # Get framerate fps = get_app().project.get(["fps"]) fps_float = float(fps["num"]) / float(fps["den"]) # Determine frame # of clip start_of_clip = round(float(self.transforming_clip.data["start"]) * fps_float) position_of_clip = (float(self.transforming_clip.data["position"]) * fps_float) + 1 playhead_position = float(get_app().window.preview_thread.current_frame) clip_frame_number = round(playhead_position - position_of_clip) + start_of_clip + 1 # Get properties of clip at current frame raw_properties = json.loads(self.transforming_clip_object.PropertiesJSON(clip_frame_number)) # Get size of current video player player_width = self.rect().width() player_height = self.rect().height() # Determine original size of clip's reader source_width = self.transforming_clip.data['reader']['width'] source_height = self.transforming_clip.data['reader']['height'] source_size = QSize(source_width, source_height) # Determine scale of clip scale = self.transforming_clip.data['scale'] if scale == openshot.SCALE_FIT: source_size.scale(player_width, player_height, Qt.KeepAspectRatio) elif scale == openshot.SCALE_STRETCH: source_size.scale(player_width, player_height, Qt.IgnoreAspectRatio) elif scale == openshot.SCALE_CROP: width_size = QSize(player_width, round(player_width / (float(source_width) / float(source_height)))) height_size = QSize(round(player_height / (float(source_height) / float(source_width))), player_height) if width_size.width() >= player_width and width_size.height() >= player_height: source_size.scale(width_size.width(), width_size.height(), Qt.KeepAspectRatio) else: source_size.scale(height_size.width(), height_size.height(), Qt.KeepAspectRatio) # Get new source width / height (after scaling mode applied) source_width = source_size.width() source_height = source_size.height() # Init X/Y x = 0.0 y = 0.0 # Get scaled source image size (scale_x, scale_y) sx = max(float(raw_properties.get('scale_x').get('value')), 0.001) sy = max(float(raw_properties.get('scale_y').get('value')), 0.001) scaled_source_width = source_width * sx scaled_source_height = source_height * sy # Determine gravity of clip gravity = self.transforming_clip.data['gravity'] if gravity == openshot.GRAVITY_TOP_LEFT: x += self.centeredViewport(self.width(), self.height()).x() # nudge right y += self.centeredViewport(self.width(), self.height()).y() # nudge down elif gravity == openshot.GRAVITY_TOP: x = (player_width - scaled_source_width) / 2.0 # center y += self.centeredViewport(self.width(), self.height()).y() # nudge down elif gravity == openshot.GRAVITY_TOP_RIGHT: x = player_width - scaled_source_width # right x -= self.centeredViewport(self.width(), self.height()).x() # nudge left y += self.centeredViewport(self.width(), self.height()).y() # nudge down elif gravity == openshot.GRAVITY_LEFT: y = (player_height - scaled_source_height) / 2.0 # center x += self.centeredViewport(self.width(), self.height()).x() # nudge right elif gravity == openshot.GRAVITY_CENTER: x = (player_width - scaled_source_width) / 2.0 # center y = (player_height - scaled_source_height) / 2.0 # center elif gravity == openshot.GRAVITY_RIGHT: x = player_width - scaled_source_width # right y = (player_height - scaled_source_height) / 2.0 # center x -= self.centeredViewport(self.width(), self.height()).x() # nudge left elif gravity == openshot.GRAVITY_BOTTOM_LEFT: y = (player_height - scaled_source_height) # bottom x += self.centeredViewport(self.width(), self.height()).x() # nudge right y -= self.centeredViewport(self.width(), self.height()).y() # nudge up elif gravity == openshot.GRAVITY_BOTTOM: x = (player_width - scaled_source_width) / 2.0 # center y = (player_height - scaled_source_height) # bottom y -= self.centeredViewport(self.width(), self.height()).y() # nudge up elif gravity == openshot.GRAVITY_BOTTOM_RIGHT: x = player_width - scaled_source_width # right y = (player_height - scaled_source_height) # bottom x -= self.centeredViewport(self.width(), self.height()).x() # nudge left y -= self.centeredViewport(self.width(), self.height()).y() # nudge up # Track gravity starting coordinate self.gravity_point = QPointF(x, y) # Scale to fit in widget final_size = QSize(source_width, source_height) # Adjust x,y for location x_offset = raw_properties.get('location_x').get('value') y_offset = raw_properties.get('location_y').get('value') x += (scaledPix.width() * x_offset) y += (scaledPix.height() * y_offset) self.transform = QTransform() # Apply translate/move if x or y: self.transform.translate(x, y) # Apply scale if sx or sy: self.transform.scale(sx, sy) # Apply shear shear_x = raw_properties.get('shear_x').get('value') shear_y = raw_properties.get('shear_y').get('value') if shear_x or shear_y: self.transform.shear(shear_x, shear_y) # Apply rotation rotation = raw_properties.get('rotation').get('value') if rotation: origin_x = x - self.centeredViewport(self.width(), self.height()).x() + (scaled_source_width / 2.0) origin_y = y - self.centeredViewport(self.width(), self.height()).y() + (scaled_source_height / 2.0) self.transform.translate(origin_x, origin_y) self.transform.rotate(rotation) self.transform.translate(-origin_x, -origin_y) # Apply transform painter.setTransform(self.transform) # Draw transform corners and center origin circle # Corner size cs = 6.0 os = 12.0 # Calculate 4 corners coordinates self.topLeftHandle = QRectF(0.0, 0.0, cs/sx, cs/sy) self.topRightHandle = QRectF(source_width - (cs/sx), 0, cs/sx, cs/sy) self.bottomLeftHandle = QRectF(0.0, source_height - (cs/sy), cs/sx, cs/sy) self.bottomRightHandle = QRectF(source_width - (cs/sx), source_height - (cs/sy), cs/sx, cs/sy) # Draw 4 corners painter.fillRect(self.topLeftHandle, QBrush(QColor("#53a0ed"))) painter.fillRect(self.topRightHandle, QBrush(QColor("#53a0ed"))) painter.fillRect(self.bottomLeftHandle, QBrush(QColor("#53a0ed"))) painter.fillRect(self.bottomRightHandle, QBrush(QColor("#53a0ed"))) # Calculate 4 side coordinates self.topHandle = QRectF(0.0 + (source_width / 2.0) - (cs/sx/2.0), 0, cs/sx, cs/sy) self.bottomHandle = QRectF(0.0 + (source_width / 2.0) - (cs/sx/2.0), source_height - (cs/sy), cs/sx, cs/sy) self.leftHandle = QRectF(0.0, (source_height / 2.0) - (cs/sy/2.0), cs/sx, cs/sy) self.rightHandle = QRectF(source_width - (cs/sx), (source_height / 2.0) - (cs/sy/2.0), cs/sx, cs/sy) # Draw 4 sides (centered) painter.fillRect(self.topHandle, QBrush(QColor("#53a0ed"))) painter.fillRect(self.bottomHandle, QBrush(QColor("#53a0ed"))) painter.fillRect(self.leftHandle, QBrush(QColor("#53a0ed"))) painter.fillRect(self.rightHandle, QBrush(QColor("#53a0ed"))) # Calculate center coordinate self.centerHandle = QRectF((source_width / 2.0) - (os/sx), (source_height / 2.0) - (os/sy), os/sx*2.0, os/sy*2.0) # Draw origin painter.setBrush(QColor(83, 160, 237, 122)) painter.setPen(Qt.NoPen) painter.drawEllipse(self.centerHandle) # Draw translucent rectangle self.clipRect = QRectF(0, 0, final_size.width(), final_size.height()) # Remove transform painter.resetTransform() # End painter painter.end() self.mutex.unlock()