-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathspatial.py
1351 lines (1180 loc) · 61.2 KB
/
spatial.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
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
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
# Tested on Windows 11. Moving items within the window not perfect here, but it is using Qt drag and drop
# Key is to avoid QListView and QFileSystemModel because they are not suited for our purpose of creating a spatial file manager
import sys
import os
import json
import subprocess
import math
import shutil
from PyQt6.QtCore import Qt, QPoint, QSize, QDir, QRect, QMimeData, QUrl, QFileSystemWatcher, QFileInfo, QTimer, QRegularExpression, QObject, QEvent
from PyQt6.QtGui import QFontMetrics, QPainter, QPen, QAction, QDrag, QColor, QPainter, QPen, QBrush, QPixmap, QKeySequence, QFont, QIcon, QShortcut, QRegularExpressionValidator, QCursor
from PyQt6.QtWidgets import QApplication, QWidget, QVBoxLayout, QScrollArea, QLabel, QSizePolicy, QMainWindow, QDialogButtonBox
from PyQt6.QtWidgets import QStatusBar, QComboBox, QFileIconProvider, QMenuBar, QGridLayout, QMessageBox, QMenu, QDialog, QLineEdit, QGraphicsScene, QGraphicsPixmapItem
if sys.platform == "win32":
from win32com.client import Dispatch
import windows_context_menu
import windows_file_operations
import appdir
class SpatialFiler(QMainWindow):
def __init__(self, path=None, is_desktop_window=False):
super().__init__()
self.path = os.path.normpath(path) if path else QDir.homePath()
self.setWindowTitle(self.path)
self.setGeometry(100, 100, 800, 600)
self.is_desktop_window = is_desktop_window
self.is_spring_opened = False
# Set folder icon on window; unfortunately Windows doesn't use this for the taskbar icon
icon_provider = QFileIconProvider()
icon = icon_provider.icon(QFileInfo(self.path))
self.setWindowIcon(icon)
self.setAcceptDrops(True)
# There might be a file .DS_Spatial in the directory that contains the window position and size. If it exists, read the settings from it.
# Example file content:
# {"position": {"x": 499, "y": 242}, "size": {"width": 800, "height": 600}, "items": [{"name": "known_hosts", "x": 110, "y": 0}, {"name": "known_hosts.old", "x": 220, "y": 0}]}
settings_file = os.path.join(self.path, app.desktop_settings_file)
if os.path.exists(settings_file):
with open(settings_file, "r") as file:
try:
settings = json.load(file)
print("Settings from %s" % (settings_file))
# Check if there is a position for the window in the settings file; if yes, set the window position
if "position" in settings:
self.move(settings["position"]["x"], settings["position"]["y"])
# Check if there is a size for the window in the settings file; if yes, set the window size
if "size" in settings:
self.resize(settings["size"]["width"], settings["size"]["height"])
# Check if the window is out of the screen; if yes, move it to the top-left corner
if self.x() < 0 or self.y() < 0:
self.move(0, 0)
except json.JSONDecodeError as e:
print(f"Error reading settings file: {e}")
else:
print(f"Settings file {settings_file} does not exist")
# Create the central widget
self.central_widget = QWidget()
self.setCentralWidget(self.central_widget)
self.layout = QVBoxLayout(self.central_widget)
self.scroll_area = QScrollArea(self.central_widget)
self.scroll_area.setContentsMargins(0, 0, 0, 0)
self.scroll_area.setWidgetResizable(True)
self.container = QWidget()
palette = self.container.palette()
palette.setColor(self.container.backgroundRole(), Qt.GlobalColor.white)
self.container.setPalette(palette)
self.scroll_area.setWidget(self.container)
self.layout.addWidget(self.scroll_area)
self.layout.setContentsMargins(0, 0, 0, 0)
self.central_widget.setLayout(self.layout)
# Create the menu bar
self.menu_bar = QMenuBar(self)
self.setMenuBar(self.menu_bar)
self.init_menu_bar()
# Initialize other components
self.items = []
self.vertical_spacing = 0
self.line_height = app.icon_size + QFontMetrics(self.font()).height() + 16
self.horizontal_spacing = 0
self.item_width_for_positioning = 150
self.start_x = 0
self.start_y = 0
self.populate_items()
self.dragging = False
self.last_pos = QPoint(0, 0)
self.selected_files = []
self.selection_rect = QRect(0, 0, 0, 0)
self.is_selecting = False
# Setup status bar with a dropdown if this is not the desktop window
if not self.is_desktop_window:
self.status_bar = QStatusBar()
self.setStatusBar(self.status_bar)
self.dropdown = QComboBox()
self.populate_dropdown()
self.dropdown.currentIndexChanged.connect(self.on_dropdown_changed)
self.status_bar.addPermanentWidget(self.dropdown)
self.status_bar.setSizeGripEnabled(False)
self.status_bar.setSizePolicy(QSizePolicy.Policy.MinimumExpanding, QSizePolicy.Policy.Fixed)
# Add a 10px wide spacer to the right of the dropdown to leave some space for the resize handle
spacer_widget = QWidget()
spacer_widget.setFixedWidth(15)
spacer_widget.setSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Fixed)
self.status_bar.addPermanentWidget(spacer_widget)
# Timer to update the status bar every 5 seconds
self.timer = QTimer()
self.timer.timeout.connect(self.update_status_bar)
self.timer.start(5000)
self.update_status_bar()
# To keep track of drag distances
self.initial_position = None
# Watch for changes in the directory
self.file_watcher = QFileSystemWatcher()
self.file_watcher.directoryChanged.connect(self.directory_changed)
self.file_watcher.fileChanged.connect(self.file_changed)
self.file_watcher.addPath(self.path)
# To keep track of spring-loaded folders needing to be closed
self.installEventFilter(self)
# To keep track of spring-loaded folders needing to be closed
def eventFilter(self, obj: QObject, event: QEvent) -> bool:
if event.type() == QEvent.Type.DragLeave:
# Check whether mouse coordinates are inside or outside of the window, and print the result
mouse_is_inside = self.rect().contains(self.mapFromGlobal(QCursor.pos()))
print("Mouse is inside:", mouse_is_inside)
if not mouse_is_inside and self.is_spring_opened:
self.close()
# Handle the drag leave event here
return True # Return True if the event is handled
return super().eventFilter(obj, event)
def keyPressEvent(self, event):
# Handle Tab and Shift-Tab to select the next and previous item
if event.key() == Qt.Key.Key_Tab:
if QApplication.keyboardModifiers() == Qt.KeyboardModifier.ShiftModifier:
# FIXME: Why does this never get called?
self.select_previous_item()
else:
self.select_next_item()
def select_next_item(self):
print("Selecting next item")
if self.selected_files:
item = self.selected_files[0]
self.selected_files.remove(item)
else:
item = self.items[0]
item.unhighlight()
# Select the next item if there is one, otherwise select the first item
if item in self.items:
index = self.items.index(item)
if index < len(self.items) - 1:
next_item = self.items[index + 1]
else:
next_item = self.items[0]
self.selected_files = [next_item]
next_item.highlight()
def select_previous_item(self):
print("Selecting previous item")
if self.selected_files:
item = self.selected_files[0]
self.selected_files.remove(item)
else:
item = self.items[len(self.items) - 1]
item.unhighlight()
# Select the previous item if there is one, otherwise select the last item
if item in self.items:
index = self.items.index(item)
if index > 0:
previous_item = self.items[index - 1]
else:
previous_item = self.items[-1]
self.selected_files = [previous_item]
previous_item.highlight()
def populate_dropdown(self):
try:
path = self.path
if path.startswith("//") or path.startswith("\\\\"):
print("Skipping network path") # TODO: Implement network path handling for Windows
self.dropdown.hide()
return
paths = []
while os.path.exists(path):
paths.append(path)
if os.path.normpath(path) == os.path.normpath(QDir.rootPath()):
break
# On Windows, stop at the drive letter, otherwise we can get an infinite loop
if sys.platform == "win32" and len(path) == 3 and path[1] == ":":
break
path = os.path.dirname(path)
paths.reverse()
for path in paths:
self.dropdown.addItem(robust_filename(path))
self.dropdown.setItemData(self.dropdown.count() - 1, path, Qt.ItemDataRole.UserRole)
self.dropdown.setCurrentIndex(self.dropdown.count() - 1)
except Exception as e:
print(f"Error populating dropdown: {e}")
def on_dropdown_changed(self):
selected_path = self.dropdown.currentData(Qt.ItemDataRole.UserRole)
print("Dropdown changed, selected path:", selected_path)
self.dropdown.blockSignals(True)
self.dropdown.setCurrentIndex(self.dropdown.count() - 1)
self.dropdown.blockSignals(False)
self.open(selected_path)
def update_status_bar(self):
path = self.path
item_count = len([f for f in QDir(path).entryList() if f not in [".", ".."]])
try:
free_space = shutil.disk_usage(path).free
if free_space < 1024:
free_space_str = f"{free_space} Bytes"
elif free_space < 1024 ** 2:
free_space_str = f"{free_space / 1024:.2f} KB"
elif free_space < 1024 ** 3:
free_space_str = f"{free_space / (1024 ** 2):.2f} MB"
else:
free_space_str = f"{free_space / (1024 ** 3):.2f} GB"
except Exception as e:
free_space_str = "Unknown"
print(f"Error getting free space: {e}")
self.status_bar.showMessage(f"{item_count} items, {free_space_str} available")
def open(self, path):
i = Item(path, True, QPoint(0, 0), self.container)
i.open(None)
i = None
def directory_changed(self, path):
if not os.path.exists(self.path):
self.close()
return
# Remove items from the window that are not in the directory anymore
items_to_remove = []
for item in self.items:
if not os.path.exists(item.path):
items_to_remove.append(item)
for item in items_to_remove:
item.hide()
if self.container.layout():
self.container.layout().removeWidget(item)
self.items.remove(item)
item.deleteLater()
self.populate_items() # This adds new items to the window
self.update_container_size()
def file_changed(self, path):
if not os.path.exists(self.path):
self.close()
return
self.update_container_size()
def paintEvent(self, event):
if self.is_selecting:
painter = QPainter(self)
painter.setPen(QPen(Qt.GlobalColor.gray, 1))
painter.drawRect(self.selection_rect)
def init_menu_bar(self):
# File Menu
file_menu = self.menu_bar.addMenu("File")
self.open_action = QAction("Open", self)
self.open_action.setShortcuts([QKeySequence("Ctrl+O"), QKeySequence("Shift+Ctrl+O"), QKeySequence("Ctrl+Down"), QKeySequence("Shift+Ctrl+Down")])
# "Enter" is a non-modifier key shortcut, so we need to use a QShortcut object to catch it
self.open_action.triggered.connect(self.open_selected_items)
self.open_shortcut = QShortcut(QKeySequence("Enter"), self)
self.open_shortcut.activated.connect(self.open_selected_items)
self.open_action.setEnabled(False)
if self.is_desktop_window == True:
self.close_action = QAction("Quit", self)
self.close_action.setShortcut("Ctrl+Q")
else:
self.close_action = QAction("Close", self)
self.close_action.setShortcut("Ctrl+W")
self.close_action.triggered.connect(self.close)
file_menu.addAction(self.open_action)
file_menu.addSeparator()
file_menu.addAction(self.close_action)
# Edit Menu
edit_menu = self.menu_bar.addMenu("Edit")
self.cut_action = QAction("Cut", self)
self.cut_action.setShortcut("Ctrl+X")
self.cut_action.triggered.connect(self.cut_selected_items)
self.copy_action = QAction("Copy", self)
self.copy_action.setShortcut("Ctrl+C")
self.copy_action.triggered.connect(self.copy_selected_items)
self.paste_action = QAction("Paste", self)
self.paste_action.setShortcut("Ctrl+V")
self.paste_action.triggered.connect(self.paste_items)
self.delete_action = QAction("Delete", self)
self.delete_action.setShortcut("Delete")
edit_menu.addAction(self.cut_action)
edit_menu.addAction(self.copy_action)
edit_menu.addAction(self.paste_action)
edit_menu.addSeparator()
edit_menu.addAction(self.delete_action)
for action in [self.cut_action, self.copy_action, self.delete_action]:
action.setEnabled(False)
edit_menu.addSeparator()
self.select_all_action = QAction("Select All", self)
self.select_all_action.setShortcut("Ctrl+A")
self.select_all_action.triggered.connect(self.select_all)
edit_menu.addAction(self.select_all_action)
# Go Menu
go_menu = self.menu_bar.addMenu("Go")
parent = os.path.dirname(self.path)
up_action = QAction("Up", self)
up_action.setShortcuts(["Ctrl+Up", "Ctrl+Shift+Up"])
up_action.triggered.connect(self.open_parent)
if not os.path.exists(parent) or os.path.normpath(self.path) == os.path.normpath(QDir.rootPath()):
up_action.setDisabled(True)
go_menu.addAction(up_action)
go_menu.addSeparator()
home_action = QAction("Home", self)
home_action.triggered.connect(self.open_home)
go_menu.addAction(home_action)
if sys.platform == "win32":
start_menu_action = QAction("Applications", self)
start_menu_action.triggered.connect(self.open_start_menu_folder)
go_menu.addAction(start_menu_action)
# View Menu
view_menu = self.menu_bar.addMenu("View")
if os.path.normpath(self.path) == get_desktop_directory():
align_items_desktop_action = QAction("Align Items", self)
align_items_desktop_action.triggered.connect(self.align_items_desktop)
view_menu.addAction(align_items_desktop_action)
else:
align_items_action = QAction("Align Items", self)
align_items_action.triggered.connect(self.align_items)
view_menu.addAction(align_items_action)
align_items_staggered_action = QAction("Align Items Staggered", self)
align_items_staggered_action.triggered.connect(self.align_items_staggered)
view_menu.addAction(align_items_staggered_action)
align_items_circle_action = QAction("Align Items in Circle", self)
align_items_circle_action.triggered.connect(self.align_items_circle)
view_menu.addAction(align_items_circle_action)
view_menu.addSeparator()
adjust_window_size_action = QAction("Adjust Window Size", self)
adjust_window_size_action.triggered.connect(self.adjust_window_size)
view_menu.addAction(adjust_window_size_action)
# Help Menu
help_menu = self.menu_bar.addMenu("Help")
about_action = QAction("About", self)
about_action.triggered.connect(self.show_about)
help_menu.addAction(about_action)
help_menu.addSeparator
if "log_console" in sys.modules:
app.log_console.add_menu_items(help_menu, self)
def select_all(self):
for item in self.items:
self.selected_files.clear()
self.selected_files.append(item)
item.highlight()
def cut_selected_items(self):
app.to_cut = True
self.copy_to_clipboard()
def copy_selected_items(self):
app.to_cut = False
self.copy_to_clipboard()
def paste_items(self):
clipboard = QApplication.clipboard()
urls = clipboard.mimeData().urls()
if not urls:
return
paths = [url.toLocalFile() for url in urls]
if app.to_cut:
if sys.platform == "win32":
windows_file_operations.move_files_with_dialog(paths, self.path)
else:
if sys.platform == "win32":
windows_file_operations.copy_files_with_dialog(paths, self.path)
def copy_to_clipboard(self):
clipboard = QApplication.clipboard()
mime_data = QMimeData()
mime_data.setUrls([QUrl.fromLocalFile(file.path) for file in self.selected_files])
clipboard.setMimeData(mime_data)
def open_parent(self):
# Detect whether the Shift key is pressed; if yes; if yes, close the current window if it is not the fullscreen desktop window
parent = os.path.dirname(self.path)
if (QApplication.keyboardModifiers() == Qt.KeyboardModifier.ControlModifier | Qt.KeyboardModifier.ShiftModifier) and self.is_desktop_window == False:
if os.path.exists(parent):
self.open(parent)
self.close()
else:
if os.path.exists(parent):
self.open(parent)
def open_home(self):
# TODO: Detect whether the Shift key is pressed; if yes; if yes, close the current window if it is not the fullscreen desktop window
self.open(QDir.homePath())
def open_start_menu_folder(self):
# TODO: Detect whether the Shift key is pressed; if yes, close the current window if it is not the fullscreen desktop window
self.start_menu_folder = os.path.join(os.environ['APPDATA'], 'Microsoft', 'Windows', 'Start Menu', 'Programs')
self.open(self.start_menu_folder)
def adjust_window_size(self):
# Adjust the window size to fit the items
max_x = max(item.x() + item.width() for item in self.items) + 30
max_y = max(item.y() + item.height() for item in self.items) + 40
# If the window has a status bar, add its height to the window height
if self.status_bar.isVisible():
max_y += self.status_bar.height()
self.resize(max_x, max_y)
def populate_items(self):
if os.path.normpath(self.path) == get_desktop_directory():
# Add every disk in the system
print("Adding disks")
for disk in QDir.drives():
if not any(item.name == robust_filename(disk.path()) for item in self.items):
# The name of the disk is the first part of the path, e.g. "C:" or "D:"
disk_name = disk.path()
print("Adding disk", disk_name)
self.add_item(disk.path(), True)
# Add the Trash item
if not any(item.name == app.trash_name for item in self.items):
print("Adding Trash item")
trash = os.path.join(self.path, app.trash_name)
self.add_item(trash, True)
try:
entries = os.listdir(self.path)
if not entries:
print("No items found.")
else:
for entry in entries:
# Skip if already in the list
if any(item.path == self.path for item in self.items):
continue
# .DS_Spatial is a special file that we don't want to show
if entry == app.desktop_settings_file:
continue
# ~/Desktop is a special case; we don't want to show it
if self.path == os.path.basename(get_desktop_directory()) and entry == "Desktop":
continue
entry_path = os.path.join(self.path, entry)
is_directory = os.path.isdir(entry_path)
# print(f"Adding item: {entry}")
self.add_item(entry_path, is_directory)
except Exception as e:
print(f"Error accessing directory: {e}")
def calculate_max_width(self):
return max(item.width() for item in self.items) if self.items else 150
def add_item(self, path, is_directory):
if any(item.path == path for item in self.items):
return
position = QPoint(self.start_x + len(self.items) % 5 * (self.calculate_max_width() + self.horizontal_spacing),
self.start_y + len(self.items) // 5 * (self.line_height + self.vertical_spacing))
# Check whether a position is provided in the .DS_Spatial file; if yes, use it
settings_file = os.path.join(self.path, app.desktop_settings_file)
if os.path.exists(settings_file):
with open(settings_file, "r") as file:
try:
settings = json.load(file)
for item in settings["items"]:
if item["name"].replace("$Recycle.Bin", app.trash_name) == robust_filename(path):
position = QPoint(item["x"], item["y"])
except json.JSONDecodeError as e:
print(f"Error reading settings file: {e}")
item = Item(path, is_directory, position, self.container)
item.move(position)
item.show()
self.items.append(item)
self.update_container_size()
def update_container_size(self):
if len(self.items) > 0:
max_x = max(item.x() + item.width() for item in self.items) + 10
max_y = max(item.y() + item.height() for item in self.items) + 10
self.container.setMinimumSize(QSize(max_x, max_y))
def mousePressEvent(self, event):
for item in self.items:
item.text_label_unhighlight()
scroll_pos = QPoint(self.scroll_area.horizontalScrollBar().value(),
self.scroll_area.verticalScrollBar().value())
adjusted_pos = event.pos() + scroll_pos
if event.button() == Qt.MouseButton.LeftButton:
clicked_item = None
for item in self.items:
if (item.x() <= adjusted_pos.x() <= item.x() + item.width()) and \
(item.y() <= adjusted_pos.y() <= item.y() + item.height()):
# Find out if the click was on the icon or not,
# assuming the icon at the center bottom of the item rectangle;
# TODO: Find a better way to determine if the click was on the icon independent of the geometry of the item,
# similar to how we do it for right-clicks in the context menu
icon_center_x = item.x() + item.width() / 2
icon_center_y = item.y() + item.icon_size / 2
if (icon_center_x - item.icon_size / 2 <= adjusted_pos.x() <= icon_center_x + item.icon_size / 2) and \
(icon_center_y <= adjusted_pos.y() <= icon_center_y + item.icon_size):
# Clicked on the icon
clicked_item = item
break
if clicked_item:
if event.modifiers() == Qt.KeyboardModifier.ControlModifier:
if clicked_item in self.selected_files:
self.selected_files.remove(clicked_item)
clicked_item.unhighlight()
else:
self.selected_files.append(clicked_item)
clicked_item.highlight()
else:
if clicked_item not in self.selected_files:
self.selected_files = [clicked_item]
for f in self.items:
if f != clicked_item:
f.unhighlight()
clicked_item.highlight()
self.dragging = True
self.last_pos = adjusted_pos
self.update_menu_state()
# Create a QGraphicsScene to hold the icons
scene = QGraphicsScene()
icon_spacing = 10 # Space between icons
x_offset = 0
# Add each selected item's icon to the scene
for item in self.selected_files:
icon_pixmap = item.icon_label.pixmap()
pixmap_item = QGraphicsPixmapItem(icon_pixmap)
pixmap_item.setPos(x_offset, 0) # Position the icon
scene.addItem(pixmap_item)
x_offset += icon_pixmap.width() + icon_spacing # Update offset for next icon
# FIXME: Use the items' real positions instead; can this be done without using a QGraphicsView?
# Create a QPixmap from the scene
scene_rect = scene.itemsBoundingRect()
combined_pixmap = QPixmap(scene_rect.size().toSize())
combined_pixmap.fill(QColor(0, 0, 0, 0))
painter = QPainter(combined_pixmap)
scene.render(painter, target=scene_rect, source=scene_rect)
painter.end()
# Set the combined pixmap for the drag
drag = QDrag(self)
mime_data = QMimeData()
for item in self.selected_files:
mime_data.setUrls([QUrl.fromLocalFile(f.path) for f in self.selected_files])
drag.setMimeData(mime_data)
drag.setPixmap(combined_pixmap)
drag.setHotSpot(QPoint(int(app.icon_size / 2), int(app.icon_size / 2)))
drag.exec()
else:
self.is_selecting = True
self.selection_rect = QRect(adjusted_pos.x(), adjusted_pos.y(), 0, 0)
self.update()
self.selected_files = []
for item in self.items:
item.unhighlight()
self.update_menu_state()
def mouseMoveEvent(self, event):
scroll_pos = QPoint(self.scroll_area.horizontalScrollBar().value(),
self.scroll_area.verticalScrollBar().value())
adjusted_pos = event.pos() + scroll_pos
if self.dragging:
# Check if at least one of the selected items is being dragged, if not, return
if not any(item.underMouse() for item in self.selected_files):
return
print("Dragging")
# Let Qt drag the selected items
# Set mime data
mime_data = QMimeData()
mime_data.setUrls([QUrl.fromLocalFile(f.path) for f in self.selected_files])
drag = QDrag(self)
drag.setMimeData(mime_data)
drag.setPixmap(self.selected_files[0].icon_label.pixmap())
# TODO: Make it so that the icon doesn't jump to be at the top left corner of the mouse cursor
# FIXME: Instead of hardcoding the hot spot to be half the icon size, it should be the position of the mouse cursor relative to the item
drag.setHotSpot(QPoint(int(app.icon_size/2), int(app.icon_size/2)))
drag.exec()
elif self.is_selecting:
self.selection_rect = QRect(min(self.selection_rect.x(), adjusted_pos.x()),
min(self.selection_rect.y(), adjusted_pos.y()),
abs(adjusted_pos.x() - self.selection_rect.x()),
abs(adjusted_pos.y() - self.selection_rect.y()))
self.update()
for item in self.items:
if (self.selection_rect.x() <= item.x() + item.width() and
item.x() <= self.selection_rect.x() + self.selection_rect.width() and
self.selection_rect.y() <= item.y() + item.height() and
item.y() <= self.selection_rect.y() + self.selection_rect.height()):
if item not in self.selected_files:
self.selected_files.append(item)
item.highlight()
else:
if item in self.selected_files:
self.selected_files.remove(item)
item.unhighlight()
def mouseReleaseEvent(self, event):
if self.dragging:
self.dragging = False
self.update_container_size()
elif self.is_selecting:
self.is_selecting = False
self.selection_rect = QRect(0, 0, 0, 0)
self.update()
def open_selected_items(self):
for item in self.selected_files:
item.open(None)
if (QApplication.keyboardModifiers() == Qt.KeyboardModifier.ControlModifier | Qt.KeyboardModifier.ShiftModifier) and self.is_desktop_window == False:
self.close()
def update_menu_state(self):
# Enable/disable menu actions based on the selection
has_selection = bool(self.selected_files)
self.open_action.setEnabled(has_selection)
self.cut_action.setEnabled(has_selection)
self.copy_action.setEnabled(has_selection)
self.delete_action.setEnabled(has_selection)
def closeEvent(self, event):
# Remove the window from the dictionary of open windows
if self.path in app.open_windows:
del app.open_windows[self.path]
# Store window position and size in .DS_Spatial JSON file in the directory of the window
settings_file = os.path.join(self.path, app.desktop_settings_file)
if os.access(self.path, os.W_OK):
settings = {}
settings["position"] = {"x": self.pos().x(), "y": self.pos().y()}
# Determine the screen this window is on
for screen in QApplication.screens():
if screen.geometry().contains(QApplication.activeWindow().frameGeometry()):
settings["screen"] = {"x": screen.geometry().x(), "y": screen.geometry().y(), "width": screen.geometry().width(), "height": screen.geometry().height()}
break
settings["size"] = {"width": self.width(), "height": self.height()}
settings["items"] = []
for item in self.items:
if item.name != app.desktop_settings_file:
settings["items"].append({"name": robust_filename(item.path), "x": item.pos().x(), "y": item.pos().y()})
try:
with open(settings_file, "w") as file:
json.dump(settings, file, indent=4)
print(f"Written settings to {settings_file}")
except Exception as e:
print(f"Error writing settings file: {e}")
else:
print(f"Cannot write to {settings_file}")
event.accept()
def dragEnterEvent(self, event):
if self.initial_position is None:
self.initial_position = event.position()
print("Drag enter event")
if event.mimeData().hasUrls():
event.accept()
else:
event.ignore()
def startDrag(self, event):
self.initial_position = event.pos()
def dropEvent(self, event):
initial_position = self.initial_position
self.initial_position = None
print("Drop event")
if event.mimeData().hasUrls():
urls = event.mimeData().urls()
for url in urls:
path = url.toLocalFile()
# NOTE: normpath needs to be used to avoid issues with different path separators like / and \ on Windows
print("Dropped file:", os.path.normpath(path))
print("Dropped in window for path:", os.path.normpath((self.path)))
# Check if the file is already in the directory; if yes, just move its position
if os.path.normpath(os.path.dirname(path)) == os.path.normpath(self.path):
print("File was moved within the same directory")
distance = (event.position() - initial_position).manhattanLength()
print("Distance from initial position:", distance)
# Ignore moves below a threshold distance
# QApplication.startDragDistance() is the default value that Qt uses for this
if distance < 20:
event.ignore()
return
for item in self.items:
if os.path.normpath(item.path) == os.path.normpath(path):
drop_position = event.position()
print("Moving to coordinates", drop_position.x(), drop_position.y())
# FIXME: Apparently, QDropEvent's pos() method gives the position of the mouse cursor at the time of the drop event.
# That is not what we want. We want the position of the item that is being dropped, not the mouse cursor.
# Do we need mapToGlobal() or mapFromGlobal()? Or do we need to do something differently in the startDrag event first, like adding all selected item locations to the drag event?
pixmap_height = item.icon_label.pixmap().height()
drop_position = QPoint(int(drop_position.x()), int(drop_position.y() - pixmap_height))
# The next line currently works because the mouse is set to be in the center of the item when the drag starts,
# but that is not a good solution because it makes the dragged icon jump at the beginning of the drag
drop_position = QPoint(drop_position.x() - int(item.width()/2), drop_position.y() - int(app.icon_size/4))
# Take into consideration the scroll position
drop_position += QPoint(self.scroll_area.horizontalScrollBar().value(), self.scroll_area.verticalScrollBar().value())
# If the Alt modifier key is pressed, move to something that is a multiple of 24 - this is kind of a grid
if event.modifiers() == Qt.KeyboardModifier.AltModifier:
drop_position = QPoint(int(drop_position.x() / app.icon_size) * app.icon_size, int(drop_position.y() / app.icon_size) * app.icon_size)
item.move(drop_position)
break
else:
# Files from another window are dropped on this window
file_paths = [url.toLocalFile() for url in urls]
# Path onto which the files were dropped
drop_target = self.path
if not file_paths:
return
try:
menu = QMenu()
move_action = menu.addAction("Move")
copy_action = menu.addAction("Copy")
link_action = menu.addAction("Link")
menu.addSeparator()
cancel_action = menu.addAction("Cancel")
action = menu.exec(QCursor.pos())
if action == move_action:
if sys.platform == 'win32':
windows_file_operations.move_files_with_dialog(file_paths, drop_target)
elif action == copy_action:
if sys.platform == 'win32':
windows_file_operations.copy_files_with_dialog(file_paths, drop_target)
elif action == link_action:
if sys.platform == 'win32':
windows_file_operations.create_shortcuts_with_dialog(file_paths, drop_target)
except Exception as e:
QMessageBox.critical(self, "Error", f"{e}")
event.accept()
else:
event.ignore()
def align_items(self):
if not self.items:
return
num_columns = self.width() // self.item_width_for_positioning
current_column = 0
current_row = 0
# Iterate over the items
for item in self.items:
# Calculate the new position of the item
new_x = current_column * (self.item_width_for_positioning + self.horizontal_spacing)
new_y = current_row * (self.line_height + self.vertical_spacing)
# If the item's text is wider than the item's icon, need to adjust the x position by moving it to the left
if item.text_label.width() > item.icon_label.width():
new_x -= int((item.text_label.width() - item.icon_label.width()) / 2)
# Space on top and at the left of the window, at the top 10 pixels, at the left half of the item width
new_x += int(self.item_width_for_positioning/4)
new_y += 10
# Move the item to the new position
item.move(new_x, new_y)
# Increment the current column
current_column += 1
# If the current column is equal to the number of columns, reset it and increment the current row
if current_column == num_columns:
current_column = 0
current_row += 1
# Update the container size
self.update_container_size()
if not self.is_desktop_window:
self.adjust_window_size()
def align_items_staggered(self):
if not self.items:
return
num_columns = (self.width() // self.item_width_for_positioning)
line_height = int(self.line_height - 1.1 * app.icon_size) # 0.5
current_column = 0
current_row = 0
# Sort the items by name
self.items.sort(key=lambda x: x.name, reverse=False)
# Iterate over the items
for i, item in enumerate(self.items):
# Calculate the new position of the item
if current_row % 2 == 0: # Even row
new_x = current_column * (self.item_width_for_positioning + self.horizontal_spacing + app.icon_size)
else: # Odd row
new_x = (current_column + 0.5) * (self.item_width_for_positioning + self.horizontal_spacing + app.icon_size)
new_y = current_row * (line_height + self.vertical_spacing)
# If the item's text is wider than the item's icon, need to adjust the x position by moving it to the left
if item.text_label.width() > item.icon_label.width():
new_x -= int((item.text_label.width() - item.icon_label.width()) / 2)
# Space on top and at the left of the window, at the top 10 pixels, at the left half of the item width
new_x += int(self.item_width_for_positioning/4)
new_y += 10
# Move the item to the new position
item.move(int(new_x), int(new_y))
# Increment the current column
if current_row % 2 == 0: # Even row
current_column += 1
if current_column >= num_columns:
current_column = 0
current_row += 1
else: # Odd row
current_column += 1
if current_column >= num_columns - 1:
current_column = 0
current_row += 1
# Update the container size
self.update_container_size()
if not self.is_desktop_window:
self.adjust_window_size()
def align_items_desktop(self):
if not self.items:
return
num_rows = (self.height() // self.line_height) - 1
start_x = self.width() - self.item_width_for_positioning
start_y = 10
space_on_top = 10
def position_item(item, column, row):
new_x = start_x - column * (self.item_width_for_positioning + self.horizontal_spacing)
new_y = start_y + row * (self.line_height + self.vertical_spacing)
if item.text_label.width() > item.icon_label.width():
new_x -= int((item.text_label.width() - item.icon_label.width()) / 2)
new_x += int(self.item_width_for_positioning / 4)
new_y += space_on_top
item.move(new_x, new_y)
current_column, current_row = 0, 0
for item in self.items:
if item.name == app.trash_name:
continue
position_item(item, current_column, current_row)
current_row += 1
if current_row == num_rows:
current_row = 0
current_column += 1
if current_row == num_rows - 1 and current_column == 0:
current_row = 0
current_column += 1
# Position the Trash item
trash = next((item for item in self.items if item.name == app.trash_name), None)
if trash:
position_item(trash, 0, num_rows - 1)
def align_items_circle(self):
if not self.items:
return
radius = self.width() // 2 - self.horizontal_spacing - self.item_width_for_positioning // 2
# Calculate the center of the circle
circle_center_x = radius + self.item_width_for_positioning // 2
circle_center_y = radius + self.vertical_spacing
# Iterate over the items
for i, item in enumerate(self.items):
# Calculate the new position of the item
angle = i * 2 * math.pi / len(self.items)
new_x = circle_center_x + radius * math.cos(angle)
new_y = circle_center_y + radius * math.sin(angle)
# If the item's text is wider than the item's icon, need to adjust the x position by moving it to the left
if item.text_label.width() > item.icon_label.width():
new_x -= int((item.text_label.width() - item.icon_label.width()) / 2)
# Move the item to the new position
item.move(int(new_x), int(new_y))
if not self.is_desktop_window:
self.adjust_window_size()
def show_about(self):
dialog = QMessageBox(self)
dialog.setIconPixmap(app.icon.pixmap(app.icon_size, app.icon_size))
dialog.setWindowTitle("About")
dialog.setText("Spatial File Manager\n\nA simple file manager that uses a spatial interface.")
dialog.exec()
def robust_filename(path):
# Use this instead of os.path.basename to avoid issues on Windows
name = os.path.basename(path)
# If the path is e.g., "C:/", the name should be "C:"
if name == "":
name = path
# Remove the final slash if it ends with one
if name.endswith("/") or name.endswith("\\"):
name = name[:-1]
return name
class Item(QWidget):
def __init__(self, path, is_directory, position, parent=None):
super().__init__(parent)
self.path = os.path.normpath(path)
self.name = robust_filename(path)
# For spring-loaded folders
self.hover_timer = QTimer(self)
self.hover_timer.setSingleShot(True)
self.hover_timer.timeout.connect(self.spring_open)
# On Windows, files ending with .lnk are shortcuts; we remove the final extension from the name
if sys.platform == "win32" and self.name.endswith(".lnk"):
self.name = os.path.splitext(self.name)[0]
self.is_directory = is_directory
self.position = position
self.setAcceptDrops(True)
icon_provider = QFileIconProvider()
# Trash
if self.path == os.path.normpath(get_desktop_directory() + "/" + app.trash_name):
icon = icon_provider.icon(QFileIconProvider.IconType.Trashcan).pixmap(app.icon_size, app.icon_size)
if sys.platform == 'win32':
sys_drive = os.getenv('SystemDrive')
self.path = f"{sys_drive}\\$Recycle.Bin"
else:
self.path = QDir.homePath() + '/.local/share/Trash/files/'
elif appdir.is_appdir(self.path):
A = appdir.AppDir(self.path)
icon_path = A.get_icon_path()
if icon_path:
icon = QIcon(icon_path).pixmap(app.icon_size, app.icon_size)
else:
icon = icon_provider.icon(QFileInfo(self.path)).pixmap(app.icon_size, app.icon_size)
else:
icon = icon_provider.icon(QFileInfo(self.path)).pixmap(app.icon_size, app.icon_size)
# Maximum 150 pixels wide, elide the text in the middle
font_metrics = QFontMetrics(self.font())
self.elided_name = font_metrics.elidedText(self.name, Qt.TextElideMode.ElideMiddle, 150)
# For screenshotting: Replace each letter in the elided name with a random letter; preserve the length. Preserve the case of the letters.
# import random
# import string
# self.elided_name = "".join(random.choice(string.ascii_letters) if c.isalpha() else c for c in self.elided_name)
# if len(self.elided_name) > 12:
# self.elided_name = self.elided_name[5:]
# Set icon size and padding
self.icon_size = app.icon_size
padding = 0 # Padding around icon and text
# Calculate the text width
text_width = font_metrics.horizontalAdvance(self.elided_name)
widget_width = max(self.icon_size, text_width) + padding * 2