Ejemplo n.º 1
0
class ConfigDialog(QDialog):
    def __init__(self, parent=None):
        QDialog.__init__(self, parent)
        self._layout = QVBoxLayout(self)
        self._tabdialog = QTabWidget(self)
        self._layout.addWidget(self._tabdialog)
        w = _config_dialog.Ui_config_dialog()
        w.setupUi(self._tabdialog)
        self.widgets = w
        
        self._buttonbox = QDialogButtonBox(QDialogButtonBox.Save|QDialogButtonBox.Cancel)
        self._buttonbox.setParent(self)
        signal_connect(self._buttonbox, SIGNAL("accepted()"), self.accept)
        signal_connect(self._buttonbox, SIGNAL("rejected()"), self.reject)
        self._layout.addWidget(self._buttonbox)
        
        self._layout.setContentsMargins(3,3,3,3)
        
        self.font = QFont()
        self.color = QColor()
        
        self.config = yobot_interfaces.component_registry.get_component("client-config")
        
        self.account_items = {}
        
        signal_connect(w.account_add, SIGNAL("clicked()"), lambda: self.add_modify_account(add=True))
        signal_connect(w.account_edit, SIGNAL("clicked()"), lambda: self.add_modify_account(add=False))
        signal_connect(w.account_del, SIGNAL("clicked()"), self.remove_account)
        
        signal_connect(w.select_color, SIGNAL("clicked()"), lambda: self.change_formatting(color=True))
        signal_connect(w.select_font, SIGNAL("clicked()"), lambda: self.change_formatting(font=True))
        
        signal_connect(w.agent_address, SIGNAL("editingFinished()"), self.change_agent)
        self.connect_global_bool(w.html_relsize, "appearance", "use_html_relsize")
        self.connect_global_bool(w.show_joinpart, "appearance", "show_joinpart")
        self.input_validated = True
        
        self.setWindowTitle("Yobot Configuration")
        
    def connect_global_bool(self, widget, dictname, optname, default=False):
        signal_connect(widget, SIGNAL("toggled(bool)"),
                       lambda b: self.config.globals.setdefault(dictname, {}).__setitem__(optname, b))
        
    def load_settings(self):
        w = self.widgets
        if not self.config:
            log_warn("config object not available! bailing")
            return
        #for font..
        appearance = self.config.globals.setdefault("appearance", {})
        family = appearance.get("font_family", None)
        size = appearance.get("font_size", None)
        color = appearance.get("font_color", None)
        
        if family: self.font.setFamily(family)
        if size: self.font.setPointSize(size)
        if color: self.color.setNamedColor(color)
        
        bold = appearance.get("font_bold", False)
        italic = appearance.get("font_italic", False)
        underline = appearance.get("font_underline", False)
        html_relsize = appearance.get("use_html_relsize", False)
        show_joinpart = appearance.get("show_joinpart", False)
        
        self.font.setBold(bold)
        self.font.setItalic(italic)
        self.font.setUnderline(underline)
        w.html_relsize.setChecked(html_relsize)
        w.show_joinpart.setChecked(show_joinpart)
        
        self.change_formatting()
        
        #for the agent...
        agent = self.config.globals.get("agent_address", None)
        if agent: w.agent_address.setText(agent)
        self.change_agent()
        #for accounts:
        for a in self.config.accounts:
            log_warn("got account", a)
            if a.get("name", None) and a.get("improto", None):
                #get name and icon
                name, icon = getProtoIconAndName(getattr(yobotproto, a["improto"], ""))
                log_debug(icon, name)
                i = QTreeWidgetItem((a["name"], name))
                i.setIcon(1, icon)
                i.setData(0, ITEM_PLACEHOLDER_ROLE, a)
                self.account_items[i] = a
                self.widgets.accounts.addTopLevelItem(i)
                
    
    def remove_account(self):
        #get current item:
        w = self.widgets
        item = w.accounts.currentItem()
        
        #get the index (ugh.. this is tedious)
        itemindex = w.accounts.indexOfTopLevelItem(item)
        if itemindex == -1:
            log_err("couldn't get index!")
            return
        
        account = self.account_items[item]
        #remove the item from the widget:
        w.accounts.takeTopLevelItem(itemindex)
        
        #find the account in our global config list
        index = -1
        for i in xrange(0, len(self.config.accounts)):
            a = self.config.accounts[i]
            if str(a["name"]) == str(account["name"]) and str(a["improto"]) == str(account["improto"]):
                index = i
                break
            else:
                pass
        if index >= 0:
            log_debug("index:", index)
            self.config.accounts.pop(index)
        #finally, remove it from the mapping
        self.account_items.pop(item)
    
    def add_modify_account(self, add=False):
        dlg = AccountSettingsDialog(self)
        if not add:
            item = self.widgets.accounts.currentItem()
            if not item:
                return
            account = self.account_items.get(item)
            if not account:
                return
            #account = item.data(0, ITEM_PLACEHOLDER_ROLE).toPyObject()
            dlg.fill_from(account)
        else:
            item = QTreeWidgetItem()
        result = dlg.exec_()
        
        if not result == QDialog.Accepted:
            return
        new_a = dlg.values
        
        item.setText(0, new_a["name"])
        #get icon and name...
        name, icon = getProtoIconAndName(getattr(yobotproto, new_a["improto"], -1))
        item.setText(1, name)
        item.setIcon(1, icon)
        if add:
            if self.account_exists(new_a):
                print "account already exists.. not adding"
                return
            item.setData(0, ITEM_PLACEHOLDER_ROLE, new_a)
            self.widgets.accounts.addTopLevelItem(item)
            self.config.accounts.append(new_a)
        else:
            account.update(new_a)
            
    def account_exists(self, d):
        for a in self.config.accounts:
            if d["name"] == a["name"] and d["improto"] == a["improto"]:
                return True
        return False
    
    def change_formatting(self, color=False, font=False):
        if color:
            _color = QColorDialog.getColor(self.color, self, "Select Color")
            if _color.isValid():
                self.color = _color
        elif font:
            self.font, _ = QFontDialog.getFont(self.font, self, "Select Font")
            
        widgetformatter(self.widgets.sample_text, self.font, self.color,
                        klass="QPlainTextEdit")
        
        #now, change the config objects..
        fmt = self.config.globals["appearance"]
        fmt.update({
            "font_family":str(self.font.family()),
            "font_size":int(self.font.pointSize()),
            "font_color":str(self.color.name()),
            "font_bold":bool(self.font.bold()),
            "font_italic":bool(self.font.italic()),
            "font_underline":bool(self.font.underline())
        })
    
    def change_agent(self):
        #bah.. the same boring thing as always
        s = str(self.widgets.agent_address.text())
        if len(s.rsplit(":", 1)) == 2 and not str.isdigit(s.rsplit(":",1)[1]):
            self.input_validated = False
            self.widgets.agent_address.setStyleSheet("background-color:red;")
        else:
            self.widgets.agent_address.setStyleSheet("background-color:green")
            if s:
                self.config.globals["agent_address"] = str(s)
            self.input_validated = True
            
            
    
    def accept(self):
        #do some stuff first, like save
        if self.input_validated:
            self.config.save()
            QDialog.accept(self)
        else:
            QErrorMessage(self).showMessage("Bad input.. (somewhere?)")