Skip to content

Commit 471f841

Browse files
committed
Handle cases where the user cancels the example project creation (#2590)
1 parent c0a6734 commit 471f841

File tree

2 files changed

+28
-21
lines changed

2 files changed

+28
-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

0 commit comments

Comments
 (0)