source: orange/Orange/OrangeCanvas/preview/previewmodel.py @ 11189:6e8b14ab7b9a

Revision 11189:6e8b14ab7b9a, 4.8 KB checked in by Ales Erjavec <ales.erjavec@…>, 17 months ago (diff)

Preserve empty scheme title, display 'untitled' placeholders where appropriate.

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        self.__name = ""
88
89        if name is None:
90            name = "Untitled"
91
92        self.setName(name)
93
94        if description is None:
95            description = "No description."
96        self.setDescription(description)
97
98        if thumbnail is None:
99            thumbnail = UNKNOWN_SVG
100        self.setThumbnail(thumbnail)
101
102        if icon is not None:
103            self.setIcon(icon)
104
105        if path is not None:
106            self.setPath(path)
107
108    def name(self):
109        """Return the name (title) of the item (same as `text()`.
110        """
111        return self.__name
112
113    def setName(self, value):
114        """Set the item name. `value` if not empty will be used as
115        the items DisplayRole otherwise an 'untitled' placeholder will
116        be used.
117
118        """
119        self.__name = value
120
121        if not value:
122            self.setText("untitled")
123        else:
124            self.setText(value)
125
126    def description(self):
127        """Return the detailed description for the item.
128
129        This is stored as `DescriptionRole`, if no data is set then
130        return the string for `WhatsThisRole`.
131
132        """
133        desc = self.data(DescriptionRole)
134
135        if desc.isValid():
136            return desc.toString()
137
138        whatsthis = self.data(Qt.WhatsThisRole)
139        return whatsthis.toString()
140
141    def setDescription(self, description):
142        self.setData(description, DescriptionRole)
143        self.setWhatsThis(description)
144
145    def thumbnail(self):
146        """Return the thumbnail SVG string for the preview item.
147
148        This is stored as `ThumbnailSVGRole`
149        """
150        thumb = self.data(ThumbnailSVGRole)
151        if thumb.isValid():
152            return thumb.toString()
153
154    def setThumbnail(self, thumbnail):
155        """Set the thumbnail SVG contents as a string.
156
157        When set it also overrides the icon role.
158
159        """
160        self.setData(thumbnail, ThumbnailSVGRole)
161
162        # TODO: how to set the icon from the svg contents string
163        # without writing it to disc
164        import tempfile
165        with tempfile.NamedTemporaryFile(
166                prefix="thumbnail", suffix=".svg", delete=False
167                ) as f:
168            f.write(thumbnail)
169            f.flush()
170            icon = QIcon(f.name)
171            self.setIcon(icon)
172
173    def path(self):
174        """Return the path item data.
175        """
176        return self.data(PathRole).toString()
177
178    def setPath(self, path):
179        """Set the path data of the item.
180
181        .. note:: This also sets the Qt.StatusTipRole
182
183        """
184        self.setData(path, PathRole)
185        self.setStatusTip(path)
186        self.setToolTip(path)
187
188
189class PreviewItemDelegate(QStyledItemDelegate):
190    def __init__(self, ):
191        QStyledItemDelegate.__init__(self,)
Note: See TracBrowser for help on using the repository browser.