create slice button
[sface.git] / sface / screens / configscreen.py
index 6b8e86b..37db423 100644 (file)
 
-from PyQt4.QtCore import SIGNAL, Qt
-from PyQt4.QtGui import QWidget, QLabel, QLineEdit, QPushButton, QCheckBox, QVBoxLayout, QSizePolicy
+from PyQt4.QtCore import *
+from PyQt4.QtGui import *
 
-from sface.sficonfig import config
+from sface.config import config
 from sface.screens.sfascreen import SfaScreen
 
+from sfa.util.version import version_core
+from sface.version import version_dict
+
+from sface.sficreate import CreateWindow
+
+static_labels = {
+    'slice' :  [ 
+        "Sface : %s (%s)" % (version_dict()['code_tag'], version_dict()['code_url']),
+        "with (local) SFA libs : %s (%s)" % (version_core()['code_tag'],version_core()['code_url']),
+        ] ,
+    'registry': "usual port for registry: 12345",
+    'slicemgr': ["usual port for slice manager: 12347","usual port for aggregate: 12346"],
+}        
+
 class ConfigWidget(QWidget):
     def __init__(self, parent):
         QWidget.__init__(self, parent)
+        # init can be called several times for when the config dir is changed
+        self.inited=False
+        self.init ()
 
-        layout = QVBoxLayout()
+    def store_local (self, name, value):
+        setattr (self, 'widget_'+name, value)
+    def retrieve_local (self, name):
+        return getattr (self, 'widget_'+name, None)
+
+    def init (self):
+        # if already inited we just need to set the values
+        if self.inited:
+            for (field,msg) in config.field_labels():
+                edit = self.retrieve_local(field)
+                if isinstance (edit,QCheckBox):
+                    if config.is_true(config.get(field)): edit.setCheckState (Qt.Checked)
+                    else: edit.setCheckState (Qt.Unchecked)
+                else:
+                    edit.setText (config.get(field) or "")
+            return
+
+        # otherwise we need to build the whole thing up
+        glayout = QGridLayout()
+        row = 0
         for (field,msg) in config.field_labels():
-            # label
-            layout.addWidget(QLabel(msg,self))
+
+            if static_labels.has_key(field):
+                labels=static_labels[field]
+                if not isinstance(labels,list): labels = [ labels, ]
+                for label in labels:
+                    glayout.addWidget(QLabel(label),row,1)
+                    row+=1
             # edit : text or checkbox
             default=config.field_default(field)
             if isinstance(default,bool):
                 edit=QCheckBox(msg)
-                edit.setCheckState(config.get(field))
+                self.store_local (field, edit)
+                if config.is_true(config.get(field)):
+                    edit.setCheckState(Qt.Checked)
             else:
-                edit=QLineEdit(config.get(field), self)
+                edit=QLineEdit(config.get(field) or "", self)
+                self.store_local (field, edit)
                 edit.setAttribute(Qt.WA_MacShowFocusRect, 0)
             setattr(self,field,edit)
-            layout.addWidget (edit)
 
-        for (action,label) in [('apply','Apply'),
-                               ('save','Apply & Save')]:
+            glayout.addWidget(QLabel(msg+":",self), row, 0)
+            glayout.addWidget(edit, row, 1)
+
+            row += 1
+
+        hlayout = QHBoxLayout()
+        hlayout.addStretch()
+        def conf_button (action,label):
             button=QPushButton(label, self)
             button.setSizePolicy(QSizePolicy.Maximum, QSizePolicy.Maximum)
-            layout.addWidget(button, 0, Qt.AlignRight)
-            layout.addStretch()
+            hlayout.addWidget(button)
+            hlayout.addSpacing(10)
             self.connect(button, SIGNAL('clicked()'), getattr(self,action))
+
+        conf_button ('load','Load Conf. Dir')
+        
+        # the config dir edit dialog
+        edit=QLineEdit (config.get_dirname(),self)
+        self.store_local('config_dirname',edit)
+        edit.setAttribute(Qt.WA_MacShowFocusRect, 0)
+        edit.setSizePolicy(QSizePolicy.Expanding,QSizePolicy.Maximum)
+        self.connect(edit,SIGNAL ('returnPressed()'), self.load)
+        hlayout.addWidget (edit)
+        hlayout.addSpacing(10)
+
+        conf_button ('createSlice', 'Create New Slice'),
+        conf_button ('apply','Apply Only'),
+        conf_button ('save','Apply && Save')
+
+        layout = QVBoxLayout()
+        layout.addLayout(glayout)
+        layout.addLayout(hlayout)
+        layout.addStretch()
         self.setLayout(layout)
+        self.inited=True
 
+    def createSlice(self):
+        dlg = CreateWindow(parent=self)
+        dlg.exec_()
+        if (dlg.sliceWasCreated):
+            self.slice.setText(dlg.getHrn())
+            self.save()
 
     def apply(self):
+        print 'applying'
         for field in config.fields():
             widget=getattr(self,field)
             if isinstance(widget,QCheckBox):
-                config.set(field,widget.checkState())
+                if widget.checkState() == Qt.Checked:
+                    config.set(field, True)
+                else:
+                    config.set(field, False)
             else:
-                config.set(field,str(widget.text()))
-        self.parent().setStatus("<font color='green'>Settings loaded for current session</font>",timeout=5000)
+                config.set(field, str(widget.text()))
+
+        self.parent().setStatus("<font color='green'>Settings loaded for current session</font>", timeout=5000)
         config.display("after apply")
 
+        self.parent().signalAll('configurationChanged')
+
+
     def save(self):
         self.apply()
         config.save_config()
-        self.parent().setStatus("<font color='green'>Configuration saved!</font>", timeout=3000)
-        
+        self.parent().setStatus("<font color='green'>Configuration saved in %s !</font>"%config.config_file(), timeout=5000)
+
+    # switch to another config dir
+    def load(self):
+        # obtain new dor somehow
+
+        edit=self.retrieve_local('config_dirname')
+        newdir=str(edit.text())
+        newdir+='/'
+        print 'installing',newdir
+        config.set_dirname (newdir)
+        self.init()
+        self.parent().signalAll('configurationChanged')
+
 
 class ConfigScreen(SfaScreen):
     def __init__(self, parent):
         SfaScreen.__init__(self, parent)
         
         widget = ConfigWidget(self)
-        self.init(widget, "Configure", "Configure the OneLab Federation GUI")
+        self.init(widget, "Configure", "Configure the OneLab SFA crawler")
+