class MyMainWindow(QMainWindow):
    def __init__(self, ch=0, parent=None, name="Temperatur Simulation"):
        super().__init__(parent)
        self.setWindowTitle(name)
        self.__channel = ch

        cw = QWidget(self)
        layout = QVBoxLayout(cw)

        self.setCentralWidget(cw)
        self.__label = QLabel("Temperatur:        ", self)
        self.__dial = QDial(self)
        self.__dial.setRange(100, 400)
        self.__dial.setSingleStep(1)
        self.__dial.setPageStep(10)
        self.__dial.setTracking(False)

        layout.addWidget(QLabel("Channel: {}".format(ch), self))
        layout.addWidget(self.__label)
        layout.addWidget(self.__dial)
        self.__dial.valueChanged.connect(self.__sliderChanged)
        self.__dial.setValue(200)
        self.__sliderChanged()

    def __sliderChanged(self):
        temp = self.__dial.value() / 10
        self.__label.setText("Temperatur: {:.1f}°".format(temp))
        temp = temp * 4096.0 / 330
        with open('/tmp/wiringPiSPI_{}'.format(self.__channel), 'w') as f:
            fcntl.flock(f, fcntl.LOCK_EX)
            f.write("{} {} {}".format(6, int(temp // 256), int(temp % 256)))
            fcntl.flock(f, fcntl.LOCK_UN)
class ControlWidget(QDialog):
    def __init__(self, control_board, parent=None):
        super(ControlWidget, self).__init__(parent)
        self.control_board = control_board
        self.originalPalette = QApplication.palette()

        styleComboBox = QComboBox()
        styleComboBox.addItems(QStyleFactory.keys())

        styleLabel = QLabel("&Style:")
        styleLabel.setBuddy(styleComboBox)

        self.useStylePaletteCheckBox = QCheckBox("&Use style's standard palette")
        self.useStylePaletteCheckBox.setChecked(True)

        self.createCarCmdWidget()

        mainLayout = QGridLayout()
        mainLayout.addWidget(self.cmdGroupBox, 0, 0)
        self.setLayout(mainLayout)

        self.setWindowTitle("Styles")
        self.changeStyle('Windows')

    def changeStyle(self, styleName):
        QApplication.setStyle(QStyleFactory.create(styleName))
        self.changePalette()

    def changePalette(self):
        if (self.useStylePaletteCheckBox.isChecked()):
            QApplication.setPalette(QApplication.style().standardPalette())
        else:
            QApplication.setPalette(self.originalPalette)


    def createControlWidget(self):
        control_group_box = QGroupBox("Speed")

        self.speed_dial = QDial(self.cmdGroupBox)
        self.speed_dial.setRange(-100, 100)
        self.speed_dial.setValue(0)
        self.speed_dial.setNotchesVisible(True)
        self.speed_dial.setTracking(False)
        self.speed_dial.valueChanged.connect(self.speed_changed)

        self.speed_stop_button = QPushButton("Stop")
        self.speed_stop_button.released.connect(self.speed_stop)

        layout = QGridLayout()
        layout.addWidget(self.speed_dial, 1, 1)
        layout.addWidget(self.speed_stop_button, 3, 1)

        control_group_box.setLayout(layout)

        self.cmd_layout.addWidget(control_group_box, 1, 1)

        control_group_box = QGroupBox("Steering")

        self.steering_dial = QDial(self.cmdGroupBox)
        self.steering_dial.setRange(-100, 100)
        self.steering_dial.setValue(0)
        self.steering_dial.setNotchesVisible(True)
        self.steering_dial.setTracking(False)
        self.steering_dial.valueChanged.connect(self.steering_changed)

        self.steering_stop_button = QPushButton("Stop")
        self.steering_stop_button.released.connect(self.steering_stop)

        layout = QGridLayout()
        layout.addWidget(self.steering_dial, 1, 1)
        layout.addWidget(self.steering_stop_button, 3, 1)

        control_group_box.setLayout(layout)

        self.cmd_layout.addWidget(control_group_box, 1, 2)

        self.arm_button = QPushButton("Arm")
        self.arm_button.released.connect(self.set_arm_mode)

        self.manual_button = QPushButton("Manual")
        self.manual_button.released.connect(self.set_manual_mode)

        self.cmd_layout.addWidget(self.arm_button, 1, 3)
        self.cmd_layout.addWidget(self.manual_button, 2, 3)




    def steering_changed(self):
        control_board.set_steering(self.steering_dial.value())

    def steering_stop(self):
        control_board.set_steering(0)

    def speed_changed(self):
        control_board.set_speed(self.speed_dial.value())

    def speed_stop(self):
        control_board.set_speed(0)

    def set_arm_mode(self):
        control_board.set_mode(car_state.ARM)

    def set_manual_mode(self):
        control_board.set_mode(car_state.MANUAL)

    def createCarCmdWidget(self):
        self.cmdGroupBox = QGroupBox("Cmd group")
        self.cmd_layout = QGridLayout()

        self.createControlWidget()

        self.cmdGroupBox.setLayout(self.cmd_layout)