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  # Filter out effect (if needed)
110  if win.effectsFilter.text() != "":
111  if not win.effectsFilter.text().lower() in self.app._tr(title).lower() and not win.effectsFilter.text().lower() in self.app._tr(description).lower():
112  continue
113 
114  # Check for thumbnail path (in build-in cache)
115  thumb_path = os.path.join(info.IMAGES_PATH, "cache", icon_name)
116 
117  # Check built-in cache (if not found)
118  if not os.path.exists(thumb_path):
119  # Check user folder cache
120  thumb_path = os.path.join(info.CACHE_PATH, icon_name)
121 
122  # Generate thumbnail (if needed)
123  if not os.path.exists(thumb_path):
124 
125  try:
126  # Reload this reader
127  clip = openshot.Clip(icon_path)
128  reader = clip.Reader()
129 
130  # Open reader
131  reader.Open()
132 
133  # Save thumbnail
134  reader.GetFrame(0).Thumbnail(thumb_path, 98, 64, os.path.join(info.IMAGES_PATH, "mask.png"), "",
135  "#000", True)
136  reader.Close()
137 
138  except:
139  # Handle exception
140  msg = QMessageBox()
141  msg.setText(_("{} is not a valid image file.".format(icon_path)))
142  msg.exec_()
143  continue
144 
145  row = []
146 
147  # Append thumbnail
148  col = QStandardItem()
149  col.setIcon(QIcon(thumb_path))
150  col.setText(self.app._tr(title))
151  col.setToolTip(self.app._tr(title))
152  col.setFlags(Qt.ItemIsSelectable | Qt.ItemIsEnabled | Qt.ItemIsUserCheckable | Qt.ItemIsDragEnabled)
153  row.append(col)
154 
155  # Append Name
156  col = QStandardItem("Name")
157  col.setData(self.app._tr(title), Qt.DisplayRole)
158  col.setText(self.app._tr(title))
159  col.setFlags(Qt.ItemIsSelectable | Qt.ItemIsEnabled | Qt.ItemIsUserCheckable | Qt.ItemIsDragEnabled)
160  row.append(col)
161 
162  # Append Description
163  col = QStandardItem("Description")
164  col.setData(self.app._tr(description), Qt.DisplayRole)
165  col.setFlags(Qt.ItemIsSelectable | Qt.ItemIsEnabled | Qt.ItemIsUserCheckable | Qt.ItemIsDragEnabled)
166  row.append(col)
167 
168  # Append Category
169  col = QStandardItem("Category")
170  col.setData(category, Qt.DisplayRole)
171  col.setText(category)
172  col.setFlags(Qt.ItemIsSelectable | Qt.ItemIsEnabled | Qt.ItemIsUserCheckable | Qt.ItemIsDragEnabled)
173  row.append(col)
174 
175  # Append Path
176  col = QStandardItem("Effect")
177  col.setData(effect_name, Qt.DisplayRole)
178  col.setText(effect_name)
179  col.setFlags(Qt.ItemIsSelectable | Qt.ItemIsEnabled | Qt.ItemIsUserCheckable | Qt.ItemIsDragEnabled)
180  row.append(col)
181 
182  # Append ROW to MODEL (if does not already exist in model)
183  if not effect_name in self.model_names:
184  self.model.appendRow(row)
185  self.model_names[effect_name] = effect_name
186 
187  def __init__(self, *args):
188 
189  # Create standard model
190  self.app = get_app()
192  self.model.setColumnCount(5)
193  self.model_names = {}
def get_app
Returns the current QApplication instance of OpenShot.
Definition: app.py:55