def setBackgroundImage(self, imagefilename): # code taken from the backgroundImage.cpp file that is part of the QGLViewer library self.u_max = 1.0 self.v_max = 1.0 # load image img = QtGui.QImage(imagefilename) if img.isNull(): QtCore.qWarning("Unable to load file, unsupported file format") return # QtCore.qWarning("Loading " + imagefilename + " " + str(img.width()) + "x" + str(img.height()) +" pixels") self.imageWidth = float(img.width()) self.imageHeight = float(img.height()) # QQQ QtCore.qWarning(str(self.imageWidth) + " " + str(self.imageHeight)) # 1E-3 needed. Just try with width=128 and see ! newWidth = 1<<(int)(1+log(img.width() -1+1E-3) / log(2.0)) newHeight = 1<<(int)(1+log(img.height()-1+1E-3) / log(2.0)) self.u_max = img.width() / float(newWidth) self.v_max = img.height() / float(newHeight) if ((img.width()!=newWidth) or (img.height()!=newHeight)): #QtCore.qWarning("Image size set to " + str(newWidth) + "x" + str(newHeight) + " pixels") img = img.copy(0, 0, newWidth, newHeight) glImg = QtGui.QImage(QtOpenGL.QGLWidget.convertToGLFormat(img)) # flipped 32bit RGBA # Bind the img texture... glTexImage2D(GL_TEXTURE_2D, 0, 4, glImg.width(), glImg.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, glImg.bits().asstring(glImg.numBytes())) # Another way to bind img texture: # if self.textureId: # self.deleteTexture(self.textureId) # self.textureId = None # self.textureId = self.bindTexture(img,GL_TEXTURE_2D,GL_RGBA) # if self.textureId : # glBindTexture(GL_TEXTURE_2D, self.textureId) self.bgimage = True
def __init__(self, project, parent=None): super(Preview, self).__init__(parent) wanted_size = 50 self.project = project layout = QtGui.QGridLayout() icon_name = ":/images/resources/openalea_icon2.png" if len(project.icon): if project.icon[0] is not ":": # local icon icon_name = project.path / project.icon # else native icon from oalab.gui.resources image = QtGui.QImage(icon_name) label = QtGui.QLabel() label.setPixmap(QtGui.QPixmap(image)) size = image.size() if size.height() > wanted_size or size.width() > wanted_size: # Auto-rescale if image is bigger than wanted_size x wanted_size label.setScaledContents(True) label.setMinimumSize(wanted_size, wanted_size) label.setMaximumSize(wanted_size, wanted_size) layout.addWidget(label, 0, 0) layout.addWidget( QtGui.QLabel("<b><FONT SIZE = 40>" + pretty_print(project.name) + "<\b>"), 0, 1) i = 1 for label in Project.DEFAULT_METADATA: layout.addWidget(QtGui.QLabel(label), i, 0) # GBY Review: # QLabel expects a QString and QString is equivalent to unicode # so you must convert str to unicode to support non ASCII characters correctly (for example accent in author's name) # If project meta-info encoding is utf-8, you can write projet.author.decode('utf-8') # Just put accents or greek characters in test data to check such problems # GBY Review: if amount of metainfo grows, QTextEdit can be more convenient layout.addWidget( QtGui.QLabel(pretty_print(getattr(project, label))), i, 1) i += 1 layout.addWidget(QtGui.QLabel("Items:"), i, 0, 1, 2) model_list = QtGui.QTextEdit() layout.addWidget(model_list, i + 1, 0, 1, 2) model_list.setText(html_item_summary(project)) open_button = QtGui.QPushButton("Open this project") open_button.clicked.connect(self.on_project_opened) layout.addWidget(open_button, i + 2, 0) self.setLayout(layout)
def timerEvent(self, event): # -- compute the view rect -- rect = self.scene.sceneRect() scale = self.scale or rect.width() / self.width transfo = QtGui.QTransform.fromScale(scale, scale) rect = transfo.mapRect(rect).toRect() # -- our canvas filled with white -- self.image = QtGui.QImage(rect.width(), rect.height(), QtGui.QImage.Format_RGB32) self.image.fill(0xFFFFFFFF) painter = QtGui.QPainter(self.image) painter.setRenderHint(QtGui.QPainter.Antialiasing) self.scene.render(painter) self.image.save(self.filename + '.png', "png") self.killTimer(self.timerId) QtGui.QApplication.exit()