Extratcing the example project now works
This commit is contained in:
@@ -15,6 +15,7 @@ novelWriter.qhc
|
|||||||
__pycache__
|
__pycache__
|
||||||
|
|
||||||
# Sample Project
|
# Sample Project
|
||||||
|
/nw/assets/sample.zip
|
||||||
/sample/cache
|
/sample/cache
|
||||||
/sample/meta
|
/sample/meta
|
||||||
*.bak
|
*.bak
|
||||||
|
|||||||
+82
-14
@@ -32,7 +32,7 @@ import nw
|
|||||||
from os import path, mkdir, listdir, unlink, rename, rmdir
|
from os import path, mkdir, listdir, unlink, rename, rmdir
|
||||||
from lxml import etree
|
from lxml import etree
|
||||||
from time import time
|
from time import time
|
||||||
from shutil import make_archive
|
from shutil import make_archive, unpack_archive, copyfile
|
||||||
|
|
||||||
from PyQt5.QtWidgets import QMessageBox
|
from PyQt5.QtWidgets import QMessageBox
|
||||||
|
|
||||||
@@ -175,6 +175,21 @@ class NWProject():
|
|||||||
"""Create a new project by populating the project tree with a
|
"""Create a new project by populating the project tree with a
|
||||||
few starter items.
|
few starter items.
|
||||||
"""
|
"""
|
||||||
|
popMinimal = True
|
||||||
|
popCustom = False
|
||||||
|
popSample = False
|
||||||
|
if "popMinimal" in projData:
|
||||||
|
popMinimal = projData["popMinimal"]
|
||||||
|
if "popCustom" in projData:
|
||||||
|
popCustom = projData["popCustom"]
|
||||||
|
if "popSample" in projData:
|
||||||
|
popSample = projData["popSample"]
|
||||||
|
|
||||||
|
# Check if we're extracting the sample project.
|
||||||
|
# This is handled differently as it isn't actually a new project.
|
||||||
|
if popSample:
|
||||||
|
return self.extractSampleProject(projData)
|
||||||
|
|
||||||
# Project Title and Authors
|
# Project Title and Authors
|
||||||
if "projName" in projData:
|
if "projName" in projData:
|
||||||
self.setProjectName(projData["projName"])
|
self.setProjectName(projData["projName"])
|
||||||
@@ -185,17 +200,9 @@ class NWProject():
|
|||||||
if "projAuthors" in projData:
|
if "projAuthors" in projData:
|
||||||
self.setBookAuthors(projData["projAuthors"])
|
self.setBookAuthors(projData["projAuthors"])
|
||||||
|
|
||||||
popMinimal = True
|
|
||||||
popSample = False
|
|
||||||
popCustom = False
|
|
||||||
if "popMinimal" in projData:
|
|
||||||
popMinimal = projData["popMinimal"]
|
|
||||||
if "popSample" in projData:
|
|
||||||
popSample = projData["popSample"]
|
|
||||||
if "popCustom" in projData:
|
|
||||||
popCustom = projData["popCustom"]
|
|
||||||
|
|
||||||
if popMinimal:
|
if popMinimal:
|
||||||
|
# Creating a minimal project with a few root folders, a
|
||||||
|
# single chapter folder with a single file.
|
||||||
hNovel = self.newRoot("Novel", nwItemClass.NOVEL)
|
hNovel = self.newRoot("Novel", nwItemClass.NOVEL)
|
||||||
hChars = self.newRoot("Characters", nwItemClass.CHARACTER)
|
hChars = self.newRoot("Characters", nwItemClass.CHARACTER)
|
||||||
hWorld = self.newRoot("Plot", nwItemClass.PLOT)
|
hWorld = self.newRoot("Plot", nwItemClass.PLOT)
|
||||||
@@ -203,10 +210,11 @@ class NWProject():
|
|||||||
hChapF = self.newFolder("New Chapter", nwItemClass.NOVEL, hNovel)
|
hChapF = self.newFolder("New Chapter", nwItemClass.NOVEL, hNovel)
|
||||||
hScene = self.newFile("New Scene", nwItemClass.NOVEL, hChapF)
|
hScene = self.newFile("New Scene", nwItemClass.NOVEL, hChapF)
|
||||||
|
|
||||||
elif popSample:
|
|
||||||
pass
|
|
||||||
|
|
||||||
elif popCustom:
|
elif popCustom:
|
||||||
|
# Create a project structure based on selected root folders
|
||||||
|
# and a number of chapters and scenes selected in the
|
||||||
|
# wizard's custom page.
|
||||||
|
|
||||||
# Create Root Folders
|
# Create Root Folders
|
||||||
hNovel = self.newRoot("Novel", nwItemClass.NOVEL)
|
hNovel = self.newRoot("Novel", nwItemClass.NOVEL)
|
||||||
if "addRoots" in projData:
|
if "addRoots" in projData:
|
||||||
@@ -255,12 +263,72 @@ class NWProject():
|
|||||||
# Fallback just in case.
|
# Fallback just in case.
|
||||||
self.newRoot("Novel", nwItemClass.NOVEL)
|
self.newRoot("Novel", nwItemClass.NOVEL)
|
||||||
|
|
||||||
|
# Finalise
|
||||||
self.projOpened = time()
|
self.projOpened = time()
|
||||||
self.setProjectChanged(True)
|
self.setProjectChanged(True)
|
||||||
self.saveProject(autoSave=True)
|
self.saveProject(autoSave=True)
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
def extractSampleProject(self, projData):
|
||||||
|
"""Make a copy of the sample project.
|
||||||
|
First, try to copy the content of the sample folder to the new
|
||||||
|
project path, or if the folder doesn't exist, look for the zip
|
||||||
|
file in the assets folder.
|
||||||
|
"""
|
||||||
|
if "projName" in projData:
|
||||||
|
projName = projData["projName"]
|
||||||
|
else:
|
||||||
|
projName = "Sample Project"
|
||||||
|
|
||||||
|
if "projPath" in projData:
|
||||||
|
projPath = projData["projPath"]
|
||||||
|
else:
|
||||||
|
logger.error("No project path set for the example project")
|
||||||
|
return False
|
||||||
|
|
||||||
|
srcSample = path.abspath(path.join(self.mainConf.appRoot, "sample"))
|
||||||
|
pkgSample = path.join(self.mainConf.assetPath, "sample.zip")
|
||||||
|
|
||||||
|
isSuccess = False
|
||||||
|
if path.isfile(pkgSample):
|
||||||
|
|
||||||
|
try:
|
||||||
|
unpack_archive(pkgSample, projPath)
|
||||||
|
isSuccess = True
|
||||||
|
except Exception as e:
|
||||||
|
self.makeAlert(
|
||||||
|
["Failed to create a new example project.", str(e)], nwAlert.ERROR
|
||||||
|
)
|
||||||
|
|
||||||
|
elif path.isdir(srcSample):
|
||||||
|
|
||||||
|
try:
|
||||||
|
srcProj = path.join(srcSample, nwFiles.PROJ_FILE)
|
||||||
|
dstProj = path.join(projPath, nwFiles.PROJ_FILE)
|
||||||
|
copyfile(srcProj, dstProj)
|
||||||
|
|
||||||
|
srcContent = path.join(srcSample, "content")
|
||||||
|
dstContent = path.join(projPath, "content")
|
||||||
|
for srcFile in listdir(srcContent):
|
||||||
|
srcDoc = path.join(srcContent, srcFile)
|
||||||
|
dstDoc = path.join(dstContent, srcFile)
|
||||||
|
copyfile(srcDoc, dstDoc)
|
||||||
|
|
||||||
|
isSuccess = True
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
self.makeAlert(
|
||||||
|
["Failed to create a new example project.", str(e)], nwAlert.ERROR
|
||||||
|
)
|
||||||
|
|
||||||
|
if isSuccess:
|
||||||
|
self.clearProject()
|
||||||
|
self.theParent.openProject(projPath)
|
||||||
|
self.theParent.rebuildIndex()
|
||||||
|
|
||||||
|
return isSuccess
|
||||||
|
|
||||||
def clearProject(self):
|
def clearProject(self):
|
||||||
"""Clear the data for the current project, and set them to
|
"""Clear the data for the current project, and set them to
|
||||||
default values.
|
default values.
|
||||||
|
|||||||
@@ -248,20 +248,20 @@ class ProjWizardPopulatePage(QWizardPage):
|
|||||||
vS = self.mainConf.pxInt(12)
|
vS = self.mainConf.pxInt(12)
|
||||||
fS = self.mainConf.pxInt(4)
|
fS = self.mainConf.pxInt(4)
|
||||||
|
|
||||||
self.popSample = QRadioButton("Fill the project with sample files")
|
|
||||||
self.popMinimal = QRadioButton("Fill the project with a minimal set of items")
|
self.popMinimal = QRadioButton("Fill the project with a minimal set of items")
|
||||||
self.popCustom = QRadioButton("Show detailed options for filling the project")
|
self.popCustom = QRadioButton("Show detailed options for filling the project")
|
||||||
|
self.popSample = QRadioButton("Fill the project with example files")
|
||||||
self.popMinimal.setChecked(True)
|
self.popMinimal.setChecked(True)
|
||||||
|
|
||||||
self.popBox = QVBoxLayout()
|
self.popBox = QVBoxLayout()
|
||||||
self.popBox.setSpacing(fS)
|
self.popBox.setSpacing(fS)
|
||||||
self.popBox.addWidget(self.popMinimal)
|
self.popBox.addWidget(self.popMinimal)
|
||||||
self.popBox.addWidget(self.popSample)
|
|
||||||
self.popBox.addWidget(self.popCustom)
|
self.popBox.addWidget(self.popCustom)
|
||||||
|
self.popBox.addWidget(self.popSample)
|
||||||
|
|
||||||
self.registerField("popSample", self.popSample)
|
|
||||||
self.registerField("popMinimal", self.popMinimal)
|
self.registerField("popMinimal", self.popMinimal)
|
||||||
self.registerField("popCustom", self.popCustom)
|
self.registerField("popCustom", self.popCustom)
|
||||||
|
self.registerField("popSample", self.popSample)
|
||||||
|
|
||||||
# Assemble
|
# Assemble
|
||||||
self.outerBox = QVBoxLayout()
|
self.outerBox = QVBoxLayout()
|
||||||
|
|||||||
+8
-5
@@ -293,11 +293,14 @@ class GuiMain(QMainWindow):
|
|||||||
|
|
||||||
logger.info("Creating new project")
|
logger.info("Creating new project")
|
||||||
if self.theProject.setProjectPath(projPath, newProject=True):
|
if self.theProject.setProjectPath(projPath, newProject=True):
|
||||||
self.theProject.newProject(projData)
|
if self.theProject.newProject(projData):
|
||||||
self.rebuildTree()
|
self.rebuildTree()
|
||||||
self.saveProject()
|
self.saveProject()
|
||||||
self.hasProject = True
|
self.hasProject = True
|
||||||
self.statusBar.setRefTime(self.theProject.projOpened)
|
self.statusBar.setRefTime(self.theProject.projOpened)
|
||||||
|
else:
|
||||||
|
self.theProject.clearProject()
|
||||||
|
return False
|
||||||
else:
|
else:
|
||||||
self.theProject.clearProject()
|
self.theProject.clearProject()
|
||||||
return False
|
return False
|
||||||
|
|||||||
Reference in New Issue
Block a user