1"""
2 @file
3 @brief This file contains the transitions file treeview, used by the main window
4 @author Jonathan Thomas <jonathan@openshot.org>
5
6 @section LICENSE
7
8 Copyright (c) 2008-2018 OpenShot Studios, LLC
9 (http://www.openshotstudios.com). This file is part of
10 OpenShot Video Editor (http://www.openshot.org), an open-source project
11 dedicated to delivering high quality video editing and animation solutions
12 to the world.
13
14 OpenShot Video Editor is free software: you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation, either version 3 of the License, or
17 (at your option) any later version.
18
19 OpenShot Video Editor is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with OpenShot Library.  If not, see <http://www.gnu.org/licenses/>.
26 """
27
28from PyQt5.QtCore import Qt, QSize, QPoint
29from PyQt5.QtGui import QDrag
30from PyQt5.QtWidgets import QTreeView, QAbstractItemView, QMenu, QSizePolicy
31
32from classes.app import get_app
33from classes.logger import log
34
35
36class TransitionsTreeView(QTreeView):
37    """ A TreeView QWidget used on the main window """
38    drag_item_size = 48
39
40    def contextMenuEvent(self, event):
41        # Set context menu mode
42        app = get_app()
43        app.context_menu_object = "transitions"
44
45        menu = QMenu(self)
46        menu.addAction(self.win.actionThumbnailView)
47        menu.popup(event.globalPos())
48
49    def startDrag(self, event):
50        """ Override startDrag method to display custom icon """
51
52        # Get first column indexes for all selected rows
53        selected = self.selectionModel().selectedRows(0)
54
55        # Get image of current item
56        current = self.selectionModel().currentIndex()
57        if not current.isValid() and selected:
58            current = selected[0]
59
60        if not current.isValid():
61            log.warning("No draggable items found in model!")
62            return False
63
64        # Get icon from column 0 on same row as current item
65        icon = current.sibling(current.row(), 0).data(Qt.DecorationRole)
66
67        # Start drag operation
68        drag = QDrag(self)
69        drag.setMimeData(self.model().mimeData(selected))
70        drag.setPixmap(icon.pixmap(QSize(self.drag_item_size, self.drag_item_size)))
71        drag.setHotSpot(QPoint(self.drag_item_size / 2, self.drag_item_size / 2))
72        drag.exec_()
73
74    def refresh_columns(self):
75        """Hide certain columns"""
76        if type(self) == TransitionsTreeView:
77            # Only execute when the treeview is active
78            self.hideColumn(2)
79            self.hideColumn(3)
80
81    def __init__(self, model):
82        # Invoke parent init
83        QTreeView.__init__(self)
84
85        # Get a reference to the window object
86        self.win = get_app().window
87
88        # Get Model data
89        self.transition_model = model
90
91        # Keep track of mouse press start position to determine when to start drag
92        self.setAcceptDrops(True)
93        self.setDragEnabled(True)
94        self.setDropIndicatorShown(True)
95
96        self.setModel(self.transition_model.proxy_model)
97
98        # Remove the default selection model and wire up to the shared one
99        self.selectionModel().deleteLater()
100        self.setSelectionMode(QAbstractItemView.SingleSelection)
101        self.setSelectionBehavior(QAbstractItemView.SelectRows)
102        self.setSelectionModel(self.transition_model.selection_model)
103
104        # Setup header columns
105        self.setIconSize(QSize(75, 62))
106        self.setIndentation(0)
107        self.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
108        self.setWordWrap(True)
109        self.setStyleSheet('QTreeView::item { padding-top: 2px; }')
110        self.transition_model.ModelRefreshed.connect(self.refresh_columns)
111
112        self.refresh_columns()
113