Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor improvements #16

Merged
merged 5 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions tests/test_fs_explorer.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,17 @@ def test_line_edited(self, tmp_path, qtbot):
assert fse.root == root
assert fse.pathBar.text() == text

def test_goto_parent_directory(self, tmp_path, qtbot):
"""Make sure the root can be set."""
fse = fs_explorer.FSExplorer()
qtbot.addWidget(fse)

path = tmp_path / "test_path"
path.mkdir()
fse.set_root(str(path))
fse.goto_parent_directory()
assert fse.root == str(tmp_path)

def test_new_file(self, tmp_path, qtbot, caplog, monkeypatch):
"""Make sure a new file behaves correctly."""
fse = fs_explorer.FSExplorer()
Expand Down
6 changes: 4 additions & 2 deletions typstwriter/editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def open_file(self, path):
def open_file_dialog(self):
"""Open a dialog to open an existing file."""
filters = "Typst Files (*.typ);;Any File (*)"
path, cd = QtWidgets.QFileDialog.getOpenFileName(self, "Open File", QtCore.QDir.homePath(), filters)
path, cd = QtWidgets.QFileDialog.getOpenFileName(self, "Open File", state.working_directory.Value, filters)
if path:
self.open_file(path)

Expand Down Expand Up @@ -205,10 +205,12 @@ def childsavedstate_changed(self, savestate):

@QtCore.Slot(str)
def childpath_changed(self, path):
"""Update tab name based on new path."""
"""Update tab name and icon based on new path."""
i = self.TabWidget.indexOf(self.sender())
name = os.path.relpath(path, start=state.working_directory.Value)
icon = util.FileIconProvider().icon(QtCore.QFileInfo(path))
self.TabWidget.tabBar().setTabText(i, name)
self.TabWidget.tabBar().setTabIcon(i, icon)

@QtCore.Slot()
def update_tab_names(self):
Expand Down
21 changes: 20 additions & 1 deletion typstwriter/fs_explorer.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def __init__(self, parent):
self.context_path = None

self.action_open = QtWidgets.QAction("Open", triggered=self.handle_open)
self.action_open_external = QtWidgets.QAction("Open with external program", triggered=self.handle_open_external)
self.action_new_file = QtWidgets.QAction("New file", triggered=self.handle_new_file)
self.action_new_folder = QtWidgets.QAction("New folder", triggered=self.handle_new_folder)
self.action_rename = QtWidgets.QAction("Rename", triggered=self.handle_rename)
Expand All @@ -44,6 +45,10 @@ def handle_open(self):
"""Trigger opening a file."""
self.parent().open_file.emit(self.context_path)

def handle_open_external(self):
"""Trigger opening a file."""
util.open_with_external_program(self.context_path)

def handle_new_file(self):
"""Trigger creating a new file."""
self.parent().new_file_in_dir(self.context_path)
Expand Down Expand Up @@ -92,6 +97,7 @@ def __init__(self, parent):
FSContextMenu.__init__(self, parent)

self.addAction(self.action_open)
self.addAction(self.action_open_external)
self.addAction(self.action_rename)
self.addAction(self.action_delete)
self.addAction(self.action_copy_path)
Expand Down Expand Up @@ -130,12 +136,19 @@ def __init__(self):
self.pathBar = QtWidgets.QLineEdit(self)
self.pathBar.setText("")
self.pathBar.editingFinished.connect(self.line_edited)

self.folderAction = QtWidgets.QAction(
QtGui.QIcon.fromTheme(QtGui.QIcon.FolderOpen, QtGui.QIcon(util.icon_path("folder.svg"))), "open"
)
self.folderAction.triggered.connect(self.open_directory_dialog)
self.pathBar.addAction(self.folderAction, QtWidgets.QLineEdit.LeadingPosition)

self.parentAction = QtWidgets.QAction(
QtGui.QIcon.fromTheme(QtGui.QIcon.GoUp, QtGui.QIcon(util.icon_path("parent_dir.svg"))), "up"
)
self.parentAction.triggered.connect(self.goto_parent_directory)
self.pathBar.addAction(self.parentAction, QtWidgets.QLineEdit.TrailingPosition)

self.tree_view = QtWidgets.QTreeView()
self.filesystem_model = QtWidgets.QFileSystemModel(self.tree_view)
self.filesystem_model.setIconProvider(util.FileIconProvider())
Expand Down Expand Up @@ -172,7 +185,7 @@ def __init__(self):
root_dir = os.path.expanduser(config.get("General", "working_directory"))
if not os.path.exists(root_dir):
root_dir = os.path.expanduser("~/")
self.set_root(root_dir)
self.set_root(os.path.normpath(root_dir))

def open_directory_dialog(self):
"""Open a dialog to select root path and open said path."""
Expand Down Expand Up @@ -221,6 +234,12 @@ def doubleclicked(self, index):
if os.path.isdir(path):
self.set_root(path)

@QtCore.Slot()
def goto_parent_directory(self):
"""Set the parent of the current directory as root."""
path = os.path.dirname(self.root)
self.set_root(path)

def rightclicked(self, event):
"""Handle right click."""
index = self.tree_view.indexAt(event)
Expand Down
72 changes: 72 additions & 0 deletions typstwriter/icons/parent_dir.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion typstwriter/mainwindow.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,8 @@ def set_compiler_output_visibility(self, visibility):

def open_config(self):
"""Open config file."""
util.open_with_external_program(config.writepath)
config.write()
util.open_with_external_program(config.writepath)

def closeEvent(self, event): # noqa: N802
"""Handle close event."""
Expand Down