CMS 3D CMS Logo

ImageExporter.py
Go to the documentation of this file.
1 import os.path
2 
3 from PyQt4 import QtCore, QtGui
4 
5 import logging
6 
7 class ImageExporter(QtCore.QObject):
8  def __init__(self, parent=None):
9  QtCore.QObject.__init__(self, parent)
10  self._imageFormats = ["pdf", "ps"] + [str(f) for f in QtGui.QImageWriter.supportedImageFormats()]
11  fileFilters = [self._fileFilter(ft) for ft in self._imageFormats]
12  self._fileFiltersString = ";;".join(fileFilters)
13  self._selectedFileFilter = fileFilters[0]
15 
16  def _fileFilter(self, ext):
17  return ext.upper() +" File (*."+ ext.lower() + ")"
18 
19  def exportPdfHelper(self, widget, painter, offset):
20  """ This function recursively does the painting for exportPdf().
21  """
22  picture = QtGui.QPicture()
23  QtGui.QPainter.setRedirected(widget, picture)
24  QtGui.QApplication.instance().sendEvent(widget, QtGui.QPaintEvent(widget.rect()))
25  QtGui.QPainter.restoreRedirected(widget)
26  painter.drawPicture(offset, picture)
27 
28  for child in widget.children():
29  if isinstance(child, QtGui.QWidget) and child.isVisible():
30  self.exportPdfHelper(child, painter, offset + child.pos())
31 
32  def exportPdf(self, widget, filename, format=QtGui.QPrinter.PdfFormat):
33  """ This function draws itself into a pdf file with given filename.
34 
35  format is any format type supported by QtGui.QPrinter.
36  """
37  logging.debug(self.__class__.__name__ +": exportPdf()")
38 
39  printer = QtGui.QPrinter(QtGui.QPrinter.ScreenResolution)
40  printer.setOutputFileName(filename)
41  printer.setOutputFormat(format)
42  printer.setFullPage(True)
43  printer.setPaperSize(QtCore.QSizeF(widget.size()), QtGui.QPrinter.DevicePixel)
44  painter = QtGui.QPainter()
45  painter.begin(printer)
46 
47  self.exportPdfHelper(widget, painter, QtCore.QPoint(0,0))
48  painter.end()
49 
50  def exportImageDialog(self, widget):
51  if self._exportImageFileName:
52  defaultname = self._exportImageFileName
53  else:
54  defaultname = QtCore.QCoreApplication.instance().getLastOpenLocation()
55 
56  filter = QtCore.QString(self._selectedFileFilter)
57  filename = str(QtGui.QFileDialog.getSaveFileName(self.parent(),"Save image...",defaultname, self._fileFiltersString, filter))
58  ext = str(filter).split(" ")[0].lower() # extract ext from selected filter
59  self.exportImage(widget, filename)
60 
61  def exportImage(self, widget, filename, ext=""):
62  self._exportImageFileName = filename
63  name = filename
64 
65  if os.path.splitext(filename)[1].lower().strip(".") in self._imageFormats:
66  name=os.path.splitext(filename)[0]
67  ext=os.path.splitext(filename)[1].lower().strip(".")
68 
69  self._selectedFileFilter = self._fileFilter(ext)
70  if ext == "pdf":
71  self.exportPdf(widget, name +"."+ ext, QtGui.QPrinter.PdfFormat)
72  elif ext == "ps":
73  self.exportPdf(widget, name +"."+ ext, QtGui.QPrinter.PostScriptFormat)
74  else:
75  childrenRect = widget.childrenRect()
76  margin = 10
77  picture = QtGui.QPixmap.grabWidget(widget, childrenRect.left() - margin, childrenRect.top() - margin, childrenRect.width() + 2*margin, childrenRect.height() + 2*margin)
78 
79  # grabWidget() does not work on Mac OS X with color gradients
80  # grabWidget() grabs pixels directly from screen --> can lead to problems if window is partly covered
81  picture.save(name+"."+ext,ext)
static std::string join(char **cmd)
Definition: RemoteFile.cc:18
def exportImage(self, widget, filename, ext="")
def exportPdfHelper(self, widget, painter, offset)
def exportPdf(self, widget, filename, format=QtGui.QPrinter.PdfFormat)
double split
Definition: MVATrainer.cc:139