Workaround for Qt 5 regression causing custom colors in color chooser dialogs to not be remembered between calibre sessions

This commit is contained in:
Kovid Goyal 2014-08-25 20:07:35 +05:30
parent f87d50c327
commit 2c0af0e0fe
3 changed files with 36 additions and 6 deletions

View File

@ -14,7 +14,7 @@ ORG_NAME = 'KovidsBrain'
APP_UID = 'libprs500'
from calibre import prints
from calibre.constants import (islinux, iswindows, isbsd, isfrozen, isosx,
plugins, config_dir, filesystem_encoding, DEBUG, isxp)
plugins, config_dir, filesystem_encoding, isxp)
from calibre.utils.config import Config, ConfigProxy, dynamic, JSONConfig
from calibre.ebooks.metadata import MetaInformation
from calibre.utils.date import UNDEFINED_DATE
@ -242,8 +242,7 @@ config = _config()
# }}}
QSettings.setPath(QSettings.IniFormat, QSettings.UserScope, config_dir)
QSettings.setPath(QSettings.IniFormat, QSettings.SystemScope,
config_dir)
QSettings.setPath(QSettings.IniFormat, QSettings.SystemScope, config_dir)
QSettings.setDefaultFormat(QSettings.IniFormat)
# Turn off DeprecationWarnings in windows GUI
@ -876,7 +875,7 @@ def setup_gui_option_parser(parser):
class Application(QApplication):
def __init__(self, args, force_calibre_style=False, override_program_name=None, headless=False):
def __init__(self, args, force_calibre_style=False, override_program_name=None, headless=False, color_prefs=gprefs):
self.file_event_hook = None
if override_program_name:
args = [override_program_name] + args[1:]
@ -916,6 +915,13 @@ class Application(QApplication):
self._file_open_paths = []
self._file_open_lock = RLock()
if not isosx:
# OS X uses a native color dialog that does not support custom
# colors
self.color_prefs = color_prefs
self.read_custom_colors()
self.lastWindowClosed.connect(self.save_custom_colors)
if isxp:
error_dialog(None, _('Windows XP not supported'), '<p>' + _(
'calibre versions newer than 2.0 do not run on Windows XP. This is'
@ -1002,6 +1008,29 @@ class Application(QApplication):
else:
return QApplication.event(self, e)
@dynamic_property
def current_custom_colors(self):
from PyQt5.Qt import QColorDialog, QColor
def fget(self):
return [col.getRgb() for col in
(QColorDialog.customColor(i) for i in xrange(QColorDialog.customCount()))]
def fset(self, colors):
num = min(len(colors), QColorDialog.customCount())
for i in xrange(num):
QColorDialog.setCustomColor(i, QColor(*colors[i]))
return property(fget=fget, fset=fset)
def read_custom_colors(self):
colors = self.color_prefs.get('custom_colors_for_color_dialog', None)
if colors is not None:
self.current_custom_colors = colors
def save_custom_colors(self):
# Qt 5 regression, it no longer saves custom colors
colors = self.current_custom_colors
if colors != self.color_prefs.get('custom_colors_for_color_dialog', None):
self.color_prefs.set('custom_colors_for_color_dialog', colors)
_store_app = None
class SanitizeLibraryPath(object):

View File

@ -47,13 +47,14 @@ def _run(args, notify=None):
# errors during initialization of plugins that use the polish container
# infrastructure.
importlib.import_module('calibre.customize.ui')
from calibre.gui2.tweak_book import tprefs
from calibre.gui2.tweak_book.ui import Main
parser = option_parser()
opts, args = parser.parse_args(args)
decouple('edit-book-')
override = 'calibre-edit-book' if islinux else None
app = Application(args, override_program_name=override)
app = Application(args, override_program_name=override, color_prefs=tprefs)
app.load_builtin_fonts()
app.setWindowIcon(QIcon(I('tweak.png')))
Application.setOrganizationName(ORG_NAME)

View File

@ -993,7 +993,7 @@ def main(args=sys.argv):
opts, args = parser.parse_args(args)
open_at = float(opts.open_at.replace(',', '.')) if opts.open_at else None
override = 'calibre-ebook-viewer' if islinux else None
app = Application(args, override_program_name=override)
app = Application(args, override_program_name=override, color_prefs=vprefs)
app.load_builtin_fonts()
app.setWindowIcon(QIcon(I('viewer.png')))
QApplication.setOrganizationName(ORG_NAME)