diff options
Diffstat (limited to 'src/ui.py')
-rw-r--r-- | src/ui.py | 321 |
1 files changed, 174 insertions, 147 deletions
@@ -4,6 +4,7 @@ from common import * from editorui.editorcommon import * from editorui.editormain import * import os +import os.path class KPLayerList(QtGui.QWidget): @@ -13,22 +14,25 @@ class KPLayerList(QtGui.QWidget): self.layout = QtGui.QVBoxLayout() self.layout.setSpacing(0) - self.model = KP.map.layerModel - self.listView = QtGui.QListView() - self.listView.setModel(self.model) - self.listView.selectionModel().currentRowChanged.connect(self.handleRowChanged) self.layout.addWidget(self.listView) self.toolbar = QtGui.QToolBar() self.layout.addWidget(self.toolbar) self.setupToolbar(self.toolbar) - self.setButtonStates() + self.updateModel() self.setLayout(self.layout) + def updateModel(self): + self.model = KP.map.layerModel + self.listView.setModel(self.model) + self.listView.selectionModel().currentRowChanged.connect(self.handleRowChanged) + self.setButtonStates() + + def setupToolbar(self, tb): self.actAddTile = tb.addAction(KP.icon('LayerNewTile'), 'Add Tile Layer', self.addTileLayer) self.actAddDoodad = tb.addAction(KP.icon('LayerNewObjects'), 'Add Doodad Layer', self.addDoodadLayer) @@ -51,8 +55,13 @@ class KPLayerList(QtGui.QWidget): def setButtonStates(self): index = self.selectedLayerIndex() + layer = KP.map.layers[index] + + self.actRemove.setEnabled( + (index != -1) and + (len(KP.map.layers) > 1) and + (not isinstance(layer, KPPathLayer))) - self.actRemove.setEnabled((index != -1) and (len(KP.map.layers) > 1)) self.actMoveUp.setEnabled(index > 0) self.actMoveDown.setEnabled((index != -1) and (index < (len(KP.map.layers) - 1))) @@ -127,9 +136,6 @@ class KPLayerList(QtGui.QWidget): class KPDoodadSelector(QtGui.QWidget): - objChanged = QtCore.pyqtSignal(int, QtGui.QListWidgetItem) - - def __init__(self): """Initialises the widget.""" @@ -138,55 +144,62 @@ class KPDoodadSelector(QtGui.QWidget): self.layout = QtGui.QVBoxLayout() self.layout.setSpacing(0) - self.doodadList = QtGui.QListWidget() - self.doodadList.setViewMode(1) - self.doodadList.setWrapping(True) - self.doodadList.setDragDropMode(1) - self.doodadList.setSelectionMode(1) - self.doodadList.setResizeMode(1) - self.doodadList.setGridSize(QtCore.QSize(128, 128)) - self.doodadList.setIconSize(QtCore.QSize(100, 100)) - self.doodadList.setSpacing(4) + self.listView = QtGui.QListView() + self.listView.setViewMode(self.listView.IconMode) + self.listView.setWrapping(True) + self.listView.setDragDropMode(self.listView.DragOnly) + self.listView.setSelectionMode(self.listView.SingleSelection) + self.listView.setResizeMode(self.listView.Adjust) + self.listView.setGridSize(QtCore.QSize(128, 128)) + self.listView.setIconSize(QtCore.QSize(100, 100)) + self.listView.setSpacing(4) self.toolbar = QtGui.QToolBar() - self.layout.addWidget(self.toolbar) - self.addDoodadButton = self.toolbar.addAction(QtGui.QIcon(), 'Add', self.addDoodadfromFile) + self.addDoodadButton = self.toolbar.addAction(QtGui.QIcon(), 'Add', self.addDoodadFromFile) self.removeDoodadButton = self.toolbar.addAction(QtGui.QIcon(), 'Remove', self.removeDoodad) + self.updateModel() - self.layout.addWidget(self.doodadList) - + self.layout.addWidget(self.toolbar) + self.layout.addWidget(self.listView) self.setLayout(self.layout) - - self.nextIndex = 0 - - self.doodadList.currentItemChanged.connect(self.handleRowChanged) - + + + def updateModel(self): + self.model = KP.map.doodadModel + self.listView.setModel(self.model) + self.listView.selectionModel().currentRowChanged.connect(self.handleRowChanged) + self.setButtonStates() def keyPressEvent(self, event): - - self.doodadList.keyPressEvent(event) + self.listView.keyPressEvent(event) if event.key() == QtCore.Qt.Key_Delete or event.key() == QtCore.Qt.Key_Backspace: + doodad = self.selectedDoodad() + if doodad is None: + return + # TODO: Check if selected msgBox = QtGui.QMessageBox(QtGui.QMessageBox.Warning, "Delete Doodad?", "Are you sure you want to delete this doodad? This action cannot be undone.", QtGui.QMessageBox.NoButton, self) msgBox.addButton("Delete", QtGui.QMessageBox.AcceptRole) msgBox.addButton("Cancel", QtGui.QMessageBox.RejectRole) if msgBox.exec_() == QtGui.QMessageBox.AcceptRole: - self.removeDoodad() + KP.map.removeDoodad(doodad) def addDoodad(self, image, name): + # TODO: REMOVE """Takes a name and a QPixmap and turns it into an icon, then goes ahead and makes a doodad. Doodads are QListWidget items with an index number as Qt.UserRole #32.""" doodie = QtGui.QListWidgetItem(QtGui.QIcon(image), name) + # !! doodie.setSizeHint(QtCore.QSize(128,128)) doodie.setData(32, self.nextIndex) doodie.setToolTip(name) @@ -196,15 +209,13 @@ class KPDoodadSelector(QtGui.QWidget): self.nextIndex += 1 - def addDoodadfromFile(self): + def addDoodadFromFile(self): """Asks the user for files to load in as doodads.""" - log = QtGui.QFileDialog() - log.setFileMode(3); - files = QtGui.QFileDialog.getOpenFileNames(self, "Choose an image or several image files.", "", "Images (*.png *.jpeg *.jpg *.bmp)") + if files: for image in files: name = os.path.basename(unicode(image)).split('.')[0] @@ -212,54 +223,34 @@ class KPDoodadSelector(QtGui.QWidget): pix = QtGui.QPixmap() pix.load(image) - self.addDoodad(pix, name) + KP.map.addDoodad(name, pix) def removeDoodad(self): """Removes selected doodad""" - item = self.doodadList.currentRow() - - if item != -1: - self.doodadList.takeItem(item) - - - def getDoodad(self, index): - """Retrieves a doodad by index""" - index = QtCore.QVariant(index) - widget = self.doodadList - - for i in xrange(widget.count()): - item = widget.item(i) - if item.data(32) == index: - return item - - - def getDoodadImage(self, index, width, height): - """Retrieves a doodad pixmap by index""" - index = QtCore.QVariant(index) - widget = self.doodadList + item = self.selectedDoodad() + if item: + KP.map.removeDoodad(item) + - for i in xrange(widget.count()): - item = widget.item(i) - if item.data(32) == index: - return item.icon().pixmap(width, height) + def setButtonStates(self): + index = self.selectedDoodadIndex() - def getDoodads(self): - """Returns a list of all doodads. + self.removeDoodadButton.setEnabled(index != -1) - Name can be retrieved with doodad.text() - Image with doodad.icon().pixmap(doodad.icon().availableSizes()[0]) - Index with doodad.data(32)""" - # TODO: FIX THIS - return self.doodadList.items() + def selectedDoodadIndex(self): + return self.listView.selectionModel().currentIndex().row() + def selectedDoodad(self): + return KP.map.doodadDefinitions[self.listView.selectionModel().currentIndex().row()] + selectedDoodadChanged = QtCore.pyqtSignal(object) - @QtCore.pyqtSlot(QtGui.QListWidgetItem) - def handleRowChanged(self, current): - """Throws a signal emitting the current object when changed""" - self.objChanged.emit(current.data(32).toPyObject(), current) + @QtCore.pyqtSlot(QtCore.QModelIndex, QtCore.QModelIndex) + def handleRowChanged(self, current, previous): + self.selectedDoodadChanged.emit(KP.map.doodadDefinitions[current.row()]) + self.setButtonStates() class KPObjectSelector(QtGui.QWidget): @@ -423,6 +414,22 @@ class KPMainWindow(QtGui.QMainWindow): self.setupDocks() self.setupMenuBar() + self.refreshMapState() + + + def _createAction(self, internalName, callback, title): + act = QtGui.QAction(title, self) + act.triggered.connect(callback) + self.actions[internalName] = act + return act + + def setupActions(self): + self.actions = {} + + self._createAction('new', self.newMap, '&New') + self._createAction('open', self.openMap, '&Open...') + self._createAction('save', self.saveMap, '&Save') + self._createAction('saveAs', self.saveMapAs, 'Save &As...') def setupMenuBar(self): mb = self.menuBar() @@ -430,12 +437,12 @@ class KPMainWindow(QtGui.QMainWindow): from PyQt4.QtGui import QKeySequence f = mb.addMenu('&File') - self.fa = f.addAction('New') # N - self.fb = f.addAction('Open...') # O + self.fa = f.addAction('New', self.newMap, QKeySequence("Ctrl+N")) + self.fb = f.addAction('Open...', self.openMap, QKeySequence("Ctrl+O")) self.fc = f.addAction('Open Recent') # f.addSeparator() - self.fd = f.addAction('Save') # S - self.fe = f.addAction('Save As...') # Shift S + self.fd = f.addAction('Save', self.saveMap, QKeySequence("Ctrl+S")) + self.fe = f.addAction('Save As...', self.saveMapAs, QKeySequence("Ctrl+Shift+S")) self.ff = f.addAction('Export...') # E f.addSeparator() self.fg = f.addAction('Take Screenshot...', self.screenshot, QKeySequence("Ctrl+Alt+S")) @@ -461,7 +468,7 @@ class KPMainWindow(QtGui.QMainWindow): self.lg = l.addAction('Move Layer to Bottom', self.layerList.moveBottom, QKeySequence("Ctrl+Shift+Down")) l.addSeparator() self.lh = l.addAction('Add Tileset...', self.moveTilesetToFolder, QKeySequence("Ctrl+Shift+T")) - self.li = l.addAction('Add Doodad...', self.doodadSelector.addDoodadfromFile, QKeySequence("Ctrl+Shift+R")) + self.li = l.addAction('Add Doodad...', self.doodadSelector.addDoodadFromFile, QKeySequence("Ctrl+Shift+R")) a = mb.addMenu('Animate') self.aa = a.addAction('Play Animations', self.playAnim, QKeySequence("Ctrl+P")) @@ -479,9 +486,18 @@ class KPMainWindow(QtGui.QMainWindow): self.wd = w.addAction('Actual Size', self.ZoomActual, QKeySequence("Ctrl+=")) self.wh = w.addAction('Show Wii Zoom', self.showWiiZoom) w.addSeparator() - self.we = w.addAction('Hide Layer Palette', self.showHideLayer, QKeySequence("Ctrl+1")) - self.wf = w.addAction('Show Object Palette', self.showHideObject, QKeySequence("Ctrl+2")) - self.wg = w.addAction('Show Doodad Palette', self.showHideDoodad, QKeySequence("Ctrl+3")) + + layerAction = self.layerListDock.toggleViewAction() + layerAction.setShortcut(QKeySequence("Ctrl+1")) + w.addAction(layerAction) + + objectAction = self.objectSelectorDock.toggleViewAction() + objectAction.setShortcut(QKeySequence("Ctrl+2")) + w.addAction(objectAction) + + doodadAction = self.doodadSelectorDock.toggleViewAction() + doodadAction.setShortcut(QKeySequence("Ctrl+3")) + w.addAction(doodadAction) h = mb.addMenu('Help') self.ha = h.addAction('About Koopatlas') @@ -493,26 +509,22 @@ class KPMainWindow(QtGui.QMainWindow): self.layerList = KPLayerList() self.layerListDock = QtGui.QDockWidget('Layers') self.layerListDock.setWidget(self.layerList) - self.layerListDock.visibilityChanged.connect(self.showHideLayerDock) self.layerList.selectedLayerChanged.connect(self.handleSelectedLayerChanged) - self.layerList.playPaused.connect(self.scene.playPause) - self.layerList.playPaused.connect(self.playButtonChanged) + self.layerList.playPaused.connect(self.playAnim) self.objectSelector = KPObjectSelector() self.objectSelector.objChanged.connect(self.handleSelectedObjectChanged) self.objectSelectorDock = QtGui.QDockWidget('Objects') self.objectSelectorDock.setWidget(self.objectSelector) - self.objectSelectorDock.visibilityChanged.connect(self.showHideObjectDock) self.objectSelectorDock.hide() self.doodadSelector = KPDoodadSelector() - self.doodadSelector.objChanged.connect(self.handleSelectedDoodadChanged) + self.doodadSelector.selectedDoodadChanged.connect(self.handleSelectedDoodadChanged) self.doodadSelectorDock = QtGui.QDockWidget('Doodads') self.doodadSelectorDock.setWidget(self.doodadSelector) - self.doodadSelectorDock.visibilityChanged.connect(self.showHideDoodadDock) self.doodadSelectorDock.hide() self.addDockWidget(Qt.RightDockWidgetArea, self.layerListDock) @@ -520,6 +532,28 @@ class KPMainWindow(QtGui.QMainWindow): self.addDockWidget(Qt.RightDockWidgetArea, self.doodadSelectorDock) + def refreshMapState(self): + self.layerList.updateModel() + self.doodadSelector.updateModel() + + self.scene = KPMapScene() + self.editor.assignNewScene(self.scene) + self.updateTitlebar() + + def updateTitlebar(self): + path = KP.map.filePath + if path is None: + effectiveName = 'Untitled Map' + else: + effectiveName = os.path.basename(path) + + self.setWindowTitle('%s - Koopatlas' % effectiveName) + + def checkDirty(self): + return False + + + ##################### # Slots for Widgets # ##################### @@ -527,34 +561,31 @@ class KPMainWindow(QtGui.QMainWindow): @QtCore.pyqtSlot(KPLayer) def handleSelectedLayerChanged(self, layer): self.scene.setCurrentLayer(layer) + + showObjects, showDoodads = False, False if isinstance(layer, KPDoodadLayer): - self.objectSelectorDock.hide() - self.doodadSelectorDock.show() + showDoodads = True elif isinstance(layer, KPTileLayer): KP.map.reloadTileset(layer.tileset) - - self.doodadSelectorDock.hide() - self.objectSelectorDock.show() + showObjects = True self.objectSelector.setModel(KP.map.loadedTilesets[layer.tileset].getModel()) - - else: - self.objectSelectorDock.hide() - self.doodadSelectorDock.hide() + + self.objectSelectorDock.setVisible(showObjects) + self.doodadSelectorDock.setVisible(showDoodads) @QtCore.pyqtSlot(int, KPTileObject) def handleSelectedObjectChanged(self, index, obj): - self.editor.paintNext = obj - self.editor.paintNextID = index + self.editor.objectToPaint = obj + self.editor.objectIDToPaint = index - @QtCore.pyqtSlot(int, QtGui.QListWidgetItem) - def handleSelectedDoodadChanged(self, index, obj): - self.editor.paintNext = obj - self.editor.paintNextID = index + @QtCore.pyqtSlot(object) + def handleSelectedDoodadChanged(self, doodad): + self.editor.doodadToPaint = doodad ######################## @@ -563,6 +594,48 @@ class KPMainWindow(QtGui.QMainWindow): # File ######################## + def newMap(self): + if self.checkDirty(): return + + KP.map = KPMap() + self.refreshMapState() + + def openMap(self): + if self.checkDirty(): return + + target = unicode(QtGui.QFileDialog.getOpenFileName( + self, 'Open Map', '', 'Koopatlas map (*.kpmap)')) + + if len(target) == 0: + return + + import mapfile + obj = mapfile.load(open(target, 'rb').read()) + obj.filePath = target + KP.map = obj + self.refreshMapState() + + def saveMap(self, forceNewName=False): + target = KP.map.filePath + + if target is None or forceNewName: + dialogDir = '' if target is None else os.path.dirname(target) + target = unicode(QtGui.QFileDialog.getSaveFileName( + self, 'Save Map', dialogDir, 'Koopatlas map (*.kpmap)')) + + if len(target) == 0: + return + + KP.map.filePath = target + self.updateTitlebar() + + KP.map.save() + + + + def saveMapAs(self): + self.saveMap(True) + @QtCore.pyqtSlot() @@ -650,7 +723,7 @@ class KPMainWindow(QtGui.QMainWindow): @QtCore.pyqtSlot() def playButtonChanged(self): - if self.scene.playing == True: + if self.scene.playing: self.aa.setText('Stop Animations') self.layerList.actPlayPause.setIcon(KP.icon('AStop')) self.layerList.actPlayPause.setText('Stop') @@ -670,52 +743,6 @@ class KPMainWindow(QtGui.QMainWindow): # Window ######################## - @QtCore.pyqtSlot(bool) - def showHideLayerDock(self, visible): - if visible: - self.we.setText('Hide Layer Palette') - else: - self.we.setText('Show Layer Palette') - - - @QtCore.pyqtSlot(bool) - def showHideObjectDock(self, visible): - if visible: - self.wf.setText('Hide Object Palette') - else: - self.wf.setText('Show Object Palette') - - - @QtCore.pyqtSlot(bool) - def showHideDoodadDock(self, visible): - if visible: - self.wg.setText('Hide Doodad Palette') - else: - self.wg.setText('Show Doodad Palette') - - - @QtCore.pyqtSlot() - def showHideLayer(self): - if self.layerListDock.isVisible(): - self.layerListDock.hide() - else: - self.layerListDock.show() - - @QtCore.pyqtSlot() - def showHideObject(self): - if self.objectSelectorDock.isVisible(): - self.objectSelectorDock.hide() - else: - self.objectSelectorDock.show() - - @QtCore.pyqtSlot() - def showHideDoodad(self): - if self.doodadSelectorDock.isVisible(): - self.doodadSelectorDock.hide() - else: - self.doodadSelectorDock.show() - - @QtCore.pyqtSlot() def ZoomActual(self): """Handle zooming to the editor size""" |