Source code for schrodinger.application.matsci.qualityslider
"""
A QSlider that allows the user to select Jaguar keywords based on
Coarse/Medium/Accurate settings. The keywords are displayed to the user in a
QLineEdit that by default is editable by the user. The main widget is a QFrame
object that has a .slider property that is the QSlider and a .line_edit property
that is the QLineEdit.
Alternatively, the Frame can contain radiobuttons instead of a QSlider
Copyright Schrodinger, LLC. All rights reserved.
"""
from schrodinger.application.matsci import jagwidgets
from schrodinger.Qt import QtCore
from schrodinger.Qt import QtWidgets
from schrodinger.ui.qt import swidgets
Qt = QtCore.Qt
[docs]class QualitySlider(QtWidgets.QFrame):
"""
A widget that allows the user to see and edit the Jaguar keywords for three
different accuracy levels.
The QFrame contains either a QSlider or set of QRadioButtons and an
SLabeldLineEdit. The QSlider/QRadioButtons can be used to switch between
accuracy levels, and the keywords for that accuracy level are displayed in
the SLabeledLineEdit
"""
COARSE = 0
MEDIUM = 1
ACCURATE = 2
LABELS = {COARSE: 'Coarse', MEDIUM: 'Medium', ACCURATE: 'Accurate'}
[docs] def __init__(self,
parent=None,
label='Calculation quality:',
layout=None,
coarse="",
medium="",
accurate="",
radiobuttons=False):
"""
Create a QualitySlider instance
:type parent: QWidget
:param parent: The parent of this QFrame
:type label: str
:param label: The label shown in the frame
:type layout: QBoxLayout
:param layout: The layout to add this QFrame to
:type coarse: str
:param coarse: The keyword string for coarse calculations
:type medium: str
:param medium: The keyword string for medium calculations
:type accurate: str
:param accurate: The keyword string for accurate calculations
:type radiobuttons: True if the QFrame should contain 3 radiobuttons
rather than a QSlider to pick the accuracy level
"""
QtWidgets.QFrame.__init__(self, parent)
self.layout = swidgets.SVBoxLayout()
layout.addLayout(self.layout)
if radiobuttons:
# Set up Radiobuttons
labels = [self.LABELS[x] for x in range(3)]
blayout = swidgets.SHBoxLayout()
self.layout.addLayout(blayout)
self.label = swidgets.SLabel(label, layout=blayout)
self.button_group = swidgets.SRadioButtonGroup(
labels=labels,
layout=blayout,
nocall=True,
command=self.buttonChanged)
blayout.addStretch()
self.slider = None
self.label = None
else:
self.button_group = None
# Set up the label
label_layout = swidgets.SHBoxLayout()
label_layout.addStretch()
self.label = swidgets.SLabel(label, layout=label_layout)
label_layout.addStretch()
self.layout.addLayout(label_layout)
# Set up the QSlider
self.slider = QtWidgets.QSlider(Qt.Horizontal, self)
self.slider.setRange(self.COARSE, self.ACCURATE)
self.slider.setTracking(True)
self.slider.setTickPosition(self.slider.NoTicks)
self.layout.addWidget(self.slider)
self.label_prefix = label
# Set up the options dialog
button_layout = swidgets.SHBoxLayout(indent=True, layout=self.layout)
self.jaguar_options_dlg = jagwidgets.JaguarOptionsDialog(
parent,
show_spin_treatment=True,
layout=button_layout,
default_keywords=coarse # Needed to disable symmetry checkbox
)
button_layout.addStretch()
self.keywords = {}
self.keywords[self.COARSE] = coarse
self.keywords[self.MEDIUM] = medium
self.keywords[self.ACCURATE] = accurate
if self.slider:
self.slider.valueChanged.connect(self.valueChanged)
self.slider.setValue(self.MEDIUM)
else:
self.buttonChanged()
if layout is not None:
layout.addWidget(self)
[docs] def valueChanged(self, newvalue):
"""
Callback for when the QSlider changes value - change the keywords
displayed in the KeywordEdit
:type newvalue: int
:param newvalue: The current value of the QSlider
"""
if not self.slider:
raise RuntimeError('buttonChanged requires a QualitySlider created'
' with radiobuttons=False')
self.updateKeywordEdit(newvalue)
self.label.setText(self.label_prefix + self.LABELS[newvalue])
[docs] def updateKeywordEdit(self, setting):
"""
Change the keywords in the KeywordEdit to be those for the accuracy
level of setting
:type setting: int
:param setting: One of the class constants COARSE, MEDIUM or ACCURATE
"""
self.jaguar_options_dlg.setKeywords(self.keywords[setting])
[docs] def setKeywords(self, setting, keywords, append=False):
"""
Set the keywords for a particular accuracy level
:type setting: int
:param setting: One of the class constants COARSE, MEDIUM or ACCURATE
:type keywords: str
:param keywords: The string to use for the new keywords
:type append: bool
:param append: True if the keywords string should be added to the
current string, False (default) if it should replace the current string.
"""
if append:
self.keywords[setting].append(keywords)
else:
self.keywords[setting] = keywords
self.updateKeywordEdit(self.getCurrentSetting())
[docs] def getKeywordString(self):
"""
Return the current string in the KeywordEdit
:rtype: str
:return: The current string in the KeywordEdit
"""
return self.jaguar_options_dlg.getKeywordString()
[docs] def getKeywordDict(self):
"""
Return a dictionary whose keys are keywords and values are keyword
values for the current KeywordEdit contents.
:rtype: dict
:return: Dictionary of keyword/value pairs
:raise ValueError: if any tokens do not match the keyword=value format
"""
return self.jaguar_options_dlg.getKeywordDict()
[docs] def getCurrentSetting(self):
"""
Return the current setting of the QSlider/QRadioButtons
:rtype: int
:return: The current setting of the QSlider/QRadioButtons - one of the
class constants COARSE, MEDIUM or ACCURATE
"""
if self.slider:
return self.slider.value()
else:
return self.button_group.checkedId()
[docs] def reset(self):
"""
Reset the widget
"""
self.button_group.setTextChecked(self.LABELS[self.COARSE])
self.buttonChanged()