From a7a8006145cccde8a1bbdc09cdba72a7734e25b8 Mon Sep 17 00:00:00 2001 From: Nao Pross Date: Sun, 8 Nov 2020 22:09:40 +0100 Subject: Add regexp search feature --- gui.py | 52 ++++++++++++++++++++++++++++++++++++++++++++++++---- muddle.ui | 35 +++++++++-------------------------- 2 files changed, 57 insertions(+), 30 deletions(-) diff --git a/gui.py b/gui.py index d5ea752..b2fc552 100644 --- a/gui.py +++ b/gui.py @@ -15,18 +15,30 @@ import tempfile from PyQt5 import uic from PyQt5.QtGui import QFont from PyQt5.Qt import QStyle -from PyQt5.QtCore import Qt, QThread, pyqtSlot, pyqtSignal, QObject + +from PyQt5.QtCore import ( + Qt, + QThread, + pyqtSlot, + pyqtSignal, + QObject, + QSortFilterProxyModel, +) + from PyQt5.QtWidgets import ( QApplication, QMainWindow, QWidget, + QTreeView, QTreeWidget, QTreeWidgetItem, QTreeWidgetItemIterator, + QHeaderView, QGridLayout, QHBoxLayout, QPushButton, QToolButton, + QLineEdit, QProgressBar, QTabWidget, QPlainTextEdit @@ -94,6 +106,7 @@ class MoodleItem(QTreeWidgetItem): self.setChildIndicatorPolicy(QTreeWidgetItem.DontShowIndicatorWhenChildless) self.setText(0, html.unescape(self.metadata.title)) + class MoodleFetcher(QThread): loadedItem = pyqtSignal(MoodleItem.Type, object) @@ -146,17 +159,28 @@ class MoodleFetcher(QThread): return [] +class MoodleTreeFilterModel(QSortFilterProxyModel): + def __init__(self): + super().__init__() + + class MoodleTreeWidget(QTreeWidget): def __init__(self, parent): super().__init__(parent) self.itemDoubleClicked.connect(self.onItemDoubleClicked, Qt.QueuedConnection) + self.setHeaderLabels(["Item", "Size"]) + self.sortByColumn(0, Qt.AscendingOrder) + self.lastInsertedItem = None self.worker = None @pyqtSlot(str, str) def refresh(self, instance_url, token): - if not self.worker or not self.worker.isFinished(): + if not self.worker or self.worker.isFinished(): + self.setSortingEnabled(False) + # TODO: remove elements if present + self.worker = MoodleFetcher(self, instance_url, token) self.worker.loadedItem.connect(self.onWorkerLoadedItem) self.worker.finished.connect(self.onWorkerDone) @@ -222,7 +246,6 @@ class MoodleTreeWidget(QTreeWidget): @pyqtSlot() def onWorkerDone(self): log.debug("worker done") - self.sortByColumn(0, Qt.AscendingOrder) self.setSortingEnabled(True) @@ -249,12 +272,33 @@ class MuddleWindow(QMainWindow): self.loghandler.newLogMessage.connect(self.onNewLogMessage) logging.getLogger("muddle").addHandler(self.loghandler) + # set up proxymodel for moodle treeview + moodleTreeWidget = MoodleTreeWidget(None) + self.filter = MoodleTreeFilterModel() + self.filter.setRecursiveFilteringEnabled(True) + + moodleTreeView = self.findChild(QTreeView, "moodleTree") + self.filter.setSourceModel(moodleTreeWidget.model()) + moodleTreeView.setModel(self.filter) + moodleTreeView.setColumnWidth(0, 420) + + # refresh moodle treeview refreshBtn = self.findChild(QToolButton, "refreshBtn") - moodleTreeWidget = self.findChild(MoodleTreeWidget, "moodleTree") refreshBtn.clicked.connect(lambda b: moodleTreeWidget.refresh(instance_url, token)) + # searchbar + searchBar = self.findChild(QLineEdit, "searchBar") + searchBar.textChanged.connect(self.onSearchBarTextChanged) + self.show() + @pyqtSlot(str) + def onSearchBarTextChanged(self, text): + if not text: + self.filter.invalidateFilter() + else: + self.filter.setFilterRegExp(text) + @pyqtSlot(str) def onNewLogMessage(self, msg): self.findChild(QPlainTextEdit, "logsTab").appendPlainText(msg) diff --git a/muddle.ui b/muddle.ui index e25a1a9..9737c7a 100644 --- a/muddle.ui +++ b/muddle.ui @@ -57,13 +57,13 @@ - false + true - Search + Search (regexp) @@ -77,23 +77,6 @@ - - - - false - - - - Item - - - - - Size - - - - @@ -111,6 +94,13 @@ + + + + false + + + @@ -223,13 +213,6 @@ - - - MoodleTreeWidget - QTreeWidget -
gui
-
-
-- cgit v1.2.1