display local SFA version for now
[sface.git] / sface / screens / configscreen.py
1
2 from PyQt4.QtCore import *
3 from PyQt4.QtGui import *
4
5 from sface.config import config
6 from sface.screens.sfascreen import SfaScreen
7
8 from sfa.util.version import version_core
9
10 static_labels = {
11     'slice' :  "local SFA version : %s" % version_core()['code_tag'],
12     'registry': "usual port for registry: 12345",
13     'slicemgr': ["usual port for slice manager: 12347","usual port for aggregate: 12346"],
14 }        
15
16 class ConfigWidget(QWidget):
17     def __init__(self, parent):
18         QWidget.__init__(self, parent)
19
20         glayout = QGridLayout()
21         row = 0
22         for (field,msg) in config.field_labels():
23             # edit : text or checkbox
24             default=config.field_default(field)
25             if static_labels.has_key(field):
26                 labels=static_labels[field]
27                 if not isinstance(labels,list): labels = [ labels, ]
28                 for label in labels:
29                     glayout.addWidget(QLabel(label),row,1)
30                     row+=1
31             if isinstance(default,bool):
32                 edit=QCheckBox(msg)
33                 if config.is_true(config.get(field)):
34                     edit.setCheckState(Qt.Checked)
35             else:
36                 edit=QLineEdit(config.get(field) or "", self)
37                 edit.setAttribute(Qt.WA_MacShowFocusRect, 0)
38             setattr(self,field,edit)
39
40             glayout.addWidget(QLabel(msg+":",self), row, 0)
41             glayout.addWidget(edit, row, 1)
42
43             row += 1
44
45         hlayout = QHBoxLayout()
46         hlayout.addStretch()
47         for (action,label) in [('apply','Apply'),
48                                ('save','Apply && Save')]:
49             button=QPushButton(label, self)
50             button.setSizePolicy(QSizePolicy.Maximum, QSizePolicy.Maximum)
51             hlayout.addWidget(button, 0, Qt.AlignRight)
52             hlayout.addSpacing(10)
53             self.connect(button, SIGNAL('clicked()'), getattr(self,action))
54
55         layout = QVBoxLayout()
56         layout.addLayout(glayout)
57         layout.addLayout(hlayout)
58         layout.addStretch()
59         self.setLayout(layout)
60
61
62     def apply(self):
63         for field in config.fields():
64             widget=getattr(self,field)
65             if isinstance(widget,QCheckBox):
66                 if widget.checkState() == Qt.Checked:
67                     config.set(field, True)
68                 else:
69                     config.set(field, False)
70             else:
71                 config.set(field, str(widget.text()))
72
73         self.parent().setStatus("<font color='green'>Settings loaded for current session</font>", timeout=5000)
74         config.display("after apply")
75
76         self.parent().signalAll('configurationChanged')
77
78
79     def save(self):
80         self.apply()
81         config.save_config()
82         self.parent().setStatus("<font color='green'>Configuration saved in %s !</font>"%config.filename(), timeout=5000)
83
84 class ConfigScreen(SfaScreen):
85     def __init__(self, parent):
86         SfaScreen.__init__(self, parent)
87         
88         widget = ConfigWidget(self)
89         self.init(widget, "Configure", "Configure the OneLab SFA crawler")
90