def add_image_data_resource(self, imgdata, imageFileName): self.abuffer = QBuffer() self.abuffer.open(QBuffer.ReadWrite) self.abuffer.write(imgdata.getvalue()) self.abuffer.close() self.abuffer.open(QBuffer.ReadOnly) iReader = QImageReader(self.abuffer, img_format ) the_image = iReader.read() # the_image = the_image0.scaledToWidth(figure_width) # save the image in a file # imageFileName = "image" + str(self.image_counter) + "." + img_format self.image_data_dict[imageFileName] = imgdata # self.image_counter +=1 imageFormat = QTextImageFormat() imageFormat.setName(imageFileName) imageFormat.setWidth(image_width) self.image_dict[imageFileName] = the_image #insert the image in the text document text_doc = self._teditor.document() text_doc.addResource(QTextDocument.ImageResource, QUrl(imageFileName), the_image)
def append_image(self, fig=None): #This assumes that an image is there waiting to be saved from matplotlib self.imgdata = StringIO.StringIO() if fig is None: pyplot.savefig(self.imgdata, transparent = False, format = img_format) else: fig.savefig(self.imgdata, transparent = False, format = img_format) self.abuffer = QBuffer() self.abuffer.open(QBuffer.ReadWrite) self.abuffer.write(self.imgdata.getvalue()) self.abuffer.close() self.abuffer.open(QBuffer.ReadOnly) iReader = QImageReader(self.abuffer, img_format ) the_image = iReader.read() # the_image = the_image0.scaledToWidth(figure_width) # save the image in a file imageFileName = "image" + str(self.image_counter) + "." + img_format self.image_data_dict[imageFileName] = self.imgdata self.image_counter +=1 imageFormat = QTextImageFormat() imageFormat.setName(imageFileName) imageFormat.setWidth(image_width) self.image_dict[imageFileName] = the_image #insert the image in the text document text_doc = self._teditor.document() text_doc.addResource(QTextDocument.ImageResource, QUrl(imageFileName), the_image) cursor = self._teditor.textCursor() cursor.movePosition(QTextCursor.End) cursor.insertImage(imageFormat)
def _set_image(self, v): if v != self._count: self._count = v if v in self._images: pix = self._images[v] else: reader = QImageReader(self._path) c = v % self._column_cnt r = v / self._column_cnt w, h = 22, 22 reader.setClipRect(QRect(c * w, r * h, w, h)) pix = QPixmap() pix.convertFromImage(reader.read()) self._images[v] = pix self.label.setPixmap(pix)
def load_icons(self): for format in QImageReader.supportedImageFormats(): self.supported_image_formats.add('.' + str(format)) it = QDirIterator(":", QDirIterator.Subdirectories) while it.hasNext(): path = it.next() fn, ext = os.path.splitext(path) if ext in self.supported_image_formats: image = QImage(path) self.icons.append(image) self.paths.append(path)
def icon(name, extension=None, style=None, use_inheritance=True, allow_theme=True, _always_return=True): """ Find an icon with the given ``name`` and ``extension`` and return a :class:`PySide.QtGui.QIcon` for that icon. ================ =========== ============ Argument Default Description ================ =========== ============ name The name of the icon to load. extension The desired filename extension of the icon to load. If this isn't set, a list of supported formats will be used. style The style to load the icon from. If this isn't set, the current style will be assumed. use_inheritance ``True`` Whether or not to search the parent style if the given style doesn't contain an icon. allow_theme ``True`` Whether or not to fall back on Qt icon themes if an icon cannot be found. ================ =========== ============ """ if style: if isinstance(style, basestring): style = addons.get('style', style) elif not isinstance(style, StyleInfo): raise TypeError("Can only activate StyleInfo instances!") else: style = _current_style # If we don't have a style, return a null icon now. if not style: return QIcon() # Right, time to find the icon. if isinstance(extension, (tuple, list)): extensions = extension elif extension: extensions = [extension] else: extensions = (str(ext) for ext in QImageReader.supportedImageFormats()) # Iteration powers, activate! for ext in extensions: filename = '%s.%s' % (name, ext) icon_path = path.join('images', filename) if style.path.exists(icon_path): # We've got it, but what is it? if (not isinstance(style.path_source, basestring) or style.path_source.startswith('py:')): # pkg_resource! Do things the fun and interesting way. with style.path.open(icon_path) as f: pixmap = QPixmap() pixmap.loadFromData(f.read()) return QIcon(pixmap) # Just a regular file. Open normally. return QIcon(style.path.abspath(icon_path)) # Still here? We didn't find our icon then. If we're inheriting, then call # icon again for the style we inherit from. if use_inheritance and style.inherits: for parent in style.inherits: result = icon(name, extension, parent, True, False, False) if result: return result # For one last try, see if we can use the theme icons. if allow_theme and QIcon.hasThemeIcon(name): return QIcon.fromTheme(name) # We don't have an icon. Return a null QIcon. if _always_return: return QIcon()