Source code for schrodinger.application.jaguar.gui.filedialog
"""
Custom file dialogs for the Jaguar GUIs
"""
from collections import OrderedDict
from schrodinger.Qt import QtWidgets
from schrodinger.Qt.QtCore import Qt
from schrodinger.ui.qt import filedialog
JAG_IN_FILTER = "Jaguar Input (*.in)"
JAG_IN_SUFFIX = "in"
[docs]class FileDialogWithExtraCombo(filedialog.FileDialog):
    """
    A Qt file dialog with an extra combo box about the filename text edit
    """
    _JAG_IN_FILTER = JAG_IN_FILTER
    _JAG_IN_SUFFIX = JAG_IN_SUFFIX
[docs]    def __init__(self, parent, caption, lbl_text, combo_items):
        """
        Create a new file dialog with the specified label text and combo box
        items.
        :param parent: The Qt parent widget
        :type parent: `PyQt5.QtWidgets.QWidget`
        :param caption: The title of the dialog
        :type caption: str
        :param lbl_text: The label text to display next to the extra combo box
        :type lbl_text: str
        :param combo_items: A list of strings to add to the combo box
        :type combo_items: tuple
        """
        filters = self._JAG_IN_FILTER
        super(FileDialogWithExtraCombo, self).__init__(parent,
                                                       caption=caption,
                                                       filter=filters)
        # Needed for customization to work, on Qt5:
        self.setOption(QtWidgets.QFileDialog.DontUseNativeDialog, True)
        self.setDefaultSuffix(self._JAG_IN_SUFFIX)
        lbl = QtWidgets.QLabel(lbl_text, self)
        combo = QtWidgets.QComboBox(self)
        combo.addItems(combo_items)
        self._combo = combo
        self._insertWidgets(lbl, combo)
    def _insertWidgets(self, lbl, combo):
        """
        Insert the given widgets above the filename text edit
        :param lbl: The label to insert
        :type lbl: `PyQt5.QtWidgets.QLabel`
        :param combo: The combo box to insert
        :type combo: `PyQt5.QtWidgets.QComboBox`
        :note: QFileDialog does not have an API to add widgets, so the Qt FAQ
            recommends directly adding widgets to the dialog's layout: http://qt-
            project.org/faq/answer/how_can_i_add_widgets_to_my_qfiledialog_instance.
            This function will break if the QFileDialog layout ever changes.
        """
        glayout = self.layout()
        for row in (3, 2):
            for col in range(3):
                if col == 2:
                    if row == 3:
                        # There's nothing in (3, 2) because (2, 2) contains a
                        # button box that takes up two rows
                        continue
                    elif row == 2:
                        # (2, 2) contains a button box that spans two rows
                        row_span = 2
                else:
                    row_span = 1
                widget = glayout.itemAtPosition(row, col).widget()
                glayout.addWidget(widget, row + 1, col, row_span, 1)
        glayout.addWidget(lbl, 2, 0)
        glayout.addWidget(combo, 2, 1, Qt.AlignLeft)
[docs]    def selectedOption(self):
        """
        Return the currently selected index for the extra combo box
        :return: The currently selected index for the extra combo box
        :rtype: int
        """
        return self._combo.currentIndex()
[docs]class ReadFileDialog(FileDialogWithExtraCombo):
    """
    An Open dialog both with an extra combo box to specify what should be read
    from the specified file.
    :cvar GEOM_AND_SETTINGS: The index of the "Geometry and settings" option, as
        returned by selectedOption().
    :vartype GEOM_AND_SETTINGS: int
    :cvar GEOM_ONLY: The index of the "Geometry only" option, as returned by
        selectedOption().
    :vartype GEOM_ONLY: int
    :cvar SETTINGS_ONLY: The index of the "Settings only" option, as returned by
        selectedOption().
    :vartype SETTINGS_ONLY: int
    """
    # This class does't implement the "Custom File Filter..." option from the
    # C++ GUI
    GEOM_AND_SETTINGS = 0
    GEOM_ONLY = 1
    SETTINGS_ONLY = 2
    OPTION_TEXT = OrderedDict([(GEOM_AND_SETTINGS, 'Geometry and settings'),
                               (GEOM_ONLY, 'Geometry only'),
                               (SETTINGS_ONLY, 'Settings only')])
[docs]    def __init__(self, parent, allowed_options=None):
        caption = "Jaguar Read"
        lbl_text = "Read as:"
        if allowed_options:
            allowed_items = [
                self.OPTION_TEXT[x]
                for x in allowed_options
                if x in self.OPTION_TEXT
            ]
        else:
            allowed_items = list(self.OPTION_TEXT.values())
        super(ReadFileDialog, self).__init__(parent, caption, lbl_text,
                                             allowed_items)
        self.setAcceptMode(self.AcceptOpen)
        self.setFileMode(QtWidgets.QFileDialog.ExistingFile)