# vPresent is an immersive presentation creation and display application.
# Copyright (C) 2007-2008 by Infiscape Corporation
#
# 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.

from PyQt4 import QtCore, QtGui
import translateui
import gui.util.environment as environ
from wheel import Wheel

class Translate(QtGui.QWidget, translateui.Ui_TranslateBase):
   def __init__(self, translateElement, parent=None):
      QtGui.QWidget.__init__(self, parent)
      self.setupUi(self)

      self.mTranslateElement = translateElement
      self._updateUi()

   def setupUi(self, widget):
      translateui.Ui_TranslateBase.setupUi(self, widget)

      # Replace all sliders with wheels
      # - Rename var to end in wheel
      grid_name = 'gridlayout'
      slider_names = ["mXTranslationSlider","mYTranslationSlider","mZTranslationSlider"]
      for slider_name in slider_names:
         slider = getattr(self,slider_name)
         grid = getattr(self,grid_name)
         assert (slider is not None and grid is not None)
         
         slider_index = grid.indexOf(slider)
         slider_location = grid.getItemPosition(slider_index)
         grid.removeWidget(slider)
         slider.setParent(None)
         delattr(self,slider_name)
         
         wheel_name = slider_name.replace("Slider","Wheel")
         wheel = Wheel(self)
         wheel.setRange(-1000.0, 1000.0, 1.0, 1.0)
         grid.addWidget(wheel, *slider_location)
         setattr(self,wheel_name, wheel)

      self.widgets = [self.mXTranslation, self.mYTranslation, self.mZTranslation,
                      self.mXTranslationWheel, self.mYTranslationWheel, self.mZTranslationWheel]

      for w in self.widgets[:3]:
         self.connect(w, QtCore.SIGNAL("valueChanged(double)"),
                      self.onValueChanged)

      for w in self.widgets[3:]:
         self.connect(w, QtCore.SIGNAL("valueChanged(double)"),
                      self.onWheelChanged)
      #self.connect(self.mLatRes, QtCore.SIGNAL("valueChanged(int)"),
      #             self.onValueChanged)
      #self.connect(self.mLonRes, QtCore.SIGNAL("valueChanged(int)"),
      #             self.onValueChanged)

   def _updateUi(self):
      # Disable signals while changing values.
      for w in self.widgets:
         w.blockSignals(True)

      xform = self.mTranslateElement.dr3d_transform
      self.mXTranslation.setValue(xform[3][0])
      self.mYTranslation.setValue(xform[3][1])
      self.mZTranslation.setValue(xform[3][2])
      self.mXTranslationWheel.setValue(xform[3][0])
      self.mYTranslationWheel.setValue(xform[3][1])
      self.mZTranslationWheel.setValue(xform[3][2])

      for w in self.widgets:
         w.blockSignals(False)

   def _updateElement(self):
      xform = self.mTranslateElement.dr3d_transform
      xform.setTranslate(self.mXTranslation.value(),
                         self.mYTranslation.value(),
                         self.mZTranslation.value())
      self.mTranslateElement.dr3d_transform = xform
      self.mTranslateElement._update()
      env = environ.Environment()
      env.updateGL()

   def onValueChanged(self, value):
      self._updateElement()

   def onWheelChanged(self, value):
      self.mXTranslation.setValue(self.mXTranslationWheel.value())
      self.mYTranslation.setValue(self.mYTranslationWheel.value())
      self.mZTranslation.setValue(self.mZTranslationWheel.value())
      self._updateElement()
