forked from krathjen/studiolibrary
-
Notifications
You must be signed in to change notification settings - Fork 0
/
libraryitem.py
678 lines (513 loc) · 17.2 KB
/
libraryitem.py
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
# Copyright 2017 by Kurt Rathjen. All Rights Reserved.
#
# This library is free software: you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation, either
# version 3 of the License, or (at your option) any later version.
# This library 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
# Lesser General Public License for more details.
# You should have received a copy of the GNU Lesser General Public
# License along with this library. If not, see <http://www.gnu.org/licenses/>.
import os
import logging
from functools import partial
import studiolibrary
import studioqt
from studioqt import QtGui
from studioqt import QtCore
from studioqt import QtWidgets
logger = logging.getLogger(__name__)
class ItemError(Exception):
""""""
class ItemSaveError(ItemError):
""""""
class ItemLoadError(ItemError):
""""""
class LibraryItemSignals(QtCore.QObject):
""""""
saved = QtCore.Signal(object)
saving = QtCore.Signal(object)
loaded = QtCore.Signal(object)
class LibraryItem(studioqt.CombinedWidgetItem):
ENABLE_DELETE = False
Extensions = []
MenuName = "Library Item"
MenuIconPath = ""
TypeIconPath = ""
CreateWidgetClass = None
PreviewWidgetClass = None
_libraryItemSignals = LibraryItemSignals()
saved = _libraryItemSignals.saved
saving = _libraryItemSignals.saving
loaded = _libraryItemSignals.loaded
@classmethod
def createAction(cls, menu, libraryWidget):
"""
Return the action to be displayed when the user clicks the "plus" icon.
:type menu: QtWidgets.QMenu
:type libraryWidget: studiolibrary.LibraryWidget
:rtype: QtCore.QAction
"""
if cls.CreateWidgetClass:
icon = QtGui.QIcon(cls.MenuIconPath)
callback = partial(cls.showCreateWidget, libraryWidget)
action = QtWidgets.QAction(icon, cls.MenuName, menu)
action.triggered.connect(callback)
return action
@classmethod
def showCreateWidget(cls, libraryWidget):
"""
Show the create widget for creating a new item.
:type libraryWidget: studiolibrary.LibraryWidget
"""
widget = cls.CreateWidgetClass()
libraryWidget.setCreateWidget(widget)
@classmethod
def isValidPath(cls, path):
"""
Return True if the given path location is supported by the item.
:type path: str
:rtype: bool
"""
for ext in cls.Extensions:
if path.endswith(ext):
return True
return False
def __init__(
self,
path="",
database=None,
libraryWidget=None,
):
"""
The LibraryItem class provides an item for use with the LibraryWidget.
:type path: str
:type database: studiolibrary.Database or None
:type libraryWidget: studiolibrary.LibraryWidget or None
"""
studioqt.CombinedWidgetItem.__init__(self)
self._path = ""
self._library = None
self._iconPath = None
self._database = None
self._typePixmap = None
self._libraryWidget = None
if libraryWidget:
self.setLibraryWidget(libraryWidget)
if database:
self.setDatabase(database)
if path:
self.setPath(path)
def id(self):
"""
Return the unique id for the item.
:rtype: str
"""
return self.path()
def showToastMessage(self, text):
"""
A convenience method for showing the toast widget with the given text.
:type text: str
:rtype: None
"""
if self.libraryWidget():
self.libraryWidget().showToastMessage(text)
def showErrorDialog(self, title, text):
"""
Convenience method for showing an error dialog to the user.
:type title: str
:type text: str
:rtype: QMessageBox.StandardButton
"""
if self.libraryWidget():
self.libraryWidget().showErrorMessage(text)
return studioqt.MessageBox.critical(self.libraryWidget(), title, text)
def showExceptionDialog(self, title, error):
"""
Convenience method for showing a question dialog to the user.
:type title: str
:type error: Exception
:rtype: QMessageBox.StandardButton
"""
logger.exception(error)
return self.showErrorDialog(title, error)
def showQuestionDialog(self, title, text):
"""
Convenience method for showing a question dialog to the user.
:type title: str
:type text: str
:rtype: QMessageBox.StandardButton
"""
return studioqt.MessageBox.question(self.libraryWidget(), title, text)
def typeIconPath(self):
"""
Return the type icon path on disc.
:rtype: path or None
"""
return self.TypeIconPath
def thumbnailPath(self):
"""
Return the thumbnail location on disc for this item.
:rtype: str
"""
return studioqt.resource.get("icons", "thumbnail.png")
def showPreviewWidget(self, libraryWidget):
"""
Show the preview Widget for the item instance.
:type libraryWidget: studiolibrary.LibraryWidget
"""
widget = self.previewWidget(libraryWidget)
libraryWidget.setPreviewWidget(widget)
def previewWidget(self, libraryWidget):
"""
Return the widget to be shown when the user clicks on the item.
:type libraryWidget: studiolibrary.LibraryWidget
:rtype: QtWidgets.QWidget or None
"""
widget = None
if self.PreviewWidgetClass:
widget = self.PreviewWidgetClass(item=self)
return widget
def contextEditMenu(self, menu, items=None):
"""
Called when the user would like to edit the item from the menu.
The given menu is shown as a submenu of the main context menu.
:type menu: QtWidgets.QMenu
:type items: list[LibraryItem]
:rtype: None
"""
if self.ENABLE_DELETE:
action = QtWidgets.QAction("Delete", menu)
action.triggered.connect(self.showDeleteDialog)
menu.addAction(action)
menu.addSeparator()
action = QtWidgets.QAction("Rename", menu)
action.triggered.connect(self.showRenameDialog)
menu.addAction(action)
action = QtWidgets.QAction("Move to", menu)
action.triggered.connect(self.showMoveDialog)
menu.addAction(action)
action = QtWidgets.QAction("Show in Folder", menu)
action.triggered.connect(self.showInFolder)
menu.addAction(action)
def contextMenu(self, menu, items=None):
"""
Called when the user right clicks on the item.
:type menu: QtWidgets.QMenu
:type items: list[LibraryItem]
:rtype: None
"""
pass
def showInFolder(self):
"""Open the file explorer at the given path location."""
path = self.path()
studiolibrary.showInFolder(path)
def name(self):
"""
Return the name of the item.
:rtype: str
"""
return self.text("Name")
def url(self):
"""Used by the mime data when dragging/dropping the item."""
return QtCore.QUrl("file:///" + self.path())
def dirname(self):
"""
:rtype: str
"""
return os.path.dirname(self.path())
def extension(self):
"""
:rtype: str
"""
_, extension = os.path.splitext(self.path())
return extension
def exists(self):
"""
:rtype: bool
"""
return os.path.exists(self.path())
def mtime(self):
"""
:rtype: float
"""
return os.path.getmtime(self.path())
def ctime(self):
"""
Return when the item was created.
:rtype: str
"""
path = self.path()
if os.path.exists(path):
return int(os.path.getctime(path))
return None
def libraryWidget(self):
"""
:rtype: studiolibrary.LibraryWidget or None
"""
return self._libraryWidget
def setLibraryWidget(self, libraryWidget):
"""
:rtype: studiolibrary.LibraryWidget or None
"""
self._libraryWidget = libraryWidget
def setDatabase(self, database):
"""
Set the database for the item.
:type database: studiolibrary.Database
"""
self._database = database
def database(self):
"""
Return the database for the item.
:rtype: studiolibrary.Database or None
"""
if not self._database and self.libraryWidget():
return self.libraryWidget().database()
return self._database
def setIconPath(self, path):
"""
Set the icon path for the current item.
:type path: str
:rtype: None
"""
self._iconPath = path
def iconPath(self):
"""
Return the icon path for the current item.
:rtype: None
"""
return self._iconPath
def mimeText(self):
"""
:rtype: str
"""
return self.path()
def path(self):
"""
Return the path for the item.
:rtype: str
"""
return self.text("Path")
def setPath(self, path):
"""
Set the path location on disc for the item.
:type path: str
:rtype: None
"""
if not path:
raise ItemError('Cannot set an empty item path.')
self.resetImageSequence()
path = studiolibrary.normPath(path)
dirname, basename, extension = studiolibrary.splitPath(path)
name = os.path.basename(path)
category = os.path.basename(dirname)
self.setName(name)
self.setText("Path", path)
self.setText("Category", category)
if os.path.exists(path):
modified = os.path.getmtime(path)
timeAgo = studiolibrary.timeAgo(modified)
self.setText("Modified", timeAgo)
self.setSortText("Modified", str(modified))
self.setText("Type", extension)
def load(self):
"""Reimplement this method for loading any item data."""
logger.debug(u'Loading "{0}"'.format(self.name()))
LibraryItem.loaded.emit(self)
def save(self, path=None, contents=None):
"""
Save the item to the given path.
:type path: str or None
:type contents: list[str] or None
:rtype: None
"""
path = path or self.path()
contents = contents or []
self.setPath(path)
path = self.path()
logger.debug(u'Item Saving: {0}'.format(path))
self.saving.emit(self)
if os.path.exists(path):
self.showAlreadyExistsDialog()
studiolibrary.movePaths(contents, path)
if self.database():
self.database().addPath(path)
if self.libraryWidget():
self.libraryWidget().addItem(self, select=True)
self.saved.emit(self)
logger.debug(u'Item Saved: {0}'.format(self.path()))
# -----------------------------------------------------------------
# Support for copy and rename
# -----------------------------------------------------------------
def delete(self):
"""
Delete the item from disc and the database.
:rtype: None
"""
path = self.path()
studiolibrary.removePath(path)
if self.database():
self.database().removePath(path)
if self.libraryWidget():
self.libraryWidget().refresh()
def copy(self, dst):
"""
Make a copy/duplicate the current item to the given destination.
:type dst: str
:rtype: None
"""
src = self.path()
path = studiolibrary.copyPath(src, dst)
self.setPath(path)
if self.database():
self.database().addPath(path)
def move(self, dst):
"""
Move the current item to the given destination.
:type dst: str
:rtype: None
"""
src = self.path()
dst = studiolibrary.movePath(src, dst)
self.setPath(dst)
if self.database():
self.database().renamePath(src, dst)
def rename(self, dst, extension=None, force=True):
"""
Rename the current path to given destination path.
:type dst: str
:type force: bool
:type extension: bool or None
:rtype: None
"""
src = self.path()
extension = extension or self.extension()
if dst and extension not in dst:
dst += extension
dst = studiolibrary.renamePath(src, dst)
self.setPath(dst)
if self.database():
self.database().renamePath(src, dst)
if self.libraryWidget():
self.libraryWidget().refreshSelection()
def showRenameDialog(self, parent=None):
"""
Show the rename dialog.
:type parent: QtWidgets.QWidget
"""
parent = parent or self.libraryWidget()
name, button = studioqt.MessageBox.input(
parent,
"Rename item",
"Rename the current item to:",
inputText=self.name()
)
if button == QtWidgets.QDialogButtonBox.Ok:
try:
self.rename(name)
except Exception, e:
self.showExceptionDialog("Rename Error", e)
raise
return button
def showMoveDialog(self, parent=None):
"""
Show the move to browser dialog.
:type parent: QtWidgets.QWidget
"""
title = "Move To..."
path = os.path.dirname(self.dirname())
dst = QtWidgets.QFileDialog.getExistingDirectory(parent, title, path)
if dst:
try:
self.move(dst)
except Exception, e:
self.showExceptionDialog("Move Error", e)
raise
def showDeleteDialog(self):
"""
Show the delete item dialog.
:rtype: None
"""
text = 'Are you sure you want to delete this item?'
button = self.showQuestionDialog("Delete Item", text)
if button == QtWidgets.QDialogButtonBox.Yes:
try:
self.delete()
except Exception, e:
self.showExceptionDialog("Delete Error", e)
raise
def showAlreadyExistsDialog(self):
"""
Show a warning dialog if the item already exists on save.
:rtype: None
"""
if not self.libraryWidget():
raise ItemSaveError("Item already exists!")
path = self.path()
title = "Item already exists"
text = 'Would you like to move the existing item "{}" to the trash?'
text = text.format(self.name())
button = self.libraryWidget().showTrashItemsDialog(
[self],
title=title,
text=text
)
if button == QtWidgets.QMessageBox.Cancel:
raise ItemSaveError("Saving was canceled.")
elif button != QtWidgets.QMessageBox.Yes:
raise ItemSaveError("You cannot save over an existing item.")
self.setPath(path)
# -----------------------------------------------------------------
# Support for painting the type icon
# -----------------------------------------------------------------
def typePixmap(self):
"""
Return the type pixmap for the plugin.
:rtype: QtWidgets.QPixmap
"""
if not self._typePixmap:
iconPath = self.typeIconPath()
if iconPath and os.path.exists(iconPath):
self._typePixmap = QtGui.QPixmap(iconPath)
return self._typePixmap
def typeIconRect(self, option):
"""
Return the type icon rect.
:rtype: QtGui.QRect
"""
padding = 2 * self.dpi()
r = self.iconRect(option)
x = r.x() + padding
y = r.y() + padding
rect = QtCore.QRect(x, y, 13 * self.dpi(), 13 * self.dpi())
return rect
def paintTypeIcon(self, painter, option):
"""
Draw the item type icon at the top left.
:type painter: QtWidgets.QPainter
:type option: QtWidgets.QStyleOptionViewItem
:rtype: None
"""
rect = self.typeIconRect(option)
typePixmap = self.typePixmap()
if typePixmap:
painter.setOpacity(0.5)
painter.drawPixmap(rect, typePixmap)
painter.setOpacity(1)
def paint(self, painter, option, index):
"""
Overriding the paint method to draw the tag icon and type icon.
:type painter: QtWidgets.QPainter
:type option: QtWidgets.QStyleOptionViewItem
:rtype: None
"""
studioqt.CombinedWidgetItem.paint(self, painter, option, index)
painter.save()
try:
if index.column() == 0:
self.paintTypeIcon(painter, option)
finally:
painter.restore()