def __init__(self, parent=None): QtGui.QWidget.__init__(self, parent) self.ui = Ui_MainWindow() self.ui.setupUi(self) # set the list model self.imageModel = ImageListModel() self.ui.imageList.setModel(self.imageModel) self.itemDelegate = ImageDelegate() self.ui.imageList.setItemDelegate(self.itemDelegate) # also for current behavior self.behaviorModel = BehaviorListModel(self.imageModel) self.ui.currentBehavior.setModel(self.behaviorModel) self.ui.currentBehavior.setItemDelegate(self.itemDelegate) # create context menu for image list self.ui.imageList.setContextMenuPolicy(QtCore.Qt.ActionsContextMenu) self.ui.imageList.addAction(self.ui.actionDelete_Image) # enable drag and drop self.ui.imageList.setDragEnabled(True) self.ui.currentBehavior.setAcceptDrops(True) self.ui.currentBehavior.setDropIndicatorShown(True) self.ui.currentBehavior.setDragDropMode(QtGui.QAbstractItemView.DropOnly) # connect actions self.connect(self.ui.actionImport_Images, QtCore.SIGNAL('triggered()'), self.importImages) self.connect(self.ui.imageList.selectionModel(), QtCore.SIGNAL("selectionChanged(QItemSelection, QItemSelection)"), self.imageListSelectionChanged) self.ui.actionDelete_Image.setEnabled(False)
class MainWindow(QtGui.QMainWindow): def __init__(self, parent=None): QtGui.QWidget.__init__(self, parent) self.ui = Ui_MainWindow() self.ui.setupUi(self) # set the list model self.imageModel = ImageListModel() self.ui.imageList.setModel(self.imageModel) self.itemDelegate = ImageDelegate() self.ui.imageList.setItemDelegate(self.itemDelegate) # also for current behavior self.behaviorModel = BehaviorListModel(self.imageModel) self.ui.currentBehavior.setModel(self.behaviorModel) self.ui.currentBehavior.setItemDelegate(self.itemDelegate) # create context menu for image list self.ui.imageList.setContextMenuPolicy(QtCore.Qt.ActionsContextMenu) self.ui.imageList.addAction(self.ui.actionDelete_Image) # enable drag and drop self.ui.imageList.setDragEnabled(True) self.ui.currentBehavior.setAcceptDrops(True) self.ui.currentBehavior.setDropIndicatorShown(True) self.ui.currentBehavior.setDragDropMode(QtGui.QAbstractItemView.DropOnly) # connect actions self.connect(self.ui.actionImport_Images, QtCore.SIGNAL('triggered()'), self.importImages) self.connect(self.ui.imageList.selectionModel(), QtCore.SIGNAL("selectionChanged(QItemSelection, QItemSelection)"), self.imageListSelectionChanged) self.ui.actionDelete_Image.setEnabled(False) def importImages(self): images = QtGui.QFileDialog.getOpenFileNames(self, "Select images to import", "", "Images (*.png *.xpm *.jpg)") for i in images: self.importImage(i) #self.imageModel.emit(QtCore.SIGNAL("dataChanged(const QModelIndex &, const QModelIndex &)"), 1, 0) self.imageModel.onDataChanged() self.recreatePackedTexture() def imageListSelectionChanged(self, newSel, oldSel): if newSel.count() == 0: self.ui.actionDelete_Image.setEnabled(False) else: self.ui.actionDelete_Image.setEnabled(True) def importImage(self, fileName): try: image = PILImage.open(str(fileName)) image.load() image = image.convert("RGBA") except: QtGui.QMessageBox.warning(self, "Unable to load image", "Unable to load " + fileName) return self.imageModel.addImage(Image(image), os.path.basename(str(fileName))) def recreatePackedTexture(self): #create list of images imageRects = [] for k,v in self.imageModel.images.iteritems(): imageRects.append(v) rs = RectStorage() rs.packRectangles(imageRects) outImage = PILImage.new("RGBA", rs.dimensions) for ri in rs.rects: outImage.paste(ri.image, (ri.x, ri.y, ri.x + ri.width, ri.y + ri.height)) qimage = PILImageToQImage(outImage) pixmap = QtGui.QPixmap(qimage) self.ui.outTexture.setPixmap(pixmap)