class ServiceBrowser(QWidget): def __init__(self, parent = None): QWidget.__init__(self, parent) self.serviceManager = QServiceManager(self) self.registerExampleServices() self.initWidgets() self.reloadServicesList() self.setWindowTitle(self.tr("Services Browser")) def __del__(self): self.unregisterExampleServices() def currentInterfaceImplChanged(self, current, previous): if not current: return descriptor = current.data(Qt.UserRole) self.reloadAttributesList() self.reloadAttributesRadioButtonText() if descriptor.isValid(): self.defaultInterfaceButton.setText(self.tr("Set as default implementation for %s" % str(descriptor.interfaceName()))) self.defaultInterfaceButton.setEnabled(True) def reloadServicesList(self): self.servicesListWidget.clear() services = self.serviceManager.findServices() for serv in services: self.servicesListWidget.addItem(serv) self.servicesListWidget.addItem(self.showAllServicesItem) self._services = services def reloadInterfaceImplementationsList(self): serviceName = None allServices = self.servicesListWidget.currentItem().text() == self.showAllServicesItem.text() if self.servicesListWidget.currentItem() and not allServices: serviceName = self.servicesListWidget.currentItem().text() self.interfacesGroup.setTitle(self.tr("Interfaces implemented by %s" % str(serviceName))) else: self.interfacesGroup.setTitle(self.tr("All interface implementations")) descriptors = self.serviceManager.findInterfaces(serviceName) self.attributesListWidget.clear() self.interfacesListWidget.clear() self._i = [] for desc in descriptors: text = "%s %d.%d" % (desc.interfaceName(), desc.majorVersion(), desc.minorVersion()) if not serviceName: text += " (" + desc.serviceName() + ")" defaultInterfaceImpl = self.serviceManager.interfaceDefault(desc.interfaceName()) if desc == defaultInterfaceImpl: text += self.tr(" (default)") item = QListWidgetItem(text) item.setData(Qt.UserRole, desc) item._data = desc self.interfacesListWidget.addItem(item) self.defaultInterfaceButton.setEnabled(False) def reloadAttributesList(self): item = self.interfacesListWidget.currentItem() if not item: return selectedImpl = item.data(Qt.UserRole) implementationRef = None if self.selectedImplRadioButton.isChecked(): implementationRef = self.serviceManager.loadInterface(selectedImpl) else: implementationRef = self.serviceManager.loadInterface(selectedImpl.interfaceName()) self.attributesListWidget.clear() if not implementationRef: self.attributesListWidget.addItem(self.tr("(Error loading service plugin)")) return metaObject = implementationRef.metaObject() self.attributesGroup.setTitle(self.tr("Invokable attributes for %s class" % metaObject.className())) for i in range(metaObject.methodCount()): method = metaObject.method(i) self.attributesListWidget.addItem("[METHOD] " + method.signature()) for i in range(metaObject.propertyCount()): p = metaObject.property(i) self.attributesListWidget.addItem("[PROPERTY] " + p.name()) def setDefaultInterfaceImplementation(self): item = self.interfacesListWidget.currentItem() if not item: return descriptor = item.data(Qt.UserRole) if descriptor.isValid(): if self.serviceManager.setInterfaceDefault(descriptor): currentIndex = self.interfacesListWidget.row(item) self.reloadInterfaceImplementationsList() self.interfacesListWidget.setCurrentRow(currentIndex) else: print "Unable to set default service for interface:", descriptor.interfaceName() def registerExampleServices(self): exampleXmlFiles = ["filemanagerservice.xml", "bluetoothtransferservice.xml"] for fileName in exampleXmlFiles: path = "./xmldata/" + fileName self.serviceManager.addService(path) def unregisterExampleServices(self): self.serviceManager.removeService("FileManagerService") self.serviceManager.removeService("BluetoothTransferService") def reloadAttributesRadioButtonText(self): item = self.interfacesListWidget.currentItem() if not item: return selectedImpl = item.data(Qt.UserRole) defaultImpl = self.serviceManager.interfaceDefault(selectedImpl.interfaceName()) self.defaultImplRadioButton.setText(self.tr("Default implementation for %s\n(currently provided by %s)" % (str(defaultImpl.interfaceName()), str(defaultImpl.serviceName())))) def initWidgets(self): self.showAllServicesItem = QListWidgetItem(self.tr("(All registered services)")) self.servicesListWidget = QListWidget() self.interfacesListWidget = QListWidget() self.interfacesListWidget.addItem(self.tr("(Select a service)")) self.attributesListWidget = QListWidget() self.attributesListWidget.addItem(self.tr("(Select an interface implementation)")) self.interfacesListWidget.setMinimumWidth(450) self.servicesListWidget.currentItemChanged.connect(self.reloadInterfaceImplementationsList) self.interfacesListWidget.currentItemChanged.connect(self.currentInterfaceImplChanged) self.defaultInterfaceButton = QPushButton(self.tr("Set as default implementation")) self.defaultInterfaceButton.setEnabled(False) self.defaultInterfaceButton.clicked.connect(self.setDefaultInterfaceImplementation) self.selectedImplRadioButton = QRadioButton(self.tr("Selected interface implementation")) self.defaultImplRadioButton = QRadioButton(self.tr("Default implementation")) self.selectedImplRadioButton.setChecked(True) self.radioButtons = QButtonGroup(self) self.radioButtons.addButton(self.selectedImplRadioButton) self.radioButtons.addButton(self.defaultImplRadioButton) self.radioButtons.buttonClicked.connect(self.reloadAttributesList) self.servicesGroup = QGroupBox(self.tr("Show services for:")) servicesLayout = QVBoxLayout() servicesLayout.addWidget(self.servicesListWidget) self.servicesGroup.setLayout(servicesLayout) self.interfacesGroup = QGroupBox(self.tr("Interface implementations")) interfacesLayout = QVBoxLayout() interfacesLayout.addWidget(self.interfacesListWidget) interfacesLayout.addWidget(self.defaultInterfaceButton) self.interfacesGroup.setLayout(interfacesLayout) self.attributesGroup = QGroupBox(self.tr("Invokable attributes")) attributesLayout = QVBoxLayout() self.attributesGroup.setLayout(attributesLayout) attributesLayout.addWidget(self.attributesListWidget) attributesLayout.addWidget(QLabel(self.tr("Show attributes for:"))) attributesLayout.addWidget(self.selectedImplRadioButton) attributesLayout.addWidget(self.defaultImplRadioButton) self.attributesGroup.setLayout(attributesLayout) layout = QGridLayout() layout.addWidget(self.servicesGroup, 0, 0) layout.addWidget(self.attributesGroup, 0, 1, 2, 1) layout.addWidget(self.interfacesGroup, 1, 0) self.setLayout(layout)
class ServiceBrowser(QWidget): def __init__(self, parent=None): QWidget.__init__(self, parent) self.serviceManager = QServiceManager(self) self.registerExampleServices() self.initWidgets() self.reloadServicesList() self.setWindowTitle(self.tr("Services Browser")) def __del__(self): self.unregisterExampleServices() def currentInterfaceImplChanged(self, current, previous): if not current: return descriptor = current.data(Qt.UserRole) self.reloadAttributesList() self.reloadAttributesRadioButtonText() if descriptor.isValid(): self.defaultInterfaceButton.setText( self.tr("Set as default implementation for %s" % str(descriptor.interfaceName()))) self.defaultInterfaceButton.setEnabled(True) def reloadServicesList(self): self.servicesListWidget.clear() services = self.serviceManager.findServices() for serv in services: self.servicesListWidget.addItem(serv) self.servicesListWidget.addItem(self.showAllServicesItem) self._services = services def reloadInterfaceImplementationsList(self): serviceName = None allServices = self.servicesListWidget.currentItem().text( ) == self.showAllServicesItem.text() if self.servicesListWidget.currentItem() and not allServices: serviceName = self.servicesListWidget.currentItem().text() self.interfacesGroup.setTitle( self.tr("Interfaces implemented by %s" % str(serviceName))) else: self.interfacesGroup.setTitle( self.tr("All interface implementations")) descriptors = self.serviceManager.findInterfaces(serviceName) self.attributesListWidget.clear() self.interfacesListWidget.clear() self._i = [] for desc in descriptors: text = "%s %d.%d" % (desc.interfaceName(), desc.majorVersion(), desc.minorVersion()) if not serviceName: text += " (" + desc.serviceName() + ")" defaultInterfaceImpl = self.serviceManager.interfaceDefault( desc.interfaceName()) if desc == defaultInterfaceImpl: text += self.tr(" (default)") item = QListWidgetItem(text) item.setData(Qt.UserRole, desc) item._data = desc self.interfacesListWidget.addItem(item) self.defaultInterfaceButton.setEnabled(False) def reloadAttributesList(self): item = self.interfacesListWidget.currentItem() if not item: return selectedImpl = item.data(Qt.UserRole) implementationRef = None if self.selectedImplRadioButton.isChecked(): implementationRef = self.serviceManager.loadInterface(selectedImpl) else: implementationRef = self.serviceManager.loadInterface( selectedImpl.interfaceName()) self.attributesListWidget.clear() if not implementationRef: self.attributesListWidget.addItem( self.tr("(Error loading service plugin)")) return metaObject = implementationRef.metaObject() self.attributesGroup.setTitle( self.tr("Invokable attributes for %s class" % metaObject.className())) for i in range(metaObject.methodCount()): method = metaObject.method(i) self.attributesListWidget.addItem("[METHOD] " + method.signature()) for i in range(metaObject.propertyCount()): p = metaObject.property(i) self.attributesListWidget.addItem("[PROPERTY] " + p.name()) def setDefaultInterfaceImplementation(self): item = self.interfacesListWidget.currentItem() if not item: return descriptor = item.data(Qt.UserRole) if descriptor.isValid(): if self.serviceManager.setInterfaceDefault(descriptor): currentIndex = self.interfacesListWidget.row(item) self.reloadInterfaceImplementationsList() self.interfacesListWidget.setCurrentRow(currentIndex) else: print "Unable to set default service for interface:", descriptor.interfaceName( ) def registerExampleServices(self): exampleXmlFiles = [ "filemanagerservice.xml", "bluetoothtransferservice.xml" ] for fileName in exampleXmlFiles: path = "./xmldata/" + fileName self.serviceManager.addService(path) def unregisterExampleServices(self): self.serviceManager.removeService("FileManagerService") self.serviceManager.removeService("BluetoothTransferService") def reloadAttributesRadioButtonText(self): item = self.interfacesListWidget.currentItem() if not item: return selectedImpl = item.data(Qt.UserRole) defaultImpl = self.serviceManager.interfaceDefault( selectedImpl.interfaceName()) self.defaultImplRadioButton.setText( self.tr( "Default implementation for %s\n(currently provided by %s)" % (str(defaultImpl.interfaceName()), str(defaultImpl.serviceName())))) def initWidgets(self): self.showAllServicesItem = QListWidgetItem( self.tr("(All registered services)")) self.servicesListWidget = QListWidget() self.interfacesListWidget = QListWidget() self.interfacesListWidget.addItem(self.tr("(Select a service)")) self.attributesListWidget = QListWidget() self.attributesListWidget.addItem( self.tr("(Select an interface implementation)")) self.interfacesListWidget.setMinimumWidth(450) self.servicesListWidget.currentItemChanged.connect( self.reloadInterfaceImplementationsList) self.interfacesListWidget.currentItemChanged.connect( self.currentInterfaceImplChanged) self.defaultInterfaceButton = QPushButton( self.tr("Set as default implementation")) self.defaultInterfaceButton.setEnabled(False) self.defaultInterfaceButton.clicked.connect( self.setDefaultInterfaceImplementation) self.selectedImplRadioButton = QRadioButton( self.tr("Selected interface implementation")) self.defaultImplRadioButton = QRadioButton( self.tr("Default implementation")) self.selectedImplRadioButton.setChecked(True) self.radioButtons = QButtonGroup(self) self.radioButtons.addButton(self.selectedImplRadioButton) self.radioButtons.addButton(self.defaultImplRadioButton) self.radioButtons.buttonClicked.connect(self.reloadAttributesList) self.servicesGroup = QGroupBox(self.tr("Show services for:")) servicesLayout = QVBoxLayout() servicesLayout.addWidget(self.servicesListWidget) self.servicesGroup.setLayout(servicesLayout) self.interfacesGroup = QGroupBox(self.tr("Interface implementations")) interfacesLayout = QVBoxLayout() interfacesLayout.addWidget(self.interfacesListWidget) interfacesLayout.addWidget(self.defaultInterfaceButton) self.interfacesGroup.setLayout(interfacesLayout) self.attributesGroup = QGroupBox(self.tr("Invokable attributes")) attributesLayout = QVBoxLayout() self.attributesGroup.setLayout(attributesLayout) attributesLayout.addWidget(self.attributesListWidget) attributesLayout.addWidget(QLabel(self.tr("Show attributes for:"))) attributesLayout.addWidget(self.selectedImplRadioButton) attributesLayout.addWidget(self.defaultImplRadioButton) self.attributesGroup.setLayout(attributesLayout) layout = QGridLayout() layout.addWidget(self.servicesGroup, 0, 0) layout.addWidget(self.attributesGroup, 0, 1, 2, 1) layout.addWidget(self.interfacesGroup, 1, 0) self.setLayout(layout)
class ServiceManager(QObject): def __init__(self): QObject.__init__(self) self._services = [] self._errorMessage = "" self._emailEnabled = False self._addressEnabled = True self.manager = QServiceManager(self) self.reloadServicesList() def reloadServicesList(self): self._services = [] for service in self.manager.findServices(): self._services.append(service) self.onServicesChanged.emit() onServicesChanged = Signal() @Property("QStringList", notify=onServicesChanged) def servicesNames(self): return self._services @Slot(str, int, result="QStringList") def serviceImplementations(self, serviceName, serviceIndex): print "Interfaces implemented by ", serviceName self._implementations = [] for descriptor in self.manager.findInterfaces(serviceName): impSpec = "%s %d.%d" % (descriptor.interfaceName(), descriptor.majorVersion(), descriptor.minorVersion()) if not serviceName: impSpec += " (" + descriptor.serviceName() + ")" default = self.manager.interfaceDefault(descriptor.interfaceName()) if descriptor == default: impSpec += " (default)" self._implementations.append((impSpec, descriptor)) return [x[0] for x in self._implementations] @Slot(str, int, str, int, result="QStringList") def implementationDetails(self, implementationSpec, implementationIndex, serviceName, serviceIndex): selectedImplementation = self._implementations[implementationIndex][1] implementationRef = self.manager.loadInterface(selectedImplementation) attributes = [] if not implementationRef: return ["(Error loading service plugin)"] metaObject = implementationRef.metaObject() for i in range(metaObject.methodCount()): method = metaObject.method(i) attributes.append("[METHOD] " + method.signature()) for i in range(metaObject.propertyCount()): p = metaObject.property(i) attributes.append("[PROPERTY] " + p.name()) return attributes if attributes else ["(no attributes found)"] @Slot(int) def setDefault(self, index): descriptor = self._implementations[index][1] if descriptor.isValid(): if self.manager.setInterfaceDefault(descriptor): pass