org-editor/main.py
2021-09-15 00:04:39 +02:00

211 lines
5.7 KiB
Python
Executable File

#!/usr/bin/env python3
import logging
import os
import sys
import time
import webbrowser
from PySide2.QtCore import QObject, QThread, Signal, Slot
from PySide2.QtWidgets import (QApplication, QDialog, QFrame, QGroupBox,
QHBoxLayout, QLabel, QLineEdit, QProgressBar,
QPushButton, QScrollArea, QScroller, QTabBar,
QVBoxLayout)
import doc_manager
DOCS_PATH = os.environ["ORG_PATH"]
class LoadDoneSignal(QObject):
sig = Signal(doc_manager.DocumentManager)
class DocumentLoader(QThread):
def __init__(self, manager):
QThread.__init__(self, None)
self.manager = manager
self.signal = LoadDoneSignal()
def run(self):
self.manager.load()
self.signal.sig.emit(self.manager)
class Dialog(QDialog):
def __init__(self):
super(Dialog, self).__init__()
self.setWindowTitle("OrgEditor")
scrSize = self.screen().size()
self.resize(scrSize.width() / 1.5, scrSize.height() / 1.5)
self.loader = None
self.manager = doc_manager.DocumentManager(DOCS_PATH)
layout = QVBoxLayout()
# Edit box
self.progressBar = QProgressBar()
self.progressBar.setRange(0, 0) # Make undetermined
layout.addWidget(self.progressBar)
self.edit = QLineEdit("", placeholderText="Search for notes")
self.edit.textEdited.connect(self.on_text_edited)
layout.addWidget(self.edit)
layout.setSpacing(0)
self.results = QScrollArea(widgetResizable=True)
layout.addWidget(self.results)
QScroller.grabGesture(
self.results.viewport(),
QScroller.LeftMouseButtonGesture,
)
# Options
self.tabBar = QTabBar(shape=QTabBar.RoundedSouth)
self.tabBar.addTab("Agenda")
self.tabBar.addTab("Notes")
self.tabBar.addTab("Tasks")
self.tabBar.currentChanged.connect(self.update_tab)
layout.addWidget(self.tabBar)
self.setLayout(layout)
self.startLoad()
@Slot()
def on_text_edited(self):
if self.tabBar.currentIndex() != 1:
self.tabBar.setCurrentIndex(1)
else:
self.loadNotes()
@Slot()
def update_tab(self):
tabIndex = self.tabBar.currentIndex()
if tabIndex == 0:
self.loadAgenda()
elif tabIndex == 1:
self.loadNotes()
elif tabIndex == 2:
self.loadTasks()
def startLoad(self):
self.edit.setDisabled(True)
self.edit.setVisible(False)
self.tabBar.setDisabled(True)
self.progressBar.setVisible(True)
self.loader = DocumentLoader(self.manager)
self.loader.signal.sig.connect(self.longoperationcomplete)
self.loading_start_time = time.time()
self.loader.start()
def endLoad(self):
self.edit.setDisabled(False)
self.edit.setVisible(True)
self.tabBar.setDisabled(False)
self.progressBar.setVisible(False)
self.update_tab()
def longoperationcomplete(self, data):
logging.info(
"Loading complete in {:.3f}s".format(time.time() - self.loading_start_time)
)
self.endLoad()
def loadAgenda(self):
agenda = self.manager.get_agenda()
old = self.results.layout()
if old:
print("Deleting old")
old.deleteLater()
layout = QVBoxLayout()
for item in agenda.with_hour:
layout.addWidget(self.build_agenda_task_widget(item))
# if len(agenda.with_hour) > 0 and len(agenda.no_hour) > 0:
# layout.addWidget(QSplitter())
for item in agenda.no_hour:
layout.addWidget(self.build_agenda_task_widget(item))
frame = QFrame(self.results)
frame.setLayout(layout)
self.results.setWidget(frame)
def build_agenda_task_widget(self, item):
box = QHBoxLayout()
frame = QGroupBox()
frame.setLayout(box)
state_button = QPushButton(text=f"{item.state or '-'}", maximumWidth=60)
if item.is_done:
state_button.setFlat(True)
box.addWidget(state_button)
box.addWidget(QLabel(text=f"{item.scheduled.time}", maximumWidth=200))
box.addWidget(QLabel(text=f"{item.title}"))
def on_clicked():
state_button.setText("DONE")
# state_button.setFlat(True)
# item.state = 'DONE'
if not item.is_done:
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
def loadNotes(self):
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):
logging.warning("loadTasks not yet implemented")
# Create the Qt Application
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO, format="%(levelname)-8s %(message)s")
app = QApplication(sys.argv)
dialog = Dialog()
sys.exit(dialog.exec_())