コード例 #1
0
ファイル: gui.py プロジェクト: IARI/lightshow
    def __init__(self):
        QObject.__init__(self)
        super(Gui, self).__init__()
        self.setupUi(self)
        self.show()

        # noinspection PyUnresolvedReferences
        self.list_leds.itemSelectionChanged.connect(self.selection_changed)

        self.settings = QSettings('light_show', 'router')
        self.input_routerip.setText(self.settings.value('address', ""))
        self.input_user.setText(self.settings.value('user', 'root'))
        self.input_path.setText(self.settings.value('path', Gui.DEFAULT_PATH))
        self.edt_script.setPlainText(self.settings.value('prog', ''))

        self.router = FreifunkRouter()
        self.router.connection_changed.connect(self.connection_event)
        self.router.model_changed.connect(self.update_selection)

        self.connection_event(None)

        self.program = None

        # noinspection PyUnresolvedReferences
        self.btn_run.clicked.connect(self.runProgram)
        # noinspection PyUnresolvedReferences
        self.btn_stop.clicked.connect(self.stopProgram)

        # noinspection PyUnresolvedReferences
        self.btn_connect.clicked.connect(self.connect)
        # noinspection PyUnresolvedReferences
        self.btn_all.clicked.connect(self.list_leds.selectAll)
        # noinspection PyUnresolvedReferences
        self.btn_none.clicked.connect(self.list_leds.clearSelection)
コード例 #2
0
ファイル: gui.py プロジェクト: IARI/lightshow
class Gui(QMainWindow, Ui_MainWindow):
    # default path for led directories are
    DEFAULT_PATH = '/sys/devices/platform/leds-gpio/leds/'

    def __init__(self):
        QObject.__init__(self)
        super(Gui, self).__init__()
        self.setupUi(self)
        self.show()

        # noinspection PyUnresolvedReferences
        self.list_leds.itemSelectionChanged.connect(self.selection_changed)

        self.settings = QSettings('light_show', 'router')
        self.input_routerip.setText(self.settings.value('address', ""))
        self.input_user.setText(self.settings.value('user', 'root'))
        self.input_path.setText(self.settings.value('path', Gui.DEFAULT_PATH))
        self.edt_script.setPlainText(self.settings.value('prog', ''))

        self.router = FreifunkRouter()
        self.router.connection_changed.connect(self.connection_event)
        self.router.model_changed.connect(self.update_selection)

        self.connection_event(None)

        self.program = None

        # noinspection PyUnresolvedReferences
        self.btn_run.clicked.connect(self.runProgram)
        # noinspection PyUnresolvedReferences
        self.btn_stop.clicked.connect(self.stopProgram)

        # noinspection PyUnresolvedReferences
        self.btn_connect.clicked.connect(self.connect)
        # noinspection PyUnresolvedReferences
        self.btn_all.clicked.connect(self.list_leds.selectAll)
        # noinspection PyUnresolvedReferences
        self.btn_none.clicked.connect(self.list_leds.clearSelection)

    def runProgram(self):
        try:
            self.program.stop()
        except:
            pass
        self.program = Program(self.readProgram(), self.router)
        self.program.start()
        print('program started')

    def readProgram(self):
        l_progs = re.split("[\r\n]+", self.edt_script.toPlainText().strip())
        l_prog = re.split("[\s]+", l_progs[0])
        print(l_prog)
        return l_prog

    def cursorChanged(self):
        l_prog = self.readProgram()
        self.program.resetProgramData(l_prog)

    def stopProgram(self):
        if self.program and self.program.isAlive:
            try:
                self.program.stop()
                print('program stopped')
            except:
                print("program can't be stopped")

    @property
    def all_led_list_items(self):
        return map(self.list_leds.item, range(self.list_leds.count()))

    def connection_event(self, connected):
        connected = bool(connected)
        color = 'green' if connected else 'red'
        self.btn_connect.setIcon(QIcon(":/icons/icons/{}.png".format(color)))
        self.frame_upper.setEnabled(connected)
        if connected:
            self.make_led_list()

    def make_led_list(self):
        self.list_leds.clear()
        for name, path in self.router.leds:
            this_item = QListWidgetItem(name)
            # this_item.setFlags(this_item.flags() | Qt.ItemIsEditable)
            self.list_leds.addItem(this_item)

    def update_selection(self):
        self.list_leds.blockSignals(True)
        for i, v in zip(self.all_led_list_items, self.router[:]):
            i.setSelected(bool(v))
        self.list_leds.blockSignals(False)

    def selection_changed(self):
        items = self.all_led_list_items
        self.router[:] = [int(i.isSelected()) for i in items]

    def connect(self):
        address = self.input_routerip.text()
        user = self.input_user.text()
        password = self.input_password.text()
        path = self.input_path.text()
        self.router.connect(address, user or "root", password, path)

    def closeEvent(self, event):
        self.stopProgram()
        self.router.disconnect()
        self.settings.setValue('address', self.input_routerip.text())
        self.settings.setValue('user', self.input_user.text())
        # self.settings.setValue('password', self.input_user.text())
        self.settings.setValue('path', self.input_path.text())
        self.settings.setValue('prog', self.edt_script.toPlainText())