manuskript/manuskript/settings.py

334 lines
9.9 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
from PyQt5.QtWidgets import qApp
from manuskript.enums import Outline
import logging
LOGGER = logging.getLogger(__name__)
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",
2017-10-24 01:40:55 +13:00
"iconSize": 24,
},
"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
fullscreenSettings = {
"autohide-top": True,
"autohide-bottom": True,
"autohide-left": True,
}
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 = [""]
progressChars = False
countSpaces = True
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
2017-11-16 09:05:48 +13:00
outlineViewColumns = [Outline.title, Outline.POV, Outline.status,
Outline.compile, Outline.wordCount, Outline.goal,
Outline.goalPercentage, Outline.label]
2015-06-18 06:45:24 +12:00
corkBackground = {
"color": "#926239",
"image": "writingdesk"
2015-06-18 06:45:24 +12:00
}
2017-10-15 04:11:17 +13:00
corkStyle = "new"
defaultTextType = "md"
2015-06-20 04:47:45 +12:00
fullScreenTheme = "spacedreams"
2015-06-26 03:06:07 +12:00
textEditor = {
"background": "",
"fontColor": "",
2015-06-26 03:06:07 +12:00
"font": qApp.font().toString(),
"misspelled": "#F00",
"lineSpacing": 100,
"tabWidth": 20,
"indent": False,
2015-06-26 03:06:07 +12:00
"spacingAbove": 5,
"spacingBelow": 5,
"textAlignment": 0, # 0: left, 1: center, 2: right, 3: justify
"cursorWidth": 1,
"cursorNotBlinking": False,
"maxWidth": 600,
"marginsLR": 0,
"marginsTB": 20,
"backgroundTransparent": False,
2017-11-28 22:26:43 +13:00
"alwaysCenter": False,
"focusMode": False # "line", "paragraph", "sentence"
2015-06-26 03:06:07 +12:00
}
2017-10-24 01:40:55 +13:00
2015-07-04 09:00:54 +12:00
revisions = {
"keep": False,
2015-07-04 09:00:54 +12:00
"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" # simple, fiction
saveToZip = False
2017-11-11 04:26:23 +13:00
dontShowDeleteWarning = False
2017-10-24 01:40:55 +13:00
def initDefaultValues():
"""
Load some default values based on system's settings.
Is called anytime we open/create a project.
"""
global textEditor
if not textEditor["background"]:
from manuskript.ui import style as S
textEditor["background"] = S.base
if not textEditor["fontColor"]:
from manuskript.ui import style as S
textEditor["fontColor"] = S.text
2016-03-05 12:35:14 +13:00
def save(filename=None, protocol=None):
2017-10-24 01:40:55 +13:00
2015-06-30 00:21:57 +12:00
global spellcheck, dict, corkSliderFactor, viewSettings, corkSizeFactor, folderView, lastTab, openIndexes, \
progressChars, autoSave, autoSaveDelay, saveOnQuit, autoSaveNoChanges, autoSaveNoChangesDelay, outlineViewColumns, \
2017-10-15 04:11:17 +13:00
corkBackground, corkStyle, fullScreenTheme, defaultTextType, textEditor, revisions, frequencyAnalyzer, viewMode, \
saveToZip, dontShowDeleteWarning, fullscreenSettings
2017-10-24 01:40:55 +13:00
allSettings = {
"viewSettings": viewSettings,
"fullscreenSettings": fullscreenSettings,
"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,
"progressChars": progressChars,
"countSpaces": countSpaces,
"autoSave":autoSave,
"autoSaveDelay":autoSaveDelay,
# TODO: Settings Cleanup Task -- Rename saveOnQuit to saveOnProjectClose -- see PR #615
"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,
2017-10-15 04:11:17 +13:00
"corkStyle": corkStyle,
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,
"saveToZip": saveToZip,
2017-11-11 04:26:23 +13:00
"dontShowDeleteWarning": dontShowDeleteWarning,
2016-03-05 12:35:14 +13:00
}
2017-10-24 01:40:55 +13:00
#pp=pprint.PrettyPrinter(indent=4, compact=False)
#print("Saving:")
#pp.pprint(allSettings)
2017-10-24 01:40:55 +13:00
# 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)
2016-03-05 12:35:14 +13:00
2016-03-10 23:01:35 +13:00
def load(string, fromString=False, protocol=None):
"""fromString=True is deprecated, it shouldn't be used."""
2015-06-17 23:25:46 +12:00
global allSettings
2017-10-24 01:40:55 +13:00
if not string:
LOGGER.error("Cannot load settings.")
return
allSettings = json.loads(string)
2016-03-10 23:01:35 +13:00
#pp=pprint.PrettyPrinter(indent=4, compact=False)
#print("Loading:")
#pp.pprint(allSettings)
2017-10-24 01:40:55 +13:00
# FIXME: use dict.update(dict) to update settings in newer versions.
if "viewSettings" in allSettings:
global viewSettings
viewSettings = allSettings["viewSettings"]
2017-10-24 01:40:55 +13:00
for cat, name, default in [
("Tree", "iconSize", 24), # Added in 0.6.0
]:
if not name in viewSettings[cat]:
viewSettings[cat][name] = default
if "fullscreenSettings" in allSettings:
global fullscreenSettings
fullscreenSettings = allSettings["fullscreenSettings"]
if "dict" in allSettings:
global dict
dict = allSettings["dict"]
2017-10-24 01:40:55 +13:00
if "spellcheck" in allSettings:
global spellcheck
spellcheck = allSettings["spellcheck"]
2017-10-24 01:40:55 +13:00
if "corkSizeFactor" in allSettings:
global corkSizeFactor
corkSizeFactor = allSettings["corkSizeFactor"]
2017-10-24 01:40:55 +13:00
if "folderView" in allSettings:
global folderView
folderView = allSettings["folderView"]
2017-10-24 01:40:55 +13:00
2015-06-16 06:30:18 +12:00
if "lastTab" in allSettings:
global lastTab
lastTab = allSettings["lastTab"]
2017-10-24 01:40:55 +13:00
2015-06-30 00:21:57 +12:00
if "openIndexes" in allSettings:
global openIndexes
openIndexes = allSettings["openIndexes"]
2017-10-24 01:40:55 +13:00
if "progressChars" in allSettings:
global progressChars
progressChars = allSettings["progressChars"]
if "countSpaces" in allSettings:
global countSpaces
countSpaces = allSettings["countSpaces"]
if "autoSave" in allSettings:
global autoSave
autoSave = allSettings["autoSave"]
2017-10-24 01:40:55 +13:00
if "autoSaveDelay" in allSettings:
global autoSaveDelay
autoSaveDelay = allSettings["autoSaveDelay"]
2017-10-24 01:40:55 +13:00
if "saveOnQuit" in allSettings:
global saveOnQuit
saveOnQuit = allSettings["saveOnQuit"]
2017-10-24 01:40:55 +13:00
2015-06-18 04:40:55 +12:00
if "autoSaveNoChanges" in allSettings:
global autoSaveNoChanges
autoSaveNoChanges = allSettings["autoSaveNoChanges"]
2017-10-24 01:40:55 +13:00
2015-06-18 04:40:55 +12:00
if "autoSaveNoChangesDelay" in allSettings:
global autoSaveNoChangesDelay
autoSaveNoChangesDelay = allSettings["autoSaveNoChangesDelay"]
2017-10-24 01:40:55 +13:00
2015-06-18 04:40:55 +12:00
if "outlineViewColumns" in allSettings:
global outlineViewColumns
outlineViewColumns = allSettings["outlineViewColumns"]
2017-10-24 01:40:55 +13:00
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"]
2017-10-24 01:40:55 +13:00
2017-10-15 04:11:17 +13:00
if "corkStyle" in allSettings:
global corkStyle
corkStyle = allSettings["corkStyle"]
2017-10-24 01:40:55 +13:00
2015-06-20 04:47:45 +12:00
if "fullScreenTheme" in allSettings:
global fullScreenTheme
2015-06-24 04:22:39 +12:00
fullScreenTheme = allSettings["fullScreenTheme"]
2017-10-24 01:40:55 +13:00
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"]
added = {
"textAlignment": 0, # Added in 0.5.0
"cursorWidth": 1,
"cursorNotBlinking": False, # Added in 0.6.0
"maxWidth": 600,
"marginsLR": 0,
"marginsTB": 20,
"backgroundTransparent": False, # Added in 0.6.0
2017-11-28 22:26:43 +13:00
"alwaysCenter": False, # Added in 0.7.0
2017-12-06 11:18:32 +13:00
"focusMode": False,
}
for k in added:
if not k in textEditor: textEditor[k] = added[k]
if textEditor["cursorNotBlinking"]:
qApp.setCursorFlashTime(0)
else:
from manuskript.functions import mainWindow
qApp.setCursorFlashTime(mainWindow()._defaultCursorFlashTime)
2015-07-04 09:00:54 +12:00
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"]
if "saveToZip" in allSettings:
global saveToZip
2017-10-24 01:40:55 +13:00
saveToZip = allSettings["saveToZip"]
2017-11-11 04:26:23 +13:00
if "dontShowDeleteWarning" in allSettings:
global dontShowDeleteWarning
dontShowDeleteWarning = allSettings["dontShowDeleteWarning"]