# 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 billboardui
import gui.util.environment as environ

class Billboard(QtGui.QWidget, billboardui.Ui_BillboardBase):
   def __init__(self, billboardElement, parent=None):
      QtGui.QWidget.__init__(self, parent)
      self.setupUi(self)

      self.mBillboardElement = billboardElement
      self._updateUi()

   def setupUi(self, widget):
      billboardui.Ui_BillboardBase.setupUi(self, widget)

      self.widgets = [self.mXSize, self.mYSize, self.mXRes, self.mYRes]
      self.attrs = ['vpresent_width', 'vpresent_height',
                    'vpresent_width_res', 'vpresent_height_res']
      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(int)"),
                      self.onValueChanged)

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

      for (w,a) in zip(self.widgets, self.attrs):
         w.setValue(getattr(self.mBillboardElement,a))

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

   def _updateElement(self):
      for (w,a) in zip(self.widgets, self.attrs):
         setattr(self.mBillboardElement, a, w.value())

      self.mBillboardElement._update(True)
      env = environ.Environment()
      env.updateGL()

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