<commit_before># -*- coding: utf-8-*-

"""
Author: <NAME> <<EMAIL>>
Copyright © 2008-2009
"""
from mdlib.log import ConsoleLogger, DEBUG
logger = ConsoleLogger("track-editor", DEBUG)

from PyQt4.QtCore import *
from PyQt4.QtGui import *

from gui.autogen import *
from gui.qt.plugins.tileeditorview import *

from preview import TrackGenerator

import sys, string, cPickle
from copy import deepcopy
from subprocess import Popen

import echo

class GUI(QMainWindow):
    def __init__(self, controller, *args):
        apply(QMainWindow.__init__, (self,) + args)
        self.controller = controller
        self._setupUi()
    
    def __getattr__(self,attr):
        try:
            return self.__dict__[attr]
        except KeyError, e:
            try:
                return self._mainWin.__dict__[attr]
            except KeyError, e:
                return self._tools.__dict__[attr]
      
    def _setupUi(self):
        self.setWindowTitle("Test")
        self.setGeometry(0,0,800,600)
        
        self._mainWin = Ui_MainWindow()
        self._mainWin.setupUi(self)
        
        # put the central widget in a layout. Qt Designer doesn't allow it :/
        layout = QBoxLayout(QBoxLayout.LeftToRight)
        layout.addWidget(self.tileEditorViewContainer)
        self.centralwidget.setLayout(layout)
        
        self._tools = Ui_toolsDock()
        toolsDock = QDockWidget("Tools", self)
        self._tools.setupUi(toolsDock)
        self.addDockWidget(QtCore.Qt.RightDockWidgetArea, toolsDock)
        
        # directions
        self.connect(self._tools.actionForward, \
                 SIGNAL("toggled(bool)"),self.controller.toggleDirectionForward)
        self.connect(self._tools.actionBackward, \
                 SIGNAL("toggled(bool)"),self.controller.toggleDirectionBackward)
        self.connect(self._tools.actionLeft, \
                 SIGNAL("toggled(bool)"),self.controller.toggleDirectionLeft)
        self.connect(self._tools.actionRight, \
                 SIGNAL("toggled(bool)"),self.controller.toggleDirectionRight)
        
        # types
        self.connect(self._tools.actionNeutral, \
                 SIGNAL("toggled(bool)"),self.controller.toggleNeutralType)
        self.connect(self._tools.actionJump, \
                 SIGNAL("toggled(bool)"),self.controller.toggleJumpType)
        self.connect(self._tools.actionSlow, \
                 SIGNAL("toggled(bool)"),self.controller.toggleSlowType)
        self.connect(self._tools.actionSpeed, \
                 SIGNAL("toggled(bool)"),self.controller.toggleSpeedType)
        self.connect(self._tools.actionInvert, \
                 SIGNAL("toggled(bool)"),self.controller.toggleInvertType)
        self.connect(self._tools.actionBounceBack, \
                 SIGNAL("toggled(bool)"),self.controller.toggleBounceBackType)
        
        # toolbar
        self.connect(self._mainWin.actionNew, SIGNAL("triggered()"),\
                     self.controller.newTrack)
        self.connect(self._mainWin.actionOpen, SIGNAL("triggered()"),\
                     self.controller.openTrack)
        self.connect(self._mainWin.actionSave, SIGNAL("triggered()"),\
                     self.controller.saveTrack)
        self.connect(self._mainWin.actionQuit, SIGNAL("triggered()"),\
                     self.controller.quit)
        self.connect(self._mainWin.actionExport, SIGNAL("triggered()"),\
                     self.controller.exportTrack)
        self.connect(self._mainWin.actionPreview, SIGNAL("triggered()"),\
                     self.controller.previewTrack)
        
        # mode
        self.connect(self._mainWin.actionDirectionMode, 
                     SIGNAL("triggered()"), self.controller.toggleModeDirection)
        self.connect(self._mainWin.actionElevationMode, 
                     SIGNAL("triggered()"), self.controller.toggleModeElevation)
        

class TrackEditor(object):

    SEPARATOR = '!'
    EXPORT_CMD = 'egg-qtess -cs z-up -tbnauto -up 5 -o %s %s'
    TEMP_FILE = '/tmp/test.egg'
    
    def __init__(self, argv):
        self.app = QApplication(argv)
        self.gui = GUI(self)
        self.trackGenerator = TrackGenerator()
        
        self.tileView = self.gui.tileEditorView
        self.tileModel = TileEditorModel(TileEditorView.TILES_NUM_X,
                                         TileEditorView.TILES_NUM_Y)
        self.tileController = TileEditorController(self.tileView,
                                           self.tileModel,
                                           defaultDirection=Direction.FORWARD)
        self.tileView.setController(self.tileController)
        self.tileModel.addView(self.tileView)
        self.tileModel.addView(self.gui.mvcLcdNumber)
        
        self.gui.mvcLcdNumber.setModel(self.tileModel)
        
        self._prevDirAction = self.gui.actionForward
        self._prevDirType = self.gui.actionNeutral
    
    def exportTrack(self):
        fileName = QFileDialog.getSaveFileName(self.gui, "Save track (*.egg)")
        if fileName != '':
            # TODO to launch in a new thread, progress bar etc...
            self.trackGenerator.generate(self.tileModel.tiles)
            # FIXME functionality should be here but for the moment I leave it 
            # in the track generator in order to save from the preview
            self.trackGenerator.saveTo(self.TEMP_FILE)
            # run egg-qtess
            cmd= self.EXPORT_CMD % (fileName, self.TEMP_FILE)
            Popen(cmd.split())
            logger.info("Track succesfully exported to %s" % fileName)
        
    
    def newTrack(self):
        self.tileModel.reset()
    
    def saveTrack(self):
        fileName = QFileDialog.getSaveFileName(self.gui, "Save map (*.map)")
        if fileName != '':
            f = open(fileName, "wb")
            tiles = self.tileModel.tiles
            cPickle.dump(tiles, f)
    
    def openTrack(self):
        self.tileModel.reset()
        fileName = QFileDialog.getOpenFileName(self.gui, "Map file (*.map)")
        if fileName != '':
            f = open(fileName, "rb")
            tiles = cPickle.load(f)
            self.tileModel.tiles = tiles
            self.tileView.update()
            
    def quit(self):
        # TODO ask to save track
        sys.exit(1)
        
    def previewTrack(self):
        tiles = self.tileModel.tiles
        self.trackGenerator.generate(tiles)
        self.trackGenerator.showTrack()
        
    def toggleNeutralType(self, toggled):
        if not self.tileController.getCurrentType() == TileType.NEUTRAL:
            self._prevDirType.toggle()
            self._prevDirType = self.gui.actionNeutral
            self.tileController.currentType = TileType.NEUTRAL
    
    def toggleJumpType(self, toggled):
        if not self.tileController.getCurrentType() == TileType.JUMP:
            self._prevDirType.toggle()
            self._prevDirType = self.gui.actionJump
            self.tileController.currentType = TileType.JUMP
    
    def toggleSpeedType(self, toggled):
        if not self.tileController.getCurrentType() == TileType.SPEED:
            self._prevDirType.toggle()
            self._prevDirType = self.gui.actionSpeed
            self.tileController.currentType = TileType.SPEED<commit_msg>improved save, open and export<commit_after># -*- coding: utf-8-*-

"""
Author: <NAME> <<EMAIL>>
Copyright © 2008-2009
"""
from mdlib.log import ConsoleLogger, DEBUG
logger = ConsoleLogger("track-editor", DEBUG)

from mdlib.panda import tools

from PyQt4.QtCore import *
from PyQt4.QtGui import *

from gui.autogen import *
from gui.qt.plugins.tileeditorview import *

from preview import TrackGenerator

import sys, string, cPickle, time, os
from copy import deepcopy

import echo

class GUI(QMainWindow):
    def __init__(self, controller, *args):
        apply(QMainWindow.__init__, (self,) + args)
        self.controller = controller
        self._setupUi()
    
    def __getattr__(self,attr):
        try:
            return self.__dict__[attr]
        except KeyError, e:
            try:
                return self._mainWin.__dict__[attr]
            except KeyError, e:
                return self._tools.__dict__[attr]
      
    def _setupUi(self):
        self.setWindowTitle("Test")
        self.setGeometry(0,0,800,600)
        
        self._mainWin = Ui_MainWindow()
        self._mainWin.setupUi(self)
        
        # put the central widget in a layout. Qt Designer doesn't allow it :/
        layout = QBoxLayout(QBoxLayout.LeftToRight)
        layout.addWidget(self.tileEditorViewContainer)
        self.centralwidget.setLayout(layout)
        
        self._tools = Ui_toolsDock()
        toolsDock = QDockWidget("Tools", self)
        self._tools.setupUi(toolsDock)
        self.addDockWidget(QtCore.Qt.RightDockWidgetArea, toolsDock)
        
        # directions
        self.connect(self._tools.actionForward, \
                 SIGNAL("toggled(bool)"),self.controller.toggleDirectionForward)
        self.connect(self._tools.actionBackward, \
                 SIGNAL("toggled(bool)"),self.controller.toggleDirectionBackward)
        self.connect(self._tools.actionLeft, \
                 SIGNAL("toggled(bool)"),self.controller.toggleDirectionLeft)
        self.connect(self._tools.actionRight, \
                 SIGNAL("toggled(bool)"),self.controller.toggleDirectionRight)
        
        # types
        self.connect(self._tools.actionNeutral, \
                 SIGNAL("toggled(bool)"),self.controller.toggleNeutralType)
        self.connect(self._tools.actionJump, \
                 SIGNAL("toggled(bool)"),self.controller.toggleJumpType)
        self.connect(self._tools.actionSlow, \
                 SIGNAL("toggled(bool)"),self.controller.toggleSlowType)
        self.connect(self._tools.actionSpeed, \
                 SIGNAL("toggled(bool)"),self.controller.toggleSpeedType)
        self.connect(self._tools.actionInvert, \
                 SIGNAL("toggled(bool)"),self.controller.toggleInvertType)
        self.connect(self._tools.actionBounceBack, \
                 SIGNAL("toggled(bool)"),self.controller.toggleBounceBackType)
        
        # toolbar
        self.connect(self._mainWin.actionNew, SIGNAL("triggered()"),\
                     self.controller.newTrack)
        self.connect(self._mainWin.actionOpen, SIGNAL("triggered()"),\
                     self.controller.openTrack)
        self.connect(self._mainWin.actionSave, SIGNAL("triggered()"),\
                     self.controller.saveTrack)
        self.connect(self._mainWin.actionQuit, SIGNAL("triggered()"),\
                     self.controller.quit)
        self.connect(self._mainWin.actionExport, SIGNAL("triggered()"),\
                     self.controller.exportTrack)
        self.connect(self._mainWin.actionPreview, SIGNAL("triggered()"),\
                     self.controller.previewTrack)
        
        # mode
        self.connect(self._mainWin.actionDirectionMode, 
                     SIGNAL("triggered()"), self.controller.toggleModeDirection)
        self.connect(self._mainWin.actionElevationMode, 
                     SIGNAL("triggered()"), self.controller.toggleModeElevation)
        

class TrackEditor(object):

    SEPARATOR = '!'
    TEMP_FILE = '/tmp/test.egg'
    QTESS_FILE = '/tmp/qtess.egg'
    MAP_FORMAT_VERSION = "1.0"
    
    def __init__(self, argv):
        self.app = QApplication(argv)
        self.gui = GUI(self)
        self.trackGenerator = TrackGenerator()
        
        self.tileView = self.gui.tileEditorView
        self.tileModel = TileEditorModel(TileEditorView.TILES_NUM_X,
                                         TileEditorView.TILES_NUM_Y)
        self.tileController = TileEditorController(self.tileView,
                                           self.tileModel,
                                           defaultDirection=Direction.FORWARD)
        self.tileView.setController(self.tileController)
        self.tileModel.addView(self.tileView)
        self.tileModel.addView(self.gui.mvcLcdNumber)
        
        self.gui.mvcLcdNumber.setModel(self.tileModel)
        
        self._prevDirAction = self.gui.actionForward
        self._prevDirType = self.gui.actionNeutral
        
        self._currentTrack = None
    
    def exportTrack(self):
        fileName = QFileDialog.getSaveFileName(self.gui, "Save track (*.egg)")
        if fileName != '':
            # TODO to launch in a new thread, progress bar etc...
            self.trackGenerator.generate(self.tileModel.tiles)
            # FIXME the egg should be written here but for the moment I leave it 
            # in the track generator in order to save from the preview
            self.trackGenerator.saveTo(self.TEMP_FILE)
            # tessellate
            tools.tessellate(self.TEMP_FILE, self.QTESS_FILE)
            
            # sleep in order to give egg-qtess some time to run
            while not os.path.exists(self.QTESS_FILE):
                time.sleep(0.2)
            
            tools.groupify(self.QTESS_FILE, fileName)
            
            logger.info("Track succesfully exported to %s" % fileName)
        
    
    def newTrack(self):
        self.tileModel.reset()
    
    def saveTrack(self):
        def _saveTrack(fileName, tiles, version):
            logger.info("Saving track as %s" % fileName)
            f = open(fileName, "wb")
            cPickle.dump(version, f, -1)
            cPickle.dump(tiles, f, -1)
            
        if self._currentTrack is None:
            fileName = QFileDialog.getSaveFileName(self.gui, "Save map (*.map)")
            if fileName != '':
                self._currentTrack = fileName
                _saveTrack(self._currentTrack, self.tileModel.tiles, \
                           self.MAP_FORMAT_VERSION)
        else:
            _saveTrack(self._currentTrack, self.tileModel.tiles, \
                       self.MAP_FORMAT_VERSION)
            
            
    
    def openTrack(self):
        self.tileModel.reset()
        fileName = QFileDialog.getOpenFileName(self.gui, "Map file (*.map)")
        if fileName != '':
            logger.info("Opening track %s" % fileName)
            f = open(fileName, "rb")
            version = cPickle.load(f)
            if version == self.MAP_FORMAT_VERSION:
                tiles = cPickle.load(f)
                self.tileModel.tiles = tiles
                self.tileView.update()
                self._currentTrack = fileName
            else:
                QErrorMessage().showMessage("Map with format version %s \
                are not supported" % version)
            
    def quit(self):
        # TODO ask to save track
        sys.exit(1)
        
    def previewTrack(self):
        tiles = self.tileModel.tiles
        self.trackGenerator.generate(tiles)
        self.trackGenerator.showTrack()
        
    def toggleNeutralType(self, toggled):
        if not self.tileController.getCurrentType() == TileType.NEUTRAL:
            self._prevDirType.toggle()
            self._prevDirType = self.gui.actionNeutral
            self.tileController.currentType = TileType.NEUTRAL
    
    def toggleJumpType(self, toggled):
        if not self.tileController.getCurrentType() == TileType.JUMP:
            self._prevDirType.toggle()
            self._prevDirType = self.gui.actionJump
            self.tileController.currentType = TileType.JUMP
    
    def toggleSpeedType(self, toggled):
        if not self.tileController.getCurrentType() == TileType.SPEED:
            self._prevDirType.toggle()
            self._prevDirType = self.gui.actionSpeed
            self.tileController.currentType = TileType.SPEED
{'fraction_non_alphanumeric': 0.10275545350172216, 'fraction_numerical': 0.0029978313560403112, 'mean_word_length': 2.0755198116908593, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6854298', 'n_tokens_mistral': 4518, 'n_tokens_neox': 4405, 'n_words': 738}