-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
607 lines (526 loc) · 22.6 KB
/
Copy pathmain.py
File metadata and controls
607 lines (526 loc) · 22.6 KB
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
import sys
import os
import shutil
from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QVBoxLayout, QHBoxLayout, QPushButton, QLabel, QStackedWidget, QFileIconProvider, QStyle, QFrame, QTextEdit, QScrollArea
from PyQt5.QtGui import QIcon, QPixmap, QKeyEvent, QColor, QPainter, QImage, QFont, QPalette
from PyQt5.QtCore import Qt, QThread, pyqtSignal, QSize, QFileInfo, QEvent, QRect, QTimer
import fitz # PyMuPDF for PDF preview
from docx import Document # python-docx for DOCX preview
class FileLoader(QThread):
file_loaded = pyqtSignal(str, str)
def __init__(self, desktop_path):
super().__init__()
self.desktop_path = desktop_path
def run(self):
for file in os.listdir(self.desktop_path):
file_path = os.path.join(self.desktop_path, file)
if os.path.isfile(file_path):
self.file_loaded.emit(file, file_path)
class RoundedWidget(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.setObjectName("roundedWidget")
def paintEvent(self, event):
painter = QPainter(self)
painter.setRenderHint(QPainter.Antialiasing)
painter.setBrush(QColor("#F0F0F0"))
painter.setPen(Qt.NoPen)
painter.drawRoundedRect(self.rect(), 20, 20)
class FileCard(QWidget):
def __init__(self, file_path):
super().__init__()
self.file_path = file_path
self.layout = QVBoxLayout(self)
self.layout.setContentsMargins(20, 20, 20, 20)
self.layout.setSpacing(15)
self.setStyleSheet("""
QWidget {
background-color: #FFFFFF;
border-radius: 20px;
}
QLabel {
color: #333333;
}
""")
self.title = QLabel(os.path.basename(file_path))
self.title.setStyleSheet("""
font-size: 24px;
font-weight: bold;
background-color: transparent;
color: #2C3E50;
padding: 10px;
""")
self.title.setAlignment(Qt.AlignCenter)
self.layout.addWidget(self.title)
self.content_area = QWidget()
self.content_layout = QVBoxLayout(self.content_area)
self.content_layout.setAlignment(Qt.AlignCenter)
self.layout.addWidget(self.content_area)
self.icon_label = QLabel()
self.icon_label.setAlignment(Qt.AlignCenter)
self.content_layout.addWidget(self.icon_label, alignment=Qt.AlignCenter)
self.preview_text = QTextEdit()
self.preview_text.setReadOnly(True)
self.preview_text.setVisible(False)
self.preview_text.setStyleSheet("""
QTextEdit {
font-size: 16px;
line-height: 1.6;
background-color: #F9F9F9;
padding: 15px;
border-radius: 10px;
border: 1px solid #E0E0E0;
}
""")
self.content_layout.addWidget(self.preview_text)
self.file_info = QLabel()
self.file_info.setStyleSheet("""
font-size: 18px;
background-color: transparent;
color: #34495E;
padding: 10px;
""")
self.file_info.setAlignment(Qt.AlignCenter)
self.layout.addWidget(self.file_info)
self.update_file_info()
def update_file_info(self):
file_info = self.get_file_info()
self.file_info.setText(f"{file_info['size']}")
self.load_preview()
def get_file_info(self):
file_stats = os.stat(self.file_path)
file_size = file_stats.st_size
if file_size < 1024:
size_str = f"{file_size} B"
elif file_size < 1024 * 1024:
size_str = f"{file_size / 1024:.1f} KB"
else:
size_str = f"{file_size / (1024 * 1024):.1f} MB"
return {
"name": os.path.basename(self.file_path),
"size": size_str,
}
def load_preview(self):
file_extension = os.path.splitext(self.file_path)[1].lower()
if file_extension in ['.png', '.jpg', '.jpeg']:
pixmap = QPixmap(self.file_path)
if not pixmap.isNull():
self.icon_label.setPixmap(pixmap.scaled(300, 300, Qt.KeepAspectRatio, Qt.SmoothTransformation))
self.icon_label.setVisible(True)
self.preview_text.setVisible(False)
else:
self.icon_label.setText("Error loading image")
elif file_extension == '.pdf':
try:
with fitz.open(self.file_path) as doc:
text = ""
for page in doc:
text += page.get_text()
if len(text) > 1000:
break
self.preview_text.setText(text[:1000] + "..." if len(text) > 1000 else text)
self.icon_label.setVisible(False)
self.preview_text.setVisible(True)
except Exception as e:
self.preview_text.setText(f"Error loading PDF: {str(e)}")
self.icon_label.setVisible(False)
self.preview_text.setVisible(True)
elif file_extension == '.docx':
try:
doc = Document(self.file_path)
text = "\n".join([para.text for para in doc.paragraphs])
self.preview_text.setText(text[:1000] + "..." if len(text) > 1000 else text)
self.icon_label.setVisible(False)
self.preview_text.setVisible(True)
except Exception as e:
self.preview_text.setText(f"Error loading DOCX: {str(e)}")
self.icon_label.setVisible(False)
self.preview_text.setVisible(True)
else:
icon = QFileIconProvider().icon(QFileInfo(self.file_path))
pixmap = icon.pixmap(QSize(128, 128))
self.icon_label.setPixmap(pixmap)
self.icon_label.setVisible(True)
self.preview_text.setVisible(False)
def resizeEvent(self, event):
super().resizeEvent(event)
self.adjust_content_size()
def adjust_content_size(self):
available_height = self.height() - self.title.height() - self.file_info.height() - 80
self.content_area.setFixedHeight(max(available_height, 200))
if self.icon_label.isVisible():
icon_size = min(int(self.width() * 0.7), int(available_height * 0.8), 300)
self.icon_label.setFixedSize(icon_size, icon_size)
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("Desktop File Swiper")
self.setStyleSheet("""
QMainWindow {
background-color: #F5F5F5;
}
QWidget {
font-family: 'Segoe UI', Arial, sans-serif;
}
""")
self.central_widget = QWidget()
self.setCentralWidget(self.central_widget)
self.layout = QVBoxLayout(self.central_widget)
self.layout.setSpacing(20)
self.layout.setContentsMargins(20, 20, 20, 20)
self.undo_button = QPushButton("Undo")
self.undo_button.clicked.connect(self.on_undo)
self.undo_button.setStyleSheet("""
QPushButton {
font-size: 18px;
font-weight: bold;
border-radius: 15px;
padding: 10px 20px;
background-color: #3498DB;
color: white;
border: none;
}
QPushButton:hover {
background-color: #2980B9;
}
QPushButton:pressed {
background-color: #2573A7;
}
""")
self.undo_label = QLabel("↑ Up Arrow")
self.undo_label.setStyleSheet("""
QLabel {
font-size: 14px;
color: #7F8C8D;
margin-top: 5px;
}
""")
self.undo_label.setAlignment(Qt.AlignCenter)
undo_layout = QVBoxLayout()
undo_layout.addWidget(self.undo_button)
undo_layout.addWidget(self.undo_label)
self.layout.addLayout(undo_layout, alignment=Qt.AlignRight)
self.stack = QStackedWidget()
self.layout.addWidget(self.stack, 1)
button_layout = QHBoxLayout()
self.discard_button = QPushButton("Put in Clutter")
self.keep_button = QPushButton("Keep on Desktop")
self.discard_button.clicked.connect(self.on_discard)
self.keep_button.clicked.connect(self.on_keep)
button_style = """
QPushButton {
font-size: 22px;
font-weight: bold;
border-radius: 25px;
padding: 15px 30px;
border: none;
}
QPushButton:hover {
background-color: %s;
}
QPushButton:pressed {
background-color: %s;
}
"""
self.discard_button.setStyleSheet(button_style % ("#E74C3C", "#C0392B") + "QPushButton { background-color: #FF4040; color: white; }")
self.keep_button.setStyleSheet(button_style % ("#F1C40F", "#F39C12") + "QPushButton { background-color: #FFC000; color: white; }")
button_layout.addWidget(self.discard_button)
button_layout.addWidget(self.keep_button)
self.layout.addLayout(button_layout)
# Create and style labels for keyboard commands
self.discard_label = QLabel("← Left Arrow")
self.keep_label = QLabel("→ Right Arrow")
label_style = """
QLabel {
font-size: 14px;
color: #7F8C8D;
margin-top: 5px;
}
"""
self.discard_label.setStyleSheet(label_style)
self.keep_label.setStyleSheet(label_style)
self.discard_label.setAlignment(Qt.AlignCenter)
self.keep_label.setAlignment(Qt.AlignCenter)
# Add labels under the buttons
label_layout = QHBoxLayout()
label_layout.addWidget(self.discard_label)
label_layout.addWidget(self.keep_label)
self.layout.addLayout(label_layout)
self.desktop_path = os.path.join(os.path.expanduser("~"), "Desktop")
self.clutter_folder = os.path.join(self.desktop_path, "Desktop Clutter")
if not os.path.exists(self.clutter_folder):
os.makedirs(self.clutter_folder)
self.file_loader = FileLoader(self.desktop_path)
self.file_loader.file_loaded.connect(self.add_file)
self.file_loader.start()
self.undo_stack = []
self.current_files = []
QApplication.instance().installEventFilter(self)
# ... (keep the rest of the methods as they were in the previous version)
def resizeEvent(self, event):
super().resizeEvent(event)
self.adjust_layout()
def adjust_layout(self):
for i in range(self.stack.count()):
widget = self.stack.widget(i)
if isinstance(widget, FileCard):
widget.adjust_content_size()
# Adjust button sizes
button_width = max(int(self.width() * 0.4), 200)
button_height = max(int(self.height() * 0.1), 50)
self.discard_button.setFixedSize(button_width, button_height)
self.keep_button.setFixedSize(button_width, button_height)
# Adjust undo button size
undo_width = max(int(self.width() * 0.2), 100)
undo_height = max(int(self.height() * 0.06), 30)
self.undo_button.setFixedSize(undo_width, undo_height)
# Adjust font sizes
base_font_size = max(int(self.width() * 0.02), 12)
self.discard_button.setStyleSheet(self.discard_button.styleSheet() + f"font-size: {base_font_size}px;")
self.keep_button.setStyleSheet(self.keep_button.styleSheet() + f"font-size: {base_font_size}px;")
self.undo_button.setStyleSheet(self.undo_button.styleSheet() + f"font-size: {int(base_font_size * 0.8)}px;")
# Adjust label font sizes
label_font_size = max(int(base_font_size * 0.7), 10)
label_style = f"font-size: {label_font_size}px; color: #7F8C8D; margin-top: 5px;"
self.discard_label.setStyleSheet(label_style)
self.keep_label.setStyleSheet(label_style)
self.undo_label.setStyleSheet(label_style)
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("Desktop File Swiper")
self.setStyleSheet("""
QMainWindow {
background-color: #F5F5F5;
}
QWidget {
font-family: 'Segoe UI', Arial, sans-serif;
}
""")
self.central_widget = QWidget()
self.setCentralWidget(self.central_widget)
self.layout = QVBoxLayout(self.central_widget)
# Undo button and label
undo_container = QWidget()
undo_layout = QVBoxLayout(undo_container)
self.undo_button = QPushButton("Undo")
self.undo_button.clicked.connect(self.on_undo)
self.undo_button.setStyleSheet("""
QPushButton {
font-size: 18px;
font-weight: bold;
border-radius: 15px;
padding: 10px 20px;
background-color: #3498DB;
color: white;
border: none;
}
QPushButton:hover {
background-color: #2980B9;
}
QPushButton:pressed {
background-color: #2573A7;
}
""")
self.undo_label = QLabel("↑ Up Arrow")
self.undo_label.setStyleSheet("""
QLabel {
font-size: 14px;
color: #7F8C8D;
margin-top: 5px;
}
""")
self.undo_label.setAlignment(Qt.AlignCenter)
undo_layout.addWidget(self.undo_button)
undo_layout.addWidget(self.undo_label)
undo_container.setLayout(undo_layout)
self.layout.addWidget(undo_container, alignment=Qt.AlignRight)
self.stack = QStackedWidget()
self.layout.addWidget(self.stack, 1)
self.button_layout = QHBoxLayout()
self.discard_button = QPushButton("Put in Clutter")
self.keep_button = QPushButton("Keep on Desktop")
self.discard_button.clicked.connect(self.on_discard)
self.keep_button.clicked.connect(self.on_keep)
button_style = """
QPushButton {
font-size: 22px;
font-weight: bold;
border-radius: 25px;
padding: 15px 30px;
border: none;
}
QPushButton:hover {
background-color: %s;
}
QPushButton:pressed {
background-color: %s;
}
"""
self.discard_button.setStyleSheet(button_style % ("#E74C3C", "#C0392B") + "QPushButton { background-color: #FF4040; color: white; }")
self.keep_button.setStyleSheet(button_style % ("#F1C40F", "#F39C12") + "QPushButton { background-color: #FFC000; color: white; }")
# Create vertical layouts for buttons and their labels
discard_layout = QVBoxLayout()
keep_layout = QVBoxLayout()
# Add buttons to their respective layouts
discard_layout.addWidget(self.discard_button)
keep_layout.addWidget(self.keep_button)
# Create and style labels for keyboard commands
self.discard_label = QLabel("← Left Arrow")
self.keep_label = QLabel("→ Right Arrow")
label_style = """
QLabel {
font-size: 14px;
color: #7F8C8D;
margin-top: 5px;
}
"""
self.discard_label.setStyleSheet(label_style)
self.keep_label.setStyleSheet(label_style)
self.discard_label.setAlignment(Qt.AlignCenter)
self.keep_label.setAlignment(Qt.AlignCenter)
# Add labels to their respective layouts
discard_layout.addWidget(self.discard_label)
keep_layout.addWidget(self.keep_label)
# Add the vertical layouts to the main button layout
self.button_layout.addLayout(discard_layout)
self.button_layout.addLayout(keep_layout)
self.layout.addLayout(self.button_layout)
self.desktop_path = os.path.join(os.path.expanduser("~"), "Desktop")
self.clutter_folder = os.path.join(self.desktop_path, "Desktop Clutter")
if not os.path.exists(self.clutter_folder):
os.makedirs(self.clutter_folder)
self.file_loader = FileLoader(self.desktop_path)
self.file_loader.file_loaded.connect(self.add_file)
self.file_loader.start()
self.undo_stack = []
self.current_files = []
self.is_fullscreen = False
QApplication.instance().installEventFilter(self)
# Set initial size based on screen size
self.resize_window()
def resize_window(self):
screen = QApplication.primaryScreen().availableGeometry()
width = int(screen.width() * 0.7) # Increased from 0.5 to 0.7
height = int(screen.height() * 0.8)
self.setGeometry((screen.width() - width) // 2, (screen.height() - height) // 2, width, height)
def add_file(self, file_name, file_path):
card = FileCard(file_path)
self.stack.addWidget(card)
self.current_files.append(file_path)
if self.stack.count() == 1:
self.stack.setCurrentIndex(0)
def on_discard(self):
self.move_file_to_clutter()
self.move_to_next_file()
def on_keep(self):
if self.current_files:
kept_file = self.current_files.pop(0)
self.undo_stack.append(("keep", kept_file))
self.move_to_next_file()
def on_undo(self):
if self.undo_stack:
action, file_path = self.undo_stack.pop()
if action == "discard":
file_name = os.path.basename(file_path)
new_path = os.path.join(self.desktop_path, file_name)
try:
shutil.move(file_path, new_path)
print(f"Moved {file_name} back to Desktop")
self.add_file(file_name, new_path)
except Exception as e:
print(f"Error moving file back: {str(e)}")
elif action == "keep":
self.current_files.insert(0, file_path)
card = FileCard(file_path)
self.stack.insertWidget(0, card)
self.stack.setCurrentIndex(0)
def move_file_to_clutter(self):
if self.current_files:
file_path = self.current_files.pop(0)
file_name = os.path.basename(file_path)
new_path = os.path.join(self.clutter_folder, file_name)
try:
shutil.move(file_path, new_path)
print(f"Moved {file_name} to Desktop Clutter folder")
self.undo_stack.append(("discard", new_path))
except Exception as e:
print(f"Error moving file: {str(e)}")
def move_to_next_file(self):
if self.stack.count() > 0:
self.stack.removeWidget(self.stack.widget(0))
if self.stack.count() > 0:
self.stack.setCurrentIndex(0)
else:
self.close()
def toggle_fullscreen(self):
if self.is_fullscreen:
self.showNormal()
self.resize_window()
else:
self.showFullScreen()
self.is_fullscreen = not self.is_fullscreen
def highlight_label(self, label, color):
label.setStyleSheet(f"QLabel {{ font-size: 14px; color: {color}; margin-top: 5px; font-weight: bold; }}")
QTimer.singleShot(200, lambda: label.setStyleSheet("QLabel { font-size: 14px; color: #7F8C8D; margin-top: 5px; }"))
def eventFilter(self, obj, event):
if event.type() == QEvent.KeyPress:
key_event = QKeyEvent(event)
if key_event.key() == Qt.Key_Left:
self.on_discard()
self.highlight_label(self.discard_label, "#FF4040")
return True
elif key_event.key() == Qt.Key_Right:
self.on_keep()
self.highlight_label(self.keep_label, "#FFC000")
return True
elif key_event.key() == Qt.Key_Up:
self.on_undo()
self.highlight_label(self.undo_label, "#3498DB")
return True
elif key_event.key() == Qt.Key_F11:
self.toggle_fullscreen()
return True
return super().eventFilter(obj, event)
def showEvent(self, event):
super().showEvent(event)
self.setFocus() # Ensure the main window has focus to capture key events
def resizeEvent(self, event):
super().resizeEvent(event)
self.adjust_layout()
def adjust_layout(self):
# Adjust button sizes
button_width = max(int(self.width() * 0.3), 100) # Minimum width of 100
button_height = max(int(self.height() * 0.08), 30) # Minimum height of 30
self.discard_button.setFixedSize(button_width, button_height)
self.keep_button.setFixedSize(button_width, button_height)
# Adjust undo button size
undo_width = max(int(self.width() * 0.15), 50) # Minimum width of 50
undo_height = max(int(self.height() * 0.05), 20) # Minimum height of 20
self.undo_button.setFixedSize(undo_width, undo_height)
# Adjust font sizes
base_font_size = max(int(self.width() * 0.015), 8) # Minimum font size of 8
self.discard_button.setStyleSheet(self.discard_button.styleSheet() + f"font-size: {base_font_size}px;")
self.keep_button.setStyleSheet(self.keep_button.styleSheet() + f"font-size: {base_font_size}px;")
self.undo_button.setStyleSheet(self.undo_button.styleSheet() + f"font-size: {max(int(base_font_size * 0.8), 6)}px;")
# Adjust label font sizes
label_font_size = max(int(base_font_size * 0.7), 6) # Minimum font size of 6
label_style = f"font-size: {label_font_size}px; color: #7F8C8D; margin-top: 5px;"
self.discard_label.setStyleSheet(label_style)
self.keep_label.setStyleSheet(label_style)
self.undo_label.setStyleSheet(label_style)
# Adjust content in the stack
for i in range(self.stack.count()):
widget = self.stack.widget(i)
if isinstance(widget, FileCard):
widget.adjust_content_size()
if __name__ == '__main__':
app = QApplication(sys.argv)
app.setStyle("Fusion") # Use Fusion style for a modern look
# Set a custom font for the entire application
font = QFont("Segoe UI", 10)
app.setFont(font)
main_window = MainWindow()
main_window.setMinimumSize(600, 400) # Set a minimum window size
main_window.show()
sys.exit(app.exec_())