Skip to content

Commit 424f56e

Browse files
committed
Properties: Use main data models for context menu
- properties_tabelview will pull data directly from files_model and transition_model on the get_app().window object. - A connection to both models' ModelRefreshed signals that forces a menu data reset replaces the previous update_model() calls
1 parent ece947b commit 424f56e

File tree

1 file changed

+80
-71
lines changed

1 file changed

+80
-71
lines changed

src/windows/views/properties_tableview.py

+80-71
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@
3737
from classes import info
3838
from classes.query import Clip, Effect, Transition
3939
from windows.models.properties_model import PropertiesModel
40-
from windows.models.transition_model import TransitionsModel
41-
from windows.models.files_model import FilesModel
4240

4341
import openshot
4442

@@ -380,45 +378,51 @@ def contextMenuEvent(self, event=None, release=False):
380378
clip_id, item_type = selected_value.data()
381379
log.info("Context menu shown for %s (%s) for clip %s on frame %s" % (property_name, property_key, clip_id, frame_number))
382380
log.info("Points: %s" % points)
383-
log.info("Property: %s" % str(cur_property))
381+
382+
# Clear menu if models updated
383+
if self.menu_reset:
384+
self.choices = []
385+
self.menu_reset = False
384386

385387
# Handle reader type values
386388
if self.property_type == "reader" and not self.choices:
387389

388-
# Refresh models
389-
self.transition_model.update_model()
390-
self.files_model.update_model()
391-
392390
# Add all files
393391
file_choices = []
394-
for filesIndex in range(self.files_model.model.rowCount()):
395-
modelIndex = self.files_model.model.index(filesIndex, 0)
396-
fileItem = self.files_model.model.itemFromIndex(modelIndex)
397-
fileIcon = self.files_model.model.item(fileItem.row(), 0).icon()
398-
fileName = self.files_model.model.item(fileItem.row(), 1).text()
399-
fileParentPath = self.files_model.model.item(fileItem.row(), 4).text()
392+
for i in range(self.files_model.rowCount()):
393+
idx = self.files_model.index(i, 0)
394+
if not idx.isValid():
395+
continue
396+
icon = idx.data(Qt.DecorationRole)
397+
name = idx.sibling(idx.row(), 1).data()
398+
path = os.path.join(idx.sibling(idx.row(), 4).data(), name)
400399

401400
# Append file choice
402-
file_choices.append({"name": fileName,
403-
"value": os.path.join(fileParentPath, fileName),
401+
file_choices.append({"name": name,
402+
"value": path,
404403
"selected": False,
405-
"icon": fileIcon
404+
"icon": icon
406405
})
407406

408407
# Add root file choice
409408
self.choices.append({"name": _("Files"), "value": file_choices, "selected": False})
410409

411410
# Add all transitions
412411
trans_choices = []
413-
for transIndex in range(self.transition_model.model.rowCount()):
414-
modelIndex = self.transition_model.model.index(transIndex, 0)
415-
transItem = self.transition_model.model.itemFromIndex(modelIndex)
416-
transIcon = self.transition_model.model.item(transItem.row(), 0).icon()
417-
transName = self.transition_model.model.item(transItem.row(), 1).text()
418-
transPath = self.transition_model.model.item(transItem.row(), 3).text()
412+
for i in range(self.transition_model.rowCount()):
413+
idx = self.transition_model.index(i, 0)
414+
if not idx.isValid():
415+
continue
416+
icon = idx.data(Qt.DecorationRole)
417+
name = idx.sibling(idx.row(), 1).data()
418+
path = idx.sibling(idx.row(), 3).data()
419419

420420
# Append transition choice
421-
trans_choices.append({"name": transName, "value": transPath, "selected": False, "icon": transIcon})
421+
trans_choices.append({"name": name,
422+
"value": path,
423+
"selected": False,
424+
"icon": icon
425+
})
422426

423427
# Add root transitions choice
424428
self.choices.append({"name": _("Transitions"), "value": trans_choices, "selected": False})
@@ -476,7 +480,7 @@ def contextMenuEvent(self, event=None, release=False):
476480
# Add menu options for keyframes
477481
menu = QMenu(self)
478482
if points > 1:
479-
# Menu for more than 1 point
483+
# Menu items only for multiple points
480484
Bezier_Menu = QMenu(_("Bezier"), self)
481485
Bezier_Menu.setIcon(bezier_icon)
482486
for bezier_preset in bezier_presets:
@@ -490,55 +494,51 @@ def contextMenuEvent(self, event=None, release=False):
490494
Constant_Action.setIcon(constant_icon)
491495
Constant_Action.triggered.connect(self.Constant_Action_Triggered)
492496
menu.addSeparator()
493-
Insert_Action = menu.addAction(_("Insert Keyframe"))
494-
Insert_Action.triggered.connect(self.Insert_Action_Triggered)
495-
Remove_Action = menu.addAction(_("Remove Keyframe"))
496-
Remove_Action.triggered.connect(self.Remove_Action_Triggered)
497-
menu.popup(QCursor.pos())
498-
elif points == 1:
499-
# Menu for a single point
497+
if points >= 1:
498+
# Menu items for one or more points
500499
Insert_Action = menu.addAction(_("Insert Keyframe"))
501500
Insert_Action.triggered.connect(self.Insert_Action_Triggered)
502501
Remove_Action = menu.addAction(_("Remove Keyframe"))
503502
Remove_Action.triggered.connect(self.Remove_Action_Triggered)
504503
menu.popup(QCursor.pos())
505504

506-
if self.choices:
507-
# Menu for choices
508-
for choice in self.choices:
509-
if type(choice["value"]) != list:
510-
# Add root choice items
511-
Choice_Action = menu.addAction(_(choice["name"]))
512-
Choice_Action.setData(choice["value"])
513-
Choice_Action.triggered.connect(self.Choice_Action_Triggered)
505+
# Menu for choices
506+
if not self.choices:
507+
return
508+
for choice in self.choices:
509+
if type(choice["value"]) != list:
510+
# Just add root choice item
511+
Choice_Action = menu.addAction(_(choice["name"]))
512+
Choice_Action.setData(choice["value"])
513+
Choice_Action.triggered.connect(self.Choice_Action_Triggered)
514+
continue
515+
516+
# Add sub-choice items (for nested choice lists)
517+
# Divide into smaller QMenus (since large lists cover the entire screen)
518+
# For example: Transitions -> 1 -> sub items
519+
SubMenu = None
520+
SubMenuRoot = QMenu(_(choice["name"]), self)
521+
SubMenuSize = 24
522+
SubMenuNumber = 0
523+
for sub_choice in choice["value"]:
524+
if len(choice["value"]) > SubMenuSize:
525+
if not SubMenu or len(SubMenu.children()) == SubMenuSize or sub_choice == choice["value"][-1]:
526+
SubMenuNumber += 1
527+
if SubMenu:
528+
SubMenuRoot.addMenu(SubMenu)
529+
SubMenu = QMenu(str(SubMenuNumber), self)
530+
Choice_Action = SubMenu.addAction(_(sub_choice["name"]))
514531
else:
515-
# Add sub-choice items (for nested choice lists)
516-
# Divide into smaller QMenus (since large lists cover the entire screen)
517-
# For example: Transitions -> 1 -> sub items
518-
SubMenu = None
519-
SubMenuRoot = QMenu(_(choice["name"]), self)
520-
SubMenuSize = 24
521-
SubMenuNumber = 0
522-
for sub_choice in choice["value"]:
523-
if len(choice["value"]) > SubMenuSize:
524-
if not SubMenu or len(SubMenu.children()) == SubMenuSize or sub_choice == choice["value"][-1]:
525-
SubMenuNumber += 1
526-
if SubMenu:
527-
SubMenuRoot.addMenu(SubMenu)
528-
SubMenu = QMenu(str(SubMenuNumber), self)
529-
Choice_Action = SubMenu.addAction(_(sub_choice["name"]))
530-
else:
531-
Choice_Action = SubMenuRoot.addAction(_(sub_choice["name"]))
532-
Choice_Action.setData(sub_choice["value"])
533-
subChoiceIcon = sub_choice.get("icon")
534-
if subChoiceIcon:
535-
Choice_Action.setIcon(subChoiceIcon)
536-
Choice_Action.triggered.connect(self.Choice_Action_Triggered)
537-
menu.addMenu(SubMenuRoot)
538-
539-
# Show choice menu and release lock
540-
menu.popup(QCursor.pos())
541-
self.contextMenuEvent(event, release=True)
532+
Choice_Action = SubMenuRoot.addAction(_(sub_choice["name"]))
533+
Choice_Action.setData(sub_choice["value"])
534+
if "icon" in sub_choice:
535+
Choice_Action.setIcon(sub_choice["icon"])
536+
Choice_Action.triggered.connect(self.Choice_Action_Triggered)
537+
menu.addMenu(SubMenuRoot)
538+
539+
# Show choice menu and release lock
540+
menu.popup(QCursor.pos())
541+
self.contextMenuEvent(event, release=True)
542542

543543
def Bezier_Action_Triggered(self, preset=[]):
544544
log.info("Bezier_Action_Triggered: %s" % str(preset))
@@ -584,17 +584,28 @@ def Choice_Action_Triggered(self, event):
584584
# Update value of dropdown item
585585
self.clip_properties_model.value_updated(self.selected_item, value=choice_value)
586586

587+
def refresh_menu(self):
588+
""" Ensure we update the meun when our source models change """
589+
self.menu_reset = True
590+
587591
def __init__(self, *args):
588592
# Invoke parent init
589593
QTableView.__init__(self, *args)
590594

591595
# Get a reference to the window object
592596
self.win = get_app().window
593597

594-
# Get Model data
598+
# Create properties model
595599
self.clip_properties_model = PropertiesModel(self)
596-
self.transition_model = TransitionsModel(self)
597-
self.files_model = FilesModel(self)
600+
601+
# Get base models for files, transitions
602+
self.transition_model = self.win.transition_model.model
603+
self.files_model = self.win.files_model.model
604+
605+
# Connect to update signals, so our menus stay current
606+
self.win.files_model.ModelRefreshed.connect(self.refresh_menu)
607+
self.win.transition_model.ModelRefreshed.connect(self.refresh_menu)
608+
self.menu_reset = False
598609

599610
# Keep track of mouse press start position to determine when to start drag
600611
self.selected = []
@@ -627,8 +638,6 @@ def __init__(self, *args):
627638

628639
# Refresh view
629640
self.clip_properties_model.update_model()
630-
self.transition_model.update_model()
631-
self.files_model.update_model()
632641

633642
# Resize columns
634643
self.resizeColumnToContents(0)

0 commit comments

Comments
 (0)