Skip to content

Commit 3a9e7cc

Browse files
committed
FEAT: add drive buttons to File Explorer (closes #296)
1 parent e952be1 commit 3a9e7cc

File tree

2 files changed

+60
-6
lines changed

2 files changed

+60
-6
lines changed

larray_editor/editor.py

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@
3737
from qtpy.QtCore import Qt, QUrl, QSettings
3838
from qtpy.QtGui import QDesktopServices, QKeySequence
3939
from qtpy.QtWidgets import (QMainWindow, QWidget, QListWidget, QListWidgetItem, QSplitter, QFileDialog, QPushButton,
40-
QDialogButtonBox, QShortcut, QVBoxLayout, QGridLayout, QLineEdit,
40+
QDialogButtonBox, QShortcut,
41+
QHBoxLayout, QVBoxLayout, QGridLayout, QLineEdit,
4142
QCheckBox, QComboBox, QMessageBox, QDialog,
4243
QInputDialog, QLabel, QGroupBox, QRadioButton,
4344
QTabWidget)
@@ -60,7 +61,7 @@
6061
get_documentation_url,
6162
URLS,
6263
RecentlyUsedList,
63-
logger)
64+
logger, list_drives)
6465
from larray_editor.arraywidget import ArrayEditorWidget
6566
from larray_editor import arrayadapter
6667
from larray_editor.commands import EditSessionArrayCommand, EditCurrentArrayCommand
@@ -117,6 +118,9 @@ def __init__(self, data, title=None, readonly=False):
117118
super().__init__(parent=None)
118119
layout = QVBoxLayout()
119120
self.setLayout(layout)
121+
header_layout = self.setup_header_layout()
122+
if header_layout is not None:
123+
layout.addLayout(header_layout)
120124
array_widget = ArrayEditorWidget(self, data=data, readonly=readonly)
121125
self.array_widget = array_widget
122126
layout.addWidget(array_widget)
@@ -131,6 +135,9 @@ def __init__(self, data, title=None, readonly=False):
131135
# TODO: somehow determine better width
132136
self.resize(self.default_width, self.default_height)
133137

138+
def setup_header_layout(self):
139+
return None
140+
134141
def closeEvent(self, event):
135142
logger.debug('EditorWindow.closeEvent()')
136143
if self in opened_secondary_windows:
@@ -139,6 +146,37 @@ def closeEvent(self, event):
139146
self.array_widget.close()
140147

141148

149+
class FileExplorerWindow(EditorWindow):
150+
name = "File Explorer"
151+
152+
def create_drive_button_clicked_callback(self, drive):
153+
def callback():
154+
path = Path(drive)
155+
if not path.exists():
156+
msg = f"The {drive} drive is currently unavailable !"
157+
QMessageBox.critical(self, "Error", msg)
158+
return
159+
self.array_widget.set_data(path)
160+
return callback
161+
162+
def setup_header_layout(self):
163+
drives = list_drives()
164+
if not drives:
165+
return None
166+
167+
layout = QHBoxLayout()
168+
for drive in drives:
169+
if drive.endswith('\\'):
170+
drive = drive[:-1]
171+
button = QPushButton(drive)
172+
button.clicked.connect(
173+
self.create_drive_button_clicked_callback(drive)
174+
)
175+
layout.addWidget(button)
176+
layout.addStretch()
177+
return layout
178+
179+
142180
class AbstractEditorWindow(QMainWindow):
143181
"""Abstract Editor Window"""
144182

@@ -818,10 +856,11 @@ def display_item_in_new_window(self, list_item):
818856
assert isinstance(list_item, QListWidgetItem)
819857
varname = str(list_item.text())
820858
value = self.data[varname]
821-
self.new_editor_window(value, varname)
859+
self.new_editor_window(value, title=varname)
822860

823-
def new_editor_window(self, data, title: str, readonly: bool=False):
824-
window = EditorWindow(data, title=title, readonly=readonly)
861+
def new_editor_window(self, data, title: str=None, readonly: bool=False,
862+
cls=EditorWindow):
863+
window = cls(data, title=title, readonly=readonly)
825864
window.show()
826865
# this is necessary so that the window does not disappear immediately
827866
opened_secondary_windows.append(window)
@@ -1477,7 +1516,7 @@ def load_example(self):
14771516
self._open_file(filepath)
14781517

14791518
def open_explorer(self):
1480-
self.new_editor_window(Path('.'), title="File Explorer", readonly=True)
1519+
self.new_editor_window(Path('.'), cls=FileExplorerWindow)
14811520

14821521

14831522
class ArrayEditorWindow(AbstractEditorWindow):

larray_editor/utils.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -924,3 +924,18 @@ def new_func(*args, **kwargs):
924924
return func(*args, **kwargs)
925925
return new_func
926926
return decorator
927+
928+
929+
def list_drives():
930+
if PY312:
931+
return os.listdrives()
932+
else:
933+
try:
934+
import win32api
935+
drives_str = win32api.GetLogicalDriveStrings()
936+
return [drivestr for drivestr in drives_str.split('\000')
937+
if drivestr]
938+
except ImportError:
939+
logger.warning("Unable to list drives: on Python < 3.12,"
940+
"this needs the 'win32api' module")
941+
return []

0 commit comments

Comments
 (0)