org-editor/main.py

176 lines
4.8 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
2021-04-02 23:29:17 +00:00
from PySide2.QtWidgets import (QApplication, QDialog, QLabel, QLineEdit, QProgressBar, QScrollArea, QTabBar,
2021-09-14 21:02:40 +00:00
QVBoxLayout, QFrame, QScroller, QHBoxLayout, QPushButton, QGroupBox)
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__()
2021-04-02 23:35:34 +00:00
self.setWindowTitle("OrgEditor")
scrSize = self.screen().size()
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)
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(
self.results.viewport(), QScroller.LeftMouseButtonGesture,
)
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):
self.tabBar.setCurrentIndex(1)
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-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:
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)
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
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_())