Skip to content
This repository has been archived by the owner on Jun 24, 2024. It is now read-only.

Commit

Permalink
Merge pull request #50 from MemerGamer/48-remove-leftover-code-for-th…
Browse files Browse the repository at this point in the history
…e-settings-page

Resolve "Remove leftover code for the settings page"
  • Loading branch information
MemerGamer authored Feb 23, 2024
2 parents ec8f1e7 + a28d379 commit 0318743
Show file tree
Hide file tree
Showing 5 changed files with 267 additions and 176 deletions.
1 change: 1 addition & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Welcome to ManimStudio's documentation!
src/core/settings
src/core/project_creation_dialog
src/core/project_opening_dialog
src/core/videoeditor
src/utils/logger_utility
src/ui/welcome_ui
src/ui/settings_ui
Expand Down
7 changes: 7 additions & 0 deletions docs/src/core/videoeditor.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
videoeditor module
==================

.. automodule:: videoeditor
:members:
:undoc-members:
:show-inheritance:
192 changes: 24 additions & 168 deletions src/core/main.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
from datetime import datetime
import sys
import json
from pathlib import Path
from typing import Optional, Dict, List
from PySide6.QtWidgets import QApplication, QMainWindow, QWidget, QDialog, QHeaderView
from PySide6.QtCore import Signal, Qt
from typing import Optional, Dict
from PySide6.QtWidgets import QApplication, QMainWindow, QWidget, QHeaderView
from PySide6.QtCore import Signal
from PySide6.QtGui import (
QPixmap,
QResizeEvent,
QAction,
QStandardItemModel,
QStandardItem,
)
from PySide6.QtWidgets import QSizePolicy


# Add the parent directory of 'src' to sys.path
Expand All @@ -22,20 +18,21 @@


# UI imports
from src.ui.settings_ui import Ui_Form # noqa: E402
from src.ui.videoeditor_ui import Ui_Form as Ui_VideoEditor # noqa: E402
from src.ui.welcome_ui import Ui_MainWindow # noqa: E402


# Core imports
from src.core.project_creation_dialog import ProjectCreationDialog # noqa: E402
from src.core.project_opening_dialog import ProjectOpeningDialog # noqa: E402
from src.core.settings import ( # noqa: E402
load_settings,
load_themes,
load_current_theme,
update_settings,
get_recent_project_paths,
load_ui,
update_image,
)
from src.core.videoeditor import VideoEditorWindow # noqa: E402

# Utils imports
from src.utils.logger_utility import logger # noqa: E402
Expand All @@ -56,38 +53,28 @@ def __init__(self, parent: Optional[QWidget] = None) -> None:
self.themes: Dict = load_themes()
self.current_theme: Dict = load_current_theme()
logger.info("Main window initialized")
self.load_ui()
self.ui = load_ui(
main_window=self,
ui=Ui_MainWindow(),
settings=self.settings,
current_theme=self.current_theme,
themes=self.themes,
)
# Populate the recent projects list with the 10 latest projects
self.populate_recent_projects()
self.ui.recentProjectsTableView.doubleClicked.connect(
lambda: self.open_project_from_list(
self.ui.recentProjectsTableView.selectedIndexes()[0]
)
)

@logger.catch
def resizeEvent(self, event: QResizeEvent) -> None:
"""Resize event for the main window"""
super().resizeEvent(event)
self.update_image()
update_image(ui=self.ui, current_theme=self.current_theme)
self.update_table_view()

@logger.catch
def update_image(self) -> None:
"""Update the image based on the theme and resize event."""

if (
"latte" in self.current_theme["name"].lower()
or "light" in self.current_theme["name"].lower()
):
image_path: str = "docs/_static/ManimStudioLogoLight.png"
else:
image_path: str = "docs/_static/ManimStudioLogoDark.png"

pixmap: QPixmap = QPixmap(image_path)
if not pixmap.isNull():
scaledPixmap: QPixmap = pixmap.scaled(
self.ui.label.size(),
Qt.AspectRatioMode.KeepAspectRatio,
Qt.TransformationMode.SmoothTransformation,
)
self.ui.label.setPixmap(scaledPixmap)

self.ui.label.setAlignment(Qt.AlignmentFlag.AlignCenter)

@logger.catch
def update_table_view(self) -> None:
"""Update the table view based on the theme and resize event."""
Expand All @@ -96,55 +83,6 @@ def update_table_view(self) -> None:
QHeaderView.ResizeMode.Stretch
)

@logger.catch
def apply_stylesheet(self) -> None:
"""Apply the stylesheet to the main window and update the image based on the theme"""

# Set the custom stylesheet based on the current theme
self.customStyleSheet: str = f"background-color: {self.current_theme['background']}; color: {self.current_theme['font']}; border-color: {self.current_theme['primary']}; font-size: {self.settings['fontSize']}px; font-family: {self.settings['fontFamily']}; "
self.setStyleSheet(self.customStyleSheet)

self.customMenubarStylesheet = str(
f"background-color: {self.current_theme['primary']}; color: {self.current_theme['font']}; border-color: {self.current_theme['primary']}; font-size: {self.settings['fontSize']}px; font-family: {self.settings['fontFamily']}; "
)
self.ui.menubar.setStyleSheet(self.customMenubarStylesheet)

# Update the image
self.update_image()

self.styleSheetUpdated.emit(self.customStyleSheet)
logger.info("Stylesheet applied")

@logger.catch
def load_ui(self) -> None:
"""Load the UI from the .ui file"""

self.ui: Ui_MainWindow = Ui_MainWindow()
self.ui.setupUi(self)

self.ui.label.setSizePolicy(
QSizePolicy(QSizePolicy.Policy.Preferred, QSizePolicy.Policy.Preferred)
)

# Apply the theme
self.apply_stylesheet()

# Create new menubar item
settings_action = QAction("Settings", self)
settings_action.triggered.connect(self.open_settings_dialog)
self.ui.menubar.addAction(settings_action)

self.ui.newProjectBtn.clicked.connect(self.show_project_creation_dialog)
self.ui.openProjectBtn.clicked.connect(self.show_project_open_dialog)

# Populate the recent projects list with the 10 latest projects
self.populate_recent_projects()
self.ui.recentProjectsTableView.doubleClicked.connect(
self.open_project_from_list
)

logger.info("UI loaded")

@logger.catch
def populate_recent_projects(self):
# Create a model with 3 columns
Expand Down Expand Up @@ -187,83 +125,6 @@ def open_project_from_list(self, index):
# Now you can call the method to open the project
self.open_video_editor(project_path)

@logger.catch
def open_settings_dialog(self) -> None:
"""Open the settings dialog"""
self.settingsDialog: QDialog = QDialog()
self.uiSettings: Ui_Form = Ui_Form()
self.uiSettings.setupUi(self.settingsDialog)

# Change window title
self.settingsDialog.setWindowTitle("Settings")

# Inherit the theme from the main window
self.settingsDialog.setStyleSheet(self.customStyleSheet)
self.styleSheetUpdated.connect(self.settingsDialog.setStyleSheet)

# Load settings and themes to the dialog
self.uiSettings.fontSizeSpinBox.setValue(self.settings["fontSize"])
self.uiSettings.fontComboBox.setCurrentText(self.settings["fontFamily"])

self.uiSettings.themeComboBox.clear()

# Populate the theme combobox with full theme data
for theme_module in self.themes:
for variant in theme_module["variants"]:
variant_data = json.dumps(variant)
self.uiSettings.themeComboBox.addItem(variant["name"], variant_data)

# Set the current theme in the combobox
current_theme_index = self.uiSettings.themeComboBox.findText(
self.current_theme["name"]
)
if current_theme_index >= 0:
self.uiSettings.themeComboBox.setCurrentIndex(current_theme_index)

self.uiSettings.saveSettingsBtn.clicked.connect(
self.update_settings_from_dialog
)

self.settingsDialog.exec()

@logger.catch
def update_settings_from_dialog(self) -> None:
"""Update the settings from the dialog, and update the UI"""

# Get recentProjects array from the current settings
recentProjectPaths: List[str] = self.settings.get("recentProjectPaths", [])

# Get recentProjectCreationPaths array from the current settings
recentProjectCreationPaths: List[str] = self.settings.get(
"recentProjectCreationPaths", []
)

# Get the current values from the dialog
fontSize: int = self.uiSettings.fontSizeSpinBox.value()
fontFamily: str = self.uiSettings.fontComboBox.currentText()

# Extract full theme data from the selected item in the combobox
theme_data_json: str = self.uiSettings.themeComboBox.currentData()
selected_theme: Dict = json.loads(theme_data_json)

# Create a new settings object
new_settings: Dict = {
"fontSize": fontSize,
"fontFamily": fontFamily,
"theme": selected_theme,
"recentProjectCreationPaths": recentProjectCreationPaths,
"recentProjectPaths": recentProjectPaths,
}

# Pass the new settings to the settings module
if update_settings(new_settings):
# Update the global settings variable
self.settings = new_settings
# Update the current theme
self.current_theme = load_current_theme()
# Apply the stylesheet
self.apply_stylesheet()

@logger.catch
def show_project_creation_dialog(self) -> None:
"""Show the project creation dialog"""
Expand Down Expand Up @@ -304,12 +165,7 @@ def show_project_open_dialog(self) -> None:
def open_video_editor(self, project_file_path: str) -> None:
"""Open the video editor with the project file"""
try:
self.videoEditor: QDialog = QDialog()
self.uiVideoEditor: Ui_VideoEditor = Ui_VideoEditor()
self.uiVideoEditor.setupUi(self.videoEditor)

# Apply the theme
self.videoEditor.setStyleSheet(self.customStyleSheet)
self.videoEditor = VideoEditorWindow(self)

# Change window title as current project name with file path
self.videoEditor.setWindowTitle(f"Manim Studio - {project_file_path}")
Expand All @@ -319,7 +175,7 @@ def open_video_editor(self, project_file_path: str) -> None:

self.close()

self.videoEditor.exec()
self.videoEditor.show()
except Exception as e:
logger.error(f"Error opening video editor: {e}")
pass
Expand Down
Loading

0 comments on commit 0318743

Please sign in to comment.