new BackendCfg Qt dialog

This commit is contained in:
Romain Bignon 2010-06-21 22:59:24 +02:00
commit 4deb1398a8
2 changed files with 424 additions and 0 deletions

View file

@ -0,0 +1,240 @@
# -*- coding: utf-8 -*-
# Copyright(C) 2010 Romain Bignon
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 3 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
from PyQt4.QtGui import QDialog, QTableWidgetItem, QLabel, QLineEdit, QCheckBox, QMessageBox
from PyQt4.QtCore import SIGNAL, Qt
import re
from logging import warning
from weboob.tools.application.qt.backendcfg_ui import Ui_BackendCfg
class BackendCfg(QDialog):
def __init__(self, weboob, caps=None, parent=None):
QDialog.__init__(self, parent)
self.ui = Ui_BackendCfg()
self.ui.setupUi(self)
self.weboob = weboob
self.caps = caps
self.config_widgets = {}
self.weboob.modules_loader.load()
self.ui.configFrame.hide()
for name, module in self.weboob.modules_loader.modules.iteritems():
if not self.caps or module.has_caps(*self.caps):
self.ui.modulesList.addItem(name.capitalize())
self.loadBackendsList()
self.connect(self.ui.backendsList, SIGNAL('cellClicked(int, int)'), self.backendClicked)
self.connect(self.ui.modulesList, SIGNAL('itemSelectionChanged()'), self.modulesSelectionChanged)
self.connect(self.ui.proxyBox, SIGNAL('toggled(bool)'), self.proxyEditEnabled)
self.connect(self.ui.addButton, SIGNAL('clicked()'), self.addEvent)
self.connect(self.ui.removeButton, SIGNAL('clicked()'), self.removeEvent)
self.connect(self.ui.configButtonBox, SIGNAL('accepted()'), self.acceptBackend)
self.connect(self.ui.configButtonBox, SIGNAL('rejected()'), self.rejectBackend)
def loadBackendsList(self):
self.ui.backendsList.clearContents()
for instance_name, name, params in self.weboob.backends_config.iter_backends():
self.ui.backendsList.insertRow(0)
self.ui.backendsList.setItem(0, 0, QTableWidgetItem(instance_name))
self.ui.backendsList.setItem(0, 1, QTableWidgetItem(name))
def closeEvent(self, event):
event.accept()
def backendClicked(self, row, col):
bname = unicode(self.ui.backendsList.item(row, 0).text())
self.editBackend(bname)
def addEvent(self):
self.editBackend()
def removeEvent(self):
item = self.ui.backendsList.currentItem()
if not item:
return
bname = unicode(self.ui.backendsList.item(item.row(), 0).text())
reply = QMessageBox.question(self, self.tr('Remove a backend'),
unicode(self.tr("Are you sure you want to remove the backend '%s'?")) % bname,
QMessageBox.Yes|QMessageBox.No)
if reply != QMessageBox.Yes:
return
self.weboob.backends_config.remove_backend(bname)
self.loadBackendsList()
def editBackend(self, bname=None):
self.ui.configFrame.show()
if bname is not None:
mname, params = self.weboob.backends_config.get_backend(bname)
items = self.ui.modulesList.findItems(mname, Qt.MatchFixedString)
if not items:
print 'Module not found'
else:
self.ui.modulesList.setCurrentItem(items[0])
self.ui.modulesList.setEnabled(False)
self.ui.nameEdit.setText(bname)
self.ui.nameEdit.setEnabled(False)
if '_proxy' in params:
self.ui.proxyBox.setChecked(True)
self.ui.proxyEdit.setText(params.pop('_proxy'))
else:
self.ui.proxyBox.setChecked(False)
self.ui.proxyEdit.clear()
for key, value in params.iteritems():
l, widget = self.config_widgets[key]
if isinstance(widget, QLineEdit):
widget.setText(unicode(value))
elif isinstance(widget, QCheckBox):
widget.setChecked(value.lower() in ('1', 'true', 'yes', 'on'))
else:
warning('Unknown type field "%s": %s', key, widget)
else:
self.ui.nameEdit.clear()
self.ui.nameEdit.setEnabled(True)
self.ui.proxyBox.setChecked(False)
self.ui.proxyEdit.clear()
self.ui.modulesList.setEnabled(True)
self.ui.modulesList.setCurrentRow(-1)
def acceptBackend(self):
bname = unicode(self.ui.nameEdit.text())
selection = self.ui.modulesList.selectedItems()
if not selection:
QMessageBox.critical(self, self.tr('Unable to add a backend'),
self.tr('Please select a module'))
return
module = self.weboob.modules_loader.modules[unicode(selection[0].text()).lower()]
params = {}
missing = []
if not bname:
missing.append(self.tr('Name'))
if self.ui.proxyBox.isChecked():
params['_proxy'] = unicode(self.ui.proxyEdit.text())
if not params['_proxy']:
missing.append(self.tr('Proxy'))
for key, field in module.get_config().iteritems():
label, value = self.config_widgets[key]
if isinstance(value, QLineEdit):
params[key] = unicode(value.text())
elif isinstance(value, QCheckBox):
params[key] = '1' if value.isChecked() else '0'
else:
warning('Unknown type field "%s": %s', key, value)
if not params[key]:
params[key] = field.default
if not params[key]:
missing.append(field.description)
elif field.regexp and not re.match(field.regexp, params[key]):
QMessageBox.critical(self,
self.tr('Invalid value'),
unicode(self.tr('Invalid value for field "%s":\n\n%s')) % (field.description, params[key]))
return
if missing:
QMessageBox.critical(self,
self.tr('Missing fields'),
unicode(self.tr('Please set a value in this fields:\n%s')) % ('\n'.join(['- %s' % s for s in missing])))
return
self.weboob.backends_config.add_backend(bname, module.get_name(), params, edit=not self.ui.nameEdit.isEnabled())
self.ui.configFrame.hide()
self.loadBackendsList()
def rejectBackend(self):
self.ui.configFrame.hide()
def modulesSelectionChanged(self):
for key, (label, value) in self.config_widgets.iteritems():
label.hide()
value.hide()
self.ui.configLayout.removeWidget(label)
self.ui.configLayout.removeWidget(value)
self.config_widgets.clear()
self.ui.moduleInfo.clear()
selection = self.ui.modulesList.selectedItems()
if not selection:
return
module = self.weboob.modules_loader.modules[unicode(selection[0].text()).lower()]
self.ui.moduleInfo.setText(unicode(self.tr(
'<h1>Module %s</h1>'
'<b>Version</b>: %s<br />'
'<b>Maintainer</b>: %s<br />'
'<b>License</b>: %s<br />'
'<b>Description</b>: %s<br />'
'<b>Capabilities</b>: %s<br />'))
% (module.get_name().capitalize(),
module.get_version(),
module.get_maintainer().replace('&', '&amp;').replace('<', '&lt;').replace('>', '&gt;'),
module.get_license(),
module.get_description(),
', '.join([cap.__name__ for cap in module.iter_caps()])))
for key, field in module.get_config().iteritems():
label = QLabel(u'%s:' % field.description)
if isinstance(field.default, bool):
value = QCheckBox()
if field.default:
value.setChecked(True)
else:
value = QLineEdit()
if field.default is not None:
value.setText(unicode(field.default))
if field.is_masked:
value.setEchoMode(value.Password)
self.ui.configLayout.addRow(label, value)
self.config_widgets[key] = (label, value)
def proxyEditEnabled(self, state):
self.ui.proxyEdit.setEnabled(state)
if __name__ == '__main__':
from PyQt4.QtGui import QApplication
from weboob import Weboob
import sys
app = QApplication(sys.argv)
weboob = Weboob()
weboob.load_backends()
dlg = BackendCfg(weboob)
dlg.show()
app.exec_()

View file

@ -0,0 +1,184 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>BackendCfg</class>
<widget class="QDialog" name="BackendCfg">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>626</width>
<height>614</height>
</rect>
</property>
<property name="windowTitle">
<string>Backends configuration</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_3">
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QTableWidget" name="backendsList">
<property name="selectionBehavior">
<enum>QAbstractItemView::SelectRows</enum>
</property>
<property name="horizontalScrollMode">
<enum>QAbstractItemView::ScrollPerPixel</enum>
</property>
<property name="showGrid">
<bool>false</bool>
</property>
<property name="cornerButtonEnabled">
<bool>false</bool>
</property>
<attribute name="verticalHeaderVisible">
<bool>false</bool>
</attribute>
<attribute name="verticalHeaderVisible">
<bool>false</bool>
</attribute>
<column>
<property name="text">
<string>Name</string>
</property>
</column>
<column>
<property name="text">
<string>Module</string>
</property>
</column>
</widget>
</item>
<item>
<widget class="QFrame" name="frame">
<property name="frameShape">
<enum>QFrame::StyledPanel</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Raised</enum>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QPushButton" name="addButton">
<property name="text">
<string>Add</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="removeButton">
<property name="text">
<string>Remove</string>
</property>
</widget>
</item>
<item>
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
</item>
</layout>
</item>
<item>
<widget class="QFrame" name="configFrame">
<property name="frameShape">
<enum>QFrame::StyledPanel</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Raised</enum>
</property>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QListWidget" name="modulesList"/>
</item>
<item>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<layout class="QFormLayout" name="configLayout">
<item row="0" column="0">
<widget class="QLabel" name="label">
<property name="text">
<string>Name:</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QLineEdit" name="nameEdit"/>
</item>
<item row="1" column="0">
<widget class="QCheckBox" name="proxyBox">
<property name="text">
<string>Proxy:</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QLineEdit" name="proxyEdit">
<property name="enabled">
<bool>false</bool>
</property>
</widget>
</item>
</layout>
</item>
<item>
<widget class="QDialogButtonBox" name="configButtonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
</property>
</widget>
</item>
<item>
<widget class="QTextEdit" name="moduleInfo">
<property name="readOnly">
<bool>true</bool>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QDialogButtonBox" name="buttonBox">
<property name="standardButtons">
<set>QDialogButtonBox::Close</set>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections>
<connection>
<sender>buttonBox</sender>
<signal>clicked(QAbstractButton*)</signal>
<receiver>BackendCfg</receiver>
<slot>accept()</slot>
<hints>
<hint type="sourcelabel">
<x>312</x>
<y>591</y>
</hint>
<hint type="destinationlabel">
<x>312</x>
<y>306</y>
</hint>
</hints>
</connection>
</connections>
</ui>