Added QSplitter, removed Toolbar

This commit is contained in:
Isaak Buslovich 2024-01-17 23:10:26 +01:00
parent 998fd28691
commit 1b2529be13
Signed by: Isaak
GPG Key ID: EEC31D6437FBCC63

130
main.py
View File

@ -18,14 +18,16 @@ import platform
import sys import sys
import threading import threading
import webbrowser import webbrowser
import yaml import yaml
from PyQt6.QtCore import QFileSystemWatcher, Qt, QTimer, QSize, QRegularExpression from PyQt6.QtCore import QFileSystemWatcher, Qt, QTimer, QSize, QRegularExpression
from PyQt6.QtGui import QAction, QStandardItem, QStandardItemModel, QIcon, QPalette, QSyntaxHighlighter, \ from PyQt6.QtGui import QAction, QStandardItem, QStandardItemModel, QIcon, QPalette, QSyntaxHighlighter, \
QTextCharFormat, QColor, QFont QTextCharFormat, QColor, QFont
from PyQt6.QtWebEngineCore import QWebEngineSettings from PyQt6.QtWebEngineCore import QWebEngineSettings
from PyQt6.QtWebEngineWidgets import QWebEngineView from PyQt6.QtWebEngineWidgets import QWebEngineView
from PyQt6.QtWidgets import QApplication, QMainWindow, QTextEdit, QHBoxLayout, QWidget, QToolBar, QStatusBar, \ from PyQt6.QtWidgets import QApplication, QMainWindow, QTextEdit, QWidget, QToolBar, QStatusBar, \
QMessageBox, QTreeView, QFileDialog, QAbstractItemView, QLineEdit, QVBoxLayout, QStyle, QTabWidget, QLabel QMessageBox, QTreeView, QFileDialog, QAbstractItemView, QLineEdit, QVBoxLayout, QStyle, QTabWidget, QLabel, \
QSplitter
from fuzzywuzzy import fuzz from fuzzywuzzy import fuzz
from markdown2 import markdown from markdown2 import markdown
@ -51,32 +53,38 @@ class MarkdownEditor(QMainWindow):
self.config_path = pathlib.Path(__file__).parent / 'config.yaml' self.config_path = pathlib.Path(__file__).parent / 'config.yaml'
self.config = self.load_config() self.config = self.load_config()
self.setup_ui() self.setup_ui()
self.initialize_editor_preview()
QTimer.singleShot(0, self.post_init) QTimer.singleShot(0, self.post_init)
def initialize_editor_preview(self):
""" Initialize the editor and preview widgets """
self.editor = QTextEdit()
self.editor.textChanged.connect(self.update_word_count)
self.editor.textChanged.connect(self.update_preview)
self.highlighter = MarkdownHighlighter(self.editor.document())
self.preview = QWebEngineView()
web_engine_settings = self.preview.page().settings()
web_engine_settings.setAttribute(QWebEngineSettings.WebAttribute.LocalContentCanAccessFileUrls, True)
web_engine_settings.setAttribute(QWebEngineSettings.WebAttribute.LocalContentCanAccessRemoteUrls, True)
def post_init(self): def post_init(self):
""" Initializes the Markdown editor application with UI setup and configuration management. """ """Initializes the Markdown editor application with UI setup and configuration management."""
logging.info("Initializing the Markdown Editor...") try:
self.dataDirectory = None logging.info("Initializing the Markdown Editor...")
self.preview = None self.fileSystemWatcher = QFileSystemWatcher()
self.editor = None self.initialize_data_directory()
self.directoryTree = None self.initialize_ui()
self.config_path = pathlib.Path(__file__).parent / 'config.yaml' self.update_directory_tree_view()
self.toolbar = None self.fileSystemWatcher.directoryChanged.connect(self.update_directory_tree_view)
if not self.config_path.exists(): self.load_initial_file()
self.config = {'window': {'width': 800, 'height': 600}, self.searchTimer = QTimer()
'dataDirectory': str(pathlib.Path(__file__).parent.resolve())} self.searchTimer.setSingleShot(True)
self.save_config() self.searchTimer.timeout.connect(self.perform_search)
else: except Exception as e:
self.config = self.load_config() logging.error(f"Error during post initialization: {e}", exc_info=True)
self.fileSystemWatcher = QFileSystemWatcher() QMessageBox.critical(self, "Initialization Error", f"An error occurred during initialization: {e}")
self.initialize_data_directory() sys.exit(1)
self.initialize_ui()
self.update_directory_tree_view()
self.fileSystemWatcher.directoryChanged.connect(self.update_directory_tree_view)
self.load_initial_file()
self.searchTimer = QTimer()
self.searchTimer.setSingleShot(True)
self.searchTimer.timeout.connect(self.perform_search)
def setup_ui(self): def setup_ui(self):
""" Sets up the minimal UI components necessary for initial display. """ """ Sets up the minimal UI components necessary for initial display. """
@ -126,19 +134,26 @@ class MarkdownEditor(QMainWindow):
directory_layout = QVBoxLayout() directory_layout = QVBoxLayout()
directory_layout.addWidget(self.searchBox) directory_layout.addWidget(self.searchBox)
directory_layout.addWidget(self.directoryTree) directory_layout.addWidget(self.directoryTree)
self.editor = QTextEdit()
self.editor.textChanged.connect(self.update_word_count) # Create a widget for directory layout
self.editor.textChanged.connect(self.update_preview) directory_widget = QWidget()
self.highlighter = MarkdownHighlighter(self.editor.document()) directory_widget.setLayout(directory_layout)
self.preview = QWebEngineView()
web_engine_settings = self.preview.page().settings() # Initialize the splitters
web_engine_settings.setAttribute(QWebEngineSettings.WebAttribute.LocalContentCanAccessFileUrls, True) splitter_horizontal = QSplitter(Qt.Orientation.Horizontal)
web_engine_settings.setAttribute(QWebEngineSettings.WebAttribute.LocalContentCanAccessRemoteUrls, True) splitter_vertical = QSplitter(Qt.Orientation.Vertical)
split_layout = QHBoxLayout()
split_layout.addLayout(directory_layout, 0) # Add widgets to horizontal splitter
split_layout.addWidget(self.editor, 1) splitter_horizontal.addWidget(directory_widget)
split_layout.addWidget(self.preview, 1) splitter_horizontal.addWidget(self.editor)
layout.addLayout(split_layout)
# Add widgets to vertical splitter
splitter_vertical.addWidget(splitter_horizontal)
splitter_vertical.addWidget(self.preview)
# Add the main splitter to the layout
layout.addWidget(splitter_vertical)
central_widget.setLayout(layout) central_widget.setLayout(layout)
self.setCentralWidget(central_widget) self.setCentralWidget(central_widget)
self.setStatusBar(QStatusBar(self)) self.setStatusBar(QStatusBar(self))
@ -205,9 +220,6 @@ class MarkdownEditor(QMainWindow):
toolbar.setToolButtonStyle(Qt.ToolButtonStyle.ToolButtonTextBesideIcon) toolbar.setToolButtonStyle(Qt.ToolButtonStyle.ToolButtonTextBesideIcon)
toolbar.addAction(self.create_action("Toggle Split View", QStyle.StandardPixmap.SP_FileDialogListView, toolbar.addAction(self.create_action("Toggle Split View", QStyle.StandardPixmap.SP_FileDialogListView,
self.toggle_split_view)) self.toggle_split_view))
toolbar.addAction(
self.create_action("Toggle Toolbar", QStyle.StandardPixmap.SP_ToolBarHorizontalExtensionButton,
self.toggle_toolbar))
return toolbar return toolbar
def create_window_tab(self): def create_window_tab(self):
@ -237,16 +249,6 @@ class MarkdownEditor(QMainWindow):
action.triggered.connect(callback) action.triggered.connect(callback)
return action return action
def create_toolbar(self):
""" Creates a toolbar for the ribbon interface. """
toolbar = QToolBar()
toolbar.setIconSize(QSize(16, 16))
toolbar.setToolButtonStyle(Qt.ToolButtonStyle.ToolButtonTextBesideIcon)
toolbar.addAction(self.create_action("New", QStyle.StandardPixmap.SP_FileIcon, self.new_file))
toolbar.addAction(self.create_action("Open", QStyle.StandardPixmap.SP_DirOpenIcon, self.open_file))
toolbar.addAction(self.create_action("Save", QStyle.StandardPixmap.SP_DialogSaveButton, self.save_entry))
return toolbar
def on_search_text_changed(self, text): def on_search_text_changed(self, text):
self.searchText = text self.searchText = text
if not text: if not text:
@ -431,27 +433,6 @@ class MarkdownEditor(QMainWindow):
action.triggered.connect(action_func) action.triggered.connect(action_func)
menu.addAction(action) menu.addAction(action)
def create_tool_bar(self):
"""
Create the main toolbar for the application. It adds actions like New, Open, and Save with icons.
"""
self.toolbar = QToolBar("Main Toolbar")
self.addToolBar(self.toolbar)
self.add_actions_to_toolbar(self.toolbar, [
("New", "📄", self.new_file),
("Open", "📂", self.open_file),
("Save", "💾", self.save_entry)
])
def add_actions_to_toolbar(self, toolbar, actions):
"""
Add actions to a given toolbar.
"""
for name, emoji, handler in actions:
action = QAction(emoji + ' ' + name, self)
action.triggered.connect(handler)
toolbar.addAction(action)
@staticmethod @staticmethod
def markdown_table_to_html(markdown_content): def markdown_table_to_html(markdown_content):
""" """
@ -765,11 +746,6 @@ class MarkdownEditor(QMainWindow):
self.preview.setVisible(not self.preview.isVisible()) self.preview.setVisible(not self.preview.isVisible())
self.centralWidget().layout().activate() self.centralWidget().layout().activate()
def toggle_toolbar(self):
""" Toggle the visibility of the toolbar. """
if self.toolbar:
self.toolbar.setVisible(not self.toolbar.isVisible())
def minimize(self): def minimize(self):
""" Minimize the main window. """ """ Minimize the main window. """
self.showMinimized() self.showMinimized()