manuskript/manuskript/settings.py

254 lines
7.5 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
2015-07-04 09:00:54 +12:00
import collections
2016-03-05 12:35:14 +13:00
import json
2016-02-07 00:34:22 +13:00
import pickle
from PyQt5.QtWidgets import qApp
from manuskript.enums import Outline
2016-03-05 12:35:14 +13:00
# TODO: move some/all of those settings to application settings and not project settings
# in order to allow a shared project between several writers
viewSettings = {
"Tree": {
"Icon": "Nothing",
"Text": "Compile",
"Background": "Nothing",
2015-06-21 20:44:11 +12:00
"InfoFolder": "Nothing",
"InfoText": "Nothing",
},
"Cork": {
"Icon": "Nothing",
"Text": "Nothing",
"Background": "Nothing",
"Corner": "Label",
"Border": "Nothing",
},
"Outline": {
"Icon": "Nothing",
"Text": "Compile",
"Background": "Nothing",
},
}
2016-03-05 12:35:14 +13:00
# Application
spellcheck = False
dict = None
corkSizeFactor = 100
folderView = "cork"
2015-06-16 06:30:18 +12:00
lastTab = 0
2015-06-30 00:21:57 +12:00
openIndexes = [""]
2015-06-18 04:40:55 +12:00
autoSave = False
autoSaveDelay = 5
2015-06-18 04:40:55 +12:00
autoSaveNoChanges = True
autoSaveNoChangesDelay = 5
saveOnQuit = True
2015-06-18 04:40:55 +12:00
outlineViewColumns = [Outline.title.value, Outline.POV.value, Outline.status.value,
Outline.compile.value, Outline.wordCount.value, Outline.goal.value,
Outline.goalPercentage.value, Outline.label.value]
2015-06-18 06:45:24 +12:00
corkBackground = {
"color": "#926239",
"image": ""
}
defaultTextType = "md"
2015-06-20 04:47:45 +12:00
fullScreenTheme = "spacedreams"
2015-06-26 03:06:07 +12:00
textEditor = {
"background": "#fff",
2015-06-26 03:06:07 +12:00
"fontColor": "#000",
"font": qApp.font().toString(),
"misspelled": "#F00",
"lineSpacing": 100,
"tabWidth": 20,
"indent": True,
"spacingAbove": 5,
"spacingBelow": 5,
}
2015-07-04 09:00:54 +12:00
revisions = {
"keep": True,
"smartremove": True,
"rules": collections.OrderedDict({
10 * 60: 60, # One per minute for the last 10mn
60 * 60: 60 * 10, # One per 10mn for the last hour
60 * 60 * 24: 60 * 60, # One per hour for the last day
60 * 60 * 24 * 30: 60 * 60 * 24, # One per day for the last month
None: 60 * 60 * 24 * 7, # One per week for eternity
})
}
2016-02-09 01:50:35 +13:00
frequencyAnalyzer = {
"wordMin": 1,
"wordExclude": "a, and, or",
"phraseMin": 2,
"phraseMax": 5
}
2016-03-25 01:42:47 +13:00
viewMode = "fiction"
2015-07-04 09:00:54 +12:00
2016-03-05 12:35:14 +13:00
def save(filename=None, protocol=None):
2015-06-30 00:21:57 +12:00
global spellcheck, dict, corkSliderFactor, viewSettings, corkSizeFactor, folderView, lastTab, openIndexes, \
2015-06-18 06:45:24 +12:00
autoSave, autoSaveDelay, saveOnQuit, autoSaveNoChanges, autoSaveNoChangesDelay, outlineViewColumns, \
2016-03-25 01:42:47 +13:00
corkBackground, fullScreenTheme, defaultTextType, textEditor, revisions, frequencyAnalyzer, viewMode
allSettings = {
"viewSettings": viewSettings,
"dict": dict,
"spellcheck": spellcheck,
"corkSizeFactor": corkSizeFactor,
2015-06-16 06:30:18 +12:00
"folderView": folderView,
"lastTab": lastTab,
2015-06-30 00:21:57 +12:00
"openIndexes": openIndexes,
"autoSave":autoSave,
"autoSaveDelay":autoSaveDelay,
"saveOnQuit":saveOnQuit,
2015-06-18 04:40:55 +12:00
"autoSaveNoChanges":autoSaveNoChanges,
"autoSaveNoChangesDelay":autoSaveNoChangesDelay,
"outlineViewColumns":outlineViewColumns,
2015-06-18 06:45:24 +12:00
"corkBackground":corkBackground,
2015-06-20 04:47:45 +12:00
"fullScreenTheme":fullScreenTheme,
2015-06-25 20:01:28 +12:00
"defaultTextType":defaultTextType,
2015-06-26 03:06:07 +12:00
"textEditor":textEditor,
2015-07-04 09:00:54 +12:00
"revisions":revisions,
2016-03-25 01:42:47 +13:00
"frequencyAnalyzer": frequencyAnalyzer,
"viewMode": viewMode,
2016-03-05 12:35:14 +13:00
}
#pp=pprint.PrettyPrinter(indent=4, compact=False)
#print("Saving:")
#pp.pprint(allSettings)
2015-06-17 23:25:46 +12:00
if filename:
f = open(filename, "wb")
pickle.dump(allSettings, f)
else:
2016-03-05 12:35:14 +13:00
if protocol == 0:
# This looks stupid
# But a simple json.dumps with sort_keys will throw a TypeError
# because of unorderable types.
return json.dumps(json.loads(json.dumps(allSettings)), indent=4, sort_keys=True)
else:
return pickle.dumps(allSettings)
2016-03-10 23:01:35 +13:00
def load(string, fromString=False, protocol=None):
2015-06-17 23:25:46 +12:00
"""Load settings from 'string'. 'string' is the filename of the pickle dump.
If fromString=True, string is the data of the pickle dumps."""
global allSettings
if not fromString:
try:
f = open(string, "rb")
allSettings = pickle.load(f)
except:
print("{} doesn't exist, cannot load settings.".format(string))
return
else:
2016-03-10 23:01:35 +13:00
if protocol == 0:
allSettings = json.loads(string)
else:
allSettings = pickle.loads(string)
#pp=pprint.PrettyPrinter(indent=4, compact=False)
#print("Loading:")
#pp.pprint(allSettings)
if "viewSettings" in allSettings:
global viewSettings
viewSettings = allSettings["viewSettings"]
if "dict" in allSettings:
global dict
dict = allSettings["dict"]
if "spellcheck" in allSettings:
global spellcheck
spellcheck = allSettings["spellcheck"]
if "corkSizeFactor" in allSettings:
global corkSizeFactor
corkSizeFactor = allSettings["corkSizeFactor"]
if "folderView" in allSettings:
global folderView
folderView = allSettings["folderView"]
2015-06-16 06:30:18 +12:00
if "lastTab" in allSettings:
global lastTab
lastTab = allSettings["lastTab"]
2015-06-30 00:21:57 +12:00
if "openIndexes" in allSettings:
global openIndexes
openIndexes = allSettings["openIndexes"]
2015-06-16 06:30:18 +12:00
if "autoSave" in allSettings:
global autoSave
autoSave = allSettings["autoSave"]
if "autoSaveDelay" in allSettings:
global autoSaveDelay
autoSaveDelay = allSettings["autoSaveDelay"]
if "saveOnQuit" in allSettings:
global saveOnQuit
saveOnQuit = allSettings["saveOnQuit"]
2015-06-18 04:40:55 +12:00
if "autoSaveNoChanges" in allSettings:
global autoSaveNoChanges
autoSaveNoChanges = allSettings["autoSaveNoChanges"]
if "autoSaveNoChangesDelay" in allSettings:
global autoSaveNoChangesDelay
autoSaveNoChangesDelay = allSettings["autoSaveNoChangesDelay"]
if "outlineViewColumns" in allSettings:
global outlineViewColumns
outlineViewColumns = allSettings["outlineViewColumns"]
2015-06-18 06:45:24 +12:00
if "corkBackground" in allSettings:
global corkBackground
2015-06-20 04:47:45 +12:00
corkBackground = allSettings["corkBackground"]
if "fullScreenTheme" in allSettings:
global fullScreenTheme
2015-06-24 04:22:39 +12:00
fullScreenTheme = allSettings["fullScreenTheme"]
2015-06-25 20:01:28 +12:00
if "defaultTextType" in allSettings:
global defaultTextType
2015-06-26 03:06:07 +12:00
defaultTextType = allSettings["defaultTextType"]
if "textEditor" in allSettings:
global textEditor
2015-07-04 09:00:54 +12:00
textEditor = allSettings["textEditor"]
if "revisions" in allSettings:
global revisions
2016-02-09 01:50:35 +13:00
revisions = allSettings["revisions"]
2016-03-10 23:01:35 +13:00
# With JSON we had to convert int keys to str, and None to "null", so we roll back.
r = {}
for i in revisions["rules"]:
if i == "null":
r[None] = revisions["rules"]["null"]
2016-03-11 03:35:41 +13:00
2016-03-10 23:01:35 +13:00
elif i == None:
2016-03-11 03:35:41 +13:00
r[None] = revisions["rules"][None]
else:
r[int(i)] = revisions["rules"][i]
2016-03-10 23:01:35 +13:00
revisions["rules"] = r
2016-02-09 01:50:35 +13:00
if "frequencyAnalyzer" in allSettings:
global frequencyAnalyzer
frequencyAnalyzer = allSettings["frequencyAnalyzer"]
2016-03-25 01:42:47 +13:00
if "viewMode" in allSettings:
global viewMode
viewMode = allSettings["viewMode"]