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, QRegExp
29from PyQt5.QtGui import QDrag
30from PyQt5.QtWidgets import QListView, QAbstractItemView, QMenu
31
32from classes.app import get_app
33from classes.logger import log
34
35
36class TransitionsListView(QListView):
37    """ A QListView QWidget used on the main window """
38    drag_item_size = 48
39
40    def contextMenuEvent(self, event):
41        event.accept()
42
43        # Set context menu mode
44        app = get_app()
45        app.context_menu_object = "transitions"
46
47        menu = QMenu(self)
48        menu.addAction(self.win.actionDetailsView)
49        menu.popup(event.globalPos())
50
51    def startDrag(self, supportedActions):
52        """ Override startDrag method to display custom icon """
53
54        # Get first column indexes for all selected rows
55        selected = self.selectionModel().selectedRows(0)
56
57        # Get image of current item
58        current = self.selectionModel().currentIndex()
59        if not current.isValid() and selected:
60            current = selected[0]
61
62        if not current.isValid():
63            log.warning("No draggable items found in model!")
64            return False
65
66        # Get icon from column 0 on same row as current item
67        icon = current.sibling(current.row(), 0).data(Qt.DecorationRole)
68
69        # Start drag operation
70        drag = QDrag(self)
71        drag.setMimeData(self.model().mimeData(selected))
72        drag.setPixmap(icon.pixmap(QSize(self.drag_item_size, self.drag_item_size)))
73        drag.setHotSpot(QPoint(self.drag_item_size / 2, self.drag_item_size / 2))
74        drag.exec_()
75
76    def filter_changed(self):
77        self.refresh_view()
78
79    def refresh_view(self):
80        """Filter transitions with proxy class"""
81        filter_text = self.win.transitionsFilter.text()
82        self.model().setFilterRegExp(QRegExp(filter_text.replace(' ', '.*')))
83        self.model().setFilterCaseSensitivity(Qt.CaseInsensitive)
84        self.model().sort(Qt.AscendingOrder)
85
86    def __init__(self, model):
87        # Invoke parent init
88        QListView.__init__(self)
89
90        # Get a reference to the window object
91        self.win = get_app().window
92
93        # Get Model data
94        self.transition_model = model
95
96        # Keep track of mouse press start position to determine when to start drag
97        self.setAcceptDrops(True)
98        self.setDragEnabled(True)
99        self.setDropIndicatorShown(True)
100
101        self.setModel(self.transition_model.proxy_model)
102
103        # Remove the default selection model and wire up to the shared one
104        self.selectionModel().deleteLater()
105        self.setSelectionMode(QAbstractItemView.SingleSelection)
106        self.setSelectionBehavior(QAbstractItemView.SelectRows)
107        self.setSelectionModel(self.transition_model.selection_model)
108
109        # Setup header columns
110        self.setIconSize(QSize(131, 108))
111        self.setGridSize(QSize(102, 92))
112        self.setViewMode(QListView.IconMode)
113        self.setResizeMode(QListView.Adjust)
114        self.setUniformItemSizes(True)
115        self.setWordWrap(False)
116        self.setTextElideMode(Qt.ElideRight)
117        self.setStyleSheet('QListView::item { padding-top: 2px; }')
118
119        # setup filter events
120        app = get_app()
121        app.window.transitionsFilter.textChanged.connect(self.filter_changed)
122        app.window.refreshTransitionsSignal.connect(self.refresh_view)
123