def prepareConfigDialog(self): windowTitle = str(self.applet.name()) + " Settings" self.dialog = KDialog(None) self.dialog.setWindowTitle(windowTitle) self.config_ui = SystemServicesConfig(self.dialog, self.config()) self.dialog.setMainWidget(self.config_ui) for package in self._services: self.config_ui.addItemToList(package) self.dialog.setButtons(KDialog.ButtonCodes(KDialog.ButtonCode(KDialog.Ok | KDialog.Cancel | KDialog.Apply))) self.dialog.showButton(KDialog.Apply, False) self.connect(self.dialog, SIGNAL("applyClicked()"), self, SLOT("configAccepted()")) self.connect(self.dialog, SIGNAL("okClicked()"), self, SLOT("configAccepted()")) if self.config_ui.enabledServices[0] == '': self.setConfigurationRequired(True, "Click configure to select services") self.resize(200,200) return False return True
class SystemServicesApplet(plasmascript.Applet): """ Our main applet derived from plasmascript.Applet """ def __init__(self, parent, args=None): plasmascript.Applet.__init__(self, parent) # Available services self._services = list(link.System.Service) self._services.sort() def init(self): """ Const method for initializing the applet """ # Configuration interface support comes with plasma self.setHasConfigurationInterface(True) # Aspect ratio defined in Plasma self.setAspectRatioMode(Plasma.IgnoreAspectRatio) # Theme is a const variable holds Applet Theme self.theme = Plasma.Svg(self) # It gets default plasma theme's background self.theme.setImagePath("widgets/background") # Resize current theme as applet size self.theme.resize(self.size()) self.mainWidget = None self.layout = None # Create config dialog if self.prepareConfigDialog(): self.initPlasmoid() # It listens System.Service signals and route them to handler method link.listenSignals("System.Service", self.handler) def prepareConfigDialog(self): windowTitle = str(self.applet.name()) + " Settings" self.dialog = KDialog(None) self.dialog.setWindowTitle(windowTitle) self.config_ui = SystemServicesConfig(self.dialog, self.config()) self.dialog.setMainWidget(self.config_ui) for package in self._services: self.config_ui.addItemToList(package) self.dialog.setButtons(KDialog.ButtonCodes(KDialog.ButtonCode(KDialog.Ok | KDialog.Cancel | KDialog.Apply))) self.dialog.showButton(KDialog.Apply, False) self.connect(self.dialog, SIGNAL("applyClicked()"), self, SLOT("configAccepted()")) self.connect(self.dialog, SIGNAL("okClicked()"), self, SLOT("configAccepted()")) if self.config_ui.enabledServices[0] == '': self.setConfigurationRequired(True, "Click configure to select services") self.resize(200,200) return False return True def initPlasmoid(self): # Layout if not self.layout: self.layout = QGraphicsLinearLayout(Qt.Vertical, self.applet) self.layout.setContentsMargins(0,0,0,0) self.layout.setSpacing(0) self.layout.setSizePolicy(QSizePolicy(QSizePolicy.MinimumExpanding, QSizePolicy.MinimumExpanding)) self.setLayout(self.layout) self.constraintsEvent(Plasma.SizeConstraint) if self.mainWidget: self.mainWidget.hide() self.layout.removeAt(0) del self.mainWidget self.widgets = {} self.mainWidget = WidgetStack(self.applet) self.layout.addItem(self.mainWidget) isDescEnabled = self.config().readEntry("showdesc", QVariant(True)).toBool() for package in self._services: # If service is enabled create a proper widget and add it to the plasmoid if package in self.config_ui.enabledServices: # Get service info from comar link and then create a proper widget widget = WidgetSystemServices(self.applet, package, isDescEnabled) # Add widget to mainWidget self.mainWidget.addItem(widget) # Update the size of Plasmoid self.constraintsEvent(Plasma.SizeConstraint) def constraintsEvent(self, constraints): if constraints & Plasma.SizeConstraint: self.theme.resize(self.size()) if self.mainWidget: self.applet.setMinimumWidth(270) self.applet.setMinimumHeight(60*len(self.mainWidget._widgets)) def handler(self, package, signal, args): if self.mainWidget: self.mainWidget._widgets[str(package)].updateState(args[1]) self.update() def showConfigurationInterface(self): self.dialog.show() @pyqtSignature("configAccepted()") def configAccepted(self): # Find enabled services from list _enabledServices = [] _target = self.config_ui.serviceList for row in range(_target.count()): item = _target.itemWidget(_target.item(row)) if item.isChecked(): # I don't know why it is happening but some service strings includes & :S _enabledServices.append(str(item.text()).replace('&','')) # Write them into the config file self.config_ui.config.writeEntry("services", QVariant(_enabledServices)) self.config_ui.config.writeEntry("showdesc", QVariant(self.config_ui.showDesc.isChecked())) # Update enabled services with current ones self.config_ui.enabledServices = _enabledServices # It is very important to sync config before saving ! self.config_ui.config.sync() # Emit const Signal to save config file self.emit(SIGNAL("configNeedsSaving()")) # if there is a enabled services we dont need configure button anymore ! if len(_enabledServices) > 0: # FIXME It should be fixed in plasmascript.py, # when we dont need configuration means we dont have any reason.. self.setConfigurationRequired(False, '') # and update the widget self.initPlasmoid()