Exemple #1
0
class OWCodeViewer(OWWidget):
    name = "Code Viewer"
    description = "Display"
    icon = "icons/Code.svg"
    priority = 10
    keywords = ["source", "code", "display", "programming"]
    directory = ""

    show_configuration = False

    class Inputs:
        data = Input("Source Code", Orange.data.Table)

    #class Outputs:
    #    sample = Output("Sampled Data", Orange.data.Table)

    want_main_area = False

    def __init__(self):
        super().__init__()

        # GUI
        box = gui.widgetBox(self.controlArea, "Info")
        self.infoLabel = gui.widgetLabel(box, '')

        #self.display_no_source_selected()

        self.code_editor = CodeEditorTextEdit()
        self.controlArea.layout().addWidget(self.code_editor)

        self.configMoreButton = QPushButton("Configuration")
        self.configMoreButton.setCheckable(True)
        self.configMoreButton.clicked.connect(
            self.switch_configuration_visibility)
        self.controlArea.layout().addWidget(self.configMoreButton)

        self.configurationBox = gui.widgetBox(self.controlArea,
                                              "Configuration")
        gui.lineEdit(self.configurationBox,
                     self,
                     'directory',
                     'Source Directory',
                     callback=self.directory_changed)
        self.refresh_configuration_box()

        #Test data
        #self.directory = "C:\\Code\\samples\\juliet-test-suite\\"
        #self.set_data(Table("orangecode/test.csv"))

    def switch_configuration_visibility(self, e):
        self.show_configuration = not (self.show_configuration)
        self.refresh_configuration_box()

    def refresh_configuration_box(self):
        if (self.show_configuration):
            self.configMoreButton.setText("Configuration <<")
            self.configurationBox.show()
        else:
            self.configMoreButton.setText("Configuration >>")
            self.configurationBox.hide()

    @Inputs.data
    def set_data(self, dataset):
        if dataset is not None:
            if (len(dataset) < 1):
                self.display_no_source_selected()
            else:
                #import code
                #code.interact(local=dict(globals(), **locals()))
                self.process_line(dataset[0])
        else:
            self.display_no_source_selected()

    def directory_changed(self):
        self.code_editor.setPlainText("")
        self.update_source_file()

    def process_line(self, line):
        """
        The extraction is based on values to avoid manual configuration.
        """

        self.source_file = ""
        self.source_line = -1

        #code.interact(local=locals())

        all_attributes_index = []

        #Guessing based on values
        for var in itertools.chain(line.domain.attributes, line.domain.metas):
            i = line.domain.index(var.name)
            #print("{} -> {}".format(var.name,i))
            all_attributes_index.append(i)

        for attribute_index in all_attributes_index:
            try:
                line[attribute_index]
            except IndexError:
                print("More attributes than values on line {}".format(line))
                continue

            if (line[attribute_index] is not None):
                val = line[attribute_index].value
                if type(val) is str:
                    val_parts = val.split(":")
                    if (len(val_parts) == 2):
                        if (val_parts[1].isnumeric()):
                            self.source_file = val_parts[0]
                            self.source_line = int(val_parts[1])

        self.update_source_file()

    def update_source_file(self):
        if (self.source_file != ""):
            #Update highlighter
            filename, extension = os.path.splitext(self.source_file)
            self.code_editor.set_highlighter(extension)

            try:
                with open(self.directory + "/" + self.source_file,
                          'r') as file:
                    code = file.read()
                    self.code_editor.setPlainText(code)

                self.display_source_file()
            except IOError:
                _, err, _ = sys.exc_info()
                self.display_error(str(err))
        else:
            self.display_no_source_selected()
            return
        if (self.source_line != -1):
            #print(self.source_line)
            block = self.code_editor.document().findBlockByLineNumber(
                self.source_line - 1)
            self.code_editor.setTextCursor(QTextCursor(block))
            self.code_editor.moveCursor(QTextCursor.EndOfBlock)

    def is_source_file(self, value):
        #print(value.__class__.__name__)
        if not (isinstance(value, str)):
            return False
        for extension in [
                '.java', '.c', '.cpp', '.py', '.js', '.ruby', '.jsp'
        ]:
            if (value.endswith(extension)):
                return True
        return False

    # Information display
    def display_no_source_selected(self):
        self.infoLabel.setText('No source file selected')

    def display_file_not_found(self):
        self.infoLabel.setText('Source file not found')

    def display_error(self, message):
        self.infoLabel.setText('An error has occured: ' + message)

    def display_source_file(self):
        filename = self.source_file.split("/")[-1].split("\\")[-1]
        line = ("" if self.source_line == -1 else " ~ Line: <b>" +
                str(self.source_line) + "</b>")

        self.infoLabel.setText("Source file: <b>{}</b> {}".format(
            filename, line))
Exemple #2
0
class ControlButton(ControlBase):
    def __init__(self, *args, **kwargs):
        self._checkable = kwargs.get('checkable', False)
        super(ControlButton, self).__init__(*args, **kwargs)

        default = kwargs.get('default', None)
        if default: self.value = default

    def init_form(self):
        self._form = QPushButton()
        self._form.setSizePolicy(QSizePolicy.Minimum, QSizePolicy.Fixed)
        self._form.setCheckable(self._checkable)
        self.label = self._label
        self._form.setToolTip(self.help)

    def click(self):
        self._form.click()

    def load_form(self, data, path=None):
        pass

    def save_form(self, data, path=None):
        pass

    ##########################################################################

    @property
    def label(self):
        return ControlBase.label.fget(self)

    @label.setter
    def label(self, value):
        ControlBase.label.fset(self, value)
        self._form.setText(self._label)

    @property
    def icon(self):
        return self._form.icon()

    @icon.setter
    def icon(self, value):
        if isinstance(value, (str, bytes)):
            self._form.setIcon(QIcon(value))
        else:
            self._form.setIcon(value)

    ##########################################################################

    @property
    def value(self):
        return None

    @value.setter
    def value(self, value):
        try:
            self._form.clicked.disconnect()  # ignore previous signals if any
        except TypeError as err:
            # http://stackoverflow.com/questions/21586643/pyqt-widget-connect-and-disconnect
            pass
        self._form.clicked[bool].connect(value)

    @property
    def checked(self):
        return self._form.isChecked()

    @checked.setter
    def checked(self, value):
        self._form.setChecked(value)
Exemple #3
0
            },
            "Item 3": {
                "Parameter 1": (None, False),
                "Parameter 2": (None, True)
            },
            "Item 4": {
                "Parameter 1": (_items[:10], _items[0]),
                "Parameter 2": (None, False)
            },
            "Item 5": {
                "Parameter 1": ("", "Foo"),
                "Parameter 2": (None, False)
            },
            "Item 6": {
                "Parameter 1": ("", ""),
                "Parameter 2": (None, False)
            },
        },
    }

    dlg = SettingsDialog(w, _settings)
    dlg.setting_changed.connect(lambda *res: print(*res))
    dlg.finished.connect(lambda res: print(res, dlg.changed_settings))

    btn = QPushButton(w)
    btn.setText("Open dialog")
    btn.clicked.connect(dlg.show_dlg)

    w.show()
    sys.exit(app.exec_())
Exemple #4
0
class MainUI(QWidget):
    def __init__(self):
        super().__init__()
        self.left = Config.geometry[0]
        self.top = Config.geometry[1]
        self.width = Config.geometry[2]
        self.height = Config.geometry[3]
        self.setLayout(QHBoxLayout(self))

        # Init logger
        self.logger = Logger('mainUI', 'UI : Main')

        # Read in user prefs
        self.prefs = {}
        self.loadPrefs()

        # Load configured objects
        self.lights = Lights()
        self.lights.load()
        self.obas = OBAs()
        self.obas.load()
        self.tracs = Tracs()
        self.tracs.load()

        # Init I2C control
        if 'i2cAddress' in self.prefs.keys():
            _address = self.prefs['i2cAddress']
        else:
            _address = Config.defaultI2CAddress
        if 'i2cBus' in self.prefs.keys():
            _bus = self.prefs['i2cBus']
        else:
            _bus = Config.defaultI2CBus
        if 'i2cDebug' in self.prefs.keys():
            _debug = self.prefs['i2cDebug']
        else:
            _debug = False
        self._i2cBus = I2CBus(int(_bus), str(_address), bool(_debug))
        del _bus, _address, _debug

        # Create menu frame
        self._sidebarMenu = QFrame(self)
        self._sidebarMenu.layout = QVBoxLayout(self._sidebarMenu)
        self._sidebarMenu.setMaximumWidth(Config.menuWidth + 50)
        self._sidebarMenu.setMaximumHeight(Config.geometry[3])
        self._sidebarMenu.setStyleSheet(
            "QFrame{border-right: 1px solid black}")

        # Create menu
        _container = QWidget()
        _container.layout = QVBoxLayout()
        _container.setLayout(_container.layout)
        _scroll = QScrollArea()
        _scroll.setWidget(_container)
        _scroll.setWidgetResizable(True)
        self._sidebarMenu.layout.addWidget(_scroll)
        for _key in [
                'enableOBA', 'enableLighting', 'enableTracControl',
                'enableCamViewer', 'enableGyro'
        ]:
            if _key in self.prefs.keys():
                if self.prefs[_key]:
                    _title = {
                        'enableOBA': 'control_oba',
                        'enableLighting': 'control_light',
                        'enableTracControl': 'control_trac',
                        'enableCamViewer': 'control_cam',
                        'enableGyro': 'control_gyro'
                    }[_key]
                    _icon = {
                        'enableOBA':
                        Config.faIcon("wind"),
                        'enableLighting':
                        Config.faIcon("lightbulb"),
                        'enableTracControl':
                        Config.icon("tracControl", "rearDiff")['path'],
                        'enableCamViewer':
                        Config.faIcon("camera"),
                        'enableGyro':
                        Config.faIcon("truck-pickup")
                    }[_key]
                    _button = MenuButton(panel=_title, parent=self)
                    _button.setIcon(QIcon(_icon))
                    _container.layout.addWidget(_button)
        _settingsButton = MenuButton(panel="config_prefs", parent=self)
        _settingsButton.setIcon(QIcon(Config.faIcon("user-cog")))
        _container.layout.addWidget(_settingsButton)
        del _container, _scroll, _key, _settingsButton, _title, _icon, _button

        # Create version info label
        self._version = QLabel('v%s' % Config.version, self)
        self._version.setAlignment(Qt.AlignCenter)
        self._version.setFixedWidth(Config.menuWidth)
        self._version.setStyleSheet("QLabel{border: none}")
        self._sidebarMenu.layout.addWidget(self._version)

        # Create OSK button
        self._oskButton = QPushButton('', self)
        self._oskButton.setIcon(QIcon(Config.faIcon('keyboard')))
        self._oskButton.setFixedWidth(Config.menuWidth)
        self._oskButton.clicked.connect(self.showOSK)
        self._sidebarMenu.layout.addWidget(self._oskButton)

        # Add menu frame to main UI
        self.layout().addWidget(self._sidebarMenu)

        # Create main UI panel
        self._mainPanel = QWidget(self)
        self._mainPanel.setLayout(QVBoxLayout(self._mainPanel))
        self.layout().addWidget(self._mainPanel)

        # Init default UI
        for _key in [
                'enableOBA', 'enableLighting', 'enableTracControl',
                'enableCamViewer', 'enableGyro'
        ]:
            _cUI = None
            _uiName = None
            if _key in self.prefs.keys():
                if self.prefs[_key]:
                    if _key == 'enableOBA':
                        _cUI = OBAControlUI(self.obas.obas, self)
                        _uiName = 'control_oba'
                    elif _key == 'enableLighting':
                        _cUI = LightControlUI(self.lights.lights, self)
                        _uiName = 'control_light'
                    elif _key == 'enableTracControl':
                        _cUI = TracControlUI(self.tracs.tracs, self)
                        _uiName = 'control_trac'
                    elif _key == 'enableCamViewer':
                        _cUI = CamViewer(0)
                        _uiName = 'control_cam'
                    elif _key == 'enableGryo':
                        _cUI = Gyrometer()
                        _uiName = 'control_gyro'
            if _cUI is not None:
                break
        if _cUI is None:
            _cUI = UserPrefUI(self.prefs, parent=self)
            _uiName = 'config_prefs'
        self._currentUI = {'name': _uiName, 'obj': _cUI}
        _cUI.setParent(self)
        self._mainPanel.layout().addWidget(_cUI)
        _cUI.show()

        # Create button panel
        self._btnPanel = QWidget(self)
        self._btnPanel.setLayout(QHBoxLayout(self._btnPanel))
        self._mainPanel.layout().addWidget(self._btnPanel)

        # Create Config button
        self._configButton = QPushButton('Configure', self)
        self._configButton.setFixedHeight(50)
        self._configButton.setIcon(QIcon(Config.faIcon('cog')))
        self._configButton.clicked.connect(self.__configButtonAction)
        self._btnPanel.layout().addWidget(self._configButton)

        # Create Night Mode button
        self._nightModeButton = QPushButton('', self)
        self._nightModeButton.setFixedHeight(50)
        self._nightModeButton.setIcon(
            QIcon({
                True: Config.faIcon('sun'),
                False: Config.faIcon('moon')
            }[self.prefs['nightMode']]))
        self._nightModeButton.setText({
            True: 'Day Mode',
            False: 'Night Mode'
        }[self.prefs['nightMode']])
        self._nightModeButton.clicked.connect(self.toggleNightMode)
        self._btnPanel.layout().addWidget(self._nightModeButton)
        self.setNightMode(self.prefs['nightMode'])

    def closeEvent(self, event):
        self.savePrefs()
        self._i2cBus.deEnergizeAll()
        super(MainUI, self).closeEvent(event)

    def availablePins(self, value=None):
        if not self.prefs['allowDuplicatePins']:
            _pins = Config.outputPinList
            for _light in self.lights.lights:
                if _light.outputPin in _pins:
                    _pins.remove(_light.outputPin)
            for _oba in self.obas.obas:
                if _oba.outputPin in _pins:
                    _pins.remove(_oba.outputPin)
            for _trac in self.tracs.tracs:
                if _trac.outputPin in _pins:
                    _pins.remove(_trac.outputPin)
        else:
            _pins = Config.outputPinList
        _pins.sort()
        return _pins

    def setOutputPin(self, pin, state):
        fx = {
            True: self.i2cBus.energizeRelay,
            False: self.i2cBus.deEnergizeRelay
        }[state]
        fx(pin)

    def disableConfigButtons(self):
        for _ctrl in [self.configButton]:
            _ctrl.setEnabled(False)
        del _ctrl

    def enableConfigButtons(self):
        for _ctrl in [self.configButton]:
            _ctrl.setEnabled(True)
        del _ctrl

    def loadUI(self, ui, idx=None):
        if 'cam' in self.currentUI['name']:
            self.currentUI['obj'].stop()
        if 'gyro' in self.currentUI['name']:
            self.currentUI['obj'].stopSerial()
            self.currentUI['obj'].stopRotation()
        self.currentUI['obj'].hide()
        self.mainPanel.layout().removeWidget(self.currentUI['obj'])
        self.mainPanel.layout().removeWidget(self.btnPanel)
        _mode = ui.split('_')[0]
        _type = ui.split('_')[1]
        _ui = None
        if _mode == 'control':
            if _type == 'gyro':
                self.configButton.setText("Calibrate")
            else:
                self.configButton.setText("Configure")
            if _type == 'light':
                _ui = LightControlUI(self.lights.lights, self)
            elif _type == 'oba':
                _ui = OBAControlUI(self.obas.obas, self)
            elif _type == 'trac':
                _ui = TracControlUI(self.tracs.tracs, self)
            elif _type == 'cam':
                _ui = CamViewer(0)
                _ui.start()
            elif _type == 'gyro':
                _ui = Gyrometer(parent=self)
                _ui.startSerial()
                _ui.startRotation()
        elif _mode == 'config':
            if _type == 'light':
                _ui = LightConfigUI(self.lights.lights, self)
            elif _type == 'oba':
                _ui = OBAConfigUI(self.obas.obas, self)
            elif _type == 'trac':
                _ui = TracConfigUI(self.tracs.tracs, self)
            elif _type == 'prefs':
                _ui = UserPrefUI(self.prefs, self)
        elif _mode == 'edit':
            if _type == 'light':
                _ui = EditLightUI(self.lights.lights[idx], self)
            elif _type == 'oba':
                _ui = EditOBAUI(self.obas.obas[idx], self)
            elif _type == 'trac':
                _ui = EditTracUI(self.tracs.tracs[idx], self)
        elif _mode == 'create':
            if _type == 'light':
                _ui = AddLightUI(self)
            elif _type == 'oba':
                _ui = AddOBAUI(self)
            elif _type == 'trac':
                _ui = AddTracUI(self)
        if _ui is not None:
            _ui.setParent(self)
            self.mainPanel.layout().addWidget(_ui)
            _ui.show()
            self.currentUI = {'name': ui, 'obj': _ui}
        self.mainPanel.layout().addWidget(self.btnPanel)

    def toggleNightMode(self):
        _mode = self.prefs['nightMode']
        _value = {False: Config.dayBright, True: Config.nightBright}[not _mode]
        self.nightModeButton.setIcon({
            False: QIcon(Config.faIcon('sun')),
            True: QIcon(Config.faIcon('moon'))
        }[not _mode])
        self.nightModeButton.setText({
            False: 'Day Mode',
            True: 'Night Mode'
        }[not _mode])
        os.system("echo %s > /sys/class/backlight/rpi_backlight/brightness" %
                  _value)
        self.prefs['nightMode'] = not _mode
        self.savePrefs()
        self.logger.log('Night mode %s: backlight set to %s' %
                        (not _mode, _value))
        del _mode, _value

    def setNightMode(self, mode):
        _value = {False: Config.dayBright, True: Config.nightBright}[mode]
        self.nightModeButton.setIcon({
            False: QIcon(Config.faIcon('sun')),
            True: QIcon(Config.faIcon('moon'))
        }[mode])
        self.nightModeButton.setText({
            False: 'Day Mode',
            True: 'Night Mode'
        }[mode])
        os.system("echo %s > /sys/class/backlight/rpi_backlight/brightness" %
                  _value)
        del _value

    def loadPrefs(self):
        _pPrefs = open(Config.prefs, 'rb')
        _prefs = pickle.load(_pPrefs)
        for key in _prefs:
            self.prefs[key] = _prefs[key]
        _pPrefs.close()
        del _pPrefs, _prefs

    def savePrefs(self):
        _pPrefs = open(Config.prefs, 'wb')
        pickle.dump(self.prefs, _pPrefs)
        _pPrefs.close()

    def showOSK(self):
        if self.window().dock.isHidden():
            self.window().dock.show()
        else:
            self.window().dock.hide()

    def __configButtonAction(self):
        if self.currentUI['name'] not in ['control_camera', 'control_gyro']:
            self.disableConfigButtons()
            self.mainPanel.layout().removeWidget(self.currentUI['obj'])
            self.loadUI(self.currentUI['name'].replace('control', 'config'))
        elif self.currentUI['name'] == 'control_gyro':
            self.currentUI['obj'].calibrate()

    @property
    def mainPanel(self):
        return self._mainPanel

    @property
    def btnPanel(self):
        return self._btnPanel

    @property
    def nightModeButton(self):
        return self._nightModeButton

    @property
    def configButton(self):
        return self._configButton

    @property
    def currentUI(self):
        return self._currentUI

    @currentUI.setter
    def currentUI(self, value):
        self._currentUI = value

    @property
    def i2cBus(self):
        return self._i2cBus