=begin
Timba language interpreter.
Copyright (C) 2016  Nicolas Guzzo

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
=end

require './editor.rb'
require './view.rb'
require './about.rb'

class MainWindow < Qt::MainWindow
	
	slots	'newFile()',
			'openFile()',
			'saveFile()',
			'run()',
			'stop()',
			'next()',
    		'openFile(const QString)',
    		'set_delay(const QString &)',
    		'about()'
	
	def initialize(parent = nil)
	    super(parent)
		
		@editor = Editor.new(nil,statusBar)
		@view=View.new(nil)
		@timba=Timba.new(@view,@editor)
		@view.timba=@timba
		setupFileMenu()
		setupToolBar()
		setupStatusBar()
		
		cont=Qt::Splitter.new
		cont.addWidget(@editor)
		@scroll=Qt::ScrollArea.new
		@scroll.setWidget(@view)
		@scroll.setWidgetResizable(true)
		cont.addWidget(@scroll)
		#cont.addWidget(@view)
	    
	    setCentralWidget(cont)
	    setWindowTitle(tr("Timba"))	    
	    setWindowIcon(Qt::Icon.new("img/icon.png"))
	end
	def run()
		@runToolBar.widgetForAction(@nextAct).setEnabled(false)
		@timba.stop()
		@timba.run()
	end
	def stop()
		@timba.stop()
		@runToolBar.widgetForAction(@nextAct).setEnabled(true)
		@runToolBar.widgetForAction(@runAct).setEnabled(true)
	end
	def next()
		@runToolBar.widgetForAction(@runAct).setEnabled(false)
		@timba.next()
	end
	def newFile()
	    @editor.newFile()
	end
	def saveFile()
		@editor.saveFile()
	end
	def set_delay(s)
		@timba.set_delay(s.to_f)
	end
	def openFile(path = nil)
		@editor.openFile(path)
		@timba.parse(@editor.get_text)
		@scroll.ensureVisible(0,0)
	end

	def setupFileMenu()
	    fileMenu = Qt::Menu.new(tr("&Archivo"), self)
	    menuBar().addMenu(fileMenu)
	
	    fileMenu.addAction(tr("&Nuevo..."), self, SLOT('newFile()'),
	                        Qt::KeySequence.new(tr("Ctrl+N", "Archivo|Nuevo")))
	    fileMenu.addAction(tr("&Abrir..."), self, SLOT('openFile()'),
	                        Qt::KeySequence.new(tr("Ctrl+O", "Archivo|Abrir")))
	    fileMenu.addAction(tr("&Guardar..."), self, SLOT('saveFile()'),
	                        Qt::KeySequence.new(tr("Ctrl+S", "Archivo|Guardar")))
	    fileMenu.addAction(tr("&Salir"), $qApp, SLOT('quit()'),
	                        Qt::KeySequence.new(tr("Ctrl+Q", "Archivo|Salir")))
	    aboutMenu = Qt::Menu.new(tr("&Ayuda"), self)
		menuBar().addMenu(aboutMenu)
		aboutMenu.addAction(tr("&Acerca de..."),self,SLOT('about()'))
	    
	    
	end
	def setupToolBar()
		@runToolBar = addToolBar(tr("Run"))
		@runAct = Qt::Action.new(Qt::Icon.new("img/run.png"), tr("Ejecutar"), self)
	    @runAct.shortcut = Qt::KeySequence.new( tr("F5") )
	    @runAct.statusTip = tr("Run program")
	    connect(@runAct, SIGNAL('triggered()'), self, SLOT('run()'))
	    @runToolBar.addAction(@runAct)

	    @stopAct = Qt::Action.new(Qt::Icon.new("img/stop.png"), tr("Stop"), self)
	    @stopAct.shortcut = Qt::KeySequence.new( tr("F5") )
	    @stopAct.statusTip = tr("Stop program")
	    connect(@stopAct, SIGNAL('triggered()'), self, SLOT('stop()'))
	    @runToolBar.addAction(@stopAct)

	    @nextAct = Qt::Action.new(Qt::Icon.new("img/next.png"), tr("Next"), self)
	    @nextAct.shortcut = Qt::KeySequence.new( tr("F6") )
	    @nextAct.statusTip = tr("Proxima")
	    connect(@nextAct, SIGNAL('triggered()'), self, SLOT('next()'))
	    @runToolBar.addAction(@nextAct)
 		
 		@speedCtrl=Qt::ComboBox.new
 		@speedCtrl.addItems(["0","0.1","0.2","0.5","1","2","3","4","5"])
 		@speedCtrl.setCurrentIndex(3)
 		connect(@speedCtrl, SIGNAL('currentIndexChanged(const QString &)'), self, SLOT('set_delay(const QString &)'))
		@runToolBar.addWidget(@speedCtrl)
	end
	def setupStatusBar
		statusBar().showMessage(tr("Ready"))
	end
	def about()

		a=About.new
		a.exec
		
	end
end
