'Align nodes to a grid.'

def alignToGrid(self):

    tile_size = 150
    tiles = {}
    nodes = sorted(self.scheme().nodes, key=attrgetter('position'))
    if nodes:
        self.__undoStack.beginMacro(self.tr('Align    To    Grid'))
        for node in nodes:
            (x, y) = node.position
            x = int((round((float(x) / tile_size)) * tile_size))
            y = int((round((float(y) / tile_size)) * tile_size))
            while ((x, y) in tiles):
                x += tile_size
            self.__undoStack.push(commands.MoveNodeCommand(self.scheme(), node, node.position, (x, y)))
            tiles[(x, y)] = node
            self.__scene.item_for_node(node).setPos(x, y)
        self.__undoStack.endMacro()
