org-editor/main.py

149 lines
4.0 KiB
Python
Raw Normal View History

2021-04-02 22:16:06 +00:00
#!/usr/bin/env python3
import logging
import os
import sys
import time
from PySide2.QtCore import QObject, QThread, Signal, Slot
from PySide2.QtWidgets import (QApplication, QDialog, QGroupBox, QHBoxLayout,
QLabel, QLineEdit, QProgressBar, QPushButton,
2021-04-02 23:13:41 +00:00
QScrollArea, QTabBar, QVBoxLayout, QSplitter, QFrame)
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-04-02 23:13:41 +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
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__()
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)
self.edit = QLineEdit("", placeholderText='Search for notes')
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)
# 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 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-04-02 23:13:41 +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:
text = "{} {} {}".format(
item.scheduled.time,
item.state,
item.title,
)
label = QLabel(text=text)
layout.addWidget(label)
# if len(agenda.with_hour) > 0 and len(agenda.no_hour) > 0:
# layout.addWidget(QSplitter())
for item in agenda.no_hour:
text = "{} {} {}".format(
item.scheduled.time,
item.state,
item.title,
)
label = QLabel(text=text)
layout.addWidget(label)
frame = QFrame(self.results)
frame.setLayout(layout)
self.results.setWidget(frame)
2021-04-02 22:16:06 +00:00
def loadNotes(self):
logging.warning("loadNotes not yet implemented")
def loadTasks(self):
logging.warning("loadTasks not yet implemented")
# Create the Qt Application
2021-04-02 23:13:41 +00:00
if __name__ == '__main__':
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_())