Source code for schrodinger.trajectory.validate_trajectory

"""
A module for ensuring that the s_chorus_trajectory_file row property points to
an existing file.
"""

import os.path
import textwrap

from schrodinger.Qt import QtWidgets
from schrodinger.trajectory import utils
from schrodinger.ui.qt import filedialog

from . import traj_dialog_ui


class _TrajectoryMissingDialog(QtWidgets.QDialog):

    TRAJ_MISSING = textwrap.dedent("""
            The entry's associated trajectory:

              %s

            is missing. If you wish to view the trajectory, you must locate
            and import this directory. Alternatively, you may clear all the trajectory
            data from the entry.
            """)

    def __init__(self, traj_path, eid, parent=None):
        """
        :param traj_path: The current value of the s_chorus_trajectory_file
            property
        :type traj_path: str

        :param eid: The entry id being checked
        :type eid: str

        :param parent: The parent of the dialog
        :type parent: `QtWidgets.QWidget`
        """

        super(_TrajectoryMissingDialog, self).__init__(parent)

        self._eid = eid
        self._traj_path = traj_path
        self.ui = traj_dialog_ui.Ui_Dialog()
        self.ui.setupUi(self)
        self.setWindowTitle("Missing Trajectory")
        self.ui.text_lbl.setText(self.TRAJ_MISSING % traj_path)
        style = self.style()
        icon = style.standardIcon(style.SP_MessageBoxWarning)
        icon_size = icon.availableSizes()[0]
        pixmap = icon.pixmap(icon_size)
        self.ui.icon_lbl.setPixmap(pixmap)
        self.ui.cancel_btn.clicked.connect(self.reject)
        self.ui.remove_traj_btn.clicked.connect(self._removeTrajectory)
        self.ui.import_btn.clicked.connect(self._importFile)

    @classmethod
    def validateTrajectoryPath(cls, traj_path, eid, parent=None):
        """
        Make sure that the s_chorus_trajectory_file property for the given entry
        points to an existing file.  If it doesn't, prompt the user about what
        to do and update the entry as requested.

        :param traj_path: The current value of the s_chorus_trajectory_file
            property
        :type traj_path: str

        :param eid: The entry id being checked
        :type eid: str

        :param parent: The parent of the dialog
        :type parent: `QtWidgets.QWidget`

        :return: True if the project row's s_chorus_trajectory_file property
            points to an existing file.  False otherwise.
        :rtype: bool
        """

        if traj_path is not None and os.path.exists(traj_path):
            return True
        else:
            self = cls(traj_path, eid, parent)
            result = self.exec()
            return bool(result)

    def _removeTrajectory(self):
        """
        Remove the s_chorus_trajectory_file property.
        """

        utils.set_trajectory_path(self._eid, "")
        self.reject()

    def _importFile(self):
        """
        Allow the user to specify valid trajectory directory, the valid trajectory
        will be copied to the specified location.
        """

        new_traj_file = filedialog.get_existing_directory(self)
        if new_traj_file is not None:
            utils.set_trajectory_path(self._eid, new_traj_file)
            self.accept()


validate_trajectory_path = _TrajectoryMissingDialog.validateTrajectoryPath