Note_Qt/qt_markdown/toolbar/toolbar.py

94 lines
3.6 KiB
Python
Raw 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.

from PyQt6.QtWidgets import QWidget, QVBoxLayout, QHBoxLayout, QPushButton, QComboBox
from PyQt6.QtGui import QTextCharFormat, QFont, QTextCursor
class FormattingToolbar(QWidget):
def __init__(self, formatter):
super().__init__()
self.formatter = formatter
self.init_ui()
def init_ui(self):
layout = QVBoxLayout()
toolbar_layout = QHBoxLayout()
self.button_bold = QPushButton("Ж")
self.button_bold.setStyleSheet("font-weight: bold;")
self.button_bold.clicked.connect(lambda: self.formatter.toggle_special_symbols('**'))
self.button_italic = QPushButton("К")
self.button_italic.setStyleSheet("font-style: italic;")
self.button_italic.clicked.connect(lambda: self.formatter.toggle_special_symbols('*'))
self.button_strike = QPushButton("abc")
self.button_strike.setStyleSheet("text-decoration: line-through;")
self.button_strike.clicked.connect(lambda: self.formatter.toggle_special_symbols('~~'))
self.button_underline = QPushButton("Ч")
self.button_underline.setStyleSheet("text-decoration: underline;")
self.button_underline.clicked.connect(lambda: self.formatter.toggle_special_symbols('<u>', '</u>'))
self.button_header1 = QPushButton("H1")
self.button_header1.clicked.connect(self.insert_header1)
self.button_header2 = QPushButton("H2")
self.button_header2.clicked.connect(self.insert_header2)
self.header_drop = QComboBox()
self.header_drop.addItems(["H1", "H2", "H3", "H4", "H5", "H6"])
self.header_drop.currentIndexChanged.connect(self.handle_header_drop)
toolbar_layout.addWidget(self.button_bold)
toolbar_layout.addWidget(self.button_italic)
toolbar_layout.addWidget(self.button_strike)
toolbar_layout.addWidget(self.button_underline)
toolbar_layout.addWidget(self.button_header1)
toolbar_layout.addWidget(self.button_header2)
toolbar_layout.addWidget(self.header_drop)
layout.addLayout(toolbar_layout)
self.setLayout(layout)
def insert_header1(self):
self.insert_header('# ')
def insert_header2(self):
self.insert_header('## ')
def insert_header(self, header_symbol):
cursor = self.formatter.text_edit.textCursor()
cursor.beginEditBlock()
# Get selected text or word under cursor
cursor.select(QTextCursor.SelectionType.WordUnderCursor)
selected_text = cursor.selectedText()
# Check if there's already a header symbol at the beginning
cursor.movePosition(QTextCursor.MoveOperation.StartOfLine)
cursor.select(QTextCursor.SelectionType.LineUnderCursor)
line_text = cursor.selectedText().strip()
if line_text.startswith('#'):
# Remove existing header symbol and space
line_text = line_text.replace('#', '', 1).lstrip()
if selected_text:
# Replace the selected text with the header symbol + selected text
cursor.removeSelectedText()
cursor.insertText(header_symbol + selected_text)
else:
# If no word is selected, just insert the header symbol
cursor.insertText(header_symbol)
cursor.endEditBlock()
def handle_header_drop(self, index):
header_level = index + 1 # Since index starts from 0, but header levels start from 1
header_symbol = '#' * header_level + ' '
# Set the correct index in the headerDrop ComboBox
self.header_drop.setCurrentIndex(index)
# Call insert_header with the header_symbol
self.insert_header(header_symbol)