Skip to content
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
8 changes: 8 additions & 0 deletions novelwriter/shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,14 @@ def getProjectPath(
)
return Path(selected) if selected else None

def getProjectFolder(self, parent: QWidget, path: str | Path | None = None) -> Path | None:
"""Open the folder dialog and select a project folder."""
location = QFileDialog.getExistingDirectory(
parent, self.tr("Select Project Folder"), str(path),
options=QFileDialog.Option.ShowDirsOnly,
)
return Path(location) if location else None

def getFont(self, current: QFont, native: bool) -> tuple[QFont, bool]:
"""Open the font dialog and select a font."""
from novelwriter.extensions.modified import NFontDialog
Expand Down
41 changes: 20 additions & 21 deletions novelwriter/tools/welcome.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@
)
from PyQt6.QtGui import QAction, QCloseEvent, QFont, QPainter, QPaintEvent, QPen, QShortcut
from PyQt6.QtWidgets import (
QApplication, QFileDialog, QFormLayout, QHBoxLayout, QLabel, QLineEdit,
QListView, QMenu, QScrollArea, QStackedWidget, QStyledItemDelegate,
QApplication, QFormLayout, QHBoxLayout, QLabel, QLineEdit, QListView,
QMenu, QScrollArea, QStackedWidget, QStyledItemDelegate,
QStyleOptionViewItem, QVBoxLayout, QWidget
)

Expand Down Expand Up @@ -351,20 +351,22 @@ def _openContextMenu(self, pos: QPoint) -> None:

def _processOpenProjectRequest(self, path: str) -> None:
"""Process an open project request which may involve create."""
if path == SAMPLE_KEY and (location := QFileDialog.getExistingDirectory(
self, self.tr("Select Folder"), str(CONFIG.homePath),
options=QFileDialog.Option.ShowDirsOnly,
)):
path = str(Path(location) / SAMPLE_NAME)
data = {
"name": SAMPLE_NAME,
"path": path,
"sample": True,
}
builder = ProjectBuilder()
builder.buildProject(data)

self.openProjectRequest.emit(Path(path))
if path == SAMPLE_KEY:
if location := SHARED.getProjectFolder(self, CONFIG.homePath()):
sample = location / SAMPLE_NAME
data = {
"name": SAMPLE_NAME,
"path": sample,
"sample": True,
}
builder = ProjectBuilder()
if builder.buildProject(data):
self.openProjectRequest.emit(sample)
else:
SHARED.error(self.tr("You must select a location for the example project."))
return
else:
self.openProjectRequest.emit(Path(path))

def _selectFirstItem(self) -> None:
"""Select the first item, if any are available."""
Expand Down Expand Up @@ -709,11 +711,8 @@ def getProjectData(self) -> dict:
@pyqtSlot()
def _doBrowse(self) -> None:
"""Select a project folder."""
if path := QFileDialog.getExistingDirectory(
self, self.tr("Select Project Folder"),
str(self._basePath), options=QFileDialog.Option.ShowDirsOnly
):
self._basePath = Path(path)
if path := SHARED.getProjectFolder(self, self._basePath):
self._basePath = path
self._updateProjPath()
CONFIG.setLastPath("project", path)

Expand Down
17 changes: 17 additions & 0 deletions tests/test_tools/test_tools_welcome.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,28 @@ def testToolWelcome_Main(qtbot, monkeypatch, nwGUI, fncPath):
assert selected[0] == SAMPLE_NAME
assert selected[1] == SAMPLE_KEY
welcome.tabOpen._selectFirstItem()

# A folder must be selected
with monkeypatch.context() as mp:
mp.setattr(QFileDialog, "getExistingDirectory", lambda *a, **k: None)
welcome.tabOpen._processOpenProjectRequest(SAMPLE_KEY)

assert SHARED.lastAlert[0] == "You must select a location for the example project."

# Successful
with monkeypatch.context() as mp:
mp.setattr(QFileDialog, "getExistingDirectory", lambda *a, **k: fncPath)
welcome.tabOpen._processOpenProjectRequest(SAMPLE_KEY)

assert (fncPath / SAMPLE_NAME).is_dir()

# But can't create a duplicate
with monkeypatch.context() as mp:
mp.setattr(QFileDialog, "getExistingDirectory", lambda *a, **k: fncPath)
welcome.tabOpen._processOpenProjectRequest(SAMPLE_KEY)

assert SHARED.lastAlert[0] == "The target folder already exists. Please choose another folder."

# qtbot.stop()
welcome.close()

Expand Down