# Copyright (c) 2011-2014, B.I.Stepanov Institute of Physics, National Academy
# of Sciences of Belarus.
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation; either version 2 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc., 51
# Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

from PyQt4.QtCore import *

__all__ = ['MatlabDummyProcess']

# *****************************************************************************
class MatlabDummyProcess(QObject):
    """Class that mimics the interface of a 'MatlabProcess' but does not
    perform any optimization procedures.

    It will fire 'retrievalFinished' immediately after 'startRetrieval'. After
    that, 'getRetrievalOutput' will return the 'retrievalOutput' instance
    passed to the constructor."""

    # ---- Signals ------------------------------------------------------------
    retrievalFinished = pyqtSignal()
    outputAvailable = pyqtSignal(QString)
    iterationCompleted = pyqtSignal(int)

    # ---- Public methods -----------------------------------------------------
    def __init__(self, retrievalOutput):
        QObject.__init__(self)

        self.retrievalOutputs = [retrievalOutput]
        self.errorMessage = None

    def startRetrieval(self, errorMessage = None):
        """Fire 'retrievalFinished' as soon as program control returns to the
        application event loop.

        If 'errorMessage' is not 'None', 'getErrorMessage' will return that
        message afterwards; otherwise, it will return 'None'."""

        self.errorMessage = errorMessage

        QTimer.singleShot(0, self.onInitComplete)

    def cancelRetrieval(self):
        pass

    def getErrorMessage(self):
        return self.errorMessage

    def getRetrievalOutput(self, iteration = -1):
        return self.retrievalOutputs[iteration]

    def getAllRetrievalOutputs(self):
        return self.retrievalOutputs

    # ---- Private slots ------------------------------------------------------
    def onInitComplete(self):
        self.retrievalFinished.emit()
