EU-Utility-Plugins-Repo/plugins/clock_widget/plugin.py

274 lines
8.9 KiB
Python

"""
Clock Widget Plugin for EU-Utility
A customizable clock widget with 12/24 hour format and date display.
"""
from PyQt6.QtWidgets import (
QWidget, QVBoxLayout, QHBoxLayout, QLabel,
QPushButton, QCheckBox, QComboBox, QFrame
)
from PyQt6.QtCore import Qt, QTimer, QTime, QDate
from plugins.base_plugin import BasePlugin
class ClockWidget(QFrame):
"""Clock widget that can be added to dashboard or overlay."""
def __init__(self, config=None, parent=None):
super().__init__(parent)
self.config = config or {
'show_seconds': True,
'use_24h': False,
'show_date': True,
'font_size': 32
}
self._setup_ui()
self._start_timer()
def _setup_ui(self):
"""Setup widget UI."""
self.setStyleSheet("""
QFrame {
background-color: rgba(35, 40, 55, 220);
border: 1px solid rgba(100, 110, 130, 100);
border-radius: 8px;
padding: 10px;
}
""")
layout = QVBoxLayout(self)
layout.setSpacing(5)
layout.setContentsMargins(15, 15, 15, 15)
# Time label
self.time_label = QLabel("--:--:--")
font_size = self.config.get('font_size', 32)
self.time_label.setStyleSheet(f"""
QLabel {{
color: #4ecdc4;
font-size: {font_size}px;
font-weight: bold;
font-family: 'Consolas', 'Monaco', monospace;
}}
""")
self.time_label.setAlignment(Qt.AlignmentFlag.AlignCenter)
layout.addWidget(self.time_label)
# Date label
if self.config.get('show_date', True):
self.date_label = QLabel("----/--/--")
self.date_label.setStyleSheet("""
QLabel {
color: rgba(255, 255, 255, 150);
font-size: 14px;
}
""")
self.date_label.setAlignment(Qt.AlignmentFlag.AlignCenter)
layout.addWidget(self.date_label)
self._update_time()
def _start_timer(self):
"""Start the update timer."""
self.timer = QTimer(self)
self.timer.timeout.connect(self._update_time)
self.timer.start(1000) # Update every second
def _update_time(self):
"""Update the time display."""
current_time = QTime.currentTime()
if self.config.get('use_24h', False):
if self.config.get('show_seconds', True):
time_text = current_time.toString("HH:mm:ss")
else:
time_text = current_time.toString("HH:mm")
else:
if self.config.get('show_seconds', True):
time_text = current_time.toString("hh:mm:ss AP")
else:
time_text = current_time.toString("hh:mm AP")
self.time_label.setText(time_text)
# Update date
if self.config.get('show_date', True) and hasattr(self, 'date_label'):
current_date = QDate.currentDate()
self.date_label.setText(current_date.toString("yyyy-MM-dd"))
def update_config(self, config):
"""Update widget configuration."""
self.config.update(config)
# Re-setup UI to apply changes
self._setup_ui()
class ClockWidgetPlugin(BasePlugin):
"""Clock widget plugin for EU-Utility."""
name = "Clock Widget"
version = "1.0.0"
author = "ImpulsiveFPS"
description = "A customizable clock widget with 12/24 hour format and date display"
def initialize(self):
"""Initialize the clock widget plugin."""
self.widget = None
self.config = self.load_data('clock_config', {
'show_seconds': True,
'use_24h': False,
'show_date': True,
'font_size': 32
})
def get_ui(self):
"""Create the plugin settings UI."""
from PyQt6.QtWidgets import QWidget, QVBoxLayout, QHBoxLayout, QLabel
widget = QWidget()
layout = QVBoxLayout(widget)
layout.setSpacing(15)
layout.setContentsMargins(20, 20, 20, 20)
# Header
header = QLabel("⏰ Clock Widget")
header.setStyleSheet("font-size: 24px; font-weight: bold; color: white;")
layout.addWidget(header)
# Preview
preview_frame = QFrame()
preview_frame.setStyleSheet("""
QFrame {
background-color: rgba(35, 40, 55, 200);
border: 1px solid rgba(100, 110, 130, 100);
border-radius: 8px;
}
""")
preview_layout = QVBoxLayout(preview_frame)
preview_label = QLabel("Preview:")
preview_label.setStyleSheet("color: rgba(255, 255, 255, 150);")
preview_layout.addWidget(preview_label)
# Create preview widget
preview_widget = ClockWidget(self.config)
preview_layout.addWidget(preview_widget)
layout.addWidget(preview_frame)
# Settings
settings_frame = QFrame()
settings_frame.setStyleSheet("""
QFrame {
background-color: rgba(35, 40, 55, 200);
border-radius: 8px;
}
""")
settings_layout = QVBoxLayout(settings_frame)
settings_layout.setSpacing(10)
# 12/24 hour format
format_layout = QHBoxLayout()
format_label = QLabel("Time Format:")
format_label.setStyleSheet("color: white;")
format_layout.addWidget(format_label)
self.format_combo = QComboBox()
self.format_combo.addItems(["12-hour (AM/PM)", "24-hour"])
self.format_combo.setCurrentIndex(1 if self.config.get('use_24h') else 0)
self.format_combo.setStyleSheet("""
QComboBox {
background-color: rgba(30, 35, 45, 200);
color: white;
border: 1px solid rgba(100, 110, 130, 80);
padding: 8px;
min-width: 150px;
}
""")
format_layout.addWidget(self.format_combo)
format_layout.addStretch()
settings_layout.addLayout(format_layout)
# Show seconds
self.seconds_cb = QCheckBox("Show Seconds")
self.seconds_cb.setChecked(self.config.get('show_seconds', True))
self.seconds_cb.setStyleSheet("color: white;")
settings_layout.addWidget(self.seconds_cb)
# Show date
self.date_cb = QCheckBox("Show Date")
self.date_cb.setChecked(self.config.get('show_date', True))
self.date_cb.setStyleSheet("color: white;")
settings_layout.addWidget(self.date_cb)
layout.addWidget(settings_frame)
# Buttons
btn_layout = QHBoxLayout()
save_btn = QPushButton("💾 Save Settings")
save_btn.setStyleSheet("""
QPushButton {
background-color: #4ecdc4;
color: #141f23;
padding: 10px 20px;
border: none;
border-radius: 4px;
font-weight: bold;
}
""")
save_btn.clicked.connect(self._save_settings)
btn_layout.addWidget(save_btn)
create_btn = QPushButton("📌 Create Widget")
create_btn.setStyleSheet("""
QPushButton {
background-color: #ff8c42;
color: white;
padding: 10px 20px;
border: none;
border-radius: 4px;
font-weight: bold;
}
""")
create_btn.clicked.connect(self._create_widget)
btn_layout.addWidget(create_btn)
btn_layout.addStretch()
layout.addLayout(btn_layout)
layout.addStretch()
return widget
def _save_settings(self):
"""Save clock settings."""
self.config['use_24h'] = (self.format_combo.currentIndex() == 1)
self.config['show_seconds'] = self.seconds_cb.isChecked()
self.config['show_date'] = self.date_cb.isChecked()
self.save_data('clock_config', self.config)
# Update existing widget if any
if self.widget:
self.widget.update_config(self.config)
def _create_widget(self):
"""Create a clock widget in the overlay."""
try:
from core.widget_system import get_widget_manager
widget_manager = get_widget_manager()
# Create the clock widget
self.widget = ClockWidget(self.config)
self.widget.setWindowTitle("Clock")
self.widget.show()
print("[ClockWidget] Clock widget created and displayed")
except Exception as e:
print(f"[ClockWidget] Error creating widget: {e}")