-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalltracker.py
281 lines (226 loc) · 10.7 KB
/
calltracker.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# Import necessary libraries
import csv
import os
import sys
from datetime import datetime
# Import PyQt6 components
from PyQt6.QtCore import QModelIndex, QSettings, Qt, QTimer
from PyQt6.QtGui import QColor, QKeySequence, QPalette
from PyQt6.QtWidgets import QApplication, QHBoxLayout, QHeaderView, QMainWindow, QPushButton, QTableWidget, QTableWidgetItem, QVBoxLayout, QWidget
# Define a custom QTableWidget class to handle keyboard events
class CustomTableWidget(QTableWidget):
# Define the custom keyPressEvent method for handling keyboard events
def keyPressEvent(self, event):
if event.matches(QKeySequence.StandardKey.Copy):
self.copy_selection()
elif event.matches(QKeySequence.StandardKey.Paste):
self.paste_data()
elif event.key() in (Qt.Key.Key_Delete, Qt.Key.Key_Backspace):
self.delete_or_clear()
else:
super().keyPressEvent(event)
# Copy the selected data from the table to the clipboard
def copy_selection(self):
selected_ranges = self.selectedRanges()
if not selected_ranges:
return
first_row = selected_ranges[0].topRow()
last_row = selected_ranges[0].bottomRow()
first_col = selected_ranges[0].leftColumn()
last_col = selected_ranges[0].rightColumn()
table_data = []
for row in range(first_row, last_row + 1):
row_data = []
for col in range(first_col, last_col + 1):
item = self.item(row, col)
row_data.append(item.text() if item else '')
table_data.append('\t'.join(row_data))
clipboard = QApplication.clipboard()
clipboard.setText('\n'.join(table_data))
# Paste data from the clipboard to the table
def paste_data(self):
clipboard = QApplication.clipboard()
text = clipboard.text()
if not text:
return
rows = text.split('\n')
row_count = len(rows)
col_count = 0
for row in rows:
col_count = max(col_count, len(row.split('\t')))
current_row = self.currentRow()
current_col = self.currentColumn()
if current_row + row_count > self.rowCount():
self.setRowCount(current_row + row_count)
if current_col + col_count > self.columnCount():
self.setColumnCount(current_col + col_count)
for r, row in enumerate(rows):
cols = row.split('\t')
for c, cell in enumerate(cols):
self.setItem(current_row + r, current_col + c, QTableWidgetItem(cell))
# Delete or clear the selected data in the table
def delete_or_clear(self):
selected_indexes = self.selectedIndexes()
selected_rows = sorted(set(index.row() for index in selected_indexes))
if len(selected_rows) == 1 and self.selectionModel().isRowSelected(selected_rows[0], QModelIndex()):
self.removeRow(selected_rows[0])
else:
for index in selected_indexes:
self.setItem(index.row(), index.column(), QTableWidgetItem(""))
# Define the main TimeTracker class that extends QMainWindow
class TimeTracker(QMainWindow):
def __init__(self):
super().__init__()
self.autosave_timer = QTimer(self)
self.end_call_button = QPushButton("End Call", self)
self.new_call_button = QPushButton("New Call", self)
self.new_activity_button = QPushButton("New Activity", self)
self.table = CustomTableWidget(0, 6, self)
self.activity_table = CustomTableWidget(0, 2, self) # Table for activities
self.call_counter = 1
self.init_ui()
# Initialize the user interface
def init_ui(self):
self.setWindowTitle("Time Tracker")
self.setGeometry(100, 100, 800, 400)
central_widget = QWidget(self)
layout = QVBoxLayout(central_widget)
self.table.setHorizontalHeaderLabels(["Call Number", "Time Call Taken", "Time on Call", "Ticket Number", "Closed/Client/Callback", "Notes"])
self.table.horizontalHeader().setSectionResizeMode(5, QHeaderView.ResizeMode.Stretch)
self.table.setSelectionBehavior(QTableWidget.SelectionBehavior.SelectItems)
self.table.setSelectionMode(QTableWidget.SelectionMode.ExtendedSelection)
layout.addWidget(self.table)
self.activity_table.setHorizontalHeaderLabels(["Activity", "Notes"])
self.activity_table.horizontalHeader().setSectionResizeMode(1, QHeaderView.ResizeMode.Stretch) # Stretch the Notes column
self.activity_table.setSelectionBehavior(QTableWidget.SelectionBehavior.SelectItems)
self.activity_table.setSelectionMode(QTableWidget.SelectionMode.ExtendedSelection)
layout.addWidget(self.activity_table)
buttons_layout = QHBoxLayout()
buttons_layout.addWidget(self.new_activity_button)
buttons_layout.addWidget(self.new_call_button)
layout.addLayout(buttons_layout) # Add buttons_layout to the main layout
self.new_activity_button.clicked.connect(self.new_activity) # Connect new_activity_button to new_activity function
self.new_call_button.clicked.connect(self.new_call)
self.end_call_button.clicked.connect(self.end_call)
layout.addWidget(self.end_call_button)
self.setCentralWidget(central_widget)
self.autosave_timer.timeout.connect(self.save_data)
self.autosave_timer.start(60 * 1000) # Save data every 60 seconds
self.load_data()
# New function to create a new activity entry in the activity table
def new_activity(self):
row_position = self.activity_table.rowCount()
self.activity_table.insertRow(row_position)
self.activity_table.setCurrentCell(row_position, 0)
# Create a new call entry in the table
def new_call(self):
row_position = self.table.rowCount()
self.table.insertRow(row_position)
call_number = f"Call {row_position + 1}" # Use row_position + 1 as the call number
time_taken = datetime.now().strftime("%I:%M %p")
self.table.setItem(row_position, 0, QTableWidgetItem(call_number))
self.table.setItem(row_position, 1, QTableWidgetItem(time_taken))
self.table.setCurrentCell(row_position, 0)
# End the current call and calculate the duration
def end_call(self):
row_position = self.table.currentRow()
if row_position >= 0:
time_taken_item = self.table.item(row_position, 1)
if time_taken_item:
time_taken_str = time_taken_item.text()
time_taken = datetime.strptime(time_taken_str, "%I:%M %p")
current_time = datetime.now()
duration = int(round((current_time - time_taken).seconds / 60))
self.table.setItem(row_position, 2, QTableWidgetItem(f"{duration} minutes"))
# Save the table data to CSV files
def save_data(self):
app_dir = os.path.dirname(os.path.abspath(sys.argv[0]))
TimeTracker.save_table_data(self.table, os.path.join(app_dir, 'autosave_calls.csv'))
TimeTracker.save_table_data(self.activity_table, os.path.join(app_dir, 'autosave_activities.csv'))
# Load the table data from CSV files
def load_data(self):
app_dir = os.path.dirname(os.path.abspath(sys.argv[0]))
TimeTracker.load_table_data(self.table, os.path.join(app_dir, 'autosave_calls.csv'))
TimeTracker.load_table_data(self.activity_table, os.path.join(app_dir, 'autosave_activities.csv'))
@staticmethod
def save_table_data(table, file_name):
with open(file_name, 'w', newline='', encoding='utf-8') as csvfile:
csv_writer = csv.writer(csvfile)
for row in range(table.rowCount()):
row_data = []
for col in range(table.columnCount()):
item = table.item(row, col)
row_data.append(item.text() if item else '')
csv_writer.writerow(row_data)
@staticmethod
def load_table_data(table, file_name):
try:
with open(file_name, 'r', newline='', encoding='utf-8') as csvfile:
csv_reader = csv.reader(csvfile)
for row_data in csv_reader:
row_position = table.rowCount()
table.insertRow(row_position)
for col, data in enumerate(row_data):
table.setItem(row_position, col, QTableWidgetItem(data))
except FileNotFoundError:
pass # If the file doesn't exist, start with an empty table
# Check if dark mode is enabled in the system settings
def is_dark_mode_enabled():
qsettings = QSettings("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize", QSettings.Format.NativeFormat)
return bool(qsettings.value("AppsUseLightTheme") == 0)
# Apply dark mode styles to the application
def set_dark_mode(app_instance):
palette = QPalette()
palette.setColor(QPalette.ColorRole.Window, QColor(53, 53, 53))
palette.setColor(QPalette.ColorRole.WindowText, Qt.GlobalColor.white)
palette.setColor(QPalette.ColorRole.Base, QColor(25, 25, 25))
palette.setColor(QPalette.ColorRole.AlternateBase, QColor(53, 53, 53))
palette.setColor(QPalette.ColorRole.ToolTipBase, Qt.GlobalColor.white)
palette.setColor(QPalette.ColorRole.ToolTipText, Qt.GlobalColor.white)
palette.setColor(QPalette.ColorRole.Text, Qt.GlobalColor.white)
palette.setColor(QPalette.ColorRole.Button, QColor(53, 53, 53))
palette.setColor(QPalette.ColorRole.ButtonText, Qt.GlobalColor.white)
palette.setColor(QPalette.ColorRole.BrightText, Qt.GlobalColor.red)
palette.setColor(QPalette.ColorRole.Highlight, QColor(64, 128, 224)) # Changed highlight color
palette.setColor(QPalette.ColorRole.HighlightedText, Qt.GlobalColor.white) # Changed highlighted text color
app_instance.setPalette(palette)
style_sheet = '''
QTableWidget {
background-color: #353535;
gridline-color: #1E1E1E;
}
QHeaderView::section {
background-color: #353535;
color: white;
}
QPushButton {
background-color: #111111;
color: white;
border: 1px solid #454545;
}
QPushButton:hover {
background-color: #191919;
}
QPushButton:pressed {
background-color: #190016;
}
QWidget {
background-color: #070707;
color: white;
}
QScrollBar {
background-color: #353535;
}
QTableCornerButton::section {
background-color: #353535;
}
'''
app_instance.setStyleSheet(style_sheet)
# Main entry point for the application
if __name__ == "__main__":
app = QApplication(sys.argv)
if is_dark_mode_enabled():
set_dark_mode(app)
time_tracker = TimeTracker()
time_tracker.show()
sys.exit(app.exec())