-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathlist_view.py
More file actions
356 lines (294 loc) · 12.9 KB
/
Copy pathlist_view.py
File metadata and controls
356 lines (294 loc) · 12.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
# This file is part of Linux Show Player
#
# Copyright 2016 Francesco Ceruti <ceppofrancy@gmail.com>
#
# Linux Show Player is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Linux Show Player is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Linux Show Player. If not, see <http://www.gnu.org/licenses/>.
from PyQt5.QtCore import (
pyqtSignal,
Qt,
QDataStream,
QIODevice,
QT_TRANSLATE_NOOP,
QTimer,
)
from PyQt5.QtGui import QKeyEvent, QContextMenuEvent, QBrush, QColor
from PyQt5.QtWidgets import QTreeWidget, QHeaderView, QTreeWidgetItem, QMenu
from lisp.application import Application
from lisp.backend import get_backend
from lisp.command.model import ModelMoveItemsCommand, ModelInsertItemsCommand
from lisp.core.util import subdict
from lisp.cues.cue_factory import CueFactory
from lisp.plugins.list_layout.list_widgets import (
CueStatusIcons,
NameWidget,
PreWaitWidget,
CueTimeWidget,
NextActionIcon,
PostWaitWidget,
IndexWidget,
)
from lisp.ui.ui_utils import translate, css_to_dict, dict_to_css
class ListColumn:
def __init__(self, name, widget, resize=None, width=None, displayName=True):
self.baseName = name
self.widget = widget
self.resize = resize
self.width = width
self.displayName = displayName
self.action = None
@property
def name(self):
return translate("ListLayoutHeader", self.baseName)
class CueTreeWidgetItem(QTreeWidgetItem):
def __init__(self, cue, *args, **kwargs):
super().__init__(*args, **kwargs)
self.cue = cue
self.current = False
# TODO: use a custom Model/View
class CueListView(QTreeWidget):
keyPressed = pyqtSignal(QKeyEvent)
contextMenuInvoked = pyqtSignal(QContextMenuEvent)
# TODO: add ability to show/hide
# TODO: implement columns (cue-type / target / etc..)
COLUMNS = [
ListColumn(QT_TRANSLATE_NOOP("ListLayoutHeader", "Cue Status"), CueStatusIcons, QHeaderView.Fixed, width=45, displayName=False),
ListColumn("#", IndexWidget, QHeaderView.ResizeToContents),
ListColumn(
QT_TRANSLATE_NOOP("ListLayoutHeader", "Cue"),
NameWidget,
QHeaderView.Stretch,
),
ListColumn(
QT_TRANSLATE_NOOP("ListLayoutHeader", "Pre wait"), PreWaitWidget
),
ListColumn(
QT_TRANSLATE_NOOP("ListLayoutHeader", "Action"), CueTimeWidget
),
ListColumn(
QT_TRANSLATE_NOOP("ListLayoutHeader", "Post wait"), PostWaitWidget
),
ListColumn(QT_TRANSLATE_NOOP("ListLayoutHeader", "Next Action"), NextActionIcon, QHeaderView.Fixed, width=18, displayName=False),
]
ITEM_DEFAULT_BG = QBrush(Qt.transparent)
ITEM_CURRENT_BG = QBrush(QColor(250, 220, 0, 100))
def __init__(self, listModel, parent=None):
"""
:type listModel: lisp.plugins.list_layout.models.CueListModel
"""
super().__init__(parent)
self.__itemMoving = False
self.__scrollRangeGuard = False
# Watch for model changes
self._model = listModel
self._model.item_added.connect(self.__cueAdded)
self._model.item_moved.connect(self.__cueMoved)
self._model.item_removed.connect(self.__cueRemoved)
self._model.model_reset.connect(self.__modelReset)
# Create context menu for column headers
self.__columnMenu = QMenu()
self.__hideMenu = self.__columnMenu.addAction(QT_TRANSLATE_NOOP("ListLayoutHeaderMenu", "Hide"))
self.__showMenu = self.__columnMenu.addMenu(QT_TRANSLATE_NOOP("ListLayoutHeaderMenu","Show"))
# Setup the columns headers
self.setHeaderLabels((c.name if c.displayName else "" for c in CueListView.COLUMNS))
for i, column in enumerate(CueListView.COLUMNS):
if column.resize is not None:
self.header().setSectionResizeMode(i, column.resize)
if column.width is not None:
self.setColumnWidth(i, column.width)
# Create context menu to show the column
column.action = self.__showMenu.addAction(column.name)
column.action.triggered.connect(
lambda chk, item=i: self.showColumn(item))
self.header().setDragEnabled(True)
self.header().setStretchLastSection(False)
# Connect local context menu for headers
self.header().setContextMenuPolicy(Qt.CustomContextMenu)
self.header().customContextMenuRequested.connect(self.__openHeaderMenu)
self.setDragDropMode(self.InternalMove)
# Set some visual options
self.setIndentation(0)
self.setAlternatingRowColors(True)
self.setVerticalScrollMode(self.ScrollPerItem)
# This allow to have some spared space at the end of the scroll-area
self.verticalScrollBar().rangeChanged.connect(self.__updateScrollRange)
self.currentItemChanged.connect(
self.__currentItemChanged, Qt.QueuedConnection
)
def dragEnterEvent(self, event):
if event.mimeData().hasUrls():
if all([x.isLocalFile() for x in event.mimeData().urls()]):
event.accept()
else:
event.ignore()
else:
return super().dragEnterEvent(event)
def dropEvent(self, event):
if event.mimeData().hasUrls():
# If files are being dropped, add them as cues
get_backend().add_cue_from_urls(event.mimeData().urls())
else:
# Otherwise copy/move existing cue.
# Decode mimedata information about the drag&drop event, since only
# internal movement are allowed we assume the data format is correct
data = event.mimeData().data(
"application/x-qabstractitemmodeldatalist"
)
stream = QDataStream(data, QIODevice.ReadOnly)
# Get the starting-item row
to_index = self.indexAt(event.pos()).row()
if not 0 <= to_index <= len(self._model):
to_index = len(self._model)
rows = []
while not stream.atEnd():
row = stream.readInt()
# Skip column and data
stream.readInt()
for _ in range(stream.readInt()):
stream.readInt()
stream.readQVariant()
if rows and row == rows[-1]:
continue
rows.append(row)
if event.proposedAction() == Qt.MoveAction:
Application().commands_stack.do(
ModelMoveItemsCommand(self._model, rows, to_index)
)
elif event.proposedAction() == Qt.CopyAction:
new_cues = []
for row in sorted(rows):
new_cues.append(CueFactory.clone_cue(self._model.item(row)))
Application().commands_stack.do(
ModelInsertItemsCommand(self._model, to_index, *new_cues)
)
def contextMenuEvent(self, event):
self.contextMenuInvoked.emit(event)
def keyPressEvent(self, event):
self.keyPressed.emit(event)
# If the event object has been accepted during the `keyPressed`
# emission don't call the base implementation
if not event.isAccepted():
super().keyPressEvent(event)
def mousePressEvent(self, event):
if (
not event.buttons() & Qt.RightButton
or not self.selectionMode() == QTreeWidget.NoSelection
):
super().mousePressEvent(event)
def resizeEvent(self, event):
super().resizeEvent(event)
self.updateHeadersSizes()
def standbyIndex(self):
return self.indexOfTopLevelItem(self.currentItem())
def setStandbyIndex(self, newIndex):
if 0 <= newIndex < self.topLevelItemCount():
self.setCurrentItem(self.topLevelItem(newIndex))
def updateHeadersSizes(self):
"""Some hack to have "stretchable" columns with a minimum size
NOTE: this currently works properly with only one "stretchable" column
"""
header = self.header()
for i, column in enumerate(CueListView.COLUMNS):
if column.resize == QHeaderView.Stretch:
# Make the header calculate the content size
header.setSectionResizeMode(i, QHeaderView.ResizeToContents)
contentWidth = header.sectionSize(i)
# Make the header calculate the stretched size
header.setSectionResizeMode(i, QHeaderView.Stretch)
stretchWidth = header.sectionSize(i)
# Set the maximum size as fixed size for the section
header.setSectionResizeMode(i, QHeaderView.Fixed)
header.resizeSection(i, max(contentWidth, stretchWidth))
def __currentItemChanged(self, current, previous):
if previous is not None:
previous.current = False
self.__updateItemStyle(previous)
if current is not None:
current.current = True
self.__updateItemStyle(current)
if self.selectionMode() == QTreeWidget.NoSelection:
# Ensure the current item is in the middle of the viewport.
# This is skipped in "selection-mode" otherwise it creates
# confusion during drang&drop operations
self.scrollToItem(current, QTreeWidget.PositionAtCenter)
elif not self.selectedIndexes():
current.setSelected(True)
def __updateItemStyle(self, item):
if item.treeWidget() is not None:
css = css_to_dict(item.cue.stylesheet)
brush = QBrush()
if item.current:
widget_css = subdict(css, ("font-size",))
brush = CueListView.ITEM_CURRENT_BG
else:
widget_css = subdict(css, ("color", "font-size"))
css_bg = css.get("background")
if css_bg is not None:
color = QColor(css_bg)
color.setAlpha(150)
brush = QBrush(color)
for column in range(self.columnCount()):
self.itemWidget(item, column).setStyleSheet(
dict_to_css(widget_css)
)
item.setBackground(column, brush)
def __cuePropChanged(self, cue, property_name, _):
if property_name == "stylesheet":
self.__updateItemStyle(self.topLevelItem(cue.index))
if property_name == "name":
QTimer.singleShot(1, self.updateHeadersSizes)
def __cueAdded(self, cue):
item = CueTreeWidgetItem(cue)
item.setFlags(item.flags() & ~Qt.ItemIsDropEnabled)
cue.property_changed.connect(self.__cuePropChanged)
self.insertTopLevelItem(cue.index, item)
self.__setupItemWidgets(item)
if self.topLevelItemCount() == 1:
# If it's the only item in the view, set it as current
self.setCurrentItem(item)
else:
# Scroll to the last item added
self.scrollToItem(item)
# Ensure that the focus is set
self.setFocus()
def __cueMoved(self, before, after):
item = self.takeTopLevelItem(before)
self.insertTopLevelItem(after, item)
self.__setupItemWidgets(item)
def __cueRemoved(self, cue):
cue.property_changed.disconnect(self.__cuePropChanged)
self.takeTopLevelItem(cue.index)
def __modelReset(self):
self.reset()
self.clear()
def __setupItemWidgets(self, item):
for i, column in enumerate(CueListView.COLUMNS):
self.setItemWidget(item, i, column.widget(item))
self.updateGeometries()
def __updateScrollRange(self, min_, max_):
if not self.__scrollRangeGuard:
self.__scrollRangeGuard = True
self.verticalScrollBar().setMaximum(max_ + 1)
self.__scrollRangeGuard = False
def __openHeaderMenu(self, position):
# Prepare and open context menu
index = self.indexAt(position)
showShowMenu = False
for i, column in enumerate(CueListView.COLUMNS):
# show column name in menu if column is hidden
column.action.setVisible(self.isColumnHidden(i))
showShowMenu |= self.isColumnHidden(i)
self.__showMenu.menuAction().setVisible(showShowMenu)
action = self.__columnMenu.exec_(self.mapToGlobal(position))
if action == self.__hideMenu:
self.hideColumn(index.column())