<commit_before>#!/usr/bin/env python3
import logging
import json
import math
import os
from configparser import NoOptionError

from gi.repository import Gtk, GObject
from lib.videodisplay import VideoDisplay
import lib.connection as Connection

from lib.config import Config
from vocto.port import Port

class VideoPreviewsController(object):
    """Displays Video-Previews and selection Buttons for them"""

    def __init__(self, preview_box, win, uibuilder):
        self.log = logging.getLogger('VideoPreviewsController')

        self.win = win

        self.sources = Config.getSources()
        self.preview_players = {}
        self.previews = {}
        self.volume_sliders = {}

        self.current_source = {'a': None, 'b': None}

        # Accelerators
        accelerators = Gtk.AccelGroup()
        win.add_accel_group(accelerators)

        for idx, source in enumerate(self.sources):
            self.addPreview(uibuilder,preview_box, source, Port.SOURCES_OUT + idx)

        if Config.getLivePreviewEnabled():
            self.addPreview(uibuilder, preview_box, "LIVE", Port.LIVE_OUT )

        # connect event-handler and request initial state
        Connection.on('video_status', self.on_video_status)
        Connection.send('get_video')

        if Config.getVolumeControl():
            Connection.on('audio_status', self.on_audio_status)
            Connection.send('get_audio')

    def addPreview(self, uibuilder, preview_box, source, port):
        self.log.info('Initializing Video Preview %s', source)
        previewSize = Config.getPreviewSize()
        preview = uibuilder.load_check_widget(
            'widget_preview',
            os.path.dirname(uibuilder.uifile) + "/widgetpreview.ui")
        video = uibuilder.find_widget_recursive(preview, 'video')

        video.set_size_request(*previewSize)
        preview_box.pack_start(preview, fill=False,
                               expand=False, padding=0)

        audio_level = uibuilder.find_widget_recursive(preview, 'audio_level_display')
        player = VideoDisplay(video, port=port,
                              width=previewSize[0],
                              height=previewSize[1],
                              level_callback=audio_level.level_callback,<commit_msg>NEW: auto correct preview size if previews of configured size won't fit onto screen<commit_after>#!/usr/bin/env python3
import logging
import json
import math
import os
from configparser import NoOptionError

from gi.repository import Gtk, Gdk, GObject
from lib.videodisplay import VideoDisplay
import lib.connection as Connection

from lib.config import Config
from vocto.port import Port

class VideoPreviewsController(object):
    """Displays Video-Previews and selection Buttons for them"""

    def __init__(self, preview_box, win, uibuilder):
        self.log = logging.getLogger('VideoPreviewsController')

        self.win = win

        self.sources = Config.getSources()
        self.preview_players = {}
        self.previews = {}
        self.volume_sliders = {}

        self.current_source = {'a': None, 'b': None}

        # Accelerators
        accelerators = Gtk.AccelGroup()
        win.add_accel_group(accelerators)

        # count number of previews
        num_previews = len(self.sources)
        if Config.getLivePreviewEnabled():
            num_previews += 1

        # get preview size
        self.previewSize = Config.getPreviewSize()

        # recalculate preview size if in sum they are too large for screen
        screen = Gdk.Screen.get_default()
        if screen.get_height() < self.previewSize[1] * num_previews:
            height = screen.get_height() / num_previews
            self.previewSize = (Config.getVideoRatio()*height,height)
            self.log.warning('Resizing previews so that they fit onto screen to WxH={}x{}'.format(*self.previewSize))

        for idx, source in enumerate(self.sources):
            self.addPreview(uibuilder,preview_box, source, Port.SOURCES_OUT + idx)

        if Config.getLivePreviewEnabled():
            self.addPreview(uibuilder, preview_box, "LIVE", Port.LIVE_OUT )

        # connect event-handler and request initial state
        Connection.on('video_status', self.on_video_status)
        Connection.send('get_video')

        if Config.getVolumeControl():
            Connection.on('audio_status', self.on_audio_status)
            Connection.send('get_audio')

    def addPreview(self, uibuilder, preview_box, source, port):
        self.log.info('Initializing Video Preview %s', source)

        preview = uibuilder.load_check_widget(
            'widget_preview',
            os.path.dirname(uibuilder.uifile) + "/widgetpreview.ui")
        video = uibuilder.find_widget_recursive(preview, 'video')

        video.set_size_request(*self.previewSize)
        preview_box.pack_start(preview, fill=False,
                               expand=False, padding=0)

        audio_level = uibuilder.find_widget_recursive(preview, 'audio_level_display')
        player = VideoDisplay(video, port=port,
                              width=self.previewSize[0],
                              height=self.previewSize[1],
                              level_callback=audio_level.level_callback,
{'fraction_non_alphanumeric': 0.09234878839916047, 'fraction_numerical': 0.0019080328181644724, 'mean_word_length': 2.691549295774648, 'pattern_counts': {'":': 0, '<': 4, '<?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': '4368383', 'n_tokens_mistral': 1480, 'n_tokens_neox': 1450, 'n_words': 321}