Exemple #1
0
class SyncHTP1Dialog(QDialog, Ui_syncHtp1Dialog):

    def __init__(self, parent, prefs, signal_model):
        super(SyncHTP1Dialog, self).__init__(parent)
        self.__preferences = prefs
        self.__signal_model = signal_model
        self.__simple_signal = None
        self.__filters_by_channel = {}
        self.__current_device_filters_by_channel = {}
        self.__spinner = None
        self.__beq_filter = None
        self.__signal_filter = None
        self.__last_requested_msoupdate = None
        self.__last_received_msoupdate = None
        self.__supports_shelf = False
        self.__channel_to_signal = {}
        self.setupUi(self)
        self.setWindowFlag(Qt.WindowMinimizeButtonHint)
        self.setWindowFlag(Qt.WindowMaximizeButtonHint)
        self.syncStatus = qta.IconWidget('fa5s.unlink')
        self.syncLayout.addWidget(self.syncStatus)
        self.ipAddress.setText(self.__preferences.get(HTP1_ADDRESS))
        self.filterView.setSelectionBehavior(QAbstractItemView.SelectRows)

        from model.filter import FilterModel, FilterTableModel
        self.__filters = FilterModel(self.filterView, self.__preferences)
        self.filterView.setModel(FilterTableModel(self.__filters))
        self.filterView.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
        self.filterView.selectionModel().selectionChanged.connect(self.__on_filter_selected)
        self.__magnitude_model = MagnitudeModel('preview', self.previewChart, self.__preferences,
                                                self.get_curve_data, 'Filter',
                                                x_min_pref_key=HTP1_GRAPH_X_MIN, x_max_pref_key=HTP1_GRAPH_X_MAX,
                                                y_range_calc=DecibelRangeCalculator(30, expand=True), fill_curves=True)
        self.connectButton.setIcon(qta.icon('fa5s.check'))
        self.disconnectButton.setIcon(qta.icon('fa5s.times'))
        self.resyncFilters.setIcon(qta.icon('fa5s.sync'))
        self.deleteFiltersButton.setIcon(qta.icon('fa5s.trash'))
        self.editFilterButton.setIcon(qta.icon('fa5s.edit'))
        self.selectBeqButton.setIcon(qta.icon('fa5s.folder-open'))
        self.limitsButton.setIcon(qta.icon('fa5s.arrows-alt'))
        self.showDetailsButton.setIcon(qta.icon('fa5s.info'))
        self.createPulsesButton.setIcon(qta.icon('fa5s.wave-square'))
        self.fullRangeButton.setIcon(qta.icon('fa5s.expand'))
        self.subOnlyButton.setIcon(qta.icon('fa5s.compress'))
        self.autoSyncButton.setIcon(qta.icon('fa5s.magic'))
        self.autoSyncButton.toggled.connect(lambda b: self.__preferences.set(HTP1_AUTOSYNC, b))
        self.autoSyncButton.setChecked(self.__preferences.get(HTP1_AUTOSYNC))
        self.__ws_client = QtWebSockets.QWebSocket('', QtWebSockets.QWebSocketProtocol.Version13, None)
        self.__ws_client.error.connect(self.__on_ws_error)
        self.__ws_client.connected.connect(self.__on_ws_connect)
        self.__ws_client.disconnected.connect(self.__on_ws_disconnect)
        self.__ws_client.textMessageReceived.connect(self.__on_ws_message)
        self.__disable_on_disconnect()
        self.filterMapping.itemDoubleClicked.connect(self.__show_mapping_dialog)
        self.__restore_geometry()

    def __restore_geometry(self):
        ''' loads the saved window size '''
        geometry = self.__preferences.get(HTP1_SYNC_GEOMETRY)
        if geometry is not None:
            self.restoreGeometry(geometry)

    def closeEvent(self, QCloseEvent):
        ''' Stores the window size on close '''
        self.__preferences.set(HTP1_SYNC_GEOMETRY, self.saveGeometry())
        super().closeEvent(QCloseEvent)

    def __on_ws_error(self, error_code):
        '''
        handles a websocket client error.
        :param error_code: the error code.
        '''
        logger.error(f"error code: {error_code}")
        logger.error(self.__ws_client.errorString())

    def __on_ws_connect(self):
        logger.info(f"Connected to {self.ipAddress.text()}")
        self.__preferences.set(HTP1_ADDRESS, self.ipAddress.text())
        self.__load_peq_slots()
        self.__enable_on_connect()
        self.syncStatus.setIcon(qta.icon('fa5s.link'))
        self.syncStatus.setEnabled(True)

    def __load_peq_slots(self):
        b = self.__ws_client.sendTextMessage('getmso')
        logger.debug(f"Sent {b} bytes")

    def __on_ws_disconnect(self):
        logger.info(f"Disconnected from {self.ipAddress.text()}")
        self.__disable_on_disconnect()

    def __disable_on_disconnect(self):
        ''' Clears all relevant state on disconnect. '''
        self.connectButton.setEnabled(True)
        self.disconnectButton.setEnabled(False)
        self.ipAddress.setReadOnly(False)
        self.resyncFilters.setEnabled(False)
        self.deleteFiltersButton.setEnabled(False)
        self.editFilterButton.setEnabled(False)
        self.showDetailsButton.setEnabled(False)
        self.createPulsesButton.setEnabled(False)
        self.filtersetSelector.clear()
        self.__filters_by_channel = {}
        self.__current_device_filters_by_channel = {}
        self.beqFile.clear()
        self.__beq_filter = None
        self.selectBeqButton.setEnabled(False)
        self.addFilterButton.setEnabled(False)
        self.removeFilterButton.setEnabled(False)
        self.applyFiltersButton.setEnabled(False)
        self.autoSyncButton.setEnabled(False)
        self.__show_signal_mapping()
        self.syncStatus.setIcon(qta.icon('fa5s.unlink'))
        self.syncStatus.setEnabled(False)
        self.__filters.filter = CompleteFilter(fs=HTP1_FS, sort_by_id=True)
        self.__simple_signal = self.__create_pulse('default', self.__filters.filter)
        self.__magnitude_model.redraw()

    def __enable_on_connect(self):
        ''' Prepares the UI for operation. '''
        self.connectButton.setEnabled(False)
        self.disconnectButton.setEnabled(True)
        self.ipAddress.setReadOnly(True)
        self.resyncFilters.setEnabled(True)
        self.autoSyncButton.setEnabled(True)
        self.selectBeqButton.setEnabled(True)
        self.createPulsesButton.setEnabled(True)
        self.__show_signal_mapping()
        self.on_signal_selected()

    def __on_ws_message(self, msg):
        '''
        Handles messages from the device.
        :param msg: the message.
        '''
        if msg.startswith('mso '):
            logger.debug(f"Processing mso {msg}")
            self.__on_mso(json.loads(msg[4:]))
        elif msg.startswith('msoupdate '):
            logger.debug(f"Processing msoupdate {msg}")
            self.__on_msoupdate(json.loads(msg[10:]))
        else:
            logger.warning(f"Unknown message {msg}")
            msg_box = QMessageBox(QMessageBox.Critical, 'Unknown Message',
                                  f"Received unexpected message from {self.ipAddress.text()}")
            msg_box.setDetailedText(f"<code>{msg}</code>")
            msg_box.setTextFormat(Qt.RichText)
            msg_box.exec()
            if self.__spinner is not None:
                stop_spinner(self.__spinner, self.syncStatus)
                self.__spinner = None
                self.syncStatus.setIcon(qta.icon('fa5s.times', color='red'))

    def __on_msoupdate(self, msoupdate):
        '''
        Handles msoupdate message sent after the device is updated.
        :param msoupdate: the update.
        '''
        if len(msoupdate) > 0 and 'path' in msoupdate[0] and msoupdate[0]['path'].startswith('/peq/slots/'):
            self.__last_received_msoupdate = msoupdate
            was_requested = False
            if self.__spinner is not None:
                was_requested = True
                stop_spinner(self.__spinner, self.syncStatus)
                self.__spinner = None
            if was_requested:
                if not self.__msoupdate_matches(msoupdate):
                    self.syncStatus.setIcon(qta.icon('fa5s.times', color='red'))
                    self.show_sync_details()
                else:
                    self.syncStatus.setIcon(qta.icon('fa5s.check', color='green'))
            else:
                self.syncStatus.setIcon(qta.icon('fa5s.question', color='red'))
            self.__update_device_state(msoupdate)
            self.showDetailsButton.setEnabled(True)
        else:
            logger.debug(f"Ignoring UI driven update")

    def __update_device_state(self, msoupdate):
        ''' applies the delta from msoupdate to the local cache of the device state. '''
        for op in msoupdate:
            if 'path' in op and op['path'].startswith('/peq/slots'):
                path = op['path']
                if path[0] == '/':
                    path = path[1:]
                tokens = path.split('/')
                idx = int(tokens[2])
                channel = tokens[4]
                attrib = tokens[5]
                val = op['value']
                if channel in self.__current_device_filters_by_channel:
                    cf = self.__current_device_filters_by_channel[channel]
                    f = cf[idx].resample(HTP1_FS)
                    if attrib == 'gaindB':
                        f.gain = float(val)
                    elif attrib == 'Fc':
                        f.freq = float(val)
                    elif attrib == 'Q':
                        f.q = float(val)
                    cf.save(f)

    def show_sync_details(self):
        if self.__last_requested_msoupdate is not None and self.__last_received_msoupdate is not None:
            SyncDetailsDialog(self, self.__last_requested_msoupdate, self.__last_received_msoupdate).exec()

    def __msoupdate_matches(self, msoupdate):
        '''
        compares each operation for equivalence.
        :param msoupdate: the update
        :returns true if they match
        '''
        for idx, actual in enumerate(msoupdate):
            expected = self.__last_requested_msoupdate[idx]
            if actual['op'] != expected['op'] or actual['path'] != expected['path'] or actual['value'] != expected['value']:
                logger.error(f"msoupdate does not match {actual} vs {expected}")
                return False
        return True

    def __on_mso(self, mso):
        '''
        Handles mso message from the device sent after a getmso is issued.
        :param mso: the mso.
        '''
        version = mso['versions']['swVer']
        version = version[1:] if version[0] == 'v' or version[0] == 'V' else version
        try:
            self.__supports_shelf = semver.parse_version_info(version) > semver.parse_version_info('1.4.0')
        except:
            logger.error(f"Unable to parse version {mso['versions']['swVer']}")
            result = QMessageBox.question(self,
                                          'Supports Shelf Filters?',
                                          f"Reported software version is "
                                          f"\n\n    {version}"
                                          f"\n\nDoes this version support shelf filters?",
                                          QMessageBox.Yes | QMessageBox.No,
                                          QMessageBox.No)
            self.__supports_shelf = result == QMessageBox.Yes

        speakers = mso['speakers']['groups']
        channels = ['lf', 'rf']
        for group in [s for s, v in speakers.items() if 'present' in v and v['present'] is True]:
            if group[0:2] == 'lr' and len(group) > 2:
                channels.append('l' + group[2:])
                channels.append('r' + group[2:])
            else:
                channels.append(group)

        peq_slots = mso['peq']['slots']

        class Filters(dict):
            def __init__(self):
                super().__init__()

            def __missing__(self, key):
                self[key] = CompleteFilter(fs=HTP1_FS, sort_by_id=True)
                return self[key]

        tmp1 = Filters()
        tmp2 = Filters()
        raw_filters = {c: [] for c in channels}
        unknown_channels = set()
        for idx, s in enumerate(peq_slots):
            for c in channels:
                if c in s['channels']:
                    tmp1[c].save(self.__convert_to_filter(s['channels'][c], idx))
                    tmp2[c].save(self.__convert_to_filter(s['channels'][c], idx))
                    raw_filters[c].append(s['channels'][c])
                else:
                    unknown_channels.add(c)
        if unknown_channels:
            peq_channels = peq_slots[0]['channels'].keys()
            logger.error(f"Unknown channels encountered [peq channels: {peq_channels}, unknown: {unknown_channels}]")

        from model.report import block_signals
        with block_signals(self.filtersetSelector):
            now = self.filtersetSelector.currentText()
            self.filtersetSelector.clear()
            now_idx = -1
            for idx, c in enumerate(channels):
                self.filtersetSelector.addItem(c)
                if c == now:
                    now_idx = idx
            if now_idx > -1:
                self.filtersetSelector.setCurrentIndex(now_idx)

        self.__filters_by_channel = tmp1
        self.__current_device_filters_by_channel = tmp2
        self.__filters.filter = self.__filters_by_channel[self.filtersetSelector.itemText(0)]
        if not self.__channel_to_signal:
            self.load_from_signals()
        self.filterView.selectRow(0)
        self.__magnitude_model.redraw()
        self.syncStatus.setIcon(qta.icon('fa5s.link'))
        self.__last_received_msoupdate = None
        self.__last_requested_msoupdate = None
        self.showDetailsButton.setEnabled(False)

    @staticmethod
    def __convert_to_filter(filter_params, id):
        ft = int(filter_params['FilterType']) if SyncHTP1Dialog.__has_filter_type(filter_params) else 0
        if ft == 0:
            return PeakingEQ(HTP1_FS, filter_params['Fc'], filter_params['Q'], filter_params['gaindB'], f_id=id)
        elif ft == 1:
            return LowShelf(HTP1_FS, filter_params['Fc'], filter_params['Q'], filter_params['gaindB'], f_id=id)
        elif ft == 2:
            return HighShelf(HTP1_FS, filter_params['Fc'], filter_params['Q'], filter_params['gaindB'], f_id=id)

    @staticmethod
    def __has_filter_type(filter_params):
        return 'FilterType' in filter_params

    def add_filter(self):
        '''
        Adds the filter to the selected channel.
        '''
        selected_filter = self.__get_selected_filter()
        if selected_filter is not None:
            if self.__in_complex_mode():
                for i in self.filterMapping.selectedItems():
                    c = i.text().split(' ')[1]
                    s = self.__channel_to_signal[c]
                    if s is not None:
                        self.__apply_filter(selected_filter, s.filter)
                        if c == self.filtersetSelector.currentText():
                            self.__filters.filter = self.__filters.filter
            else:
                self.__apply_filter(selected_filter, self.__filters.filter)
                self.__filters.filter = self.__filters.filter
            self.__magnitude_model.redraw()

    @staticmethod
    def __apply_filter(source, target):
        '''
        Places the target filter in the source in the last n slots.
        :param source: the source filter.
        :param target: the target.
        '''
        max_idx = len(target)
        target.removeByIndex(range(max_idx-len(source), max_idx))
        for f in source:
            f.id = len(target)
            target.save(f)

    def __get_selected_filter(self):
        selected_filter = None
        if self.__signal_filter is not None:
            selected_filter = self.__signal_filter
        elif self.__beq_filter is not None:
            selected_filter = self.__beq_filter
        return selected_filter

    def remove_filter(self):
        '''
        Searches for the BEQ in the selected channel and highlights them for deletion.
        Auto deletes if in complex mode.
        '''
        selected_filter = self.__get_selected_filter()
        if selected_filter is not None:
            if self.__in_complex_mode():
                for i in self.filterMapping.selectedItems():
                    c = i.text().split(' ')[1]
                    s = self.__channel_to_signal[c]
                    if s is not None:
                        for r in self.__remove_from_filter(selected_filter, s.filter):
                            to_save = self.__filters if c == self.filtersetSelector.currentText() else s.filter
                            to_save.save(self.__make_passthrough(r))
                self.__magnitude_model.redraw()
            else:
                rows = self.__remove_from_filter(selected_filter, self.__filters.filter)
                self.filterView.clearSelection()
                if len(rows) > 0:
                    for r in rows:
                        self.filterView.selectRow(r)

    def __remove_from_filter(self, to_remove, target):
        filt_idx = 0
        rows = []
        for i, f in enumerate(target):
            if self.__is_equivalent(f, to_remove[filt_idx]):
                filt_idx += 1
                rows.append(i)
            if filt_idx >= len(to_remove):
                break
        return rows

    def select_filter(self, channel_name):
        '''
        loads a filter from the current signals.
        '''
        signal = self.__signal_model.find_by_name(channel_name)
        if signal is not None:
            self.__signal_filter = signal.filter
        else:
            self.__signal_filter = None
        self.__enable_filter_buttons()

    @staticmethod
    def __is_equivalent(a, b):
        return a.freq == b.freq and a.gain == b.gain and hasattr(a, 'q') and hasattr(b, 'q') and math.isclose(a.q, b.q)

    def send_filters_to_device(self):
        '''
        Sends the selected filters to the device
        '''
        if self.__in_complex_mode():
            ops, unsupported_filter_types_per_channel = self.__convert_filter_mappings_to_ops()
            channels_to_update = [i.text().split(' ')[1] for i in self.filterMapping.selectedItems()]
            unsynced_channels = []
            for c, s in self.__channel_to_signal.items():
                if s is not None:
                    if c not in channels_to_update:
                        if c in self.__current_device_filters_by_channel:
                            current = s.filter
                            device = self.__current_device_filters_by_channel[c]
                            if current != device:
                                unsynced_channels.append(c)
            if unsynced_channels:
                result = QMessageBox.question(self,
                                              'Sync all changed channels?',
                                              f"Filters in {len(unsynced_channels)} channels have changed but will not be "
                                              f"synced to the HTP-1."
                                              f"\n\nChannels: {', '.join(sorted([k for k in unsynced_channels]))}"
                                              f"\n\nDo you want to sync all changed channels? ",
                                              QMessageBox.Yes | QMessageBox.No,
                                              QMessageBox.No)
                if result == QMessageBox.Yes:
                    for c in unsynced_channels:
                        for i in range(self.filterMapping.count()):
                            item: QListWidgetItem = self.filterMapping.item(i)
                            if c == item.text().split(' ')[1]:
                                item.setSelected(True)
                    self.send_filters_to_device()
                    return
        else:
            ops, unsupported_filter_types_per_channel = self.__convert_current_filter_to_ops()
        do_send = True
        if unsupported_filter_types_per_channel:
            printed = '\n'.join([f"{k} - {', '.join(v)}" for k, v in unsupported_filter_types_per_channel.items()])
            result = QMessageBox.question(self,
                                          'Unsupported Filters Detected',
                                          f"Unsupported filter types found in the filter set:"
                                          f"\n\n{printed}"
                                          f"\n\nDo you want sync the supported filters only? ",
                                          QMessageBox.Yes | QMessageBox.No,
                                          QMessageBox.No)
            do_send = result == QMessageBox.Yes
        if do_send:
            from app import wait_cursor
            with wait_cursor():
                all_ops = [op for slot_ops in ops for op in slot_ops]
                self.__last_requested_msoupdate = all_ops
                msg = f"changemso {json.dumps(self.__last_requested_msoupdate)}"
                logger.debug(f"Sending to {self.ipAddress.text()} -> {msg}")
                self.__spinner = StoppableSpin(self.syncStatus, 'sync')
                spin_icon = qta.icon('fa5s.spinner', color='green', animation=self.__spinner)
                self.syncStatus.setIcon(spin_icon)
                self.__ws_client.sendTextMessage(msg)

    def __convert_current_filter_to_ops(self):
        '''
        Converts the selected filter to operations.
        :return: (the operations, channel with non PEQ filters)
        '''
        unsupported_filter_types_per_channel = {}
        selected_filter = self.filtersetSelector.currentText()
        ops = [self.__as_operation(idx, selected_filter, f) for idx, f in enumerate(self.__filters.filter)]
        unsupported_types = [f.filter_type for f in self.__filters.filter if self.__not_supported(f.filter_type)]
        if unsupported_types:
            unsupported_filter_types_per_channel[selected_filter] = set(unsupported_types)
        return ops, unsupported_filter_types_per_channel

    def __convert_filter_mappings_to_ops(self):
        '''
        Converts the selected filter mappings to operations.
        :return: (the operations, the channels with unsupported filter types)
        '''
        ops = []
        unsupported_filter_types_per_channel = OrderedDict()
        for i in self.filterMapping.selectedItems():
            c = i.text().split(' ')[1]
            s = self.__channel_to_signal[c]
            if s is not None:
                unsupported_types = [f.filter_type for f in s.filter if self.__not_supported(f.filter_type)]
                if unsupported_types:
                    unsupported_filter_types_per_channel[c] = sorted(set(unsupported_types))
                channel_ops = [self.__as_operation(idx, c, f) for idx, f in enumerate(s.filter)]
                base = len(channel_ops)
                remainder = 16 - base
                if remainder > 0:
                    channel_ops += [self.__as_operation(base + i, c, self.__make_passthrough(i)) for i in range(remainder)]
                ops += channel_ops
        return ops, unsupported_filter_types_per_channel

    def __not_supported(self, filter_type):
        if self.__supports_shelf is True:
            supported = filter_type == 'PEQ' or filter_type == 'LS' or filter_type == 'HS'
        else:
            supported = filter_type == 'PEQ'
        return not supported

    @staticmethod
    def __make_passthrough(idx):
        return PeakingEQ(HTP1_FS, 100, 1, 0, f_id=idx)

    def __as_operation(self, idx, channel, f):
        prefix = f"/peq/slots/{idx}/channels/{channel}"
        ops = [
            {
                'op': 'replace',
                'path': f"{prefix}/Fc",
                'value': f.freq
            },
            {
                'op': 'replace',
                'path': f"{prefix}/Q",
                'value': f.q
            },
            {
                'op': 'replace',
                'path': f"{prefix}/gaindB",
                'value': f.gain
            }
        ]
        if self.__supports_shelf:
            if isinstance(f, PeakingEQ):
                ft = 0
            elif isinstance(f, LowShelf):
                ft = 1
            elif isinstance(f, HighShelf):
                ft = 2
            else:
                raise ValueError(f"Unknown filter type {f}")
            ops.append(
                {
                    'op': 'replace',
                    'path': f"{prefix}/FilterType",
                    'value': ft
                }
            )
        return ops

    def clear_filters(self):
        '''
        Replaces the selected filters.
        '''
        selection_model = self.filterView.selectionModel()
        if selection_model.hasSelection():
            self.__filters.delete([x.row() for x in selection_model.selectedRows()])
            ids = [f.id for f in self.__filters]
            for i in range(16):
                if i not in ids:
                    self.__filters.save(self.__make_passthrough(i))
            self.applyFiltersButton.setEnabled(len(self.__filters) == 16)
            self.__magnitude_model.redraw()

    def connect_htp1(self):
        '''
        Connects to the websocket of the specified ip:port.
        '''
        self.ipAddress.setReadOnly(True)
        logger.info(f"Connecting to {self.ipAddress.text()}")
        self.__ws_client.open(QUrl(f"ws://{self.ipAddress.text()}/ws/controller"))

    def disconnect_htp1(self):
        '''
        disconnects the ws
        '''
        logger.info(f"Closing connection to {self.ipAddress.text()}")
        self.__ws_client.close()
        logger.info(f"Closed connection to {self.ipAddress.text()}")
        self.ipAddress.setReadOnly(False)

    def display_filterset(self, filterset):
        '''
        Displays the selected filterset in the chart and the table.
        :param filterset: the filterset.
        '''
        if filterset in self.__filters_by_channel:
            self.__filters.filter = self.__filters_by_channel[filterset]
            self.filterView.selectRow(0)
            self.__magnitude_model.redraw()
        else:
            logger.warning(f"Unknown filterset {filterset}")

    def resync_filters(self):
        '''
        reloads the PEQ slots
        '''
        self.__load_peq_slots()
        self.beqFile.clear()

    def select_beq(self):
        '''
        Selects a filter from the BEQ repos.
        '''
        from model.minidsp import load_as_filter

        filters, file_name = load_as_filter(self, self.__preferences, HTP1_FS, unroll=True)
        self.beqFile.setText(file_name)
        self.__beq_filter = filters
        self.__enable_filter_buttons()

    def __enable_filter_buttons(self):
        enable = self.__beq_filter is not None or self.__signal_filter is not None
        self.addFilterButton.setEnabled(enable)
        self.removeFilterButton.setEnabled(enable)

    def get_curve_data(self, reference=None):
        ''' preview of the filter to display on the chart '''
        result = []
        if len(self.__filters) > 0:
            result.append(self.__filters.get_transfer_function().get_magnitude(colour=get_filter_colour(len(result))))
            for f in self.__filters:
                result.append(f.get_transfer_function()
                               .get_magnitude(colour=get_filter_colour(len(result)), linestyle=':'))
        return result

    def reject(self):
        '''
        Ensures the HTP1 is disconnected before closing.
        '''
        self.disconnect_htp1()
        super().reject()

    def show_limits(self):
        '''
        Shows the limits dialog.
        '''
        self.__magnitude_model.show_limits()

    def show_full_range(self):
        ''' sets the limits to full range. '''
        self.__magnitude_model.show_full_range()

    def show_sub_only(self):
        ''' sets the limits to sub only. '''
        self.__magnitude_model.show_sub_only()

    def create_pulses(self):
        ''' Creates a dirac pulse with the specified filter for each channel and loads them into the signal model. '''
        for p in [self.__create_pulse(c, f) for c, f in self.__filters_by_channel.items()]:
            self.__signal_model.add(p)
        self.load_from_signals()

    def __recalc_mapping(self):
        for c in self.__filters_by_channel.keys():
            found = False
            for s in self.__signal_model:
                if s.name.endswith(f"_{c}"):
                    self.__channel_to_signal[c] = s
                    found = True
                    break
            if not found:
                self.__channel_to_signal[c] = None

    def __show_signal_mapping(self):
        show_it = self.__in_complex_mode()
        self.loadFromSignalsButton.setEnabled(show_it)
        self.filterMappingLabel.setVisible(show_it)
        self.filterMapping.setVisible(show_it)
        self.filterMappingLabel.setEnabled(show_it)
        self.filterMapping.setEnabled(show_it)
        self.selectNoneButton.setVisible(show_it)
        self.selectAllButton.setVisible(show_it)
        self.filterMapping.clear()
        for c, signal in self.__channel_to_signal.items():
            self.filterMapping.addItem(f"Channel {c} -> {signal.name if signal else 'No Filter'}")
        for i in range(self.filterMapping.count()):
            item: QListWidgetItem = self.filterMapping.item(i)
            if item.text().endswith('No Filter'):
                item.setFlags(item.flags() & ~Qt.ItemIsEnabled & ~Qt.ItemIsSelectable)
        self.filtersetLabel.setText('Preview' if show_it else 'Channel')

    def __in_complex_mode(self):
        '''
        :return: true if we have signals and are connected.
        '''
        return len(self.__signal_model) > 0 and not self.connectButton.isEnabled()

    def __create_pulse(self, c, f):
        from scipy.signal import unit_impulse
        from model.signal import SingleChannelSignalData, Signal
        signal = Signal(f"pulse_{c}", unit_impulse(4 * HTP1_FS, 'mid'), self.__preferences, fs=HTP1_FS)
        return SingleChannelSignalData(name=f"pulse_{c}", filter=f, signal=signal)

    def load_from_signals(self):
        '''
        Maps the signals to the HTP1 channels.
        '''
        self.__recalc_mapping()
        self.__show_signal_mapping()
        self.__apply_filters_to_channels()
        self.on_signal_selected()

    def __apply_filters_to_channels(self):
        '''
        Applies the filters from the mapped signals to the channels.
        '''
        for c, signal in self.__channel_to_signal.items():
            if signal is not None:
                self.__filters_by_channel[c] = signal.filter

    def __show_mapping_dialog(self, item: QListWidgetItem):
        ''' Shows the edit mapping dialog '''
        if self.filterMapping.isEnabled():
            text = item.text()
            channel_name = text.split(' ')[1]
            mapped_signal = self.__channel_to_signal.get(channel_name, None)
            EditMappingDialog(self,
                              self.__filters_by_channel.keys(),
                              channel_name,
                              self.__signal_model,
                              mapped_signal,
                              self.__map_signal_to_channel).exec()

    def __map_signal_to_channel(self, channel_name, signal):
        '''
        updates the mapping of a signal to a channel.
        :param channel_name: the channel name.
        :param signal: the mapped signal.
        '''
        if signal:
            self.__channel_to_signal[channel_name] = signal
        else:
            self.__channel_to_signal[channel_name] = None
        self.__show_signal_mapping()

    def on_signal_selected(self):
        ''' if any channels are selected, enable the sync button. '''
        is_selected = self.filterMapping.selectionModel().hasSelection()
        enable = is_selected or not self.__in_complex_mode()
        self.applyFiltersButton.setEnabled(enable)
        enable_beq = enable and self.__get_selected_filter() is not None
        self.addFilterButton.setEnabled(enable_beq)
        self.removeFilterButton.setEnabled(enable_beq)
        self.addFilterButton.setEnabled(enable_beq)
        self.removeFilterButton.setEnabled(enable_beq)

    def clear_sync_selection(self):
        ''' clears any selection for sync . '''
        self.filterMapping.selectionModel().clearSelection()

    def select_all_for_sync(self):
        ''' selects all channels for sync. '''
        self.filterMapping.selectAll()

    def __on_filter_selected(self):
        ''' enables the edit/delete buttons when selections change. '''
        selection = self.filterView.selectionModel()
        self.deleteFiltersButton.setEnabled(selection.hasSelection())
        self.editFilterButton.setEnabled(selection.hasSelection())

    def edit_filter(self):
        selection = self.filterView.selectionModel()
        if selection.hasSelection():
            if self.__in_complex_mode():
                signal = self.__signal_model.find_by_name(f"pulse_{self.filtersetSelector.currentText()}")
                # ensure edited signal is selected for sync (otherwise autosync can get v confused)
                for i in range(self.filterMapping.count()):
                    item: QListWidgetItem = self.filterMapping.item(i)
                    if self.filtersetSelector.currentText() == item.text().split(' ')[1]:
                        item.setSelected(True)
            else:
                signal = self.__simple_signal
            from model.filter import FilterDialog
            FilterDialog(self.__preferences,
                         signal,
                         self.__filters,
                         self.__on_filter_save,
                         selected_filter=self.__filters[selection.selectedRows()[0].row()],
                         valid_filter_types=['PEQ', 'Low Shelf', 'High Shelf'] if self.__supports_shelf else ['PEQ'],
                         parent=self,
                         max_filters=16,
                         x_lim=(self.__magnitude_model.limits.x_min, self.__magnitude_model.limits.x_max)).show()

    def __on_filter_save(self):
        ''' reacts to a filter being saved by redrawing the UI and syncing the filter to the HTP-1. '''
        self.__magnitude_model.redraw()
        can_sync = len(self.__filters) == 16
        self.applyFiltersButton.setEnabled(can_sync)
        if not can_sync:
            msg_box = QMessageBox()
            msg_box.setText(f"Too many filters loaded, remove {len(self.__filters) - 16} to be able to sync")
            msg_box.setIcon(QMessageBox.Warning)
            msg_box.setWindowTitle('Too Many Filters')
            msg_box.exec()
        if self.autoSyncButton.isChecked() and can_sync:
            self.send_filters_to_device()
Exemple #2
0
class FilterDialog(QDialog, Ui_editFilterDialog):
    '''
    Add/Edit Filter dialog
    '''
    is_shelf = ['Low Shelf', 'High Shelf']
    gain_required = is_shelf + ['PEQ', 'Gain']
    q_steps = [0.0001, 0.001, 0.01, 0.1]
    gain_steps = [0.1, 1.0]
    freq_steps = [0.1, 1.0, 2.0, 5.0]
    passthrough = Passthrough()

    def __init__(self,
                 preferences,
                 signal,
                 filter_model,
                 redraw_main,
                 selected_filter=None,
                 parent=None,
                 valid_filter_types=None):
        self.__preferences = preferences
        super(FilterDialog,
              self).__init__(parent) if parent is not None else super(
                  FilterDialog, self).__init__()
        self.__redraw_main = redraw_main
        # for shelf filter, allow input via Q or S not both
        self.__q_is_active = True
        # allow user to control the steps for different fields, default to reasonably quick moving values
        self.__q_step_idx = self.__get_step(
            self.q_steps, self.__preferences.get(DISPLAY_Q_STEP), 3)
        self.__s_step_idx = self.__get_step(
            self.q_steps, self.__preferences.get(DISPLAY_S_STEP), 3)
        self.__gain_step_idx = self.__get_step(
            self.gain_steps, self.__preferences.get(DISPLAY_GAIN_STEP), 0)
        self.__freq_step_idx = self.__get_step(
            self.freq_steps, self.__preferences.get(DISPLAY_FREQ_STEP), 1)
        # init the UI itself
        self.setupUi(self)
        self.__snapshot = FilterModel(self.snapshotFilterView,
                                      self.__preferences,
                                      on_update=self.__on_snapshot_change)
        self.__working = FilterModel(self.workingFilterView,
                                     self.__preferences,
                                     on_update=self.__on_working_change)
        self.__selected_id = None
        self.__decorate_ui()
        self.__set_q_step(self.q_steps[self.__q_step_idx])
        self.__set_s_step(self.q_steps[self.__s_step_idx])
        self.__set_gain_step(self.gain_steps[self.__gain_step_idx])
        self.__set_freq_step(self.freq_steps[self.__freq_step_idx])
        # underlying filter model
        self.__signal = signal
        self.__filter_model = filter_model
        if self.__filter_model.filter.listener is not None:
            logger.debug(
                f"Selected filter has listener {self.__filter_model.filter.listener.name}"
            )
        # init the chart
        self.__magnitude_model = MagnitudeModel(
            'preview',
            self.previewChart,
            preferences,
            self,
            'Filter',
            db_range_calc=dBRangeCalculator(30, expand=True),
            fill_curves=True)
        # remove unsupported filter types
        if valid_filter_types:
            to_remove = []
            for i in range(self.filterType.count()):
                if self.filterType.itemText(i) not in valid_filter_types:
                    to_remove.append(i)
            for i1, i2 in enumerate(to_remove):
                self.filterType.removeItem(i2 - i1)
        # copy the filter into the working table
        self.__working.filter = self.__filter_model.clone()
        # and initialise the view
        for idx, f in enumerate(self.__working):
            selected = selected_filter is not None and f.id == selected_filter.id
            f.id = uuid4()
            if selected is True:
                self.__selected_id = f.id
                self.workingFilterView.selectRow(idx)
        if self.__selected_id is None:
            self.__add_working_filter()

    def __select_working_filter(self):
        ''' Loads the selected filter into the edit fields. '''
        selection = self.workingFilterView.selectionModel()
        if selection.hasSelection():
            idx = selection.selectedRows()[0].row()
            self.headerLabel.setText(f"Working Filter {idx+1}")
            self.__select_filter(self.__working[idx])

    def __select_snapshot_filter(self):
        ''' Loads the selected filter into the edit fields. '''
        selection = self.snapshotFilterView.selectionModel()
        if selection.hasSelection():
            idx = selection.selectedRows()[0].row()
            self.headerLabel.setText(f"Snapshot Filter {idx+1}")
            self.__select_filter(self.__snapshot[idx])

    def __on_snapshot_change(self, _):
        ''' makes the snapshot table visible when we have one. '''
        self.snapshotFilterView.setVisible(len(self.__snapshot) > 0)
        self.snapshotViewButtonWidget.setVisible(len(self.__snapshot) > 0)
        self.__magnitude_model.redraw()
        return True

    def __on_working_change(self, visible_names):
        ''' ensure the graph redraws when a filter changes. '''
        self.__magnitude_model.redraw()
        return True

    def __decorate_ui(self):
        ''' polishes the UI by setting tooltips, adding icons and connecting widgets to functions. '''
        self.__set_tooltips()
        self.__set_icons()
        self.__connect_working_buttons()
        self.__connect_snapshot_buttons()
        self.__link_table_views()

    def __link_table_views(self):
        ''' Links the table views into the dialog. '''
        self.snapshotFilterView.setVisible(False)
        self.snapshotViewButtonWidget.setVisible(False)
        self.snapshotFilterView.setModel(FilterTableModel(self.__snapshot))
        self.snapshotFilterView.horizontalHeader().setSectionResizeMode(
            QHeaderView.Stretch)
        self.snapshotFilterView.selectionModel().selectionChanged.connect(
            self.__select_snapshot_filter)
        self.workingFilterView.setModel(FilterTableModel(self.__working))
        self.workingFilterView.horizontalHeader().setSectionResizeMode(
            QHeaderView.Stretch)
        self.workingFilterView.selectionModel().selectionChanged.connect(
            self.__select_working_filter)

    def __set_icons(self):
        self.saveButton.setIcon(qta.icon('fa5s.save'))
        self.saveButton.setIconSize(QtCore.QSize(32, 32))
        self.exitButton.setIcon(qta.icon('fa5s.sign-out-alt'))
        self.exitButton.setIconSize(QtCore.QSize(32, 32))
        self.snapFilterButton.setIcon(qta.icon('fa5s.copy'))
        self.acceptSnapButton.setIcon(qta.icon('fa5s.check'))
        self.loadSnapButton.setIcon(qta.icon('fa5s.folder-open'))
        self.resetButton.setIcon(qta.icon('fa5s.undo'))
        self.optimiseButton.setIcon(qta.icon('fa5s.magic'))
        self.addWorkingRowButton.setIcon(qta.icon('fa5s.plus'))
        self.addSnapshotRowButton.setIcon(qta.icon('fa5s.plus'))
        self.removeWorkingRowButton.setIcon(qta.icon('fa5s.minus'))
        self.removeSnapshotRowButton.setIcon(qta.icon('fa5s.minus'))
        self.limitsButton.setIcon(qta.icon('fa5s.arrows-alt'))
        self.fullRangeButton.setIcon(qta.icon('fa5s.expand'))
        self.subOnlyButton.setIcon(qta.icon('fa5s.compress'))

    def __set_tooltips(self):
        self.addSnapshotRowButton.setToolTip('Add new filter to snapshot')
        self.removeSnapshotRowButton.setToolTip(
            'Remove selected filter from snapshot')
        self.addWorkingRowButton.setToolTip('Add new filter')
        self.removeWorkingRowButton.setToolTip('Remove selected filter')
        self.snapFilterButton.setToolTip('Freeze snapshot')
        self.loadSnapButton.setToolTip('Load snapshot')
        self.acceptSnapButton.setToolTip('Apply snapshot')
        self.resetButton.setToolTip('Reset snapshot')
        self.optimiseButton.setToolTip('Optimise filters')
        self.targetBiquadCount.setToolTip(
            'Optimised filter target biquad count')
        self.saveButton.setToolTip('Save')
        self.exitButton.setToolTip('Exit')
        self.limitsButton.setToolTip('Set Graph Limits')

    def __connect_working_buttons(self):
        ''' Connects the buttons associated with the working filter. '''
        self.addWorkingRowButton.clicked.connect(self.__add_working_filter)
        self.removeWorkingRowButton.clicked.connect(
            self.__remove_working_filter)

    def __add_working_filter(self):
        ''' adds a new filter. '''
        new_filter = self.__make_default_filter()
        self.__working.save(new_filter)
        for idx, f in enumerate(self.__working):
            if f.id == new_filter.id:
                self.__selected_id = f.id
                self.workingFilterView.selectRow(idx)

    def __remove_working_filter(self):
        ''' removes the selected filter. '''
        selection = self.workingFilterView.selectionModel()
        if selection.hasSelection():
            self.__working.delete([r.row() for r in selection.selectedRows()])
            if len(self.__working) > 0:
                self.workingFilterView.selectRow(0)

    def __connect_snapshot_buttons(self):
        ''' Connects the buttons associated with the snapshot filter. '''
        self.snapFilterButton.clicked.connect(self.__snap_filter)
        self.resetButton.clicked.connect(self.__clear_snapshot)
        self.acceptSnapButton.clicked.connect(self.__apply_snapshot)
        self.loadSnapButton.clicked.connect(self.__load_filter_as_snapshot)
        self.optimiseButton.clicked.connect(self.__optimise_filter)
        self.addSnapshotRowButton.clicked.connect(self.__add_snapshot_filter)
        self.removeSnapshotRowButton.clicked.connect(
            self.__remove_snapshot_filter)

    def __add_snapshot_filter(self):
        ''' adds a new filter. '''
        new_filter = self.__make_default_filter()
        self.__snapshot.save(new_filter)
        for idx, f in enumerate(self.__snapshot):
            if f.id == new_filter.id:
                self.snapshotFilterView.selectRow(idx)

    def __make_default_filter(self):
        ''' Creates a new filter using the default preferences. '''
        return LowShelf(self.__signal.fs,
                        self.__preferences.get(FILTERS_DEFAULT_FREQ),
                        self.__preferences.get(FILTERS_DEFAULT_Q),
                        0.0,
                        f_id=uuid4())

    def __remove_snapshot_filter(self):
        ''' removes the selected filter. '''
        selection = self.snapshotFilterView.selectionModel()
        if selection.hasSelection():
            self.__snapshot.delete([r.row() for r in selection.selectedRows()])
            if len(self.__snapshot) > 0:
                self.snapshotFilterView.selectRow(0)

    def __snap_filter(self):
        '''
        Captures the current filter as the snapshot.
        '''
        self.__snapshot.filter = self.__working.clone()

    def __clear_snapshot(self):
        ''' Removes the current snapshot. '''
        self.__snapshot.delete(range(len(self.__snapshot)))

    def __apply_snapshot(self):
        ''' Saves the snapshot as the filter. '''
        if len(self.__snapshot) > 0:
            snap = self.__snapshot.filter
            self.__clear_snapshot()
            snap.description = self.__filter_model.filter.description
            self.__filter_model.filter = snap

    def __load_filter_as_snapshot(self):
        ''' Allows a filter to be loaded from a supported file format and set as the snapshot. '''
        result = QMessageBox.question(
            self, 'Load Filter or XML?',
            f"Do you want to load from a filter or a minidsp beq file?"
            f"\n\nClick Yes to load from a filter or No for a beq file",
            QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
        load_xml = result == QMessageBox.No
        loaded_snapshot = None
        if load_xml is True:
            from model.minidsp import load_as_filter
            filters, _ = load_as_filter(self, self.__preferences,
                                        self.__signal.fs)
            if filters is not None:
                loaded_snapshot = CompleteFilter(fs=self.__signal.fs,
                                                 filters=filters,
                                                 description='Snapshot')
        else:
            loaded_snapshot = load_filter(self)
        if loaded_snapshot is not None:
            self.__snapshot.filter = loaded_snapshot

    def __optimise_filter(self):
        '''
        Optimises the current filter and stores it as a snapshot.
        '''
        current_filter = self.__working.clone()
        to_save = self.targetBiquadCount.value() - current_filter.biquads
        if to_save < 0:
            optimised_filter = CompleteFilter(fs=current_filter.fs,
                                              filters=optimise_filters(
                                                  current_filter,
                                                  current_filter.fs, -to_save),
                                              description='Optimised')
            self.__snapshot.filter = optimised_filter
        else:
            QMessageBox.information(
                self, 'Optimise Filters',
                f"Current filter uses {current_filter.biquads} biquads so no optimisation required"
            )

    def show_limits(self):
        ''' shows the limits dialog for the filter chart. '''
        self.__magnitude_model.show_limits()

    def show_full_range(self):
        ''' sets the limits to full range. '''
        self.__magnitude_model.show_full_range()

    def show_sub_only(self):
        ''' sets the limits to sub only. '''
        self.__magnitude_model.show_sub_only()

    def __select_filter(self, selected_filter):
        ''' Refreshes the params and display with the selected filter '''
        from model.report import block_signals
        self.__selected_id = selected_filter.id
        # populate the fields with values if we're editing an existing filter
        if hasattr(selected_filter, 'gain'):
            with block_signals(self.filterGain):
                self.filterGain.setValue(selected_filter.gain)
        if hasattr(selected_filter, 'q'):
            with block_signals(self.filterQ):
                self.filterQ.setValue(selected_filter.q)
        if hasattr(selected_filter, 'freq'):
            with block_signals(self.freq):
                self.freq.setValue(selected_filter.freq)
        if hasattr(selected_filter, 'order'):
            with block_signals(self.filterOrder):
                self.filterOrder.setValue(selected_filter.order)
        if hasattr(selected_filter, 'type'):
            displayName = 'Butterworth' if selected_filter.type is FilterType.BUTTERWORTH else 'Linkwitz-Riley'
            with block_signals(self.passFilterType):
                self.passFilterType.setCurrentIndex(
                    self.passFilterType.findText(displayName))
        if hasattr(selected_filter, 'count') and issubclass(
                type(selected_filter), Shelf):
            with block_signals(self.filterCount):
                self.filterCount.setValue(selected_filter.count)
        with block_signals(self.filterType):
            self.filterType.setCurrentText(selected_filter.display_name)
        # configure visible/enabled fields for the current filter type
        self.enableFilterParams()
        with block_signals(self.freq):
            self.freq.setMaximum(self.__signal.fs / 2.0)

    @staticmethod
    def __get_step(steps, value, default_idx):
        for idx, val in enumerate(steps):
            if str(val) == value:
                return idx
        return default_idx

    def __write_to_filter_model(self):
        ''' Stores the filter in the model. '''
        self.__filter_model.filter = self.__working.filter
        self.__signal.filter = self.__filter_model.filter
        self.__redraw_main()

    def accept(self):
        ''' Saves the filter. '''
        self.previewFilter()
        self.__write_to_filter_model()

    def previewFilter(self):
        ''' creates a filter if the params are valid '''
        active_model, active_view = self.__get_active()
        active_model.save(self.__create_filter())
        self.__ensure_filter_is_selected(active_model, active_view)

    def __get_active(self):
        if self.headerLabel.text().startswith('Working') or len(
                self.headerLabel.text()) == 0:
            active_model = self.__working
            active_view = self.workingFilterView
        else:
            active_model = self.__snapshot
            active_view = self.snapshotFilterView
        return active_model, active_view

    def __ensure_filter_is_selected(self, active_model, active_view):
        '''
        Filter model resets the model on every change, this clears the selection so we have to restore that selection
        to ensure the row remains visibly selected while also blocking signals to avoid a pointless update of the
        fields.
        '''
        for idx, f in enumerate(active_model):
            if f.id == self.__selected_id:
                from model.report import block_signals
                with block_signals(active_view):
                    active_view.selectRow(idx)

    def getMagnitudeData(self, reference=None):
        ''' preview of the filter to display on the chart '''
        result = []
        extra = 0
        if len(self.__filter_model) > 0:
            result.append(
                self.__filter_model.getTransferFunction().getMagnitude(
                    colour=get_filter_colour(len(result))))
        else:
            extra += 1
        if len(self.__working) > 0:
            result.append(self.__working.getTransferFunction().getMagnitude(
                colour=get_filter_colour(len(result)), linestyle='-'))
        else:
            extra += 1
        if len(self.__snapshot) > 0:
            result.append(self.__snapshot.getTransferFunction().getMagnitude(
                colour=get_filter_colour(len(result) + extra), linestyle='-.'))
        else:
            extra += 1
        active_model, _ = self.__get_active()
        for f in active_model:
            if self.showIndividual.isChecked() or f.id == self.__selected_id:
                style = '--' if f.id == self.__selected_id else ':'
                result.append(f.getTransferFunction().getMagnitude(
                    colour=get_filter_colour(len(result) + extra),
                    linestyle=style))
        return result

    def create_shaping_filter(self):
        '''
        Creates a filter of the specified type.
        :return: the filter.
        '''
        filt = None
        if self.filterType.currentText() == 'Low Shelf':
            filt = LowShelf(self.__signal.fs, self.freq.value(),
                            self.filterQ.value(), self.filterGain.value(),
                            self.filterCount.value())
        elif self.filterType.currentText() == 'High Shelf':
            filt = HighShelf(self.__signal.fs, self.freq.value(),
                             self.filterQ.value(), self.filterGain.value(),
                             self.filterCount.value())
        elif self.filterType.currentText() == 'PEQ':
            filt = PeakingEQ(self.__signal.fs, self.freq.value(),
                             self.filterQ.value(), self.filterGain.value())
        elif self.filterType.currentText() == 'Gain':
            filt = Gain(self.__signal.fs, self.filterGain.value())
        elif self.filterType.currentText() == 'Variable Q LPF':
            filt = SecondOrder_LowPass(self.__signal.fs, self.freq.value(),
                                       self.filterQ.value())
        elif self.filterType.currentText() == 'Variable Q HPF':
            filt = SecondOrder_HighPass(self.__signal.fs, self.freq.value(),
                                        self.filterQ.value())
        if filt is None:
            raise ValueError(
                f"Unknown filter type {self.filterType.currentText()}")
        else:
            filt.id = self.__selected_id
        return filt

    def __create_filter(self):
        ''' creates a filter from the currently selected parameters. '''
        return self.create_pass_filter() if self.__is_pass_filter(
        ) else self.create_shaping_filter()

    def create_pass_filter(self):
        '''
        Creates a predefined high or low pass filter.
        :return: the filter.
        '''
        if self.filterType.currentText() == 'Low Pass':
            filt = ComplexLowPass(
                FilterType[self.passFilterType.currentText().upper().replace(
                    '-', '_')], self.filterOrder.value(), self.__signal.fs,
                self.freq.value())
        else:
            filt = ComplexHighPass(
                FilterType[self.passFilterType.currentText().upper().replace(
                    '-', '_')], self.filterOrder.value(), self.__signal.fs,
                self.freq.value())
        filt.id = self.__selected_id
        return filt

    def __is_pass_filter(self):
        '''
        :return: true if the current options indicate a predefined high or low pass filter.
        '''
        selected_filter = self.filterType.currentText()
        return selected_filter == 'Low Pass' or selected_filter == 'High Pass'

    def __is_gain_filter(self):
        '''
        :return: true if the current options indicate a predefined high or low pass filter.
        '''
        selected_filter = self.filterType.currentText()
        return selected_filter == 'Gain'

    def enableFilterParams(self):
        '''
        Configures the various input fields for the currently selected filter type.
        '''
        if self.__is_pass_filter():
            self.passFilterType.setVisible(True)
            self.filterOrder.setVisible(True)
            self.orderLabel.setVisible(True)
            self.filterQ.setVisible(False)
            self.filterQLabel.setVisible(False)
            self.qStepButton.setVisible(False)
            self.filterGain.setVisible(False)
            self.gainStepButton.setVisible(False)
            self.gainLabel.setVisible(False)
        else:
            self.passFilterType.setVisible(False)
            self.filterOrder.setVisible(False)
            self.orderLabel.setVisible(False)
            if self.__is_gain_filter():
                self.qStepButton.setVisible(False)
                self.filterQ.setVisible(False)
                self.filterQLabel.setVisible(False)
                self.freq.setVisible(False)
                self.freqStepButton.setVisible(False)
                self.freqLabel.setVisible(False)
            else:
                self.qStepButton.setVisible(True)
                self.filterQ.setVisible(True)
                self.filterQLabel.setVisible(True)
                self.freq.setVisible(True)
                self.freqStepButton.setVisible(True)
                self.freqLabel.setVisible(True)
            self.filterGain.setVisible(self.__is_gain_required())
            self.gainStepButton.setVisible(self.__is_gain_required())
            self.gainLabel.setVisible(self.__is_gain_required())
        is_shelf_filter = self.__is_shelf_filter()
        if is_shelf_filter:
            self.filterQ.setEnabled(self.__q_is_active)
            self.filterS.setEnabled(not self.__q_is_active)
            # set icons
            inactive_icon = qta.icon('fa5s.chevron-circle-left')
            if self.__q_is_active is True:
                self.qStepButton.setText(
                    str(self.q_steps[self.__q_step_idx % len(self.q_steps)]))
                self.sStepButton.setIcon(inactive_icon)
            else:
                self.qStepButton.setIcon(inactive_icon)
                self.sStepButton.setText(
                    str(self.q_steps[self.__s_step_idx % len(self.q_steps)]))
        self.gainStepButton.setText(
            str(self.gain_steps[self.__gain_step_idx % len(self.gain_steps)]))
        self.freqStepButton.setText(
            str(self.freq_steps[self.__freq_step_idx % len(self.freq_steps)]))
        self.filterCountLabel.setVisible(is_shelf_filter)
        self.filterCount.setVisible(is_shelf_filter)
        self.sLabel.setVisible(is_shelf_filter)
        self.filterS.setVisible(is_shelf_filter)
        self.sStepButton.setVisible(is_shelf_filter)

    def changeOrderStep(self):
        '''
        Sets the order step based on the type of high/low pass filter to ensure that LR only allows even orders.
        '''
        if self.passFilterType.currentText() == 'Butterworth':
            self.filterOrder.setSingleStep(1)
            self.filterOrder.setMinimum(1)
        elif self.passFilterType.currentText() == 'Linkwitz-Riley':
            if self.filterOrder.value() % 2 != 0:
                self.filterOrder.setValue(max(2, self.filterOrder.value() - 1))
            self.filterOrder.setSingleStep(2)
            self.filterOrder.setMinimum(2)

    def __is_gain_required(self):
        return self.filterType.currentText() in self.gain_required

    def __is_shelf_filter(self):
        return self.filterType.currentText() in self.is_shelf

    def recalcShelfFromQ(self, q):
        '''
        Updates S based on the selected value of Q.
        :param q: the q.
        '''
        gain = self.filterGain.value()
        if self.__q_is_active is True and not math.isclose(gain, 0.0):
            self.filterS.setValue(q_to_s(q, gain))

    def recalcShelfFromGain(self, gain):
        '''
        Updates S based on the selected gain.
        :param gain: the gain.
        '''
        if not math.isclose(gain, 0.0):
            max_s = round(max_permitted_s(gain), 4)
            self.filterS.setMaximum(max_s)
            if self.__q_is_active is True:
                q = self.filterQ.value()
                self.filterS.setValue(q_to_s(q, gain))
            else:
                if self.filterS.value() > max_s:
                    self.filterS.blockSignals(True)
                    self.filterS.setValue(max_s, 4)
                    self.filterS.blockSignals(False)
                self.filterQ.setValue(s_to_q(self.filterS.value(), gain))

    def recalcShelfFromS(self, s):
        '''
        Updates the shelf based on a change in S
        :param s: the new S
        '''
        gain = self.filterGain.value()
        if self.__q_is_active is False and not math.isclose(gain, 0.0):
            self.filterQ.setValue(s_to_q(s, gain))

    def handleSToolButton(self):
        '''
        Reacts to the S tool button click.
        '''
        if self.__q_is_active is True:
            self.__q_is_active = False
            self.filterS.setEnabled(True)
            self.sStepButton.setIcon(QIcon())
            self.filterQ.setEnabled(False)
            self.qStepButton.setIcon(qta.icon('fa5s.chevron-circle-left'))
        else:
            self.__s_step_idx += 1
        self.__set_s_step(self.q_steps[self.__s_step_idx % len(self.q_steps)])

    def __set_s_step(self, step_val):
        self.__preferences.set(DISPLAY_S_STEP, str(step_val))
        self.sStepButton.setText(str(step_val))
        self.filterS.setSingleStep(step_val)

    def handleQToolButton(self):
        '''
        Reacts to the q tool button click.
        '''
        if self.__q_is_active is True:
            self.__q_step_idx += 1
        else:
            self.__q_is_active = True
            self.filterS.setEnabled(False)
            self.qStepButton.setIcon(QIcon())
            self.filterQ.setEnabled(True)
            self.sStepButton.setIcon(qta.icon('fa5s.chevron-circle-left'))
        self.__set_q_step(self.q_steps[self.__q_step_idx % len(self.q_steps)])

    def __set_q_step(self, step_val):
        self.__preferences.set(DISPLAY_Q_STEP, str(step_val))
        self.qStepButton.setText(str(step_val))
        self.filterQ.setSingleStep(step_val)

    def handleGainToolButton(self):
        '''
        Reacts to the gain tool button click.
        '''
        self.__gain_step_idx += 1
        self.__set_gain_step(self.gain_steps[self.__gain_step_idx %
                                             len(self.gain_steps)])

    def __set_gain_step(self, step_val):
        self.__preferences.set(DISPLAY_GAIN_STEP, str(step_val))
        self.gainStepButton.setText(str(step_val))
        self.filterGain.setSingleStep(step_val)

    def handleFreqToolButton(self):
        '''
        Reacts to the frequency tool button click.
        '''
        self.__freq_step_idx += 1
        self.__set_freq_step(self.freq_steps[self.__freq_step_idx %
                                             len(self.freq_steps)])

    def __set_freq_step(self, step_val):
        self.__preferences.set(DISPLAY_FREQ_STEP, str(step_val))
        self.freqStepButton.setText(str(step_val))
        self.freq.setSingleStep(step_val)