-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathpreferences.py
More file actions
683 lines (580 loc) · 23.4 KB
/
preferences.py
File metadata and controls
683 lines (580 loc) · 23.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
"""Preferences widget for psyplot_gui
This module defines the :class:`Preferences` widget that creates an interface
to the rcParams of psyplot and psyplot_gui"""
# Disclaimer
# ----------
#
# Copyright (C) 2021 Helmholtz-Zentrum Hereon
# Copyright (C) 2020-2021 Helmholtz-Zentrum Geesthacht
# Copyright (C) 2016-2021 University of Lausanne
#
# This file is part of psyplot-gui and is released under the GNU LGPL-3.O license.
# See COPYING and COPYING.LESSER in the root of the repository for full
# licensing details.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License version 3.0 as
# published by the Free Software Foundation.
#
# 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 LGPL-3.0 license for more details.
#
# You should have received a copy of the GNU LGPL-3.0 license
# along with this program. If not, see <https://www.gnu.org/licenses/>.
import yaml
from warnings import warn
from psyplot_gui.compat.qtcompat import (
QTreeWidget, QTreeWidgetItem, Qt, QMenu, QAction, QTextEdit, QIcon,
QWidget, QVBoxLayout, QHBoxLayout, QtCore, QDialog, QScrollArea,
QDialogButtonBox, QStackedWidget, QListWidget, QListView, QSplitter,
QListWidgetItem, QPushButton, QFileDialog, with_qt5,
QAbstractItemView, QToolButton, QLabel, QtGui, asstring)
from psyplot_gui.common import get_icon
from psyplot_gui import rcParams as rcParams
from psyplot.config.rcsetup import (
psyplot_fname, RcParams, rcParams as psy_rcParams)
class ConfigPage(object):
"""An abstract base class for configuration pages"""
#: A signal that shall be emitted if the validation state changes
validChanged = QtCore.pyqtSignal(bool)
#: A signal that is emitted if changes are propsed. The signal should be
#: emitted with the instance of the page itself
propose_changes = QtCore.pyqtSignal(object)
#: The title for the config page
title = None
#: The icon of the page
icon = None
#: :class:`bool` that is True, if the changes in this ConfigPage are set
#: immediately
auto_updates = False
@property
def is_valid(self):
"""Check whether the page is valid"""
raise NotImplementedError
@property
def changed(self):
"""Check whether the preferences will change"""
raise NotImplementedError
def initialize(self):
"""Initialize the page"""
raise NotImplementedError
def apply_changes(self):
"""Apply the planned changes"""
raise NotImplementedError
class RcParamsTree(QTreeWidget):
"""A QTreeWidget that can be used to display a RcParams instance
This widget is populated by a :class:`psyplot.config.rcsetup.RcParams`
instance and displays whether the values are valid or not"""
#: A signal that shall be emitted if the validation state changes
validChanged = QtCore.pyqtSignal(bool)
#: A signal that is emitted if changes are propsed. It is either emitted
#: with the parent of this instance (if this is not None) or with the
#: instance itself
propose_changes = QtCore.pyqtSignal(object)
#: The :class:`~psyplot.config.rcsetup.RcParams` to display
rc = None
#: list of :class:`bool`. A boolean for each rcParams key that states
#: whether the proposed value is valid or not
valid = []
value_col = 2
def __init__(self, rcParams, validators, descriptions, *args, **kwargs):
"""
Parameters
----------
rcParams: dict
The dictionary that contains the rcParams
validators: dict
A mapping from the `rcParams` key to the validation function for
the corresponding value
descriptions: dict
A mapping from the `rcParams` key to it's description
See Also
--------
psyplot.config.rcsetup.RcParams
psyplot.config.rcsetup.RcParams.validate
psyplot.config.rcsetup.RcParams.descriptions
"""
super(RcParamsTree, self).__init__(*args, **kwargs)
self.rc = rcParams
self.validators = validators
self.descriptions = descriptions
self.setContextMenuPolicy(Qt.CustomContextMenu)
self.customContextMenuRequested.connect(self.open_menu)
self.setColumnCount(self.value_col + 1)
self.setHeaderLabels(['RcParams key', '', 'Value'])
@property
def is_valid(self):
"""True if all the proposed values in this tree are valid"""
return all(self.valid)
@property
def top_level_items(self):
"""An iterator over the topLevelItems in this tree"""
return map(self.topLevelItem, range(self.topLevelItemCount()))
def initialize(self):
"""Fill the items of the :attr:`rc` into the tree"""
rcParams = self.rc
descriptions = self.descriptions
self.valid = [True] * len(rcParams)
validators = self.validators
vcol = self.value_col
for i, (key, val) in enumerate(sorted(rcParams.items())):
item = QTreeWidgetItem(0)
item.setText(0, key)
item.setToolTip(0, key)
item.setIcon(1, QIcon(get_icon('valid.png')))
desc = descriptions.get(key)
if desc:
item.setText(vcol, desc)
item.setToolTip(vcol, desc)
child = QTreeWidgetItem(0)
item.addChild(child)
self.addTopLevelItem(item)
editor = QTextEdit(self)
# set maximal height of the editor to 3 rows
editor.setMaximumHeight(
4 * QtGui.QFontMetrics(editor.font()).height())
editor.setPlainText(yaml.dump(val))
self.setItemWidget(child, vcol, editor)
editor.textChanged.connect(
self.set_icon_func(i, item, validators[key]))
self.resizeColumnToContents(0)
self.resizeColumnToContents(1)
def set_icon_func(self, i, item, validator):
"""Create a function to change the icon of one topLevelItem
This method creates a function that can be called when the value of an
item changes to display it's valid state. The returned function changes
the icon of the given topLevelItem depending on
whether the proposed changes are valid or not and it modifies the
:attr:`valid` attribute accordingly
Parameters
----------
i: int
The index of the topLevelItem
item: QTreeWidgetItem
The topLevelItem
validator: func
The validation function
Returns
-------
function
The function that can be called to set the correct icon"""
def func():
editor = self.itemWidget(item.child(0), self.value_col)
s = asstring(editor.toPlainText())
try:
val = yaml.load(s, Loader=yaml.Loader)
except Exception as e:
item.setIcon(1, QIcon(get_icon('warning.png')))
item.setToolTip(1, "Could not parse yaml code: %s" % e)
self.set_valid(i, False)
return
try:
validator(val)
except Exception as e:
item.setIcon(1, QIcon(get_icon('invalid.png')))
item.setToolTip(1, "Wrong value: %s" % e)
self.set_valid(i, False)
else:
item.setIcon(1, QIcon(get_icon('valid.png')))
self.set_valid(i, True)
self.propose_changes.emit(self.parent() or self)
return func
def set_valid(self, i, b):
"""Set the validation status
If the validation status changed compared to the old one, the
:attr:`validChanged` signal is emitted
Parameters
----------
i: int
The index of the topLevelItem
b: bool
The valid state of the item
"""
old = self.is_valid
self.valid[i] = b
new = self.is_valid
if new is not old:
self.validChanged.emit(new)
def open_menu(self, position):
"""Open a menu to expand and collapse all items in the tree
Parameters
----------
position: QPosition
The position where to open the menu"""
menu = QMenu()
expand_all_action = QAction('Expand all', self)
expand_all_action.triggered.connect(self.expandAll)
menu.addAction(expand_all_action)
collapse_all_action = QAction('Collapse all', self)
collapse_all_action.triggered.connect(self.collapseAll)
menu.addAction(collapse_all_action)
menu.exec_(self.viewport().mapToGlobal(position))
def changed_rc(self, use_items=False):
"""Iterate over the changed rcParams
Parameters
----------
use_items: bool
If True, the topLevelItems are used instead of the keys
Yields
------
QTreeWidgetItem or str
The item identifier
object
The proposed value"""
def equals(item, key, val, orig):
return val != orig
for t in self._get_rc(equals):
yield t[0 if use_items else 1], t[2]
def selected_rc(self, use_items=False):
"""Iterate over the selected rcParams
Parameters
----------
use_items: bool
If True, the topLevelItems are used instead of the keys
Yields
------
QTreeWidgetItem or str
The item identifier
object
The proposed value"""
def is_selected(item, key, val, orig):
return item.isSelected()
for t in self._get_rc(is_selected):
yield t[0 if use_items else 1], t[2]
def _get_rc(self, filter_func=None):
"""Iterate over the rcParams
This function applies the given `filter_func` to check whether the
item should be included or not
Parameters
----------
filter_func: function
A function that accepts the following arguments:
item
The QTreeWidgetItem
key
The rcParams key
val
The proposed value
orig
The current value
Yields
------
QTreeWidgetItem
The corresponding topLevelItem
str
The rcParams key
object
The proposed value
object
The current value
"""
def no_check(item, key, val, orig):
return True
rc = self.rc
filter_func = filter_func or no_check
for item in self.top_level_items:
key = asstring(item.text(0))
editor = self.itemWidget(item.child(0), self.value_col)
val = yaml.load(asstring(editor.toPlainText()), Loader=yaml.Loader)
try:
val = rc.validate[key](val)
except:
pass
try:
include = filter_func(item, key, val, rc[key])
except:
warn('Could not check state for %s key' % key,
RuntimeWarning)
else:
if include:
yield (item, key, val, rc[key])
def apply_changes(self):
"""Update the :attr:`rc` with the proposed changes"""
new = dict(self.changed_rc())
if new != self.rc:
self.rc.update(new)
def select_changes(self):
"""Select all the items that changed comparing to the current rcParams
"""
for item, val in self.changed_rc(True):
item.setSelected(True)
class RcParamsWidget(ConfigPage, QWidget):
"""A configuration page for RcParams instances
This page displays the :class:`psyplot.config.rcsetup.RcParams` instance in
the :attr:`rc` attribute and let's the user modify it.
Notes
-----
After the initialization, you have to call the :meth:`initialize` method"""
#: the rcParams to use (must be implemented by subclasses)
rc = None
#: the :class:`RcParamsTree` that is used to display the rcParams
tree = None
@property
def propose_changes(self):
"""A signal that is emitted if the user changes the values in the
rcParams"""
return self.tree.propose_changes
@property
def validChanged(self):
"""A signal that is emitted if the user changes the valid state of this
page"""
return self.tree.validChanged
@property
def changed(self):
"""True if any changes are proposed by this config page"""
return bool(next(self.tree.changed_rc(), None))
@property
def is_valid(self):
"""True if all the settings are valid"""
return self.tree.is_valid
@property
def icon(self):
"""The icon of this instance in the :class:`Preferences` dialog"""
return QIcon(get_icon('rcParams.png'))
def __init__(self, *args, **kwargs):
super(RcParamsWidget, self).__init__(*args, **kwargs)
self.vbox = vbox = QVBoxLayout()
self.description = QLabel(
'<p>Modify the rcParams for your need. Changes will not be applied'
' until you click the Apply or Ok button.</p>'
'<p>Values must be entered in yaml syntax</p>', parent=self)
vbox.addWidget(self.description)
self.tree = tree = RcParamsTree(
self.rc, getattr(self.rc, 'validate', None),
getattr(self.rc, 'descriptions', None), parent=self)
tree.setSelectionMode(QAbstractItemView.MultiSelection)
vbox.addWidget(self.tree)
self.bt_select_all = QPushButton('Select All', self)
self.bt_select_changed = QPushButton('Select changes', self)
self.bt_select_none = QPushButton('Clear Selection', self)
self.bt_export = QToolButton(self)
self.bt_export.setText('Export Selection...')
self.bt_export.setToolTip('Export the selected rcParams to a file')
self.bt_export.setPopupMode(QToolButton.InstantPopup)
self.export_menu = export_menu = QMenu(self)
export_menu.addAction(self.save_settings_action())
export_menu.addAction(self.save_settings_action(True))
self.bt_export.setMenu(export_menu)
hbox = QHBoxLayout()
hbox.addWidget(self.bt_select_all)
hbox.addWidget(self.bt_select_changed)
hbox.addWidget(self.bt_select_none)
hbox.addStretch(1)
hbox.addWidget(self.bt_export)
vbox.addLayout(hbox)
self.setLayout(vbox)
self.bt_select_all.clicked.connect(self.tree.selectAll)
self.bt_select_none.clicked.connect(self.tree.clearSelection)
self.bt_select_changed.clicked.connect(self.tree.select_changes)
def save_settings_action(self, update=False, target=None):
"""Create an action to save the selected settings in the :attr:`tree`
Parameters
----------
update: bool
If True, it is expected that the file already exists and it will be
updated. Otherwise, existing files will be overwritten
"""
def func():
if update:
meth = QFileDialog.getOpenFileName
else:
meth = QFileDialog.getSaveFileName
if target is None:
fname = meth(
self, 'Select a file to %s' % (
'update' if update else 'create'),
self.default_path,
'YAML files (*.yml);;'
'All files (*)'
)
if with_qt5: # the filter is passed as well
fname = fname[0]
else:
fname = target
if not fname:
return
if update:
rc = self.rc.__class__(defaultParams=self.rc.defaultParams)
rc.load_from_file(fname)
old_keys = list(rc)
selected = dict(self.tree.selected_rc())
new_keys = list(selected)
rc.update(selected)
rc.dump(fname, include_keys=old_keys + new_keys,
exclude_keys=[])
else:
rc = self.rc.__class__(self.tree.selected_rc(),
defaultParams=self.rc.defaultParams)
rc.dump(fname, exclude_keys=[])
action = QAction('Update...' if update else 'Overwrite...', self)
action.triggered.connect(func)
return action
def initialize(self, rcParams=None, validators=None, descriptions=None):
"""Initialize the config page
Parameters
----------
rcParams: dict
The rcParams to use. If None, the :attr:`rc` attribute of this
instance is used
validators: dict
A mapping from the `rcParams` key to the corresponding validation
function for the value. If None, the
:attr:`~psyplot.config.rcsetup.RcParams.validate` attribute of the
:attr:`rc` attribute is used
descriptions: dict
A mapping from the `rcParams` key to it's description. If None, the
:attr:`~psyplot.config.rcsetup.RcParams.descriptions` attribute of
the :attr:`rc` attribute is used"""
if rcParams is not None:
self.rc = rcParams
self.tree.rc = rcParams
if validators is not None:
self.tree.validators = validators
if descriptions is not None:
self.tree.descriptions = descriptions
self.tree.initialize()
def apply_changes(self):
"""Apply the changes in the config page"""
self.tree.apply_changes()
class GuiRcParamsWidget(RcParamsWidget):
"""The config page for the :class:`psyplot_gui.config.rcsetup.rcParams`"""
rc = rcParams
title = 'GUI defaults'
default_path = psyplot_fname('PSYPLOTGUIRC', 'psyplotguirc.yml',
if_exists=False)
class PsyRcParamsWidget(RcParamsWidget):
"""The config page for the :class:`psyplot.config.rcsetup.rcParams`"""
rc = psy_rcParams
title = 'psyplot defaults'
default_path = psyplot_fname(if_exists=False)
class Prefences(QDialog):
"""Preferences dialog"""
@property
def bt_apply(self):
return self.bbox.button(QDialogButtonBox.Apply)
@property
def pages(self):
return map(self.get_page, range(self.pages_widget.count()))
def __init__(self, main=None):
super(Prefences, self).__init__(parent=main)
self.setWindowTitle('Preferences')
# Widgets
self.pages_widget = QStackedWidget()
self.contents_widget = QListWidget()
self.bt_reset = QPushButton('Reset to defaults')
self.bt_load_plugins = QPushButton('Load plugin pages')
self.bt_load_plugins.setToolTip(
'Load the rcParams for the plugins in separate pages')
self.bbox = bbox = QDialogButtonBox(
QDialogButtonBox.Ok | QDialogButtonBox.Apply |
QDialogButtonBox.Cancel)
# Widgets setup
# Destroying the C++ object right after closing the dialog box,
# otherwise it may be garbage-collected in another QThread
# (e.g. the editor's analysis thread in Spyder), thus leading to
# a segmentation fault on UNIX or an application crash on Windows
self.setAttribute(Qt.WA_DeleteOnClose)
self.setWindowTitle('Preferences')
self.contents_widget.setMovement(QListView.Static)
self.contents_widget.setSpacing(1)
self.contents_widget.setCurrentRow(0)
# Layout
hsplitter = QSplitter()
hsplitter.addWidget(self.contents_widget)
hsplitter.addWidget(self.pages_widget)
hsplitter.setStretchFactor(1, 1)
btnlayout = QHBoxLayout()
btnlayout.addWidget(self.bt_reset)
btnlayout.addWidget(self.bt_load_plugins)
btnlayout.addStretch(1)
btnlayout.addWidget(bbox)
vlayout = QVBoxLayout()
vlayout.addWidget(hsplitter)
vlayout.addLayout(btnlayout)
self.setLayout(vlayout)
# Signals and slots
if main is not None:
self.bt_reset.clicked.connect(main.reset_rcParams)
self.bt_load_plugins.clicked.connect(self.load_plugin_pages)
self.pages_widget.currentChanged.connect(self.current_page_changed)
self.contents_widget.currentRowChanged.connect(
self.pages_widget.setCurrentIndex)
bbox.accepted.connect(self.accept)
bbox.rejected.connect(self.reject)
self.bt_apply.clicked.connect(self.apply_clicked)
self.bt_apply.setEnabled(False)
def set_current_index(self, index):
"""Set current page index"""
self.contents_widget.setCurrentRow(index)
def current_page_changed(self, index):
configpage = self.get_page(index)
self.bt_apply.setVisible(not configpage.auto_updates)
self.check_changes(configpage)
def get_page(self, index=None):
"""Return page widget"""
if index is None:
widget = self.pages_widget.currentWidget()
else:
widget = self.pages_widget.widget(index)
return widget.widget()
def accept(self):
"""Reimplement Qt method"""
for configpage in self.pages:
if not configpage.is_valid:
continue
configpage.apply_changes()
QDialog.accept(self)
def apply_clicked(self):
# Apply button was clicked
configpage = self.get_page()
if configpage.is_valid:
configpage.apply_changes()
self.check_changes(configpage)
def add_page(self, widget):
"""Add a new page to the preferences dialog
Parameters
----------
widget: ConfigPage
The page to add"""
widget.validChanged.connect(self.bt_apply.setEnabled)
widget.validChanged.connect(
self.bbox.button(QDialogButtonBox.Ok).setEnabled)
scrollarea = QScrollArea(self)
scrollarea.setWidgetResizable(True)
scrollarea.setWidget(widget)
self.pages_widget.addWidget(scrollarea)
item = QListWidgetItem(self.contents_widget)
try:
item.setIcon(widget.icon)
except TypeError:
pass
item.setText(widget.title)
item.setFlags(Qt.ItemIsSelectable | Qt.ItemIsEnabled)
item.setSizeHint(QtCore.QSize(0, 25))
widget.propose_changes.connect(self.check_changes)
def check_changes(self, configpage):
"""Enable the apply button if there are changes to the settings"""
if configpage != self.get_page():
return
self.bt_apply.setEnabled(
not configpage.auto_updates and configpage.is_valid and
configpage.changed)
def load_plugin_pages(self):
"""Load the rcParams for the plugins in separate pages"""
validators = psy_rcParams.validate
descriptions = psy_rcParams.descriptions
for ep in psy_rcParams._load_plugin_entrypoints():
plugin = ep.load()
rc = getattr(plugin, 'rcParams', None)
if rc is None:
rc = RcParams()
w = RcParamsWidget(parent=self)
w.title = 'rcParams of ' + ep.module
w.default_path = PsyRcParamsWidget.default_path
w.initialize(rcParams=rc, validators=validators,
descriptions=descriptions)
# use the full rcParams after initialization
w.rc = psy_rcParams
self.add_page(w)