# -*- coding: iso-8859-1 -*-
#
#-------------------------------------------------------------------------------
#                    Code_Saturne version 1.3
#                    ------------------------
#
#
#     This file is part of the Code_Saturne User Interface, element of the
#     Code_Saturne CFD tool.
#
#     Copyright (C) 1998-2007 EDF S.A., France
#
#     contact: saturne-support@edf.fr
#
#     The Code_Saturne User Interface 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.
#
#     The Code_Saturne User Interface 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 the Code_Saturne Kernel; if not, write to the
#     Free Software Foundation, Inc.,
#     51 Franklin St, Fifth Floor,
#     Boston, MA  02110-1301  USA
#
#-------------------------------------------------------------------------------


"""
This module defines the electrical thermal flow modelling management.

This module contains the following classes and function:
- ElectricalModel
- ElectricalView
- PageText
- ElectricalTestCase
"""


#-------------------------------------------------------------------------------
# Library modules import
#-------------------------------------------------------------------------------


import sys, unittest
import Tix
from Tkconstants import *


#-------------------------------------------------------------------------------
# Application modules import
#-------------------------------------------------------------------------------


from Base.Common import *
import Base.Toolbox as Tool
import Base.Dialog as Dialog
import Base.TkPage as TkPage
from Base.XMLvariables import Variables


#-------------------------------------------------------------------------------
# Coal combustion model class
#-------------------------------------------------------------------------------


class ElectricalModel(Variables):
    """
    """
    def __init__(self, case):
        """
        Constructor.
        """
        self.case = case

        nModels         = self.case.xmlGetNode('thermophysical_models')
        self.node_turb  = nModels.xmlGetNode('turbulence',      'model')
        self.node_gas   = nModels.xmlInitNode('gas_combustion',  'model')
        self.node_coal  = nModels.xmlInitNode('pulverized_coal', 'model')
        self.node_joule = nModels.xmlInitNode('joule_effect',    'model')
        self.node_therm = nModels.xmlGetNode('thermal_scalar',  'model')

        self.electricalModel = ('off', 'joule', 'arc')


    def defaultElectricalValues(self):
        """
        Return in a dictionnary which contains default values.
        """
        default = {}
        default['model'] = "off"

        return default


    def getAllElectricalModels(self):
        """
        Return all defined electrical models in a tuple.
        """
        return self.electricalModel


    def electricalModelsList(self):
        """
        Create a tuple with the electrical models allowed
        by the analysis features.
        """
        electricalList = self.electricalModel

        if self.node_turb['model'] not in ('off','k-epsilon',
                                           'k-epsilon-PL',
                                           'Rij-epsilon',
                                           'Rij-SSG',
                                           'v2f-phi',
                                           'k-omega-SST'):
            electricalList = ('off',)

        return electricalList


    def setElectricalModel(self, model):
        """
        Update the electrical model markup from the XML document.
        """
        if model not in self.electricalModelsList():
            sys.exit(1)

        if model == 'off':
            self.node_joule['model']   = 'off'
            return
        else:
            self.node_gas['model']   = 'off'
            self.node_coal['model']  = 'off'
            self.node_joule['model'] = model
            self.node_therm['model'] = 'off'


    def getElectricalModel(self):
        """
        Return the current electrical model.
        """
        model = self.node_joule['model']
        if model not in self.electricalModelsList():
            model = self.defaultElectricalValues()['model']
            self.setElectricalModel(model)

        return model


#-------------------------------------------------------------------------------
# Main view class
#-------------------------------------------------------------------------------


class ElectricalView(TkPage.Page):
    """
    Class to open electrical models Page.
    """
    def _dependsPages(self, name):
        """
        Construction of the list of dependencies Pages.
        """
        pass


    def _tkControlVariables(self):
        """
        Tkinter variables declaration.
        """
        self.joule = StringVar()


    def _pageControl(self):
        """
        Instantiate the lagrangian modelling class.
        """
        self.model = electricalModel(self.case)


    def putElectricalModel(self, event=None):
        """
        """
        self.model.setElectricalModel(self.joule.get())


    def _createWidgets(self):
        """
        Create the Page layout.
        """
        t = PageText()
            
        lf = Tix.LabelFrame(self.myPage, bd=2,
                            label=t.JOULE, relief=FLAT)
        lf.label.config(font=fT)
        lf.pack(side=TOP, fill=X, padx=10, pady=10)



    def _initializeWidgets(self):
        """
        Extract resquested informations from XML document.
        This informations are used to initialize the widgets.
        For this page default values of all widgets are necessary
        included in the XML file.
        """
        pass


#-------------------------------------------------------------------------------
# Text and messages for this page
#-------------------------------------------------------------------------------


class PageText:
    """
    Storage of all texts and messages for this page.
    """
    def __init__(self):
        
        # 1) Texts
        #
        if Tool.GuiParam.lang == 'fr':
            self.KEYWORD           = "Mots clé Code_Saturne : "
            self.JOULE             = "Modèles électriques"
        else:
            self.KEYWORD           = "Code_Saturne key words: "
            self.JOULE             = "Electrical modelling"

        # 2) Messages
        #
        if Tool.GuiParam.lang == 'fr':
            self.MSG_JOULE    = "Sélectionner un modèle"
        else:
            self.MSG_JOULE    = "Select a model"


#-------------------------------------------------------------------------------
# Electrical model test case
#-------------------------------------------------------------------------------


class ElectricalTestCase(unittest.TestCase):
    """
    """
    def setUp(self):
        """
        This method is executed before all "check" methods.
        """
        from Base.XMLengine import Case
        self.case = Case()

    
    def tearDown(self):
        """
        This method is executed after all "check" methods.
        """
        del self.case


    def checkElectricalInstantiation(self):
        """
        Check whether the ElectricalModel class could be instantiated
        """
        model = None
        model = ElectricalModel(self.case)

        assert model != None, 'Could not instantiate ElectricalModel'


def suite():
    testSuite = unittest.makeSuite(ElectricalTestCase, "check")
    return testSuite

    
def runTest():
    runner = unittest.TextTestRunner()
    runner.run(suite())


#-------------------------------------------------------------------------------
# End
#-------------------------------------------------------------------------------


syntax highlighted by Code2HTML, v. 0.9.1