import os
import pygtk
pygtk.require('2.0')
import gtk


class Method:
    """Class to manipulate an object(GTK) to select Method and eventualy precision"""
    
    def __init__(self):
        self.Method_frame=gtk.Frame("Method")
        self.Method_frame.set_border_width(10)
        self.Method_frame.set_size_request(225, 275)


        Method_box = gtk.VBox(False, 3)
        Method_box.set_border_width(3)
        Method_box.show()  

        self.Button_all = gtk.RadioButton(None, "All")
        self.Button_all.connect("toggled", self.private_set_method, "all")
        self.Button_all.set_active(True)
        
        Method_box.pack_start(self.Button_all, False, False, 10)
        self.Button_all.show()

        self.Button_pair = gtk.RadioButton(self.Button_all, "Pair")
        self.Button_pair.connect("toggled", self.private_set_method,"pair")
        self.Button_pair.show()
       
        Method_box.pack_start(self.Button_pair, False, False, 10)
        

        self.Button_exponential = gtk.RadioButton(self.Button_pair, "Exponential")
        self.Button_exponential.connect("toggled", self.private_set_method, "exponential")
        Method_box.pack_start(self.Button_exponential, False, False, 10)
        self.Button_exponential.show()

        self.Precision_box = gtk.HBox(False , 2)
        
        Label = gtk.Label("Precision :")
        Label.show()
        self.Precision_box.pack_start(Label, False, False, 0)
        

        self.Adj = gtk.Adjustment(1.2, 1.001, 2.2, 0.01, 0.1, 0.2)
        
        
        Scale = gtk.HScale(self.Adj)
        Scale.set_digits(3)
        Scale.show()
        self.Precision_box.pack_start(Scale, True, True, 0)
        
        Method_box.pack_start(self.Precision_box, False, False ,0)

        
        self.Method_frame.add(Method_box)
        
        self.Method_frame.show()


    def private_set_method(self,widget,method):
        if method=='exponential':
            self.Precision_box.show()
        else:
            self.Precision_box.hide()


    def set_method(self,Objet):
        """
        Execute the method set_method et set_precision of objet passed in parameter
        Warning : Deprecated!!!
        Prefere use get_method and get_precision
        """
        print "Warning Use of the Deprecated Method set_method of the class Method"
        
        if self.Button_all.get_active():
            Objet.set_method("all")
        elif self.Button_pair.get_active():
            Objet.set_method("pair")
        elif self.Button_exponential.get_active():
            Objet.set_method("exponential")
            
            Objet.set_precision(self.Adj.get_value())
        else:
            raise "Error : this method doesnt exit"


    def update_method_(self):
        if self.Button_all.get_active():
            self.method_="all"
            self.precision_= None
        elif self.Button_pair.get_active():
            self.method_="pair"
            self.precision_= None
        elif self.Button_exponential.get_active():
            self.method_="exponential"
            self.precision_=self.Adj.get_value()
        else:
            raise "Error : this method doesnt exit"

    def get_method(self):
        self.update_method_()
        return (self.method_,self.precision_)

    def get_object(self):
        return self.Method_frame


