openlp/openlp/core/lib/settingsmanager.py

179 lines
6.6 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
# vim: autoindent shiftwidth=4 expandtab textwidth=80 tabstop=4 softtabstop=4
###############################################################################
# OpenLP - Open Source Lyrics Projection #
# --------------------------------------------------------------------------- #
2009-12-31 12:52:01 +00:00
# Copyright (c) 2008-2010 Raoul Snyman #
# Portions copyright (c) 2008-2010 Tim Bentley, Jonathan Corwin, Michael #
2010-03-21 23:58:01 +00:00
# Gorven, Scott Guerrieri, Christian Richter, Maikel Stuivenberg, Martin #
# Thompson, Jon Tibble, Carsten Tinggaard #
# --------------------------------------------------------------------------- #
# This program is free software; you can redistribute it and/or modify it #
# under the terms of the GNU General Public License as published by the Free #
# Software Foundation; version 2 of the License. #
# #
# This program is distributed in the hope that it will be useful, but WITHOUT #
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or #
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for #
# more details. #
# #
# You should have received a copy of the GNU General Public License along #
# with this program; if not, write to the Free Software Foundation, Inc., 59 #
# Temple Place, Suite 330, Boston, MA 02111-1307 USA #
###############################################################################
2010-04-26 16:41:31 +00:00
from PyQt4 import QtCore
2010-04-27 16:27:57 +00:00
from openlp.core.utils import AppLocation
class SettingsManager(object):
2009-07-08 17:18:48 +00:00
"""
2010-04-27 16:27:57 +00:00
Class to control the initial settings for the UI and provide helper
functions for the loading and saving of application settings.
2009-07-08 17:18:48 +00:00
"""
def __init__(self, screen):
self.screen = screen.current
self.width = self.screen[u'size'].width()
self.height = self.screen[u'size'].height()
self.mainwindow_height = self.height * 0.8
mainwindow_docbars = self.width / 5
self.mainwindow_left = 0
self.mainwindow_right = 0
if mainwindow_docbars > 300:
self.mainwindow_left = 300
self.mainwindow_right = 300
else:
self.mainwindow_left = mainwindow_docbars
self.mainwindow_right = mainwindow_docbars
self.slidecontroller = (self.width - (
self.mainwindow_left + self.mainwindow_right) - 100 ) / 2
2009-09-07 19:14:01 +00:00
self.slidecontroller_image = self.slidecontroller - 50
2010-04-27 16:27:57 +00:00
self.showPreviewPanel = QtCore.QSettings().value(
2010-04-26 16:41:31 +00:00
u'user interface/preview panel', True).toBool()
def togglePreviewPanel(self, isVisible):
2010-04-27 16:27:57 +00:00
QtCore.QSettings().setValue(u'user interface/preview panel',
2010-04-26 16:41:31 +00:00
QtCore.QVariant(isVisible))
2010-04-27 16:27:57 +00:00
@staticmethod
def get_last_dir(section, num=None):
"""
Read the last directory used for plugin.
``section``
The section of code calling the method. This is used in the
settings key.
``num``
Defaults to *None*. A further qualifier.
"""
if num:
name = u'last directory %d' % num
else:
name = u'last directory'
last_dir = unicode(QtCore.QSettings().value(
section + u'/' + name, u'').toString())
return last_dir
@staticmethod
def set_last_dir(section, directory, num=None):
"""
Save the last directory used for plugin.
``section``
The section of code calling the method. This is used in the
settings key.
``directory``
The directory being stored in the settings.
``num``
Defaults to *None*. A further qualifier.
"""
if num:
name = u'last directory %d' % num
else:
name = u'last directory'
QtCore.QSettings().setValue(
section + u'/' + name, QtCore.QVariant(directory))
@staticmethod
def set_list(section, name, list):
"""
Save a list to application settings.
``section``
The section of the settings to store this list.
``name``
The name of the list to save.
``list``
The list of values to save.
"""
settings = QtCore.QSettings()
old_count = settings.value(
u'%s/%s count' % (section, name), 0).toInt()[0]
new_count = len(list)
settings.setValue(
u'%s/%s count' % (section, name), QtCore.QVariant(new_count))
for counter in range (0, new_count):
settings.setValue(
u'%s/%s %d' % (section, name, counter), list[counter-1])
if old_count > new_count:
# Tidy up any old list itrms if list is smaller now
for counter in range(new_count, old_count):
settings.remove(u'%s/%s %d' % (section, name, counter))
@staticmethod
def load_list(section, name):
"""
Load a list from the config file.
``section``
The section of the settings to load the list from.
``name``
The name of the list.
"""
settings = QtCore.QSettings()
list_count = settings.value(
u'%s/%s count' % (section, name), 0).toInt()[0]
list = []
if list_count:
for counter in range(0, list_count):
item = unicode(settings.value(
u'%s/%s %d' % (section, name, counter)).toString())
if item:
list.append(item)
return list
@staticmethod
def get_files(suffix=None):
"""
Get a list of files from the data files path.
``suffix``
Defaults to *None*. The extension to search for.
"""
try:
files = os.listdir(AppLocation.get_data_path())
except:
return []
if suffix:
return_files = []
for file in files:
if file.find(u'.') != -1:
filename = file.split(u'.')
filesuffix = filename[1].lower()
filesuffix = filesuffix.lower()
# only load files with the correct suffix
if suffix.find(filesuffix) > -1 :
return_files.append(file)
return return_files
else:
# no filtering required
return files