Skip to content

Commit

Permalink
Revert changes to main_window_connector.py
Browse files Browse the repository at this point in the history
  • Loading branch information
chase9 committed Jan 4, 2025
1 parent 2c4c124 commit 3960704
Showing 1 changed file with 44 additions and 97 deletions.
141 changes: 44 additions & 97 deletions yin_yang/ui/main_window_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,13 @@

from PySide6 import QtWidgets
from PySide6.QtCore import QStandardPaths
from PySide6.QtGui import QColor, QScreen
from PySide6.QtWidgets import (
QColorDialog,
QDialogButtonBox,
QFileDialog,
QGroupBox,
QMessageBox,
)

from ..config import ConfigWatcher, Modes, config, plugins
from ..meta import ConfigEvent, PluginKey
from ..theme_switcher import set_desired_theme, set_mode
from PySide6.QtGui import QScreen, QColor
from PySide6.QtWidgets import QFileDialog, QMessageBox, QDialogButtonBox, QColorDialog, QGroupBox

from .main_window import Ui_main_window
from ..theme_switcher import set_desired_theme, set_mode
from ..meta import ConfigEvent, PluginKey
from ..config import config, Modes, plugins, ConfigWatcher

logger = logging.getLogger(__name__)

Expand All @@ -34,7 +28,10 @@ def notify(self, event: ConfigEvent, values: dict):


def reverse_dict_search(dictionary, value):
return next(key for key, value_dict in dictionary.items() if value_dict == value)
return next(
key for key, value_dict in dictionary.items()
if value_dict == value
)


class MainWindow(QtWidgets.QMainWindow):
Expand Down Expand Up @@ -68,9 +65,8 @@ def load(self):
"""Sets the values from the config to the elements"""

# set current version in statusbar
self.ui.status_bar.showMessage(
self.tr('You are using version {}', '').format(str(config.version))
)
self.ui.status_bar.showMessage(self.tr('You are using version {}', '')
.format(str(config.version)))

# set the correct mode
mode = config.mode
Expand All @@ -85,9 +81,7 @@ def load(self):
self.ui.btn_schedule.setChecked(True)
self.ui.location.setVisible(False)

self.ui.toggle_notification.setChecked(
config.get_plugin_key('notification', PluginKey.ENABLED)
)
self.ui.toggle_notification.setChecked(config.get_plugin_key('notification', PluginKey.ENABLED))
self.ui.bootOffset.setValue(config.boot_offset)

# sets the correct time based on config
Expand Down Expand Up @@ -116,10 +110,7 @@ def load_location(self):

def load_plugins(self):
# First, remove sample plugin
sample_plugin = cast(
QGroupBox,
self.ui.plugins_scroll_content.findChild(QGroupBox, 'samplePluginGroupBox'),
)
sample_plugin = cast(QGroupBox, self.ui.plugins_scroll_content.findChild(QGroupBox, 'samplePluginGroupBox'))
sample_plugin.hide()

widget: QGroupBox
Expand All @@ -128,74 +119,45 @@ def load_plugins(self):
if plugin.name.casefold() == 'notification':
continue

widget = cast(
QGroupBox,
self.ui.plugins_scroll_content.findChild(
QGroupBox, 'group' + plugin.name
),
)
widget = cast(QGroupBox, self.ui.plugins_scroll_content.findChild(QGroupBox, 'group' + plugin.name))
if widget is None:
widget = plugin.get_widget(self.ui.plugins_scroll_content)
self.ui.plugins_scroll_content_layout.addWidget(widget)

assert widget is not None, f'No widget for plugin {plugin.name} found'

widget.toggled.connect(
lambda enabled, p=plugin: config.update_plugin_key(
p.name, PluginKey.ENABLED, enabled
)
)
lambda enabled, p=plugin:
config.update_plugin_key(p.name, PluginKey.ENABLED, enabled))

if plugin.available_themes:
# uses combobox instead of line edit
for child in widget.findChildren(QtWidgets.QComboBox):
is_dark: bool = (
widget.findChildren(QtWidgets.QComboBox).index(child) == 1
)
is_dark: bool = widget.findChildren(QtWidgets.QComboBox).index(child) == 1
child.currentTextChanged.connect(
lambda text, p=plugin, dark=is_dark: config.update_plugin_key(
p.name,
PluginKey.THEME_DARK if dark else PluginKey.THEME_LIGHT,
reverse_dict_search(p.available_themes, text),
)
)
reverse_dict_search(p.available_themes, text)))
else:
children: [QtWidgets.QLineEdit] = widget.findChildren(
QtWidgets.QLineEdit
)
children: [QtWidgets.QLineEdit] = widget.findChildren(QtWidgets.QLineEdit)
children[0].textChanged.connect(
lambda text, p=plugin: config.update_plugin_key(
p.name, PluginKey.THEME_LIGHT, text
)
)
lambda text, p=plugin: config.update_plugin_key(p.name, PluginKey.THEME_LIGHT, text))
children[1].textChanged.connect(
lambda text, p=plugin: config.update_plugin_key(
p.name, PluginKey.THEME_DARK, text
)
)
lambda text, p=plugin: config.update_plugin_key(p.name, PluginKey.THEME_DARK, text))

if plugin.name == 'Wallpaper':
children: [QtWidgets.QPushButton] = widget.findChildren(
QtWidgets.QDialogButtonBox
)
children: [QtWidgets.QPushButton] = widget.findChildren(QtWidgets.QDialogButtonBox)
children[0].clicked.connect(lambda: self.select_wallpaper(False))
children[1].clicked.connect(lambda: self.select_wallpaper(True))
elif plugin.name == 'Brave':
buttons: [QtWidgets.QPushButton] = widget.findChildren(
QtWidgets.QPushButton
)
buttons: [QtWidgets.QPushButton] = widget.findChildren(QtWidgets.QPushButton)
# this could be a loop, but it didn't work somehow
color_str_0 = config.get_plugin_key(
plugin.name, PluginKey.THEME_LIGHT
)
color_str_0 = config.get_plugin_key(plugin.name, PluginKey.THEME_LIGHT)
color_0 = QColor(color_str_0)
buttons[0].clicked.connect(
lambda: self.select_color(False, color_0)
)
buttons[0].clicked.connect(lambda: self.select_color(False, color_0))

color_str_1 = config.get_plugin_key(
plugin.name, PluginKey.THEME_DARK
)
color_str_1 = config.get_plugin_key(plugin.name, PluginKey.THEME_DARK)
color_1 = QColor(color_str_1)
buttons[1].clicked.connect(lambda: self.select_color(True, color_1))
plugin = None
Expand All @@ -204,10 +166,8 @@ def update_label_enabled(self):
time_light = self.ui.inp_time_light.time().toPython()
time_dark = self.ui.inp_time_dark.time().toPython()
self.ui.label_active.setText(
self.tr('Dark mode will be active between {} and {}.').format(
time_dark.strftime("%H:%M"), time_light.strftime("%H:%M")
)
)
self.tr('Dark mode will be active between {} and {}.')
.format(time_dark.strftime("%H:%M"), time_light.strftime("%H:%M")))

def setup_config_sync(self):
# set sunrise and sunset times if mode is set to followSun or coordinates changed
Expand All @@ -226,10 +186,7 @@ def setup_config_sync(self):
self.ui.btn_box.clicked.connect(self.save_config_to_file)

self.ui.toggle_notification.toggled.connect(
lambda enabled: config.update_plugin_key(
'notification', PluginKey.ENABLED, enabled
)
)
lambda enabled: config.update_plugin_key('notification', PluginKey.ENABLED, enabled))

self.ui.bootOffset.valueChanged.connect(self.update_boot_offset)

Expand Down Expand Up @@ -273,7 +230,10 @@ def update_location(self):
elif not config.update_location:
self.ui.location_input.setEnabled(True)

coordinates = [self.ui.inp_latitude.value(), self.ui.inp_longitude.value()]
coordinates = [
self.ui.inp_latitude.value(),
self.ui.inp_longitude.value()
]
config.location = coordinates
# update message and times
self.load_times()
Expand All @@ -282,31 +242,23 @@ def select_wallpaper(self, dark: bool):
message_light = self.tr('Open light wallpaper')
message_dark = self.tr('Open dark wallpaper')
file_name, _ = QFileDialog.getOpenFileName(
self,
message_dark if dark else message_light,
self, message_dark if dark else message_light,
QStandardPaths.standardLocations(QStandardPaths.PicturesLocation)[0],
'Images (*.png *.jpg *.jpeg *.JPG *.JPEG)',
)
'Images (*.png *.jpg *.jpeg *.JPG *.JPEG)')

group_wallpaper = self.ui.plugins_scroll_content.findChild(
QtWidgets.QGroupBox, 'groupWallpaper'
)
group_wallpaper = self.ui.plugins_scroll_content.findChild(QtWidgets.QGroupBox, 'groupWallpaper')
inputs_wallpaper = group_wallpaper.findChildren(QtWidgets.QLineEdit)
i = 1 if dark else 0
inputs_wallpaper[i].setText(file_name)

def select_color(self, dark: bool, initial_color: QColor):
selected_color = QColorDialog.getColor(initial_color)
group_brave = self.ui.plugins_scroll_content.findChild(
QtWidgets.QGroupBox, 'groupBrave'
)
group_brave = self.ui.plugins_scroll_content.findChild(QtWidgets.QGroupBox, 'groupBrave')
inputs_brave = group_brave.findChildren(QtWidgets.QLineEdit)
i = 1 if dark else 0
inputs_brave[i].setText(selected_color.name())
inputs_brave[i].setStyleSheet(
f'background-color: {selected_color.name()};'
f' color: {"white" if selected_color.lightness() <= 128 else "black"}'
)
inputs_brave[i].setStyleSheet(f'background-color: {selected_color.name()};'
f' color: {"white" if selected_color.lightness() <= 128 else "black"}')

def save_config_to_file(self, button):
"""Saves the config to the file or restores values"""
Expand All @@ -332,15 +284,10 @@ def should_close(self) -> bool:

# ask the user if he wants to save changes
if self.config_changed:
message = self.tr(
'The settings have been modified. Do you want to save them?'
)
ret = QMessageBox.warning(
self,
self.tr('Unsaved changes'),
message,
QMessageBox.Save | QMessageBox.Discard | QMessageBox.Cancel,
)
message = self.tr('The settings have been modified. Do you want to save them?')
ret = QMessageBox.warning(self, self.tr('Unsaved changes'),
message,
QMessageBox.Save | QMessageBox.Discard | QMessageBox.Cancel)
match ret:
case QMessageBox.Save:
# emulate click on apply-button
Expand Down

0 comments on commit 3960704

Please sign in to comment.