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 threading
import webbrowser
import yaml
from PyQt6.QtCore import QFileSystemWatcher, Qt, QTimer, QSize, QRegularExpression
from PyQt6.QtGui import QAction, QStandardItem, QStandardItemModel, QIcon, QPalette, QSyntaxHighlighter, \
QTextCharFormat, QColor, QFont
from PyQt6.QtWebEngineCore import QWebEngineSettings
from PyQt6.QtWebEngineWidgets import QWebEngineView
from PyQt6.QtWidgets import QApplication, QMainWindow, QTextEdit, QHBoxLayout, QWidget, QToolBar, QStatusBar, \
QMessageBox, QTreeView, QFileDialog, QAbstractItemView, QLineEdit, QVBoxLayout, QStyle, QTabWidget, QLabel
from PyQt6.QtWidgets import QApplication, QMainWindow, QTextEdit, QWidget, QToolBar, QStatusBar, \
QMessageBox, QTreeView, QFileDialog, QAbstractItemView, QLineEdit, QVBoxLayout, QStyle, QTabWidget, QLabel, \
QSplitter
from fuzzywuzzy import fuzz
from markdown2 import markdown
@ -51,32 +53,38 @@ class MarkdownEditor(QMainWindow):
self.config_path = pathlib.Path(__file__).parent / 'config.yaml'
self.config = self.load_config()
self.setup_ui()
self.initialize_editor_preview()
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):
""" Initializes the Markdown editor application with UI setup and configuration management. """
logging.info("Initializing the Markdown Editor...")
self.dataDirectory = None
self.preview = None
self.editor = None
self.directoryTree = None
self.config_path = pathlib.Path(__file__).parent / 'config.yaml'
self.toolbar = None
if not self.config_path.exists():
self.config = {'window': {'width': 800, 'height': 600},
'dataDirectory': str(pathlib.Path(__file__).parent.resolve())}
self.save_config()
else:
self.config = self.load_config()
self.fileSystemWatcher = QFileSystemWatcher()
self.initialize_data_directory()
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)
"""Initializes the Markdown editor application with UI setup and configuration management."""
try:
logging.info("Initializing the Markdown Editor...")
self.fileSystemWatcher = QFileSystemWatcher()
self.initialize_data_directory()
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)
except Exception as e:
logging.error(f"Error during post initialization: {e}", exc_info=True)
QMessageBox.critical(self, "Initialization Error", f"An error occurred during initialization: {e}")
sys.exit(1)
def setup_ui(self):
""" Sets up the minimal UI components necessary for initial display. """
@ -126,19 +134,26 @@ class MarkdownEditor(QMainWindow):
directory_layout = QVBoxLayout()
directory_layout.addWidget(self.searchBox)
directory_layout.addWidget(self.directoryTree)
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)
split_layout = QHBoxLayout()
split_layout.addLayout(directory_layout, 0)
split_layout.addWidget(self.editor, 1)
split_layout.addWidget(self.preview, 1)
layout.addLayout(split_layout)
# Create a widget for directory layout
directory_widget = QWidget()
directory_widget.setLayout(directory_layout)
# Initialize the splitters
splitter_horizontal = QSplitter(Qt.Orientation.Horizontal)
splitter_vertical = QSplitter(Qt.Orientation.Vertical)
# Add widgets to horizontal splitter
splitter_horizontal.addWidget(directory_widget)
splitter_horizontal.addWidget(self.editor)
# 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)
self.setCentralWidget(central_widget)
self.setStatusBar(QStatusBar(self))
@ -205,9 +220,6 @@ class MarkdownEditor(QMainWindow):
toolbar.setToolButtonStyle(Qt.ToolButtonStyle.ToolButtonTextBesideIcon)
toolbar.addAction(self.create_action("Toggle Split View", QStyle.StandardPixmap.SP_FileDialogListView,
self.toggle_split_view))
toolbar.addAction(
self.create_action("Toggle Toolbar", QStyle.StandardPixmap.SP_ToolBarHorizontalExtensionButton,
self.toggle_toolbar))
return toolbar
def create_window_tab(self):
@ -237,16 +249,6 @@ class MarkdownEditor(QMainWindow):
action.triggered.connect(callback)
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):
self.searchText = text
if not text:
@ -431,27 +433,6 @@ class MarkdownEditor(QMainWindow):
action.triggered.connect(action_func)
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
def markdown_table_to_html(markdown_content):
"""
@ -765,11 +746,6 @@ class MarkdownEditor(QMainWindow):
self.preview.setVisible(not self.preview.isVisible())
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):
""" Minimize the main window. """
self.showMinimized()