source: orange/Orange/OrangeCanvas/preview/previewmodel.py @ 11114:16262014a6ea

Revision 11114:16262014a6ea, 4.2 KB checked in by Ales Erjavec <ales.erjavec@…>, 18 months ago (diff)

Added preview browser widget and dialog.

Line 
1"""
2Preview item model.
3"""
4
5from PyQt4.QtGui import (
6    QStandardItemModel, QStandardItem, QStyledItemDelegate, QIcon
7)
8
9from PyQt4.QtCore import Qt, QTimer
10
11from . import scanner
12
13# Preview Data Roles
14####################
15
16# Name of the item, (same as `Qt.DisplayRole`)
17NameRole = Qt.DisplayRole
18
19# Items description (items long description)
20DescriptionRole = Qt.UserRole + 1
21
22# Items url/path (where previewed resource is located).
23PathRole = Qt.UserRole + 2
24
25# Items preview SVG contents string
26ThumbnailSVGRole = Qt.UserRole + 3
27
28
29UNKNOWN_SVG = \
30"""<?xml version="1.0" encoding="UTF-8" standalone="no"?>
31<svg width="161.8mm" height="100.0mm"
32 xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"
33 version="1.2" baseProfile="tiny">
34</svg>
35"""
36
37
38class PreviewModel(QStandardItemModel):
39    """A model for preview items.
40    """
41
42    def __init__(self, parent=None, items=None):
43        QStandardItemModel.__init__(self, parent)
44
45        if items is not None:
46            self.insertColumn(0, items)
47
48    def delayedScanUpdate(self, delay=10):
49        """Run a delayed preview item scan update.
50        """
51        def iter_update(items):
52            for item in items:
53                scanner.scan_update(item)
54                yield
55
56        items = [self.item(i) for i in range(self.rowCount())]
57
58        iter_scan = iter_update(items)
59
60        def process_one():
61            try:
62                iter_scan.next()
63                QTimer.singleShot(delay, process_one)
64            except StopIteration:
65                pass
66
67        QTimer.singleShot(delay, process_one)
68
69
70class PreviewItem(QStandardItem):
71    """A preview item.
72    """
73    def __init__(self, name=None, description=None, thumbnail=None,
74                 icon=None, path=None):
75        QStandardItem.__init__(self)
76
77        if name is None:
78            name = "Untitled"
79        self.setName(name)
80
81        if description is None:
82            description = "No description."
83        self.setDescription(description)
84
85        if thumbnail is None:
86            thumbnail = UNKNOWN_SVG
87        self.setThumbnail(thumbnail)
88
89        if icon is not None:
90            self.setIcon(icon)
91
92        if path is not None:
93            self.setPath(path)
94
95    def name(self):
96        """Return the name (title) of the item (same as `text()`.
97        """
98        return self.text()
99
100    def setName(self, value):
101        """Set the name.
102        """
103        self.setText(value)
104
105    def description(self):
106        """Return the detailed description for the item.
107
108        This is stored as `DescriptionRole`, if no data is set then
109        return the string for `WhatsThisRole`.
110
111        """
112        desc = self.data(DescriptionRole)
113
114        if desc.isValid():
115            return desc.toString()
116
117        whatsthis = self.data(Qt.WhatsThisRole)
118        return whatsthis.toString()
119
120    def setDescription(self, description):
121        self.setData(description, DescriptionRole)
122        self.setWhatsThis(description)
123
124    def thumbnail(self):
125        """Return the thumbnail SVG string for the preview item.
126
127        This is stored as `ThumbnailSVGRole`
128        """
129        thumb = self.data(ThumbnailSVGRole)
130        if thumb.isValid():
131            return thumb.toString()
132
133    def setThumbnail(self, thumbnail):
134        """Set the thumbnail SVG contents as a string.
135
136        When set it also overrides the icon role.
137
138        """
139        self.setData(thumbnail, ThumbnailSVGRole)
140
141        # TODO: how to set the icon from the svg contents string
142        # without writing it to disc
143        import tempfile
144        with tempfile.NamedTemporaryFile(
145                prefix="thumbnail", suffix=".svg", delete=False
146                ) as f:
147            f.write(thumbnail)
148            f.flush()
149            icon = QIcon(f.name)
150            self.setIcon(icon)
151
152    def path(self):
153        """Return the path item data.
154        """
155        return self.data(PathRole).toString()
156
157    def setPath(self, path):
158        """Set the path data of the item.
159
160        .. note:: This also sets the Qt.StatusTipRole
161
162        """
163        self.setData(path, PathRole)
164        self.setStatusTip(path)
165        self.setToolTip(path)
166
167
168class PreviewItemDelegate(QStyledItemDelegate):
169    def __init__(self, ):
170        QStyledItemDelegate.__init__(self,)
Note: See TracBrowser for help on using the repository browser.