Krita Source Code Documentation
Loading...
Searching...
No Matches
lastdocumentslistmodel.py
Go to the documentation of this file.
1# SPDX-License-Identifier: CC0-1.0
2
3try:
4 from PyQt6.QtCore import QAbstractListModel, Qt, QSize
5 from PyQt6.QtGui import QImage
6except:
7 from PyQt5.QtCore import QAbstractListModel, Qt, QSize
8 from PyQt5.QtGui import QImage
9from krita import Krita
10import zipfile
11from pathlib import Path
12
13
14class LastDocumentsListModel(QAbstractListModel):
15
16 def __init__(self, devicePixelRatioF, parent=None):
17 super(LastDocumentsListModel, self).__init__(parent)
18
19 self.rootItem = ('Path',)
22 self.devicePixelRatioF = devicePixelRatioF
23
24 def data(self, index, role):
25 if not index.isValid():
26 return None
27
28 if index.row() >= len(self.recentDocuments):
29 return None
30
31 if role == Qt.ItemDataRole.DecorationRole:
32 return self.recentDocuments[index.row()]
33 else:
34 return None
35
36 def rowCount(self, parent):
37 return len(self.recentDocuments)
38
39 def headerData(self, section, orientation, role):
40 if orientation == Qt.Orientation.Horizontal and role == Qt.ItemDataRole.DisplayRole:
41 return self.rootItem[section]
42
43 return None
44
46 self.recentDocuments = []
47 recentDocumentsPaths = self.kritaInstance.recentDocuments()
48
49 for path in recentDocumentsPaths:
50 if path:
51 thumbnail = None
52 extension = Path(path).suffix
53 page = None
54 if extension == '.kra':
55 page = zipfile.ZipFile(path, "r")
56 thumbnail = QImage.fromData(page.read("mergedimage.png"))
57 if thumbnail.isNull():
58 thumbnail = QImage.fromData(page.read("preview.png"))
59 else:
60 thumbnail = QImage(path)
61
62 if thumbnail.isNull():
63 continue
64
65 thumbSize = QSize(int(200*self.devicePixelRatioF), int(150*self.devicePixelRatioF))
66 if thumbnail.width() <= thumbSize.width() or thumbnail.height() <= thumbSize.height():
67 thumbnail = thumbnail.scaled(thumbSize, Qt.AspectRatioMode.KeepAspectRatio, Qt.TransformationMode.FastTransformation)
68 else:
69 thumbnail = thumbnail.scaled(thumbSize, Qt.AspectRatioMode.KeepAspectRatio, Qt.TransformationMode.SmoothTransformation)
70 thumbnail.setDevicePixelRatio(self.devicePixelRatioF)
71 self.recentDocuments.append(thumbnail)
72 self.modelReset.emit()
static Krita * instance()
instance retrieve the singleton instance of the Application object.
Definition Krita.cpp:390