Project

General

Profile

Link to Exercise 7 solution

File $HOME/fwk4_training/my_bricks/FoilsWidget.py:
from PyQt4 import QtCore, QtGui

class FoilsWidget(QtGui.QWidget):
  def __init__(self, parent, onFoilSelected=None):
    QtGui.QWidget.__init__(self, parent)

    self.onFoilSelected_cb = onFoilSelected

    self.groupBox = QtGui.QGroupBox("Foils", self)
    self.radioButtons = []

    self.buttonsBox = QtGui.QVBoxLayout()
    self.groupBox.setLayout(self.buttonsBox)
    QtGui.QVBoxLayout(self)
    self.layout().addWidget(self.groupBox)

  def setFoils(self, foils_names):
    for radiobtn in self.radioButtons:
      radiobtn.close()
    self.radioButtons = []

    for foil_name in foils_names:
      radiobtn = QtGui.QRadioButton(foil_name, self.groupBox)
      QtCore.QObject.connect(radiobtn, QtCore.SIGNAL("clicked()"), self.foilSelected)
      self.buttonsBox.addWidget(radiobtn)
      self.radioButtons.append(radiobtn)

  def setFoil(self, foil_name):
    for radiobtn in self.radioButtons:
      if str(radiobtn.text()) == foil_name:
        radiobtn.setChecked(True)
        break

  def setState(self, state):
    if state in ("MOVING", ):
      self.setEnabled(False)
    else:
      self.setEnabled(True)

  def setLabel(self, label):
    self.groupBox.setTitle(label)

  def foilSelected(self):
    for radiobtn in self.radioButtons:
      if radiobtn.isChecked():
        if callable(self.onFoilSelected_cb):
          self.onFoilSelected_cb(str(radiobtn.text()))
          break

if __name__ == '__main__':
  app = QtGui.QApplication([])

  def cb(foil):
    print "Foil selected=", foil

  fw = FoilsWidget(None, onFoilSelected=cb)
  fw.setFoils(['Out', 'Cu','Ti', 'Kapton', 'Si3N4'])
  fw.setLabel("TEST")

  fw.show()
  app.exec_()