add delete slice button
[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 from sface.sficreate import CreateWindow, RemoveWindow
12
13 static_labels = {
14     'slice' :  [ 
15         "Sface : %s (%s)" % (version_dict()['code_tag'], version_dict()['code_url']),
16         "with (local) SFA libs : %s (%s)" % (version_core()['code_tag'],version_core()['code_url']),
17         ] ,
18     'registry': "usual port for registry: 12345",
19     'slicemgr': ["usual port for slice manager: 12347","usual port for aggregate: 12346"],
20 }        
21
22 class ConfigWidget(QWidget):
23     def __init__(self, parent):
24         QWidget.__init__(self, parent)
25         # init can be called several times for when the config dir is changed
26         self.inited=False
27         self.init ()
28
29     def store_local (self, name, value):
30         setattr (self, 'widget_'+name, value)
31     def retrieve_local (self, name):
32         return getattr (self, 'widget_'+name, None)
33
34     def init (self):
35         # if already inited we just need to set the values
36         if self.inited:
37             for (field,msg) in config.field_labels():
38                 edit = self.retrieve_local(field)
39                 if isinstance (edit,QCheckBox):
40                     if config.is_true(config.get(field)): edit.setCheckState (Qt.Checked)
41                     else: edit.setCheckState (Qt.Unchecked)
42                 else:
43                     edit.setText (config.get(field) or "")
44             return
45
46         # otherwise we need to build the whole thing up
47         glayout = QGridLayout()
48         row = 0
49         for (field,msg) in config.field_labels():
50
51             if static_labels.has_key(field):
52                 labels=static_labels[field]
53                 if not isinstance(labels,list): labels = [ labels, ]
54                 for label in labels:
55                     glayout.addWidget(QLabel(label),row,1)
56                     row+=1
57             # edit : text or checkbox
58             default=config.field_default(field)
59             if isinstance(default,bool):
60                 edit=QCheckBox(msg)
61                 self.store_local (field, edit)
62                 if config.is_true(config.get(field)):
63                     edit.setCheckState(Qt.Checked)
64             else:
65                 edit=QLineEdit(config.get(field) or "", self)
66                 self.store_local (field, edit)
67                 edit.setAttribute(Qt.WA_MacShowFocusRect, 0)
68             setattr(self,field,edit)
69
70             glayout.addWidget(QLabel(msg+":",self), row, 0)
71             glayout.addWidget(edit, row, 1)
72
73             row += 1
74
75         hlayout = QHBoxLayout()
76         def bottom_button (action,label,align):
77             button=QPushButton(label, self)
78             button.setSizePolicy(QSizePolicy.Maximum, QSizePolicy.Maximum)
79             hlayout.addWidget(button,0,align)
80             hlayout.addSpacing(10)
81             self.connect(button, SIGNAL('clicked()'), getattr(self,action))
82
83         bottom_button ('load','Load Config Dir',Qt.AlignLeft)
84         
85         # the config dir edit dialog
86         confdir=QLineEdit (config.get_dirname(),self)
87         self.store_local('config_dirname',confdir)
88         confdir.setAttribute(Qt.WA_MacShowFocusRect, 0)
89         confdir.setSizePolicy(QSizePolicy.Expanding,QSizePolicy.Maximum)
90         confdir.setStyleSheet("QLineEdit { width: 200px; }")
91         self.connect(confdir,SIGNAL ('returnPressed()'), self.load)
92         hlayout.addWidget (confdir,0,Qt.AlignLeft)
93         hlayout.addSpacing(10)
94
95         hlayout.addStretch()
96         bottom_button ('deleteSlice', 'Delete Slice', Qt.AlignRight),
97         bottom_button ('createSlice', 'Create New Slice', Qt.AlignRight),
98         bottom_button ('apply','Apply Only',Qt.AlignRight),
99         bottom_button ('save','Apply && Save',Qt.AlignRight)
100
101         layout = QVBoxLayout()
102         layout.addLayout(glayout)
103         layout.addLayout(hlayout)
104         layout.addStretch()
105         self.setLayout(layout)
106         self.inited=True
107
108     def createSlice(self):
109         dlg = CreateWindow(parent=self)
110         dlg.exec_()
111         if (dlg.sliceWasCreated):
112             self.slice.setText(dlg.getHrn())
113             self.save()
114
115     def deleteSlice(self):
116         dlg = RemoveWindow(hrn = config.getSlice(), parent=self)
117         dlg.exec_()
118
119     def apply(self):
120         for field in config.fields():
121             widget=getattr(self,field)
122             if isinstance(widget,QCheckBox):
123                 if widget.checkState() == Qt.Checked:
124                     config.set(field, True)
125                 else:
126                     config.set(field, False)
127             else:
128                 config.set(field, str(widget.text()))
129
130         self.parent().setStatus("<font color='green'>Settings loaded for current session</font>", timeout=5000)
131         config.display("after apply")
132
133         self.parent().signalAll('configurationChanged')
134
135
136     def save(self):
137         self.apply()
138         config.save_config()
139         self.parent().setStatus("<font color='green'>Configuration saved in %s !</font>"%config.config_file(), timeout=5000)
140
141     # switch to another config dir
142     def load(self):
143         # obtain new dor somehow
144
145         edit=self.retrieve_local('config_dirname')
146         newdir=str(edit.text())
147         newdir+='/'
148         print 'installing',newdir
149         config.set_dirname (newdir)
150         self.init()
151         self.parent().signalAll('configurationChanged')
152
153
154 class ConfigScreen(SfaScreen):
155     def __init__(self, parent):
156         SfaScreen.__init__(self, parent)
157         
158         widget = ConfigWidget(self)
159         self.init(widget, "Configure", "Configure the OneLab SFA crawler")
160