Skip to content

Commit d894375

Browse files
authored
Improve example project creation (#2592)
2 parents c0a6734 + d05ed6f commit d894375

File tree

3 files changed

+45
-21
lines changed

3 files changed

+45
-21
lines changed

novelwriter/shared.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,14 @@ def getProjectPath(
311311
)
312312
return Path(selected) if selected else None
313313

314+
def getProjectFolder(self, parent: QWidget, path: str | Path | None = None) -> Path | None:
315+
"""Open the folder dialog and select a project folder."""
316+
location = QFileDialog.getExistingDirectory(
317+
parent, self.tr("Select Project Folder"), str(path),
318+
options=QFileDialog.Option.ShowDirsOnly,
319+
)
320+
return Path(location) if location else None
321+
314322
def getFont(self, current: QFont, native: bool) -> tuple[QFont, bool]:
315323
"""Open the font dialog and select a font."""
316324
from novelwriter.extensions.modified import NFontDialog

novelwriter/tools/welcome.py

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@
3434
)
3535
from PyQt6.QtGui import QAction, QCloseEvent, QFont, QPainter, QPaintEvent, QPen, QShortcut
3636
from PyQt6.QtWidgets import (
37-
QApplication, QFileDialog, QFormLayout, QHBoxLayout, QLabel, QLineEdit,
38-
QListView, QMenu, QScrollArea, QStackedWidget, QStyledItemDelegate,
37+
QApplication, QFormLayout, QHBoxLayout, QLabel, QLineEdit, QListView,
38+
QMenu, QScrollArea, QStackedWidget, QStyledItemDelegate,
3939
QStyleOptionViewItem, QVBoxLayout, QWidget
4040
)
4141

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

352352
def _processOpenProjectRequest(self, path: str) -> None:
353353
"""Process an open project request which may involve create."""
354-
if path == SAMPLE_KEY and (location := QFileDialog.getExistingDirectory(
355-
self, self.tr("Select Folder"), str(CONFIG.homePath),
356-
options=QFileDialog.Option.ShowDirsOnly,
357-
)):
358-
path = str(Path(location) / SAMPLE_NAME)
359-
data = {
360-
"name": SAMPLE_NAME,
361-
"path": path,
362-
"sample": True,
363-
}
364-
builder = ProjectBuilder()
365-
builder.buildProject(data)
366-
367-
self.openProjectRequest.emit(Path(path))
354+
if path == SAMPLE_KEY:
355+
if location := SHARED.getProjectFolder(self, CONFIG.homePath()):
356+
sample = location / SAMPLE_NAME
357+
data = {
358+
"name": SAMPLE_NAME,
359+
"path": sample,
360+
"sample": True,
361+
}
362+
builder = ProjectBuilder()
363+
if builder.buildProject(data):
364+
self.openProjectRequest.emit(sample)
365+
else:
366+
SHARED.error(self.tr("You must select a location for the example project."))
367+
return
368+
else:
369+
self.openProjectRequest.emit(Path(path))
368370

369371
def _selectFirstItem(self) -> None:
370372
"""Select the first item, if any are available."""
@@ -709,11 +711,8 @@ def getProjectData(self) -> dict:
709711
@pyqtSlot()
710712
def _doBrowse(self) -> None:
711713
"""Select a project folder."""
712-
if path := QFileDialog.getExistingDirectory(
713-
self, self.tr("Select Project Folder"),
714-
str(self._basePath), options=QFileDialog.Option.ShowDirsOnly
715-
):
716-
self._basePath = Path(path)
714+
if path := SHARED.getProjectFolder(self, self._basePath):
715+
self._basePath = path
717716
self._updateProjPath()
718717
CONFIG.setLastPath("project", path)
719718

tests/test_tools/test_tools_welcome.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,28 @@ def testToolWelcome_Main(qtbot, monkeypatch, nwGUI, fncPath):
6969
assert selected[0] == SAMPLE_NAME
7070
assert selected[1] == SAMPLE_KEY
7171
welcome.tabOpen._selectFirstItem()
72+
73+
# A folder must be selected
74+
with monkeypatch.context() as mp:
75+
mp.setattr(QFileDialog, "getExistingDirectory", lambda *a, **k: None)
76+
welcome.tabOpen._processOpenProjectRequest(SAMPLE_KEY)
77+
78+
assert SHARED.lastAlert[0] == "You must select a location for the example project."
79+
80+
# Successful
7281
with monkeypatch.context() as mp:
7382
mp.setattr(QFileDialog, "getExistingDirectory", lambda *a, **k: fncPath)
7483
welcome.tabOpen._processOpenProjectRequest(SAMPLE_KEY)
84+
7585
assert (fncPath / SAMPLE_NAME).is_dir()
7686

87+
# But can't create a duplicate
88+
with monkeypatch.context() as mp:
89+
mp.setattr(QFileDialog, "getExistingDirectory", lambda *a, **k: fncPath)
90+
welcome.tabOpen._processOpenProjectRequest(SAMPLE_KEY)
91+
92+
assert SHARED.lastAlert[0] == "The target folder already exists. Please choose another folder."
93+
7794
# qtbot.stop()
7895
welcome.close()
7996

0 commit comments

Comments
 (0)