mediaInfoSama/mediainfosama/main_window.py

117 lines
3.7 KiB
Python
Raw Normal View History

from PySide6.QtWidgets import QWidget
from PySide6.QtCore import QSize
2022-12-26 20:17:00 +01:00
from PySide6.QtGui import QTextCharFormat, QTextCursor, QColor, QTextBlockFormat
from ui_main_window import Ui_MainWindow
from file_cmds import FileCmds
class MainWindow(QWidget, Ui_MainWindow):
def __init__(self):
super().__init__()
self.setupUi(self)
self.window_title = "mediaInfoSama"
self.setWindowTitle(self.window_title)
self.setAcceptDrops(True)
self.setupConnections()
self.splitter.setStretchFactor(0, False)
self.splitter.setStretchFactor(1, True)
2022-12-13 21:35:40 +01:00
2022-12-18 22:15:18 +01:00
self.detail_text_edit.setFont("UbuntuMono NF")
self.detail_text_edit.zoomIn(2)
2022-12-13 21:35:40 +01:00
def setupConnections(self):
self.tracks_list_widget.itemSelectionChanged.connect(self.popuplate_content)
self.search_line_edit.textChanged.connect(self.search)
2022-12-13 21:35:40 +01:00
def dragEnterEvent(self, event):
event.accept()
def dragMoveEvent(self, event):
event.accept()
def dropEvent(self, event):
self.file_path = event.mimeData().urls()[0].toLocalFile()
self.file_cmds = FileCmds(self.file_path)
if self.file_cmds.file_exist() == False:
return
self.setWindowTitle(f"{self.file_path} - {self.window_title}")
2022-12-13 21:35:40 +01:00
self.populate_tabs()
self.tracks_list_widget.item(0).setSelected(True)
2022-12-14 00:12:32 +01:00
def populate_tabs(self):
self.tracks_list_widget.clear()
self.tracks_list_widget.addItems(self.file_cmds.tabs_list)
for i in range(self.tracks_list_widget.count()):
item = self.tracks_list_widget.item(i)
height = self.tracks_list_widget.sizeHintForRow(0)
item.setSizeHint(QSize(0, height+5))
2022-12-14 00:12:32 +01:00
def popuplate_content(self):
item_selected = self.tracks_list_widget.selectedItems()
if item_selected:
self.content = self.file_cmds.tabs_content[item_selected[0].text()]
self.detail_text_edit.setPlainText(self.content)
self.search()
2022-12-26 20:17:00 +01:00
self.wrap_line_indent()
2022-12-26 20:17:00 +01:00
def wrap_line_indent(self):
cursor = QTextCursor(self.detail_text_edit.document())
cursor.select(QTextCursor.Document)
fmt = QTextBlockFormat()
fmt.setLeftMargin(100)
fmt.setTextIndent(-100)
cursor.mergeBlockFormat(fmt)
def search(self):
to_search = self.search_line_edit.text()
if not to_search:
self.detail_text_edit.setPlainText(self.content)
return
content_list = self.content.split("\n")
searched_content = []
for line in content_list:
if to_search.lower() in line.lower():
searched_content.append(line)
# elif line == "" and not searched_content[-1] == "":
elif line == "":
if searched_content and not searched_content[-1] == "":
searched_content.append(line)
searched_content = "\n".join(searched_content)
self.detail_text_edit.setPlainText(searched_content)
def add_selection():
sel = self.detail_text_edit.ExtraSelection()
sel.cursor = cursor
sel.format = fmt
selections.append(sel)
selections = []
doc = self.detail_text_edit.document()
fmt = QTextCharFormat()
fmt.setBackground(QColor(0,128,0))
cursor = QTextCursor()
while True:
cursor = doc.find(to_search, cursor)
if cursor.isNull() or cursor.atEnd():
if cursor.atEnd():
if cursor.selectedText() == to_search:
add_selection()
break
add_selection()
self.detail_text_edit.setExtraSelections(selections)