OpenShot Video Editor  2.0.0
effects_model.py
Go to the documentation of this file.
1 ##
2 #
3 # @file
4 # @brief This file contains the effects model, used by the main window
5 # @author Jonathan Thomas <jonathan@openshot.org>
6 #
7 # @section LICENSE
8 #
9 # Copyright (c) 2008-2018 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, Qt
32 from PyQt5.QtGui import *
33 from PyQt5.QtWidgets import QMessageBox
34 import openshot # Python module for libopenshot (required video editing module installed separately)
35 
36 from classes import info
37 from classes.logger import log
38 from classes.app import get_app
39 
40 try:
41  import json
42 except ImportError:
43  import simplejson as json
44 
45 
46 class EffectsStandardItemModel(QStandardItemModel):
47  def __init__(self, parent=None):
48  QStandardItemModel.__init__(self)
49 
50  def mimeData(self, indexes):
51  # Create MimeData for drag operation
52  data = QMimeData()
53 
54  # Get list of all selected file ids
55  files = []
56  for item in indexes:
57  selected_row = self.itemFromIndex(item).row()
58  files.append(self.item(selected_row, 4).text())
59  data.setText(json.dumps(files))
60  data.setHtml("effect")
61 
62  # Return Mimedata
63  return data
64 
65 
66 class EffectsModel():
67  def update_model(self, clear=True):
68  log.info("updating effects model.")
69  app = get_app()
70 
71  # Get window to check filters
72  win = app.window
73  _ = app._tr
74 
75  # Clear all items
76  if clear:
77  self.model_names = {}
78  self.model.clear()
79 
80  # Add Headers
81  self.model.setHorizontalHeaderLabels([_("Thumb"), _("Name"), _("Description")])
82 
83  # Get the folder path of effects
84  effects_dir = os.path.join(info.PATH, "effects")
85  icons_dir = os.path.join(effects_dir, "icons")
86 
87  # Get a JSON list of all supported effects in libopenshot
88  raw_effects_list = json.loads(openshot.EffectInfo.Json())
89 
90  # Loop through each effect
91  for effect_info in raw_effects_list:
92  # Get basic properties about each effect
93  effect_name = effect_info["class_name"]
94  title = effect_info["name"]
95  description = effect_info["description"]
96  icon_name = "%s.png" % effect_name.lower()
97  icon_path = os.path.join(icons_dir, icon_name)
98 
99  # Determine the category of effect (audio, video, both)
100  category = None
101  if effect_info["has_video"] and effect_info["has_audio"]:
102  category = "Audio & Video"
103  elif not effect_info["has_video"] and effect_info["has_audio"]:
104  category = "Audio"
105  icon_path = os.path.join(icons_dir, "audio.png")
106  elif effect_info["has_video"] and not effect_info["has_audio"]:
107  category = "Video"
108 
109  log.info("category: %s" % category)
110 
111  # Filter out effect (if needed)
112  if win.effectsFilter.text() != "":
113  if not win.effectsFilter.text().lower() in self.app._tr(title).lower() and not win.effectsFilter.text().lower() in self.app._tr(description).lower():
114  continue
115 
116  # Check for thumbnail path (in build-in cache)
117  thumb_path = os.path.join(info.IMAGES_PATH, "cache", icon_name)
118 
119  # Check built-in cache (if not found)
120  if not os.path.exists(thumb_path):
121  # Check user folder cache
122  thumb_path = os.path.join(info.CACHE_PATH, icon_name)
123 
124  # Generate thumbnail (if needed)
125  if not os.path.exists(thumb_path):
126 
127  try:
128  # Reload this reader
129  clip = openshot.Clip(icon_path)
130  reader = clip.Reader()
131 
132  # Open reader
133  reader.Open()
134 
135  # Save thumbnail
136  reader.GetFrame(0).Thumbnail(thumb_path, 98, 64, os.path.join(info.IMAGES_PATH, "mask.png"), "",
137  "#000", True)
138  reader.Close()
139 
140  except:
141  # Handle exception
142  msg = QMessageBox()
143  msg.setText(_("{} is not a valid image file.".format(icon_path)))
144  msg.exec_()
145  continue
146 
147  row = []
148 
149  # Append thumbnail
150  col = QStandardItem()
151  col.setIcon(QIcon(thumb_path))
152  col.setText(self.app._tr(title))
153  col.setToolTip(self.app._tr(title))
154  col.setFlags(Qt.ItemIsSelectable | Qt.ItemIsEnabled | Qt.ItemIsUserCheckable | Qt.ItemIsDragEnabled)
155  row.append(col)
156 
157  # Append Name
158  col = QStandardItem("Name")
159  col.setData(self.app._tr(title), Qt.DisplayRole)
160  col.setText(self.app._tr(title))
161  col.setFlags(Qt.ItemIsSelectable | Qt.ItemIsEnabled | Qt.ItemIsUserCheckable | Qt.ItemIsDragEnabled)
162  row.append(col)
163 
164  # Append Description
165  col = QStandardItem("Description")
166  col.setData(self.app._tr(description), Qt.DisplayRole)
167  col.setFlags(Qt.ItemIsSelectable | Qt.ItemIsEnabled | Qt.ItemIsUserCheckable | Qt.ItemIsDragEnabled)
168  row.append(col)
169 
170  # Append Category
171  col = QStandardItem("Category")
172  col.setData(category, Qt.DisplayRole)
173  col.setText(category)
174  col.setFlags(Qt.ItemIsSelectable | Qt.ItemIsEnabled | Qt.ItemIsUserCheckable | Qt.ItemIsDragEnabled)
175  row.append(col)
176 
177  # Append Path
178  col = QStandardItem("Effect")
179  col.setData(effect_name, Qt.DisplayRole)
180  col.setText(effect_name)
181  col.setFlags(Qt.ItemIsSelectable | Qt.ItemIsEnabled | Qt.ItemIsUserCheckable | Qt.ItemIsDragEnabled)
182  row.append(col)
183 
184  # Append ROW to MODEL (if does not already exist in model)
185  if not effect_name in self.model_names:
186  self.model.appendRow(row)
187  self.model_names[effect_name] = effect_name
188 
189  def __init__(self, *args):
190 
191  # Create standard model
192  self.app = get_app()
194  self.model.setColumnCount(5)
195  self.model_names = {}
def get_app
Returns the current QApplication instance of OpenShot.
Definition: app.py:55