Skip to content

Commit 5a5ecb0

Browse files
committed
Windows context menu, log console, About
1 parent a013782 commit 5a5ecb0

File tree

3 files changed

+53
-31
lines changed

3 files changed

+53
-31
lines changed

log_console.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import sys
22
import io
3-
from PyQt6.QtWidgets import QMainWindow, QPlainTextEdit
3+
from PyQt6.QtWidgets import QMainWindow, QPlainTextEdit, QMessageBox
44
from PyQt6.QtGui import QAction
55

66
"""When running a GUI application, it is useful to have a log console
@@ -29,7 +29,7 @@ def __init__(self):
2929
lambda: self.log_console.verticalScrollBar().setValue(
3030
self.log_console.verticalScrollBar().maximum()))
3131
# Should the application ever crash, show the log console
32-
sys.excepthook = self.open_log_console
32+
sys.excepthook = self.show_traceback
3333

3434
def write(self, s):
3535
# Ignore whitespace
@@ -45,11 +45,23 @@ def add_menu_items(self, menu, parent):
4545
log_console_action.triggered.connect(self.open_log_console)
4646
menu.addAction(log_console_action)
4747

48+
# Accept all parameters of sys.excepthook
4849
def open_log_console(self):
4950
if self.log_console_window.isVisible():
5051
return
5152
self.log_console_window.show()
5253

54+
def show_traceback(self, exc_type, exc_value, tb):
55+
message_box = QMessageBox()
56+
message_box.setIcon(QMessageBox.Icon.Critical)
57+
message_box.setWindowTitle('Error')
58+
message_box.setText(f'{exc_value}')
59+
import traceback
60+
traceback_str = ''.join(traceback.format_exception(exc_type, exc_value, tb))
61+
message_box.setDetailedText(str(traceback_str))
62+
message_box.setStandardButtons(QMessageBox.StandardButton.Ok)
63+
message_box.exec()
64+
5365
class Tee(object):
5466
def __init__(self, stream1, stream2):
5567
self.stream1 = stream1

main_window.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@
1414
from PyQt6.QtGui import QFileSystemModel, QAction, QPixmap, QDrag, QCursor
1515
from PyQt6.QtWebEngineWidgets import QWebEngineView # pip install PyQt6-WebEngine
1616
import mimetypes
17-
from windows_integration import show_context_menu, show_properties
18-
import windows_file_operations
17+
if sys.platform == 'win32':
18+
from windows_integration import show_context_menu, show_properties
19+
import windows_file_operations
1920
import menus
2021
import toolbar
2122
import status_bar

spatial.py

Lines changed: 36 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@
1111
import shutil
1212

1313
from PyQt6.QtCore import Qt, QPoint, QSize, QDir, QRect, QMimeData, QUrl, QFileSystemWatcher, QFileInfo, QTimer
14-
from PyQt6.QtGui import QFontMetrics, QPainter, QPen, QAction, QDrag, QColor, QPainter, QPen, QBrush, QPixmap, QKeySequence, QFont
14+
from PyQt6.QtGui import QFontMetrics, QPainter, QPen, QAction, QDrag, QColor, QPainter, QPen, QBrush, QPixmap, QKeySequence, QFont, QIcon
1515
from PyQt6.QtWidgets import QApplication, QWidget, QVBoxLayout, QScrollArea, QLabel, QSizePolicy, QMainWindow
1616
from PyQt6.QtWidgets import QStatusBar, QComboBox, QFileIconProvider, QMenuBar, QGridLayout, QMessageBox, QMenu, QDialog
1717

1818
if sys.platform == "win32":
1919
from win32com.client import Dispatch
20-
import winreg
20+
import windows_context_menu
2121

2222
class SpatialFiler(QMainWindow):
2323

@@ -749,7 +749,12 @@ def align_items_circle(self):
749749
item.move(int(new_x), int(new_y))
750750

751751
def show_about(self):
752-
QMessageBox.about(self, "About", "Spatial File Manager\n\nA simple file manager that uses a spatial interface.")
752+
dialog = QMessageBox(self)
753+
dialog.setIconPixmap(app.icon.pixmap(app.icon_size, app.icon_size))
754+
dialog.setWindowTitle("About")
755+
dialog.setText("Spatial File Manager\n\nA simple file manager that uses a spatial interface.")
756+
dialog.exec()
757+
753758

754759
def robust_filename(path):
755760
# Use this instead of os.path.basename to avoid issues on Windows
@@ -862,28 +867,33 @@ def text_label_deactivate(self):
862867
self.text_label.setStyleSheet("background-color: rgba(255, 255, 255, 0.66); color: black;")
863868

864869
def show_context_menu(self, pos):
865-
context_menu = QMenu(self)
866-
self.open_action = QAction("Open", self)
867-
self.open_action.triggered.connect(self.open)
868-
context_menu.addAction(self.open_action)
869-
context_menu.addSeparator()
870-
self.get_info_action = QAction("Get Info", self)
871-
self.get_info_action.triggered.connect(self.get_info)
872-
context_menu.addAction(self.get_info_action)
873-
context_menu.addSeparator()
874-
self.cut_action = QAction("Cut", self)
875-
self.cut_action.setDisabled(True)
876-
context_menu.addAction(self.cut_action)
877-
self.copy_action = QAction("Copy", self)
878-
self.copy_action.setDisabled(True)
879-
context_menu.addAction(self.copy_action)
880-
self.paste_action = QAction("Paste", self)
881-
self.paste_action.setDisabled(True)
882-
context_menu.addAction(self.paste_action)
883-
self.trash_action = QAction("Move to Trash", self)
884-
self.trash_action.setDisabled(True)
885-
context_menu.addAction(self.trash_action)
886-
context_menu.exec(self.mapToGlobal(pos))
870+
# On Windows, use windows_context_menu.py
871+
if sys.platform == "win32":
872+
import windows_context_menu
873+
windows_context_menu.show_context_menu(self.path)
874+
else:
875+
context_menu = QMenu(self)
876+
self.open_action = QAction("Open", self)
877+
self.open_action.triggered.connect(self.open)
878+
context_menu.addAction(self.open_action)
879+
context_menu.addSeparator()
880+
self.get_info_action = QAction("Get Info", self)
881+
self.get_info_action.triggered.connect(self.get_info)
882+
context_menu.addAction(self.get_info_action)
883+
context_menu.addSeparator()
884+
self.cut_action = QAction("Cut", self)
885+
self.cut_action.setDisabled(True)
886+
context_menu.addAction(self.cut_action)
887+
self.copy_action = QAction("Copy", self)
888+
self.copy_action.setDisabled(True)
889+
context_menu.addAction(self.copy_action)
890+
self.paste_action = QAction("Paste", self)
891+
self.paste_action.setDisabled(True)
892+
context_menu.addAction(self.paste_action)
893+
self.trash_action = QAction("Move to Trash", self)
894+
self.trash_action.setDisabled(True)
895+
context_menu.addAction(self.trash_action)
896+
context_menu.exec(self.mapToGlobal(pos))
887897

888898
def get_info(self):
889899
dialog = QDialog(self)
@@ -976,6 +986,7 @@ def get_desktop_directory():
976986
app.desktop_settings_file = ".DS_Spatial"
977987
app.trash_name = "Trash"
978988
app.icon_size = 32
989+
app.icon = QFileIconProvider().icon(QFileIconProvider.IconType.Folder)
979990

980991
# Output not only to the console but also to the GUI
981992
try:
@@ -987,8 +998,6 @@ def get_desktop_directory():
987998
sys.stdout = log_console.Tee(sys.stdout, app.log_console)
988999
sys.stderr = log_console.Tee(sys.stderr, app.log_console)
9891000

990-
991-
9921001
for screen in QApplication.screens():
9931002
# TODO: Possibly only create the desktop window on the primary screen and just show a background image on the other screens
9941003
desktop = SpatialFiler(get_desktop_directory(), is_desktop_window = True)

0 commit comments

Comments
 (0)