Note_Qt/main.py

204 lines
7.3 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import sys
import os
import json
from PyQt6.QtWidgets import QApplication, QMainWindow, QFileDialog, QListWidgetItem, QLabel
from PyQt6.QtCore import QDir, QFileSystemWatcher, Qt
from PyQt6.QtGui import QGuiApplication
from startWindow import Ui_startWindow as Ui_StartWindow
from mainWindow import Ui_MainWindow as Ui_MainWindow2
from qt_markdown.qt_markdown import QtMarkDown
CONFIG_FILE = "catalogs.json"
class StartWindow(QMainWindow):
def __init__(self):
super().__init__()
self.ui = Ui_StartWindow()
self.ui.setupUi(self)
self.setWindowTitle("Start Window")
self.ui.createCatalog.clicked.connect(self.create_catalog)
self.ui.openCatalog.clicked.connect(self.open_catalog)
self.ui.startWindowList.itemClicked.connect(self.open_selected_catalog)
self.window_center()
self.load_catalogs()
def window_center(self):
screen = QGuiApplication.primaryScreen()
screen_geometry = screen.geometry()
window_geometry = self.frameGeometry()
window_geometry.moveCenter(screen_geometry.center())
self.move(window_geometry.topLeft())
def create_catalog(self):
folder_path = QFileDialog.getSaveFileName(self, "Create New Catalog", "", "All Files (*);;Directory")[0]
if folder_path:
if not os.path.exists(folder_path):
os.makedirs(folder_path)
self.update_catalogs(folder_path)
self.open_main_window("New Catalog", folder_path)
def open_catalog(self):
folder_path = QFileDialog.getExistingDirectory(self, "Select Directory", "")
if folder_path:
self.update_catalogs(folder_path)
self.open_main_window("Existing Catalog", folder_path)
def open_main_window(self, catalog_name, folder_path):
self.main_window = MainWindow(catalog_name, folder_path)
self.main_window.show()
self.close()
def load_catalogs(self):
if os.path.exists(CONFIG_FILE):
with open(CONFIG_FILE, "r") as f:
catalogs = json.load(f)
for catalog in catalogs:
item = QListWidgetItem(catalog)
self.ui.startWindowList.addItem(item)
def update_catalogs(self, folder_path):
catalogs = []
if os.path.exists(CONFIG_FILE):
with open(CONFIG_FILE, "r") as f:
catalogs = json.load(f)
if folder_path not in catalogs:
catalogs.append(folder_path)
with open(CONFIG_FILE, "w") as f:
json.dump(catalogs, f)
def open_selected_catalog(self, item):
folder_path = item.text()
self.open_main_window("Existing Catalog", folder_path)
class MainWindow(QMainWindow):
def __init__(self, catalog_name, folder_path):
super().__init__()
self.ui = Ui_MainWindow2()
self.ui.setupUi(self)
self.setWindowTitle("Main Window")
self.showMaximized()
self.folder_path = folder_path
self.ui.createFile.clicked.connect(self.create_file)
self.ui.back.clicked.connect(self.back_to_start)
self.ui.mainList.itemClicked.connect(self.load_selected_note)
if catalog_name == "New Catalog":
self.create_file()
self.ui.noteTitleEdit.textChanged.connect(self.save_note_title)
self.ui.noteEdit.textChanged.connect(self.save_note_content)
self.show_note_edit_and_toolbar()
else:
self.show_default_label()
self.show_directory_files()
self.file_watcher = QFileSystemWatcher()
self.file_watcher.addPath(self.folder_path)
self.file_watcher.directoryChanged.connect(self.show_directory_files)
self.qt_markdown = QtMarkDown(self.ui.noteEdit)
def load_selected_note(self, item):
note_name = item.text()
self.default_note_path = os.path.join(self.folder_path, note_name)
self.load_note_content()
self.ui.noteTitleEdit.setPlainText(note_name)
self.ui.noteTitleEdit.show()
self.ui.noteEdit.show()
self.show_note_edit_and_toolbar()
if hasattr(self, 'static_label'):
self.static_label.hide()
# Подключаем сигналы для автосохранения
self.ui.noteTitleEdit.textChanged.connect(self.save_note_title)
self.ui.noteEdit.textChanged.connect(self.save_note_content)
def show_note_edit_and_toolbar(self):
if not hasattr(self, 'toolbar'):
self.qt_markdown = QtMarkDown(self.ui.noteEdit)
self.toolbar = self.qt_markdown.get_toolbar()
self.ui.noteEdit.show()
self.ui.verticalLayout.insertWidget(0, self.toolbar)
def create_file(self):
base_name = "Без_Имени"
extension = ".md"
note_title = base_name + extension
counter = 1
while os.path.exists(os.path.join(self.folder_path, note_title)):
note_title = f"{base_name}_{counter}{extension}"
counter += 1
self.default_note_path = os.path.join(self.folder_path, note_title)
with open(self.default_note_path, "w") as f:
f.write("")
self.ui.noteTitleEdit.setPlainText(note_title)
self.ui.noteEdit.setPlainText("")
self.ui.noteTitleEdit.show()
self.ui.noteEdit.show()
if hasattr(self, 'static_label'):
self.static_label.hide()
self.ui.noteTitleEdit.textChanged.connect(self.save_note_title)
self.ui.noteEdit.textChanged.connect(self.save_note_content)
def load_note_content(self):
if os.path.exists(self.default_note_path):
with open(self.default_note_path, "r") as f:
content = f.read()
self.ui.noteEdit.setPlainText(content)
def save_note_content(self):
content = self.ui.noteEdit.toPlainText()
with open(self.default_note_path, "w") as f:
f.write(content)
def save_note_title(self):
new_note_title = self.ui.noteTitleEdit.toPlainText()
new_note_path = os.path.join(self.folder_path, new_note_title)
if new_note_path != self.default_note_path:
if os.path.exists(self.default_note_path):
os.rename(self.default_note_path, new_note_path)
self.default_note_path = new_note_path
def show_directory_files(self):
if os.path.exists(self.folder_path):
self.ui.mainList.clear()
dir_content = QDir(self.folder_path).entryList(QDir.Filter.Files)
for file_name in dir_content:
item = QListWidgetItem(file_name)
self.ui.mainList.addItem(item)
def show_default_label(self):
self.ui.noteTitleEdit.hide()
self.ui.noteEdit.hide()
if hasattr(self, 'toolbar'):
self.toolbar.hide()
if not hasattr(self, 'static_label'):
self.static_label = QLabel("Выберите файл из списка или создайте новый")
self.ui.verticalLayout.addWidget(self.static_label)
else:
self.static_label.show()
def back_to_start(self):
self.start_window = StartWindow()
self.start_window.show()
self.close()
if __name__ == "__main__":
app = QApplication(sys.argv)
start_window = StartWindow()
start_window.show()
sys.exit(app.exec())