<commit_before>"""
A sample desktop application using the raumfeld library
"""
import time
from PySide.QtCore import QThread, Signal, Slot
from PySide.QtGui import QMainWindow
import raumfeld

__version__ = '0.3'


class SearchThread(QThread):

    devices_found = Signal(list)

    def run(self):
        devices = raumfeld.discover()
        self.devices_found.emit(devices)


class DeviceControlThread(QThread):

    volume_infos = Signal(bool, int)

    def __init__(self):
        super(DeviceControlThread, self).__init__()
        self.device = None
        self._needs_update_flag = False
        self.exit_flag = False

    def run(self):
        while not self.exit_flag:
            if self.device is None:
                continue

            if self._needs_update_flag:
                self.device.set_volume(self._volume)
                # self.device.set_mute(self._mute)
            else:
                self._mute = self.device.get_mute()
                self._volume = self.device.get_volume()
                self.volume_infos.emit(self._mute, self._volume)

            self._needs_update_flag = False
            time.sleep(1.0)

    def set_volume(self, volume):
        self._volume = volume
        self._needs_update_flag = True

    def set_mute(self, mute):
        self._mute = mute
        self._needs_update_flag = True


class MainWindow(QMainWindow):

    """
    Raumfeld Desktop Application
    """

    search_for_devices = Signal()

    def __init__(self, application=None):
        """
        Initializes the application's UI
        """
        from .mainwindow_ui import Ui_MainWindow as Ui
        super(MainWindow, self).__init__()
        self.ui = Ui()
        self.ui.setupUi(self)
        self.setWindowTitle('Raumfeld v%s' % __version__)
        self.setEnabled(False)

        # the dial has factor 10 more ticks than the slider
        self.ui.dialVolume.setMinimum(0)
        self.ui.dialVolume.setMaximum(1000)
        self.ui.dialVolume.setTicksPerRotation(100)

        # open a thread to search for devices
        self.search_thread = SearchThread()
        self.search_thread.devices_found.connect(self.devices_found)
        self.search_thread.start()

        # create a worker thread to communicate with the device
        self.device_thread = DeviceControlThread()
        self.device_thread.volume_infos.connect(self.volume_infos)

    @Slot(list)
    def devices_found(self, devices):
        """
        Is called when the search thread finishes searching
        """
        try:
            device = devices[0]
            self.ui.lblConnection.setText(device.friendly_name)
            self.setEnabled(True)

            self.device_thread.device = device
            self.device_thread.start()
        except IndexError:
            # no devices found, search again
            self.search_thread.start()

    @Slot(bool, int)
    def volume_infos(self, mute, volume):
        self.ui.btnMute.setChecked(not mute)
        self.ui.btnMute.setEnabled(True)
        self.ui.sliderVolume.setValue(volume)

    @Slot(int)
    def on_dialVolume_valueChanged(self, value):
        self.ui.sliderVolume.setValue(value // 10)

    @Slot(int)
    def on_sliderVolume_valueChanged(self, value):
        self.ui.dialVolume.setValue(value * 10)
        self.device_thread.set_volume(value)

    @Slot()
    def on_btnMute_clicked(self):
        self.device.set_mute(self.ui.btnMute.isChecked())
        self.ui.btnMute.setEnabled(False)

    def closeEvent(self, event):
        self.search_thread.wait()
        self.device_thread.exit_flag = True
        self.device_thread.wait()
<commit_msg>dial and slider now working nicely<commit_after>"""
A sample desktop application using the raumfeld library
"""
import time
from PySide.QtCore import QThread, Signal, Slot
from PySide.QtGui import QMainWindow
import raumfeld

__version__ = '0.3'


class SearchThread(QThread):

    devices_found = Signal(list)

    def run(self):
        devices = raumfeld.discover()
        self.devices_found.emit(devices)


class DeviceControlThread(QThread):

    volume_infos = Signal(bool, int)

    def __init__(self):
        super(DeviceControlThread, self).__init__()
        self.device = None
        self._needs_update_flag = False
        self.exit_flag = False

    def run(self):
        while not self.exit_flag:
            if self.device is None:
                continue

            if self._needs_update_flag:
                self.device.set_volume(self._volume)
                # self.device.set_mute(self._mute)
            else:
                self._mute = self.device.get_mute()
                self._volume = self.device.get_volume()
                self.volume_infos.emit(self._mute, self._volume)

            self._needs_update_flag = False
            time.sleep(1.0)

    def set_volume(self, volume):
        self._volume = volume
        self._needs_update_flag = True

    def set_mute(self, mute):
        self._mute = mute
        self._needs_update_flag = True


class MainWindow(QMainWindow):

    """
    Raumfeld Desktop Application
    """

    search_for_devices = Signal()

    def __init__(self, application=None):
        """
        Initializes the application's UI
        """
        from .mainwindow_ui import Ui_MainWindow as Ui
        super(MainWindow, self).__init__()
        self.ui = Ui()
        self.ui.setupUi(self)
        self.setWindowTitle('Raumfeld v%s' % __version__)
        self.setEnabled(False)

        # the dial has factor 10 more ticks than the slider
        self.ui.dialVolume.setMinimum(0)
        self.ui.dialVolume.setMaximum(1000)
        self.ui.dialVolume.setTicksPerRotation(100)

        # open a thread to search for devices
        self.search_thread = SearchThread()
        self.search_thread.devices_found.connect(self.devices_found)
        self.search_thread.start()

        # create a worker thread to communicate with the device
        self.device_thread = DeviceControlThread()
        self.device_thread.volume_infos.connect(self.volume_infos)

    @Slot(list)
    def devices_found(self, devices):
        """
        Is called when the search thread finishes searching
        """
        try:
            device = devices[0]
            self.ui.lblConnection.setText(device.friendly_name)
            self.setEnabled(True)

            self.device_thread.device = device
            self.device_thread.start()
        except IndexError:
            # no devices found, search again
            self.search_thread.start()

    @Slot(bool, int)
    def volume_infos(self, mute, volume):
        self.ui.btnMute.setChecked(not mute)
        self.ui.btnMute.setEnabled(True)
        self.ui.dialVolume.setValue(volume * 10)

    @Slot(int)
    def on_dialVolume_valueChanged(self, value):
        self.ui.sliderVolume.setValue(value // 10)
        self.device_thread.set_volume(value // 10)

    @Slot(int)
    def on_sliderVolume_sliderMoved(self, value):
        self.ui.dialVolume.setValue(value * 10)

    @Slot()
    def on_btnMute_clicked(self):
        self.device.set_mute(self.ui.btnMute.isChecked())
        self.ui.btnMute.setEnabled(False)

    def closeEvent(self, event):
        self.search_thread.wait()
        self.device_thread.exit_flag = True
        self.device_thread.wait()

{'fraction_non_alphanumeric': 0.0928931932887737, 'fraction_numerical': 0.005729095621334061, 'mean_word_length': 2.3266787658802177, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4418003', 'n_tokens_mistral': 2272, 'n_tokens_neox': 2136, 'n_words': 437}