-
Notifications
You must be signed in to change notification settings - Fork 0
/
UI.py
176 lines (137 loc) · 5.89 KB
/
UI.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# Module imports
import sys
import time
import platform
import dateutil.parser
from PyQt5 import QtWidgets
from PyQt5 import QtCore
from PyQt5 import QtGui
from datetime import date
# User imports
class Window(QtWidgets.QMainWindow):
# Set defaults
width = 700
height = 550
title = "OpenJournal (alpha)"
def __init__(self, journal):
super().__init__()
self.firstView = True
self.JournalController = journal
QtWidgets.QShortcut(QtGui.QKeySequence("Ctrl+W"), self, self.closeProgram)
QtWidgets.QShortcut(QtGui.QKeySequence("Ctrl+E"), self, self.changeView)
self.buildUI()
self.startDateLoop()
def checkDate(self):
if self.JournalController.journal.date != date.today():
self.JournalController.reset()
self.updateDateLabel()
self.textEditor.setPlainText(self.JournalController.journal.text)
def startDateLoop(self):
self.timer = QtCore.QTimer()
self.timer.timeout.connect(self.checkDate)
self.timer.start(1)
def buildUI(self):
# Create our pages (stacks)
self.editStack = QtWidgets.QWidget()
self.viewStack = QtWidgets.QWidget()
self.editStackUI()
self.viewStackUI()
# Put them in a stack widget so we can rotate between them
self.stack = QtWidgets.QStackedWidget(self)
self.stack.addWidget(self.editStack)
self.stack.addWidget(self.viewStack)
self.setCentralWidget(self.stack)
# Set the initial size and title
self.initSize()
self.setWindowTitle(self.title)
# Show our UI
self.show()
def editStackUI(self):
grid = QtWidgets.QGridLayout()
grid.setSpacing(0)
grid.setContentsMargins(2,2,2,2)
self.setStyleSheet("background-color: white");
# Text editor widget
self.textEditor = QtWidgets.QPlainTextEdit(self)
self.textEditor.setFrameStyle(QtWidgets.QFrame.NoFrame);
self.textEditor.setPlainText(self.JournalController.journal.text)
# Directions widget
self.instructLabel = QtWidgets.QLabel()
self.instructLabel.setText("Press " + ("Cmd" if platform.system() == "Darwin" else "Ctrl") + "+E to view journals")
self.instructLabel.setFixedHeight(18)
self.instructLabel.setStyleSheet('color: gray')
# Current date widget
self.dateLabel = QtWidgets.QLabel()
self.dateLabel.setFixedHeight(18)
self.updateDateLabel()
grid.addWidget(self.textEditor, 0, 0, 100, 1)
grid.addWidget(self.instructLabel, 0, 1, 1, 1)
grid.addWidget(self.dateLabel, 1, 1, 1, 1, QtCore.Qt.AlignRight)
self.editStack.setLayout(grid)
def viewStackUI(self):
grid = QtWidgets.QGridLayout()
grid.setSpacing(0)
grid.setContentsMargins(2,2,2,2)
self.setStyleSheet("background-color: white");
# Text viewer widget
self.textViewer = QtWidgets.QPlainTextEdit(self)
self.textViewer.setFrameStyle(QtWidgets.QFrame.NoFrame);
self.textViewer.setReadOnly(True)
# Directions widget
self.instructLabel = QtWidgets.QLabel()
self.instructLabel.setText("Press " + ("Cmd" if platform.system() == "Darwin" else "Ctrl") + "+E to edit your journal")
self.instructLabel.setFixedHeight(18)
self.instructLabel.setStyleSheet('color: gray')
# Current date widget
self.dateLabel = QtWidgets.QLabel()
self.dateLabel.setFixedHeight(18)
self.updateDateLabel()
self.backButton = QtWidgets.QPushButton("<")
self.backButton.clicked.connect(self.goBack)
self.backButton.setFixedWidth(50)
self.forwardButton = QtWidgets.QPushButton(">")
self.forwardButton.clicked.connect(self.goForward)
self.forwardButton.setFixedWidth(50)
grid.addWidget(self.textViewer, 0, 0, 100, 1)
grid.addWidget(self.instructLabel, 0, 1, 1, 2)
grid.addWidget(self.dateLabel, 1, 1, 1, 2, QtCore.Qt.AlignRight)
grid.addWidget(self.backButton, 2, 1, 1, 1, QtCore.Qt.AlignRight)
grid.addWidget(self.forwardButton, 2, 2, 1, 1, QtCore.Qt.AlignRight)
self.viewStack.setLayout(grid)
def initSize(self):
# Get user's computer resolution to properly size the window
screen = QtWidgets.QDesktopWidget().screenGeometry()
# Make the window height 9/10th's of the screen height
self.height = (8/10) * screen.height()
# Get the coordinates for a centered window
centeredX = (screen.width() / 2) - (self.width / 2)
centeredY = (screen.height() / 2) - (self.height / 2)
# Change height based on screen
self.setGeometry(centeredX, centeredY, self.width, self.height)
def updateDateLabel(self):
self.dateLabel.setText(time.strftime("%B %d, %Y"))
def goBack(self):
journal = self.JournalController.back()
if journal:
self.textViewer.setPlainText(journal.text)
self.dateLabel.setText(journal.date.strftime("%B %d, %Y"))
def goForward(self):
journal = self.JournalController.forward()
if journal:
self.textViewer.setPlainText(journal.text)
self.dateLabel.setText(journal.date.strftime("%B %d, %Y"))
def keyReleaseEvent(self, event):
if self.stack.currentIndex() == 0:
text = str(self.textEditor.toPlainText())
self.JournalController.update(text)
def closeProgram(self):
QtWidgets.QApplication.quit()
def changeView(self):
if self.stack.currentIndex() == 0:
self.stack.setCurrentIndex(1)
journal = self.JournalController.back()
journal = self.JournalController.forward()
if journal:
self.textViewer.setPlainText(journal.text)
elif self.stack.currentIndex() == 1:
self.stack.setCurrentIndex(0)