Commit f69ce547 authored by Damien Naudet's avatar Damien Naudet

Cleaned up and refactored misc. widgets.

parent 2d578793
......@@ -37,9 +37,10 @@ from functools import partial
from collections import namedtuple
from ...util.id01_spec import Id01DataMerger
from ..Widgets import (AcqParamsWidget,
AdjustedPushButton)
from ..widgets.AcqParamsWidget import AcqParamsWidget
from ..widgets.Containers import GroupBox
from ..widgets.Buttons import FixedSizePushButon
from silx.gui import qt as Qt
......@@ -165,7 +166,7 @@ class _ScansSelectDialog(Qt.QDialog):
unselBn.clicked.connect(self.__unselectClicked)
bnLayout.setColumnStretch(2, 1)
more_bn = AdjustedPushButton('More')
more_bn = FixedSizePushButon('More')
bnLayout.addWidget(more_bn, 0, 3, Qt.Qt.AlignRight)
bn_box = Qt.QDialogButtonBox(Qt.QDialogButtonBox.Ok |
......@@ -469,7 +470,7 @@ class MergeWidget(Qt.QDialog):
spec_file_edit = Qt.QLineEdit()
fm = spec_file_edit.fontMetrics()
spec_file_edit.setMinimumWidth(fm.width(' ' * 100))
spec_file_bn = AdjustedPushButton('...')
spec_file_bn = FixedSizePushButon('...')
layout.addWidget(lab,
spec_row, label_col,
Qt.Qt.AlignLeft)
......@@ -486,7 +487,7 @@ class MergeWidget(Qt.QDialog):
img_dir_edit = Qt.QLineEdit()
fm = img_dir_edit.fontMetrics()
img_dir_edit.setMinimumWidth(fm.width(' ' * 100))
img_dir_bn = AdjustedPushButton('...')
img_dir_bn = FixedSizePushButon('...')
layout.addWidget(lab,
img_path_row, label_col,
Qt.Qt.AlignLeft)
......@@ -515,7 +516,7 @@ class MergeWidget(Qt.QDialog):
Qt.QSizePolicy.Expanding))
# last row : apply button
parse_bn = AdjustedPushButton('Parse file')
parse_bn = FixedSizePushButon('Parse file')
layout.addWidget(parse_bn,
apply_bn_row, 0,
1, last_col - first_col,
......@@ -540,7 +541,7 @@ class MergeWidget(Qt.QDialog):
label = Qt.QLabel('<span style=" font-weight:600; color:#00916a;">'
'Matched scans</span>')
label.setTextFormat(Qt.Qt.RichText)
edit_scans_bn = AdjustedPushButton('Edit')
edit_scans_bn = FixedSizePushButon('Edit')
h_layout.addWidget(label)
h_layout.addWidget(edit_scans_bn)
scan_layout.addLayout(h_layout, 0, 0, 1, 2)
......@@ -588,7 +589,7 @@ class MergeWidget(Qt.QDialog):
h_layout = Qt.QHBoxLayout()
label = Qt.QLabel('<span style=" font-weight:600; color:#ff6600;">'
'Other scans</span>')
other_scans_bn = AdjustedPushButton('View')
other_scans_bn = FixedSizePushButon('View')
h_layout.addWidget(label)
h_layout.addWidget(other_scans_bn)
......@@ -633,7 +634,7 @@ class MergeWidget(Qt.QDialog):
params_gbx = GroupBox("Acq. Parameters")
grp_layout = Qt.QVBoxLayout(params_gbx)
acq_params_wid = AcqParamsWidget(highlight_change=False)
acq_params_wid = AcqParamsWidget()
self.layout().addWidget(params_gbx,
2, 0,
Qt.Qt.AlignLeft | Qt.Qt.AlignTop)
......@@ -659,7 +660,7 @@ class MergeWidget(Qt.QDialog):
master_edit.setMinimumWidth(fm.width(' ' * 50))
h_layout = Qt.QHBoxLayout()
layout.addLayout(h_layout, 0, 1, Qt.Qt.AlignLeft)
reset_bn = AdjustedPushButton('R')
reset_bn = FixedSizePushButon('R')
layout.addWidget(lab,
0, 0,
Qt.Qt.AlignLeft)
......@@ -677,7 +678,7 @@ class MergeWidget(Qt.QDialog):
outdir_edit = Qt.QLineEdit()
fm = outdir_edit.fontMetrics()
outdir_edit.setMinimumWidth(fm.width(' ' * 100))
outdir_bn = AdjustedPushButton('...')
outdir_bn = FixedSizePushButon('...')
layout.addWidget(lab,
1, 0,
Qt.Qt.AlignLeft)
......
......@@ -35,8 +35,8 @@ from collections import namedtuple
from silx.gui import qt as Qt
from ..widgets.Containers import GroupBox
from ..Widgets import (AdjustedLineEdit,
AdjustedPushButton)
from ..widgets.Buttons import FixedSizePushButon
from ..widgets.Input import StyledLineEdit
from ...process.qspace import RecipSpaceConverter
_ETA_LOWER = u'\u03B7'
......@@ -58,13 +58,13 @@ class ConversionParamsWidget(Qt.QWidget):
h_layout = Qt.QHBoxLayout()
layout.addLayout(h_layout, row, 1,
alignment=Qt.Qt.AlignLeft | Qt.Qt.AlignTop)
imgbin_h_edit = AdjustedLineEdit(5)
imgbin_h_edit = StyledLineEdit(nChar=5)
imgbin_h_edit.setValidator(Qt.QIntValidator(imgbin_h_edit))
imgbin_h_edit.setAlignment(Qt.Qt.AlignRight)
imgbin_h_edit.setText(str(_DEFAULT_IMG_BIN[0]))
h_layout.addWidget(imgbin_h_edit, alignment=Qt.Qt.AlignLeft)
h_layout.addWidget(Qt.QLabel(' x '))
imgbin_v_edit = AdjustedLineEdit(5)
imgbin_v_edit = StyledLineEdit(nChar=5)
imgbin_v_edit.setValidator(Qt.QIntValidator(imgbin_v_edit))
imgbin_v_edit.setAlignment(Qt.Qt.AlignRight)
imgbin_v_edit.setText(str(_DEFAULT_IMG_BIN[1]))
......@@ -81,17 +81,17 @@ class ConversionParamsWidget(Qt.QWidget):
h_layout = Qt.QHBoxLayout()
layout.addLayout(h_layout, row, 1,
alignment=Qt.Qt.AlignLeft | Qt.Qt.AlignTop)
qsize_x_edit = AdjustedLineEdit(5)
qsize_x_edit = StyledLineEdit(nChar=5)
qsize_x_edit.setValidator(Qt.QDoubleValidator(qsize_x_edit))
qsize_x_edit.setAlignment(Qt.Qt.AlignRight)
h_layout.addWidget(qsize_x_edit)
h_layout.addWidget(Qt.QLabel(' x '))
qsize_y_edit = AdjustedLineEdit(5)
qsize_y_edit = StyledLineEdit(nChar=5)
qsize_y_edit.setValidator(Qt.QDoubleValidator(qsize_y_edit))
qsize_y_edit.setAlignment(Qt.Qt.AlignRight)
h_layout.addWidget(qsize_y_edit)
h_layout.addWidget(Qt.QLabel(' x '))
qsize_z_edit = AdjustedLineEdit(5)
qsize_z_edit = StyledLineEdit(nChar=5)
qsize_z_edit.setValidator(Qt.QDoubleValidator(qsize_z_edit))
qsize_z_edit.setAlignment(Qt.Qt.AlignRight)
h_layout.addWidget(qsize_z_edit)
......@@ -199,7 +199,7 @@ class RecipSpaceWidget(Qt.QDialog):
h5_file_edit = Qt.QLineEdit()
fm = h5_file_edit.fontMetrics()
h5_file_edit.setMinimumWidth(fm.width(' ' * 100))
h5_file_bn = AdjustedPushButton('...')
h5_file_bn = FixedSizePushButon('...')
layout.addWidget(lab,
stretch=0,
alignment=Qt.Qt.AlignLeft)
......@@ -224,10 +224,10 @@ class RecipSpaceWidget(Qt.QDialog):
line = 0
label = Qt.QLabel('# Roi :')
xMinText = AdjustedLineEdit(width=5, read_only=True)
xMaxText = AdjustedLineEdit(width=5, read_only=True)
yMinText = AdjustedLineEdit(width=5, read_only=True)
yMaxText = AdjustedLineEdit(width=5, read_only=True)
xMinText = StyledLineEdit(nChar=5, readOnly=True)
xMaxText = StyledLineEdit(nChar=5, readOnly=True)
yMinText = StyledLineEdit(nChar=5, readOnly=True)
yMaxText = StyledLineEdit(nChar=5, readOnly=True)
roi_layout = Qt.QHBoxLayout()
roi_layout.addWidget(xMinText)
roi_layout.addWidget(xMaxText)
......@@ -238,7 +238,7 @@ class RecipSpaceWidget(Qt.QDialog):
line += 1
label = Qt.QLabel('# points :')
n_img_label = AdjustedLineEdit(width=16, read_only=True)
n_img_label = StyledLineEdit(nChar=16, readOnly=True)
nImgLayout = Qt.QHBoxLayout()
info_layout.addWidget(label, line, 0)
info_layout.addLayout(nImgLayout, line, 1, alignment=Qt.Qt.AlignLeft)
......@@ -247,7 +247,7 @@ class RecipSpaceWidget(Qt.QDialog):
line += 1
label = Qt.QLabel(u'# {0} :'.format(_ETA_LOWER))
n_angles_label = AdjustedLineEdit(5, read_only=True)
n_angles_label = StyledLineEdit(nChar=5, readOnly=True)
info_layout.addWidget(label, line, 0)
info_layout.addWidget(n_angles_label, line, 1,
alignment=Qt.Qt.AlignLeft)
......@@ -279,7 +279,7 @@ class RecipSpaceWidget(Qt.QDialog):
output_file_edit = Qt.QLineEdit()
fm = output_file_edit.fontMetrics()
output_file_edit.setMinimumWidth(fm.width(' ' * 100))
output_file_bn = AdjustedPushButton('...')
output_file_bn = FixedSizePushButon('...')
layout.addWidget(lab,
stretch=0,
alignment=Qt.Qt.AlignLeft)
......
from functools import partial
# coding: utf-8
# /*##########################################################################
#
# Copyright (c) 2015-2016 European Synchrotron Radiation Facility
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
# ###########################################################################*/
from __future__ import absolute_import
__authors__ = ["D. Naudet"]
__license__ = "MIT"
__date__ = "15/09/2016"
from silx.gui import qt as Qt
from .Input import StyledLineEdit
_MU_LOWER = u'\u03BC'
_PHI_LOWER = u'\u03C6'
_ETA_LOWER = u'\u03B7'
......@@ -10,7 +42,6 @@ class AcqParamsWidget(Qt.QWidget):
def __init__(self,
read_only=False,
highlight_change=True,
**kwargs):
super(AcqParamsWidget, self).__init__(**kwargs)
layout = Qt.QGridLayout(self)
......@@ -31,13 +62,10 @@ class AcqParamsWidget(Qt.QWidget):
return super(DblValidator, self).validate(text, pos)
def dblLineEditWidget(width):
wid = AdjustedLineEdit(width,
validator_cls=DblValidator,
read_only=read_only,
reset_on_empty=True,
highlight_change=highlight_change,
field_type=float)
wid.setReadOnly(read_only)
wid = StyledLineEdit(nChar=width,
readOnly=read_only)
wid.setValidator(DblValidator())
return wid
# ===========
......@@ -190,165 +218,5 @@ class AcqParamsWidget(Qt.QWidget):
self.__chpdeg_v_edit.setText(str(chperdeg_v))
self.__chpdeg_v = chperdeg_v
class AdjustedPushButton(Qt.QPushButton):
"""
It seems that by default QPushButtons minimum width is 75.
This is a workaround.
For _AdjustedPushButton to work text has to be set at creation time.
"""
def __init__(self, text, parent=None, padding=None):
super(AdjustedPushButton, self).__init__(text, parent)
fm = self.fontMetrics()
if padding is None:
padding = 2 * fm.width('0')
width = fm.width(self.text()) + padding
self.setMaximumWidth(width)
class AdjustedLineEdit(Qt.QLineEdit):
"""
"""
def __init__(self,
width=None,
parent=None,
padding=None,
alignment=Qt.Qt.AlignRight,
validator_cls=None,
field_type=None,
read_only=False,
reset_on_empty=False,
highlight_change=False):
super(AdjustedLineEdit, self).__init__(parent)
self.__defaultText = self.text()
self.__highlightChange = highlight_change
self.__resetOnEmpty = reset_on_empty
self.__fieldType = field_type
if width is not None:
fm = self.fontMetrics()
if padding is None:
padding = 2 * fm.width('0')
text = '0' * width
width = fm.width(text) + padding
self.setMaximumWidth(width)
self.setMinimumWidth(width)
self.setAlignment(alignment)
self.setReadOnly(read_only)
if validator_cls is not None:
self.setValidator(validator_cls(self))
self.setStyleSheet('_AdjustedLineEdit[readOnly="true"][enabled="true"]'
'{ background-color: lightGray; }')
if not self.isReadOnly():
self.textChanged.connect(self.__onTextChanged)
self.textEdited.connect(self.__updateField)
self.editingFinished.connect(partial(self.__updateField,
finished=True))
self.returnPressed.connect(partial(self.__updateField,
finished=True))
def __updateField(self, text=None, finished=False):
if text is None:
value = self.text()
else:
value = text
if len(value) > 0:
pass
elif finished and self.__resetOnEmpty:
self.resetDefault(block=True)
value = self.text()
else:
pass
if len(value) == 0:
if len(self.__defaultText) == 0:
same_txt = True
else:
same_txt = False
elif len(self.__defaultText) == 0:
same_txt = False
else:
if self.__fieldType is not None:
try:
value = self.__fieldType(value)
default_value = self.__fieldType(self.__defaultText)
except:
# TODO : filter specific exception
default_value = None
else:
default_value = self.__defaultText
if value == default_value:
same_txt = True
else:
same_txt = False
if self.__highlightChange and not same_txt:
self.setStyleSheet('_AdjustedLineEdit {'
'background-color: lightblue;}')
else:
self.setStyleSheet('')
def __onEditingFinished(self):
self.__updateField(finished=True)
def __onTextEdited(self, text):
self.__updateField(text)
def __onTextChanged(self, text):
if self.isModified() is False:
self.__defaultText = text
self.__updateField()
def resetDefault(self, block=False):
if block:
self.textChanged.disconnect(self.__onTextChanged)
self.setText(self.__defaultText)
if block:
self.textChanged.connect(self.__onTextChanged)
defaultText = property(lambda self: self.__defaultText)
def event(self, ev):
# this has to be done so that the stylesheet is reapplied when the
# "enabled" property changes
# https://wiki.qt.io/Dynamic_Properties_and_Stylesheets
if ev.type() == Qt.QEvent.EnabledChange:
self.style().unpolish(self)
self.style().polish(self)
return super(AdjustedLineEdit, self).event(ev)
class AdjustedLabel(Qt.QLabel):
"""
"""
def __init__(self,
width,
padding=None,
alignment=Qt.Qt.AlignRight,
**kwargs):
super(AdjustedLabel, self).__init__(**kwargs)
self.setFrameStyle(Qt.QFrame.Panel | Qt.QFrame.Sunken)
fm = self.fontMetrics()
if padding is None:
padding = 2 * fm.width('0')
text = '0' * width
width = fm.width(text) + padding
self.setMinimumWidth(width)
self.setAlignment(alignment)
if __name__ == '__main__':
pass
\ No newline at end of file
# coding: utf-8
# /*##########################################################################
#
# Copyright (c) 2015-2016 European Synchrotron Radiation Facility
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
# ###########################################################################*/
from __future__ import absolute_import
__authors__ = ["D. Naudet"]
__license__ = "MIT"
__date__ = "15/09/2016"
from silx.gui import qt as Qt
class FixedSizePushButon(Qt.QPushButton):
"""
It seems that by default QPushButtons minimum width is 75.
This is a workaround.
"""
_emptyWidth = 4
_padding = 1
def __init__(self, *args, **kwargs):
super(FixedSizePushButon, self).__init__(*args, **kwargs)
self.__nChar = None
self._updateSize()
def setText(self, text):
"""
Reimplemented from QPushButton::setText.
Calls _updateStyleSheet
:param text:
:return:
"""
self._updateSize()
def _updateSize(self):
"""
Sets the widget's size
:return:
"""
style = Qt.QApplication.style()
border = 2 * (self._padding +
style.pixelMetric(Qt.QStyle.PM_ButtonMargin,
widget=self))
fm = self.fontMetrics()
width = fm.width(self.text()) + border
self.setFixedWidth(width)
if __name__ == '__main__':
pass
......@@ -40,10 +40,11 @@ class StyledLineEdit(Qt.QLineEdit):
_padding = 2
def __init__(self, parent=None, nChar=None):
def __init__(self, parent=None, nChar=None, readOnly=False):
super(StyledLineEdit, self).__init__(parent)
self.__nChar = nChar
self.setAlignment(Qt.Qt.AlignLeft)
self.setReadOnly(readOnly)
self._updateStyleSheet()
def setReadOnly(self, ro):
......@@ -102,6 +103,15 @@ class StyledLineEdit(Qt.QLineEdit):
""".format(width, height)
self.setStyleSheet(sheet)
def event(self, ev):
# this has to be done so that the stylesheet is reapplied when the
# "enabled" property changes
# https://wiki.qt.io/Dynamic_Properties_and_Stylesheets
if ev.type() == Qt.QEvent.EnabledChange:
self.style().unpolish(self)
self.style().polish(self)
return super(StyledLineEdit, self).event(ev)
class StyledLabel(Qt.QLabel):
"""
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment