from common import * class KPTilesetChooserDialog(QtGui.QDialog): def __init__(self, label='Choose a tileset', specials=None): QtGui.QDialog.__init__(self) self.label = QtGui.QLabel(label) self.label.setWordWrap(True) # can't be assed to create a model self.chooser = QtGui.QListWidget() self.nameList = KP.map.tilesets.keys() self.nameList.sort() # this piece of the API is kinda shitty but whatever self.specials = specials if specials: self.chooser.addItems(self.specials) self.chooser.addItems(self.nameList) self.chooser.currentRowChanged.connect(self.handleCurrentRowChanged) self.chooser.itemActivated.connect(self.handleItemActivated) self.buttons = QtGui.QDialogButtonBox( QtGui.QDialogButtonBox.Ok | QtGui.QDialogButtonBox.Cancel) self.okButton = self.buttons.button(QtGui.QDialogButtonBox.Ok) self.okButton.setEnabled(False) self.buttons.accepted.connect(self.accept) self.buttons.rejected.connect(self.reject) self.layout = QtGui.QVBoxLayout() self.layout.addWidget(self.label) self.layout.addWidget(self.chooser) self.layout.addWidget(self.buttons) self.setLayout(self.layout) def handleCurrentRowChanged(self, row): self.okButton.setEnabled(row != -1) def handleItemActivated(self, item): self.accept() def getChoice(self): item = self.chooser.currentItem() number = self.chooser.currentRow() if item is None: return None else: if self.specials is not None and number < len(self.specials): return number else: return str(item.text()) @classmethod def run(cls, *args): dialog = cls(*args) result = dialog.exec_() if result == QtGui.QDialog.Accepted: return dialog.getChoice() else: return None