OpenShot Video Editor  2.0.0
video_widget.py
Go to the documentation of this file.
1 ##
2 #
3 # @file
4 # @brief This file contains the video preview QWidget (based on a QLabel)
5 # @author Jonathan Thomas <jonathan@openshot.org>
6 #
7 # @section LICENSE
8 #
9 # Copyright (c) 2008-2016 OpenShot Studios, LLC
10 # (http://www.openshotstudios.com). This file is part of
11 # OpenShot Video Editor (http://www.openshot.org), an open-source project
12 # dedicated to delivering high quality video editing and animation solutions
13 # to the world.
14 #
15 # OpenShot Video Editor is free software: you can redistribute it and/or modify
16 # it under the terms of the GNU General Public License as published by
17 # the Free Software Foundation, either version 3 of the License, or
18 # (at your option) any later version.
19 #
20 # OpenShot Video Editor is distributed in the hope that it will be useful,
21 # but WITHOUT ANY WARRANTY; without even the implied warranty of
22 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 # GNU General Public License for more details.
24 #
25 # You should have received a copy of the GNU General Public License
26 # along with OpenShot Library. If not, see <http://www.gnu.org/licenses/>.
27 #
28 
29 import os
30 
31 from PyQt5.QtCore import QMimeData, QSize, Qt, QCoreApplication, QPoint, QFileInfo, QRect
32 from PyQt5.QtGui import *
33 from PyQt5.QtWidgets import QLabel, QApplication, QMessageBox, QAbstractItemView, QMenu, QSizePolicy, QWidget
34 import openshot # Python module for libopenshot (required video editing module installed separately)
35 
36 from classes import updates
37 from classes import info
38 from classes.logger import log
39 from classes.settings import SettingStore
40 from classes.app import get_app
41 from windows.models.effects_model import EffectsModel
42 
43 
44 ##
45 # A QWidget used on the video display widget
46 class VideoWidget(QWidget):
47 
48  ##
49  # Custom paint event
50  def paintEvent(self, event, *args):
51 
52  # Paint custom frame image on QWidget
53  painter = QPainter(self)
54  painter.setRenderHint(QPainter.Antialiasing)
55 
56  # Fill background black
57  painter.fillRect(event.rect(), self.palette().window())
58 
59  if self.current_image:
60  # DRAW FRAME
61  # Calculate new frame image size, maintaining aspect ratio
62  pixSize = self.current_image.size()
63  pixSize.scale(event.rect().size(), Qt.KeepAspectRatio)
64 
65  # Scale image
66  scaledPix = self.current_image.scaled(pixSize, Qt.KeepAspectRatio, Qt.SmoothTransformation)
67 
68  # Calculate center of QWidget and Draw image
69  center = self.centeredViewport(self.width(), self.height())
70  painter.drawImage(center, scaledPix)
71 
72  ##
73  # Set a new aspect ratio
74  def SetAspectRatio(self, new_aspect_ratio, new_pixel_ratio):
75  self.aspect_ratio = new_aspect_ratio
76  self.pixel_ratio = new_pixel_ratio
77 
78  ##
79  # Calculate size of viewport to maintain apsect ratio
80  def centeredViewport(self, width, height):
81 
82  aspectRatio = self.aspect_ratio.ToFloat() * self.pixel_ratio.ToFloat()
83  heightFromWidth = width / aspectRatio
84  widthFromHeight = height * aspectRatio
85 
86  if heightFromWidth <= height:
87  return QRect(0, (height - heightFromWidth) / 2, width, heightFromWidth)
88  else:
89  return QRect((width - widthFromHeight) / 2.0, 0, widthFromHeight, height)
90 
91  ##
92  # Present the current frame
93  def present(self, image, *args):
94 
95  # Get frame's QImage from libopenshot
96  self.current_image = image
97 
98  # Force repaint on this widget
99  self.repaint()
100 
101  ##
102  # Connect signals to renderer
103  def connectSignals(self, renderer):
104  renderer.present.connect(self.present)
105 
106  ##
107  # Capture mouse events on video preview window
108  def mouseMoveEvent(self, event):
109  #log.info("%s,%s" % (event.x(), event.y()))
110  pass
111 
112  def __init__(self, *args):
113  # Invoke parent init
114  QWidget.__init__(self, *args)
115 
116  # Init aspect ratio settings (default values)
117  self.aspect_ratio = openshot.Fraction()
118  self.pixel_ratio = openshot.Fraction()
119  self.aspect_ratio.num = 16
120  self.aspect_ratio.den = 9
121  self.pixel_ratio.num = 1
122  self.pixel_ratio.den = 1
123 
124  # Init Qt style properties (black background, ect...)
125  p = QPalette()
126  p.setColor(QPalette.Window, Qt.black)
127  super().setPalette(p)
128  super().setAttribute(Qt.WA_OpaquePaintEvent)
129  super().setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Preferred)
130 
131  # Set mouse tracking
132  self.setMouseTracking(True)
133 
134  # Init current frame's QImage
135  self.current_image = None
136 
137  # Get a reference to the window object
138  self.win = get_app().window
function window
Definition: jquery.js:14
def connectSignals(self, renderer)
Connect signals to renderer.
def get_app()
Returns the current QApplication instance of OpenShot.
Definition: app.py:54
def paintEvent(self, event, args)
Custom paint event.
Definition: video_widget.py:50
jQuery fn size
Definition: jquery.js:9760
def __init__(self, args)
def mouseMoveEvent(self, event)
Capture mouse events on video preview window.
def SetAspectRatio(self, new_aspect_ratio, new_pixel_ratio)
Set a new aspect ratio.
Definition: video_widget.py:74
def centeredViewport(self, width, height)
Calculate size of viewport to maintain apsect ratio.
Definition: video_widget.py:80
def present(self, image, args)
Present the current frame.
Definition: video_widget.py:93
A QWidget used on the video display widget.
Definition: video_widget.py:46