org-editor/main.py

211 lines
5.7 KiB
Python
Raw Permalink Normal View History

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