source: orange/Orange/OrangeCanvas/preview/previewmodel.py @ 11139:3cab9e06f6e5

Revision 11139:3cab9e06f6e5, 4.5 KB checked in by Ales Erjavec <ales.erjavec@…>, 18 months ago (diff)

Fixes for unicode support.

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