Add base note searching functionality.

This commit is contained in:
Sergio Martínez Portela 2021-09-15 00:04:39 +02:00
parent df5931aeb7
commit a8e667dbce
2 changed files with 85 additions and 26 deletions

View File

@ -7,7 +7,8 @@ from typing import List
import org_rw import org_rw
from org_rw import OrgDoc, OrgTime from org_rw import OrgDoc, OrgTime
EXTENSIONS = ( ".org", ".org.txt" ) EXTENSIONS = (".org", ".org.txt")
def is_today(ot: OrgTime): def is_today(ot: OrgTime):
now = datetime.now() now = datetime.now()
@ -19,10 +20,12 @@ def is_today(ot: OrgTime):
class Agenda: class Agenda:
def __init__(self, /, def __init__(
with_hour: List[org_rw.Headline], self,
no_hour: List[org_rw.Headline], /,
): with_hour: List[org_rw.Headline],
no_hour: List[org_rw.Headline],
):
self.with_hour = with_hour self.with_hour = with_hour
self.no_hour = no_hour self.no_hour = no_hour
@ -52,7 +55,7 @@ class DocumentManager:
# Prune dirs # Prune dirs
i = 0 i = 0
while i < len(dirs): while i < len(dirs):
if dirs[i].startswith('.git'): if dirs[i].startswith(".git"):
del dirs[i] del dirs[i]
else: else:
i += 1 i += 1
@ -79,7 +82,6 @@ class DocumentManager:
self.docs = docs self.docs = docs
def get_agenda(self) -> Agenda: def get_agenda(self) -> Agenda:
headline_count = 0 headline_count = 0
items_in_agenda = [] items_in_agenda = []
now = datetime.now() now = datetime.now()
@ -114,5 +116,27 @@ class DocumentManager:
return Agenda( return Agenda(
with_hour=sorted(items_with_hour, key=lambda x: x.scheduled.time), with_hour=sorted(items_with_hour, key=lambda x: x.scheduled.time),
no_hour=other_items no_hour=other_items,
) )
def get_notes(self, query) -> List[org_rw.Headline]:
headline_count = 0
t0 = datetime.now()
notes = []
query = [q.lower() for q in query]
for doc in self.docs:
for hl in doc.getAllHeadlines():
headline_count += 1
data = "\n".join(hl.get_contents("raw")).lower()
if all([q in data for q in query]):
notes.append(hl)
logging.info(
"Filtered {} to {} items in {:.3f}s".format(
headline_count, len(notes), (datetime.now() - t0).total_seconds()
)
)
return notes

71
main.py
View File

@ -4,19 +4,23 @@ import logging
import os import os
import sys import sys
import time import time
import webbrowser
from PySide2.QtCore import QObject, QThread, Signal, Slot from PySide2.QtCore import QObject, QThread, Signal, Slot
from PySide2.QtWidgets import (QApplication, QDialog, QLabel, QLineEdit, QProgressBar, QScrollArea, QTabBar, from PySide2.QtWidgets import (QApplication, QDialog, QFrame, QGroupBox,
QVBoxLayout, QFrame, QScroller, QHBoxLayout, QPushButton, QGroupBox) QHBoxLayout, QLabel, QLineEdit, QProgressBar,
QPushButton, QScrollArea, QScroller, QTabBar,
QVBoxLayout)
import doc_manager import doc_manager
DOCS_PATH = os.environ['ORG_PATH'] DOCS_PATH = os.environ["ORG_PATH"]
class LoadDoneSignal(QObject): class LoadDoneSignal(QObject):
sig = Signal(doc_manager.DocumentManager) sig = Signal(doc_manager.DocumentManager)
class DocumentLoader(QThread): class DocumentLoader(QThread):
def __init__(self, manager): def __init__(self, manager):
QThread.__init__(self, None) QThread.__init__(self, None)
@ -34,8 +38,7 @@ class Dialog(QDialog):
self.setWindowTitle("OrgEditor") self.setWindowTitle("OrgEditor")
scrSize = self.screen().size() scrSize = self.screen().size()
self.resize(scrSize.width() / 1.5, self.resize(scrSize.width() / 1.5, scrSize.height() / 1.5)
scrSize.height() / 1.5)
self.loader = None self.loader = None
self.manager = doc_manager.DocumentManager(DOCS_PATH) self.manager = doc_manager.DocumentManager(DOCS_PATH)
@ -46,7 +49,7 @@ class Dialog(QDialog):
self.progressBar.setRange(0, 0) # Make undetermined self.progressBar.setRange(0, 0) # Make undetermined
layout.addWidget(self.progressBar) layout.addWidget(self.progressBar)
self.edit = QLineEdit("", placeholderText='Search for notes') self.edit = QLineEdit("", placeholderText="Search for notes")
self.edit.textEdited.connect(self.on_text_edited) self.edit.textEdited.connect(self.on_text_edited)
layout.addWidget(self.edit) layout.addWidget(self.edit)
@ -55,7 +58,8 @@ class Dialog(QDialog):
self.results = QScrollArea(widgetResizable=True) self.results = QScrollArea(widgetResizable=True)
layout.addWidget(self.results) layout.addWidget(self.results)
QScroller.grabGesture( QScroller.grabGesture(
self.results.viewport(), QScroller.LeftMouseButtonGesture, self.results.viewport(),
QScroller.LeftMouseButtonGesture,
) )
# Options # Options
@ -73,7 +77,10 @@ class Dialog(QDialog):
@Slot() @Slot()
def on_text_edited(self): def on_text_edited(self):
self.tabBar.setCurrentIndex(1) if self.tabBar.currentIndex() != 1:
self.tabBar.setCurrentIndex(1)
else:
self.loadNotes()
@Slot() @Slot()
def update_tab(self): def update_tab(self):
@ -105,7 +112,9 @@ class Dialog(QDialog):
self.update_tab() self.update_tab()
def longoperationcomplete(self, data): def longoperationcomplete(self, data):
logging.info("Loading complete in {:.3f}s".format(time.time() - self.loading_start_time)) logging.info(
"Loading complete in {:.3f}s".format(time.time() - self.loading_start_time)
)
self.endLoad() self.endLoad()
def loadAgenda(self): def loadAgenda(self):
@ -141,32 +150,58 @@ class Dialog(QDialog):
state_button.setFlat(True) state_button.setFlat(True)
box.addWidget(state_button) box.addWidget(state_button)
box.addWidget( box.addWidget(QLabel(text=f"{item.scheduled.time}", maximumWidth=200))
QLabel(text=f"{item.scheduled.time}", maximumWidth=200) box.addWidget(QLabel(text=f"{item.title}"))
)
box.addWidget(
QLabel(text=f"{item.title}")
)
def on_clicked(): def on_clicked():
state_button.setText('DONE') state_button.setText("DONE")
# state_button.setFlat(True) # state_button.setFlat(True)
# item.state = 'DONE' # item.state = 'DONE'
if not item.is_done: if not item.is_done:
state_button.clicked.connect(on_clicked) state_button.clicked.connect(on_clicked)
return frame
def build_note_task_widget(self, item):
box = QHBoxLayout()
frame = QGroupBox()
frame.setLayout(box)
titleButton = QPushButton(text=f"{item.title}")
box.addWidget(titleButton)
def on_clicked():
webbrowser.open("org-protocol://org-id?id=" + item.id)
titleButton.clicked.connect(on_clicked)
return frame return frame
def loadNotes(self): def loadNotes(self):
logging.warning("loadNotes not yet implemented") query = self.edit.text()
notes = self.manager.get_notes(query.split())
old = self.results.layout()
if old:
print("Deleting old")
old.deleteLater()
layout = QVBoxLayout()
for note in notes:
layout.addWidget(self.build_note_task_widget(note))
frame = QFrame(self.results)
frame.setLayout(layout)
self.results.setWidget(frame)
def loadTasks(self): def loadTasks(self):
logging.warning("loadTasks not yet implemented") logging.warning("loadTasks not yet implemented")
# Create the Qt Application # Create the Qt Application
if __name__ == '__main__': if __name__ == "__main__":
logging.basicConfig(level=logging.INFO, format="%(levelname)-8s %(message)s") logging.basicConfig(level=logging.INFO, format="%(levelname)-8s %(message)s")
app = QApplication(sys.argv) app = QApplication(sys.argv)