-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathgui.py
172 lines (142 loc) · 5.93 KB
/
gui.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
from PyQt5.QtWidgets import (
QApplication,
QComboBox,
QWidget,
QVBoxLayout,
QHBoxLayout,
QLabel,
QLineEdit,
QTextEdit,
QPushButton,
)
from PyQt5.QtCore import QDate, QDateTime, QTime, QEvent
import sys
import os
class ScrollableComboBox(QComboBox):
def __init__(self, items=[], wraparound=False, parent=None):
super().__init__(parent)
self.addItems(items)
self.wraparound = wraparound
self.installEventFilter(self)
def eventFilter(self, source, event):
if event.type() == QEvent.Wheel and source is self:
currentIndex = self.currentIndex()
if event.angleDelta().y() > 0: # Scroll up
newIndex = currentIndex - 1
if self.wraparound and newIndex < 0:
newIndex = self.count() - 1
else: # Scroll down
newIndex = currentIndex + 1
if self.wraparound and newIndex >= self.count():
newIndex = 0
# Update only if newIndex is within bounds or wraparound is enabled
if 0 <= newIndex < self.count() or self.wraparound:
self.setCurrentIndex(newIndex)
return True
return super().eventFilter(source, event)
class CustomDateTimePicker(QWidget):
def __init__(self, overwrite=True):
super().__init__()
self.overwrite = overwrite
self.initUI()
def initUI(self):
mainLayout = QVBoxLayout(self)
headerLayout = QHBoxLayout()
mediums = ["Message", "SMS", "WhatsApp"]
self.mediumBox = ScrollableComboBox(mediums, self)
headerLayout.addWidget(self.mediumBox)
self.recipientEntry = QLineEdit(self)
self.recipientEntry.setPlaceholderText("Recipient Name")
headerLayout.addWidget(self.recipientEntry)
mainLayout.addLayout(headerLayout)
# Message text input
self.messageText = QTextEdit(self)
self.messageText.setPlaceholderText("Your message...")
mainLayout.addWidget(self.messageText)
pickerLayout = QHBoxLayout()
currentDate = QDate.currentDate()
currentTime = QTime.currentTime()
months = [QDate.longMonthName(month) for month in range(1, 13)]
self.monthBox = ScrollableComboBox(months, self)
self.monthBox.setCurrentIndex(currentDate.month() - 1)
pickerLayout.addWidget(self.monthBox)
days = [
str(day) for day in range(1, 32)
] # Placeholder range, adjust later based on the month
self.dayBox = ScrollableComboBox(days, self)
self.dayBox.setCurrentIndex(currentDate.day() - 1)
pickerLayout.addWidget(self.dayBox)
# Adjust hours to 12-hour format
hours = [f"{hour:02d}" for hour in range(1, 13)]
self.hourBox = ScrollableComboBox(hours, wraparound=True, parent=self)
hourIn12HFormat = (currentTime.hour() % 12) or 12
self.hourBox.setCurrentIndex(hourIn12HFormat - 1)
pickerLayout.addWidget(self.hourBox)
minutes = [f"{minute:02d}" for minute in range(60)]
self.minuteBox = ScrollableComboBox(minutes, self)
self.minuteBox.setCurrentIndex(currentTime.minute())
pickerLayout.addWidget(self.minuteBox)
# Add AM/PM selection
ampm = ["AM", "PM"]
self.ampmBox = ScrollableComboBox(ampm, wraparound=True, parent=self)
self.ampmBox.setCurrentIndex(0 if currentTime.hour() < 12 else 1)
pickerLayout.addWidget(self.ampmBox)
mainLayout.addLayout(pickerLayout)
self.setLayout(mainLayout)
self.setWindowTitle("Schedule Message")
self.scheduleButton = QPushButton("Schedule", self)
self.scheduleButton.clicked.connect(self.createScheduleFile)
mainLayout.addWidget(self.scheduleButton)
def createScheduleFile(self):
recipientName = self.recipientEntry.text()
message = self.messageText.toPlainText()
month = self.monthBox.currentText()
day = self.dayBox.currentText()
hour = self.hourBox.currentText()
minute = self.minuteBox.currentText()
ampm = self.ampmBox.currentText()
medium = self.mediumBox.currentText()
s = open("SETTINGS.txt", "r").read()
lines = s.split("\n")
lines_filtered = [
e.strip() for e in lines if e != "" and not e.strip().startswith("#")
]
print(lines_filtered)
d = {e.split("=")[0]: e.split("=")[1] for e in lines_filtered}
root = os.path.abspath(d["SCHEDULED_TEXTS_DIRECTORY"])
# Constructing filename
if "=" in recipientName:
true_recipient = recipientName.split("=")[0]
else:
true_recipient = recipientName
fileName = f"{medium} {true_recipient} {month} {day}, {hour}:{minute}{ampm}.txt"
fileName = os.path.join(root, fileName)
settingsEdited = False
if "=" in recipientName:
name = recipientName.split("=")[0]
number = recipientName.split("=")[1]
if name in d.keys():
if d[name] != number:
if self.overwrite:
d[name] = number
settingsEdited = True
else:
raise ValueError(
f"Recipient {name} already exists with different number"
)
else:
d[name] = number
settingsEdited = True
if settingsEdited:
print(f"Adding {name}={number} to SETTINGS.txt")
with open("SETTINGS.txt", "w") as file:
file.write("\n".join([f"{k}={v}" for k, v in d.items()]))
# Writing the message to the file
with open(fileName, "w") as file:
file.write(message)
self.close()
if __name__ == "__main__":
app = QApplication(sys.argv)
picker = CustomDateTimePicker()
picker.show()
sys.exit(app.exec_())