Added the Config class back in fro the old project.
This commit is contained in:
@@ -17,3 +17,8 @@ Here is an overview of the core features I want to implement as a starting point
|
||||
Future features that will be added:
|
||||
|
||||
* Export options for HTML, open document format and PDF (probably via LaTeX or Pandoc).
|
||||
|
||||
## Dependencies
|
||||
|
||||
* python3-pyqt5
|
||||
* python3-appdirs
|
||||
|
||||
+3
-1
@@ -19,7 +19,9 @@ gi.require_version("Gtk","3.0")
|
||||
from os import path, remove, rename
|
||||
from PyQt5.QtWidgets import QApplication
|
||||
from nw.main import NovelWriter
|
||||
from nw.config import Config
|
||||
|
||||
__package__ = "novelWriter"
|
||||
__author__ = "Veronica Berglyd Olsen"
|
||||
__copyright__ = "Copyright 2016-2018, Veronica Berglyd Olsen"
|
||||
__credits__ = ["Veronica Berglyd Olsen"]
|
||||
@@ -70,7 +72,7 @@ logger = logging.getLogger(__name__)
|
||||
#
|
||||
|
||||
# Load the main config as a global object
|
||||
# CONFIG = Config()
|
||||
CONFIG = Config()
|
||||
|
||||
def main(sysArgs):
|
||||
"""
|
||||
|
||||
+139
@@ -0,0 +1,139 @@
|
||||
# -*- coding: utf-8 -*
|
||||
"""novelWriter Config Class
|
||||
|
||||
novelWriter – Config Class
|
||||
============================
|
||||
This class reads and store the main preferences of the application
|
||||
|
||||
File History:
|
||||
Created: 2018-0+-22 [0.1.0]
|
||||
|
||||
"""
|
||||
|
||||
import logging
|
||||
import configparser
|
||||
import nw
|
||||
|
||||
from os import path, mkdir, getcwd
|
||||
from appdirs import user_config_dir
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
class Config:
|
||||
|
||||
WIN_WIDTH = 0
|
||||
WIN_HEIGHT = 1
|
||||
|
||||
def __init__(self):
|
||||
|
||||
# Set Application Variables
|
||||
self.appName = nw.__package__
|
||||
self.appHandle = nw.__package__.lower()
|
||||
self.appURL = "https://github.com/vkbo/novelWriter"
|
||||
|
||||
# Set Paths
|
||||
self.confPath = user_config_dir(self.appHandle)
|
||||
self.confFile = self.appHandle+".conf"
|
||||
self.homePath = path.expanduser("~")
|
||||
self.appPath = path.dirname(__file__)
|
||||
self.guiPath = path.join(self.appPath,"gui")
|
||||
self.themePath = path.join(self.appPath,"themes")
|
||||
|
||||
# If config folder does not exist, make it.
|
||||
# This assumes that the os config folder itself exists.
|
||||
# TODO: This does not work on Windows
|
||||
if not path.isdir(self.confPath):
|
||||
mkdir(self.confPath)
|
||||
|
||||
# Set default values
|
||||
self.confChanged = False
|
||||
|
||||
## General
|
||||
self.winGeometry = [1600, 980]
|
||||
|
||||
# Check if config file exists
|
||||
if path.isfile(path.join(self.confPath,self.confFile)):
|
||||
self.loadConfig()
|
||||
|
||||
# Save a copy of the default config if no file exists
|
||||
if not path.isfile(path.join(self.confPath,self.confFile)):
|
||||
self.saveConfig()
|
||||
|
||||
return
|
||||
|
||||
##
|
||||
# Actions
|
||||
##
|
||||
|
||||
def loadConfig(self):
|
||||
|
||||
logger.debug("Loading config file")
|
||||
confParser = configparser.ConfigParser()
|
||||
confParser.readfp(open(path.join(self.confPath,self.confFile)))
|
||||
|
||||
# Get options
|
||||
|
||||
## Main
|
||||
cnfSec = "Main"
|
||||
if confParser.has_section(cnfSec):
|
||||
if confParser.has_option(cnfSec,"geometry"):
|
||||
self.winGeometry = self.unpackList(
|
||||
confParser.get(cnfSec,"geometry"), 2, self.winGeometry
|
||||
)
|
||||
|
||||
return
|
||||
|
||||
def saveConfig(self):
|
||||
|
||||
logger.debug("Config: Saving")
|
||||
confParser = configparser.ConfigParser()
|
||||
|
||||
# Set options
|
||||
|
||||
## Main
|
||||
cnfSec = "Main"
|
||||
confParser.add_section(cnfSec)
|
||||
confParser.set(cnfSec,"geometry", self.packList(self.winGeometry))
|
||||
|
||||
# Write config file
|
||||
confParser.write(open(path.join(self.confPath,self.confFile),"w"))
|
||||
self.confChanged = False
|
||||
|
||||
return
|
||||
|
||||
def unpackList(self, inStr, listLen, listDefault, castTo=int):
|
||||
inData = inStr.split(",")
|
||||
outData = []
|
||||
for i in range(listLen):
|
||||
try:
|
||||
outData.append(castTo(inData[i]))
|
||||
except:
|
||||
outData.append(listDefault[i])
|
||||
return outData
|
||||
|
||||
def packList(self, inData):
|
||||
return ", ".join(str(inVal) for inVal in inData)
|
||||
|
||||
##
|
||||
# Setters
|
||||
##
|
||||
|
||||
def setConfPath(self, newPath):
|
||||
if newPath is None: return
|
||||
if not path.isfile(newPath):
|
||||
logger.error("Config: File not found. Using default config path instead.")
|
||||
return
|
||||
self.confPath = path.dirname(newPath)
|
||||
self.confFile = path.basename(newPath)
|
||||
return
|
||||
|
||||
def setWinSize(self, newWidth, newHeight):
|
||||
if abs(self.winGeometry[self.WIN_WIDTH] - newWidth) >= 10:
|
||||
self.winGeometry[self.WIN_WIDTH] = newWidth
|
||||
self.confChanged = True
|
||||
if abs(self.winGeometry[self.WIN_HEIGHT] - newHeight) >= 10:
|
||||
self.winGeometry[self.WIN_HEIGHT] = newHeight
|
||||
self.confChanged = True
|
||||
return
|
||||
|
||||
# End Class Config
|
||||
@@ -0,0 +1,37 @@
|
||||
# -*- coding: utf-8 -*
|
||||
"""novelWriter GUI Main Window
|
||||
|
||||
novelWriter – GUI Main Window
|
||||
===============================
|
||||
Class holding the main window
|
||||
|
||||
File History:
|
||||
Created: 2018-0+-22 [0.1.0]
|
||||
|
||||
"""
|
||||
|
||||
import logging
|
||||
import nw
|
||||
|
||||
from os import path
|
||||
from PyQt5.QtWidgets import QWidget
|
||||
from PyQt5.QtGui import QIcon
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
class GuiMain(QWidget):
|
||||
|
||||
def __init__(self):
|
||||
QWidget.__init__(self)
|
||||
|
||||
self.mainConf = nw.CONFIG
|
||||
|
||||
self.resize(600,500)
|
||||
self.setWindowTitle("%s [%s]" % (nw.__package__, nw.__version__))
|
||||
self.setWindowIcon(QIcon(path.join(self.mainConf.appPath,"..","novelWriter.svg")))
|
||||
|
||||
self.show()
|
||||
|
||||
return
|
||||
|
||||
# END Class GuiMain
|
||||
+4
-13
@@ -13,25 +13,16 @@
|
||||
import logging
|
||||
import nw
|
||||
|
||||
from PyQt5.QtWidgets import QWidget
|
||||
from PyQt5.QtGui import QIcon
|
||||
from nw.gui.winmain import GuiMain
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
class NovelWriter(QWidget):
|
||||
class NovelWriter():
|
||||
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
|
||||
self.initGUI()
|
||||
|
||||
def initGUI(self):
|
||||
|
||||
# self.setGeometry(300, 300, 300, 220)
|
||||
self.resize(600,500)
|
||||
self.setWindowTitle("novelWriter")
|
||||
self.setWindowIcon(QIcon("novelWriter.svg"))
|
||||
|
||||
self.show()
|
||||
self.winMain = GuiMain()
|
||||
|
||||
return
|
||||
# END Class NovelWriter
|
||||
|
||||
Reference in New Issue
Block a user