diff options
author | Treeki <treeki@gmail.com> | 2011-11-04 00:04:26 +0100 |
---|---|---|
committer | Treeki <treeki@gmail.com> | 2011-11-04 00:04:26 +0100 |
commit | 2015541472eb19b8039e98bfeec3aa5678dd0b75 (patch) | |
tree | f101114b55d27883d0ca4681602e12d05a4b4c26 | |
parent | fd78999a93710ba53c915779c126e664323a728f (diff) | |
download | koopatlas-2015541472eb19b8039e98bfeec3aa5678dd0b75.tar.gz koopatlas-2015541472eb19b8039e98bfeec3aa5678dd0b75.zip |
finally fixed the layer models and such crap
-rw-r--r-- | src/mapdata.py | 87 | ||||
-rw-r--r-- | src/ui.py | 53 |
2 files changed, 103 insertions, 37 deletions
diff --git a/src/mapdata.py b/src/mapdata.py index 2a19da6..c82021b 100644 --- a/src/mapdata.py +++ b/src/mapdata.py @@ -1,4 +1,9 @@ +from common import * + class KPLayer(object): + def __repr__(self): + return "<KPLayer %r>" % self.name + def __init__(self): self.name = '' self.tileset = '' @@ -42,13 +47,95 @@ class KPMap(object): self.nextLayerNumber = 1 self.layers = [self.createNewLayer() for x in range(4)] + self.layerModel = KPMap.LayerModel(self.layers) self.nodes = [] self.paths = [] self.doodads = [] + + # LAYERS + class LayerModel(QtCore.QAbstractListModel): + def __init__(self, layerList): + QtCore.QAbstractListModel.__init__(self) + self.list = layerList + + + def headerData(self, section, orientation, role = Qt.DisplayRole): + return 'Layer' + + def rowCount(self, parent): + return len(self.list) + + def data(self, index, role = Qt.DisplayRole): + try: + if (role == Qt.DisplayRole or role == Qt.EditRole) and index.isValid(): + return self.list[index.row()].name + except IndexError: + pass + + return QtCore.QVariant() + + def flags(self, index): + if not index.isValid(): + return Qt.ItemIsEnabled + + return Qt.ItemIsEditable \ + | QtCore.QAbstractListModel.flags(self, index) + + def setData(self, index, value, role = Qt.EditRole): + if index.isValid() and role == Qt.EditRole: + value = str(value.toString()) + if len(value) > 0: + self.list[index.row()].name = value + self.dataChanged.emit(index, index) + return True + + return False + + def createNewLayer(self): layer = KPLayer() layer.name = "Layer %d" % self.nextLayerNumber self.nextLayerNumber += 1 return layer + def appendLayer(self, layer): + return self.insertLayer(len(self.layers), layer) + + def insertLayer(self, index, layer): + self.layerModel.beginInsertRows(QtCore.QModelIndex(), index, index) + self.layers.insert(index, layer) + self.layerModel.endInsertRows() + return index + + def moveLayer(self, fromIndex, toIndex): + if fromIndex == toIndex: + return + if fromIndex < 0 or toIndex < 0: + raise ValueError + if fromIndex >= len(self.layers) or toIndex > len(self.layers): + raise ValueError + + self.layerModel.beginMoveRows( + QtCore.QModelIndex(), fromIndex, fromIndex, + QtCore.QModelIndex(), toIndex) + + toMove = self.layers[fromIndex] + newIndex = ((toIndex > fromIndex) and (toIndex - 1)) or toIndex + + del self.layers[fromIndex] + self.layers.insert(newIndex, toMove) + + self.layerModel.endMoveRows() + + return newIndex + + def removeLayer(self, layer): + if layer not in self.layers: + raise ValueError + + index = self.layers.index(layer) + self.layerModel.beginRemoveRows(QtCore.QModelIndex(), index, index) + del self.layers[index] + self.layerModel.endRemoveRows() + @@ -3,38 +3,6 @@ from editorui import * class KPLayerList(QtGui.QWidget): - class LayerModel(QtCore.QAbstractListModel): - def headerData(self, section, orientation, role = Qt.DisplayRole): - return 'Layer' - - def rowCount(self, parent): - return len(KP.map.layers) - - def data(self, index, role = Qt.DisplayRole): - try: - if (role == Qt.DisplayRole or role == Qt.EditRole) and index.isValid(): - return KP.map.layers[index.row()].name - except IndexError: - pass - - return QtCore.QVariant() - - def flags(self, index): - if not index.isValid(): - return Qt.ItemIsEnabled - - return Qt.ItemIsEditable \ - | QtCore.QAbstractListModel.flags(self, index) - - def setData(self, index, value, role = Qt.EditRole): - if index.isValid() and role == Qt.EditRole: - value = str(value.toString()) - if len(value) > 0: - KP.map.layers[index.row()].name = value - self.dataChanged.emit(index, index) - return True - - return False def __init__(self): @@ -43,7 +11,7 @@ class KPLayerList(QtGui.QWidget): self.layout = QtGui.QVBoxLayout() self.layout.setSpacing(0) - self.model = KPLayerList.LayerModel() + self.model = KP.map.layerModel self.listView = QtGui.QListView() self.listView.setModel(self.model) @@ -64,14 +32,25 @@ class KPLayerList(QtGui.QWidget): tb.addAction(QtGui.QIcon(), 'Move Down', self.moveDown) + def selectedLayerIndex(self): + return self.listView.selectionModel().currentIndex().row() + def selectedLayer(self): + return KP.map.layers[self.listView.selectionModel().currentIndex().row()] + + def addLayer(self): - pass + KP.map.appendLayer(KP.map.createNewLayer()) + def removeLayer(self): - pass + KP.map.removeLayer(self.selectedLayer()) + def moveUp(self): - pass + index = self.selectedLayerIndex() + KP.map.moveLayer(index, index - 1) + def moveDown(self): - pass + index = self.selectedLayerIndex() + KP.map.moveLayer(index, index + 2) class KPMainWindow(QtGui.QMainWindow): |