OpenShot Video Editor  2.0.0
app.py
Go to the documentation of this file.
1 ##
2 #
3 # @file
4 # @brief This file creates the QApplication, and displays the main window
5 # @author Noah Figg <eggmunkee@hotmail.com>
6 # @author Jonathan Thomas <jonathan@openshot.org>
7 # @author olivier Girard <eolinwen@gmail.com>
8 #
9 # @section LICENSE
10 #
11 # Copyright (c) 2008-2018 OpenShot Studios, LLC
12 # (http://www.openshotstudios.com). This file is part of
13 # OpenShot Video Editor (http://www.openshot.org), an open-source project
14 # dedicated to delivering high quality video editing and animation solutions
15 # to the world.
16 #
17 # OpenShot Video Editor is free software: you can redistribute it and/or modify
18 # it under the terms of the GNU General Public License as published by
19 # the Free Software Foundation, either version 3 of the License, or
20 # (at your option) any later version.
21 #
22 # OpenShot Video Editor is distributed in the hope that it will be useful,
23 # but WITHOUT ANY WARRANTY; without even the implied warranty of
24 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 # GNU General Public License for more details.
26 #
27 # You should have received a copy of the GNU General Public License
28 # along with OpenShot Library. If not, see <http://www.gnu.org/licenses/>.
29 #
30 
31 import os
32 import sys
33 import platform
34 from uuid import uuid4
35 from PyQt5.QtWidgets import QApplication, QStyleFactory, QMessageBox
36 from PyQt5.QtGui import QPalette, QColor, QFontDatabase, QFont
37 from PyQt5.QtCore import Qt
38 from PyQt5.QtCore import QT_VERSION_STR
39 from PyQt5.Qt import PYQT_VERSION_STR
40 
41 from classes.logger import log
42 from classes import info, settings, project_data, updates, language, ui_util, logger_libopenshot
43 import openshot
44 
45 
46 try:
47  # Enable High-DPI resolutions
48  QApplication.setAttribute(Qt.AA_EnableHighDpiScaling)
49 except AttributeError:
50  pass # Quietly fail for older Qt5 versions
51 
52 
53 ##
54 # Returns the current QApplication instance of OpenShot
55 def get_app():
56  return QApplication.instance()
57 
58 
59 ##
60 # This class is the primary QApplication for OpenShot.
61 # mode=None (normal), mode=unittest (testing)
62 class OpenShotApp(QApplication):
63 
64  def __init__(self, *args, mode=None):
65  QApplication.__init__(self, *args)
66 
67  # Log some basic system info
68  try:
69  v = openshot.GetVersion()
70  log.info("openshot-qt version: %s" % info.VERSION)
71  log.info("libopenshot version: %s" % v.ToString())
72  log.info("platform: %s" % platform.platform())
73  log.info("processor: %s" % platform.processor())
74  log.info("machine: %s" % platform.machine())
75  log.info("python version: %s" % platform.python_version())
76  log.info("qt5 version: %s" % QT_VERSION_STR)
77  log.info("pyqt5 version: %s" % PYQT_VERSION_STR)
78  except:
79  pass
80 
81  # Setup application
82  self.setApplicationName('openshot')
83  self.setApplicationVersion(info.SETUP['version'])
84 
85  # Init settings
87  self.settings.load()
88 
89  # Init and attach exception handler
90  from classes import exceptions
91  sys.excepthook = exceptions.ExceptionHandler
92 
93  # Init translation system
95 
96  # Detect minimum libopenshot version
97  _ = self._tr
98  libopenshot_version = openshot.GetVersion().ToString()
99  if libopenshot_version < info.MINIMUM_LIBOPENSHOT_VERSION:
100  QMessageBox.warning(None, _("Wrong Version of libopenshot Detected"),
101  _("<b>Version %(minimum_version)s is required</b>, but %(current_version)s was detected. Please update libopenshot or download our latest installer.") %
102  {"minimum_version": info.MINIMUM_LIBOPENSHOT_VERSION, "current_version": libopenshot_version})
103  # Stop launching and exit
104  sys.exit()
105 
106  # Tests of project data loading/saving
108 
109  # Init Update Manager
111 
112  # It is important that the project is the first listener if the key gets update
113  self.updates.add_listener(self.project)
114 
115  # Load ui theme if not set by OS
117 
118  # Start libopenshot logging thread
120  self.logger_libopenshot.start()
121 
122  # Track which dockable window received a context menu
124 
125  # Set Font for any theme
126  if self.settings.get("theme") != "No Theme":
127  # Load embedded font
128  try:
129  log.info("Setting font to %s" % os.path.join(info.IMAGES_PATH, "fonts", "Ubuntu-R.ttf"))
130  font_id = QFontDatabase.addApplicationFont(os.path.join(info.IMAGES_PATH, "fonts", "Ubuntu-R.ttf"))
131  font_family = QFontDatabase.applicationFontFamilies(font_id)[0]
132  font = QFont(font_family)
133  font.setPointSizeF(10.5)
134  QApplication.setFont(font)
135  except Exception as ex:
136  log.error("Error setting Ubuntu-R.ttf QFont: %s" % str(ex))
137 
138  # Set Experimental Dark Theme
139  if self.settings.get("theme") == "Humanity: Dark":
140  # Only set if dark theme selected
141  log.info("Setting custom dark theme")
142  self.setStyle(QStyleFactory.create("Fusion"))
143 
144  darkPalette = self.palette()
145  darkPalette.setColor(QPalette.Window, QColor(53, 53, 53))
146  darkPalette.setColor(QPalette.WindowText, Qt.white)
147  darkPalette.setColor(QPalette.Base, QColor(25, 25, 25))
148  darkPalette.setColor(QPalette.AlternateBase, QColor(53, 53, 53))
149  darkPalette.setColor(QPalette.ToolTipBase, Qt.white)
150  darkPalette.setColor(QPalette.ToolTipText, Qt.white)
151  darkPalette.setColor(QPalette.Text, Qt.white)
152  darkPalette.setColor(QPalette.Button, QColor(53, 53, 53))
153  darkPalette.setColor(QPalette.ButtonText, Qt.white)
154  darkPalette.setColor(QPalette.BrightText, Qt.red)
155  darkPalette.setColor(QPalette.Highlight, QColor(42, 130, 218))
156  darkPalette.setColor(QPalette.HighlightedText, Qt.black)
157  darkPalette.setColor(QPalette.Disabled, QPalette.Text, QColor(104, 104, 104))
158  self.setPalette(darkPalette)
159  self.setStyleSheet("QToolTip { color: #ffffff; background-color: #2a82da; border: 0px solid white; }")
160 
161  # Create main window
162  from windows.main_window import MainWindow
163  self.window = MainWindow(mode)
164 
165  # Reset undo/redo history
166  self.updates.reset()
167  self.window.updateStatusChanged(False, False)
168 
169  log.info('Process command-line arguments: %s' % args)
170  if len(args[0]) == 2:
171  path = args[0][1]
172  if ".osp" in path:
173  # Auto load project passed as argument
174  self.window.open_project(path)
175  else:
176  # Auto import media file
177  self.window.filesTreeView.add_file(path)
178  else:
179  # Recover backup file (this can't happen until after the Main Window has completely loaded)
180  self.window.RecoverBackup.emit()
181 
182  def _tr(self, message):
183  return self.translate("", message)
184 
185  # Start event loop
186  ##
187  # Start the primary Qt event loop for the interface
188  def run(self):
189 
190  res = self.exec_()
191 
192  try:
193  self.settings.save()
194  except Exception as ex:
195  log.error("Couldn't save user settings on exit.\n{}".format(ex))
196 
197  # return exit result
198  return res
def run
Start the primary Qt event loop for the interface.
Definition: app.py:188
This class is used to track and distribute changes to listeners.
Definition: updates.py:117
def get_app
Returns the current QApplication instance of OpenShot.
Definition: app.py:55
def load_theme
Load the current OS theme, or fallback to a default one.
Definition: ui_util.py:48
logger_libopenshot
Definition: app.py:119
def init_language
Find the current locale, and install the correct translators.
Definition: language.py:42
def __init__
Definition: app.py:64
This class is the primary QApplication for OpenShot.
Definition: app.py:62
context_menu_object
Definition: app.py:123
This class allows advanced searching of data structure, implements changes interface.
Definition: project_data.py:45
This class only allows setting pre-existing keys taken from default settings file, and merges user settings on load, assumes default OS dir.
Definition: settings.py:51