Add search function

Add the possibility to search in the content
Display only line where a match was found
Highlight match
This commit is contained in:
cuissedemouche 2022-12-16 12:40:29 +00:00
parent 03c0ad0635
commit 28585c1089
4 changed files with 82 additions and 36 deletions

View File

@ -13,6 +13,9 @@
<property name="windowTitle">
<string>Form</string>
</property>
<property name="styleSheet">
<string notr="true">font: 11pt &quot;UbuntuMono NF&quot;;</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<widget class="QSplitter" name="splitter">
@ -22,7 +25,7 @@
<widget class="QWidget" name="layoutWidget">
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QLineEdit" name="lineEdit">
<widget class="QLineEdit" name="search_line_edit">
<property name="text">
<string>Search</string>
</property>

View File

@ -7,7 +7,7 @@
<x>0</x>
<y>0</y>
<width>1214</width>
<height>896</height>
<height>888</height>
</rect>
</property>
<property name="windowTitle">
@ -15,7 +15,7 @@
</property>
<property name="styleSheet">
<string notr="true">background-color: rgb(42, 42, 42);
font: 12pt &quot;UbuntuMono NF&quot;;
font: 11pt &quot;UbuntuMono NF&quot;;
selection-background-color: rgb(105, 105, 105);
color: rgb(230, 230, 230);</string>
</property>
@ -48,24 +48,27 @@ background-color: rgb(105, 105, 105);</string>
<enum>Qt::Horizontal</enum>
</property>
<property name="handleWidth">
<number>4</number>
<number>2</number>
</property>
<widget class="QWidget" name="layoutWidget">
<layout class="QVBoxLayout" name="verticalLayout">
<property name="spacing">
<number>4</number>
<number>2</number>
</property>
<item>
<widget class="QLineEdit" name="lineEdit">
<widget class="QLineEdit" name="search_line_edit">
<property name="styleSheet">
<string notr="true">background-color: rgb(42, 42, 42);</string>
</property>
<property name="text">
<string>Search</string>
<string/>
</property>
<property name="frame">
<bool>false</bool>
</property>
<property name="placeholderText">
<string>Search...</string>
</property>
</widget>
</item>
<item>
@ -80,19 +83,13 @@ background-color: rgb(105, 105, 105);</string>
</item>
</layout>
</widget>
<widget class="QPlainTextEdit" name="detail_plain_text_edit">
<widget class="QTextEdit" name="detail_text_edit">
<property name="styleSheet">
<string notr="true">background-color: rgb(42, 42, 42);</string>
</property>
<property name="frameShape">
<enum>QFrame::NoFrame</enum>
</property>
<property name="undoRedoEnabled">
<bool>false</bool>
</property>
<property name="lineWrapMode">
<enum>QPlainTextEdit::NoWrap</enum>
</property>
<property name="readOnly">
<bool>true</bool>
</property>

View File

@ -1,7 +1,8 @@
from PySide6.QtWidgets import QWidget
from PySide6.QtCore import QSize
from PySide6.QtGui import QTextCharFormat, QTextCursor, QColor
from ui_main_window import Ui_MainWindow
from file_cmds import FileCmds
from PySide6.QtCore import QSize
class MainWindow(QWidget, Ui_MainWindow):
@ -19,6 +20,7 @@ class MainWindow(QWidget, Ui_MainWindow):
def setupConnections(self):
self.tracks_list_widget.itemSelectionChanged.connect(self.popuplate_content)
self.search_line_edit.textChanged.connect(self.search)
def dragEnterEvent(self, event):
@ -54,5 +56,50 @@ class MainWindow(QWidget, Ui_MainWindow):
def popuplate_content(self):
item_selected = self.tracks_list_widget.selectedItems()
if item_selected:
text = self.file_cmds.tabs_content[item_selected[0].text()]
self.detail_plain_text_edit.setPlainText(text)
self.content = self.file_cmds.tabs_content[item_selected[0].text()]
self.detail_text_edit.setPlainText(self.content)
self.search()
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)

View File

@ -16,16 +16,16 @@ from PySide6.QtGui import (QBrush, QColor, QConicalGradient, QCursor,
QImage, QKeySequence, QLinearGradient, QPainter,
QPalette, QPixmap, QRadialGradient, QTransform)
from PySide6.QtWidgets import (QApplication, QFrame, QLineEdit, QListWidget,
QListWidgetItem, QPlainTextEdit, QSizePolicy, QSplitter,
QListWidgetItem, QSizePolicy, QSplitter, QTextEdit,
QVBoxLayout, QWidget)
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
if not MainWindow.objectName():
MainWindow.setObjectName(u"MainWindow")
MainWindow.resize(1214, 896)
MainWindow.resize(1214, 888)
MainWindow.setStyleSheet(u"background-color: rgb(42, 42, 42);\n"
"font: 12pt \"UbuntuMono NF\";\n"
"font: 11pt \"UbuntuMono NF\";\n"
"selection-background-color: rgb(105, 105, 105);\n"
"color: rgb(230, 230, 230);")
self.verticalLayout_2 = QVBoxLayout(MainWindow)
@ -38,19 +38,19 @@ class Ui_MainWindow(object):
"background-color: rgb(105, 105, 105);")
self.splitter.setFrameShadow(QFrame.Plain)
self.splitter.setOrientation(Qt.Horizontal)
self.splitter.setHandleWidth(4)
self.splitter.setHandleWidth(2)
self.layoutWidget = QWidget(self.splitter)
self.layoutWidget.setObjectName(u"layoutWidget")
self.verticalLayout = QVBoxLayout(self.layoutWidget)
self.verticalLayout.setSpacing(4)
self.verticalLayout.setSpacing(2)
self.verticalLayout.setObjectName(u"verticalLayout")
self.verticalLayout.setContentsMargins(0, 0, 0, 0)
self.lineEdit = QLineEdit(self.layoutWidget)
self.lineEdit.setObjectName(u"lineEdit")
self.lineEdit.setStyleSheet(u"background-color: rgb(42, 42, 42);")
self.lineEdit.setFrame(False)
self.search_line_edit = QLineEdit(self.layoutWidget)
self.search_line_edit.setObjectName(u"search_line_edit")
self.search_line_edit.setStyleSheet(u"background-color: rgb(42, 42, 42);")
self.search_line_edit.setFrame(False)
self.verticalLayout.addWidget(self.lineEdit)
self.verticalLayout.addWidget(self.search_line_edit)
self.tracks_list_widget = QListWidget(self.layoutWidget)
self.tracks_list_widget.setObjectName(u"tracks_list_widget")
@ -60,14 +60,12 @@ class Ui_MainWindow(object):
self.verticalLayout.addWidget(self.tracks_list_widget)
self.splitter.addWidget(self.layoutWidget)
self.detail_plain_text_edit = QPlainTextEdit(self.splitter)
self.detail_plain_text_edit.setObjectName(u"detail_plain_text_edit")
self.detail_plain_text_edit.setStyleSheet(u"background-color: rgb(42, 42, 42);")
self.detail_plain_text_edit.setFrameShape(QFrame.NoFrame)
self.detail_plain_text_edit.setUndoRedoEnabled(False)
self.detail_plain_text_edit.setLineWrapMode(QPlainTextEdit.NoWrap)
self.detail_plain_text_edit.setReadOnly(True)
self.splitter.addWidget(self.detail_plain_text_edit)
self.detail_text_edit = QTextEdit(self.splitter)
self.detail_text_edit.setObjectName(u"detail_text_edit")
self.detail_text_edit.setStyleSheet(u"background-color: rgb(42, 42, 42);")
self.detail_text_edit.setFrameShape(QFrame.NoFrame)
self.detail_text_edit.setReadOnly(True)
self.splitter.addWidget(self.detail_text_edit)
self.verticalLayout_2.addWidget(self.splitter)
@ -79,6 +77,7 @@ class Ui_MainWindow(object):
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(QCoreApplication.translate("MainWindow", u"Form", None))
self.lineEdit.setText(QCoreApplication.translate("MainWindow", u"Search", None))
self.search_line_edit.setText("")
self.search_line_edit.setPlaceholderText(QCoreApplication.translate("MainWindow", u"Search...", None))
# retranslateUi