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