source: sasview/src/sas/qtgui/Perspectives/Fitting/AssociatedComboBox.py @ cf9f39e

ESS_GUIESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_openclESS_GUI_sync_sascalc
Last change on this file since cf9f39e was 04f775d, checked in by Torin Cooper-Bennun <torin.cooper-bennun@…>, 6 years ago

cherry-pick fixed-choice param support, made more generic and cleaner

  • Property mode set to 100644
File size: 1.5 KB
Line 
1from PyQt5 import QtGui, QtWidgets
2
3
4class AssociatedComboBox(QtWidgets.QComboBox):
5    """Just a regular combo box, but associated with a particular QStandardItem so that it can be used to display and
6    select an item's current text and a restricted list of other possible text.
7
8    When the combo box's current text is changed, the change is immediately reflected in the associated item; either
9    the text itself is set as the item's data, or the current index is used; see constructor."""
10    item = None  # type: QtGui.QStandardItem
11
12    def __init__(self, item, idx_as_value=False, parent=None):
13        # type: (QtGui.QStandardItem, bool, QtWidgets.QWidget) -> None
14        """
15        Initialize widget. idx_as_value indicates whether to use the combo box's current index (otherwise, current text)
16        as the associated item's new data.
17        """
18        super(AssociatedComboBox, self).__init__(parent)
19        self.item = item
20
21        if idx_as_value:
22            self.currentIndexChanged[int].connect(self._onIndexChanged)
23        else:
24            self.currentTextChanged.connect(self._onTextChanged)
25
26    def _onTextChanged(self, text):
27        # type: (str) -> None
28        """
29        Callback for combo box's currentTextChanged. Set associated item's data to the new text.
30        """
31        self.item.setText(text)
32
33    def _onIndexChanged(self, idx):
34        # type: (int) -> None
35        """
36        Callback for combo box's currentIndexChanged. Set associated item's data to the new index.
37        """
38        self.item.setText(str(idx))
Note: See TracBrowser for help on using the repository browser.