def save_to_conf(self): """Save settings to configuration file""" for checkbox, (option, _default) in list(self.checkboxes.items()): self.set_option(option, checkbox.isChecked()) for radiobutton, (option, _default) in list(self.radiobuttons.items()): self.set_option(option, radiobutton.isChecked()) for lineedit, (option, _default) in list(self.lineedits.items()): self.set_option(option, to_text_string(lineedit.text())) for spinbox, (option, _default) in list(self.spinboxes.items()): self.set_option(option, spinbox.value()) for combobox, (option, _default) in list(self.comboboxes.items()): data = combobox.itemData(combobox.currentIndex()) self.set_option(option, from_qvariant(data, to_text_string)) for (fontbox, sizebox), option in list(self.fontboxes.items()): font = fontbox.currentFont() font.setPointSize(sizebox.value()) self.set_font(font, option) for clayout, (option, _default) in list(self.coloredits.items()): self.set_option(option, to_text_string(clayout.lineedit.text())) for (clayout, cb_bold, cb_italic), (option, _default) in list(self.scedits.items()): color = to_text_string(clayout.lineedit.text()) bold = cb_bold.isChecked() italic = cb_italic.isChecked() self.set_option(option, (color, bold, italic))
def show(self, dialog): """Generic method to show a non-modal dialog and keep reference to the Qt C++ object""" for dlg in list(self.dialogs.values()): if to_text_string(dlg.windowTitle()) \ == to_text_string(dialog.windowTitle()): dlg.show() dlg.raise_() break else: dialog.show() self.dialogs[id(dialog)] = dialog dialog.accepted.connect( lambda eid=id(dialog): self.dialog_finished(eid)) dialog.rejected.connect( lambda eid=id(dialog): self.dialog_finished(eid))
def select_file(self, edit, filters=None, new=False): """Select file. :param edit: box to display the selected file. :type edit: QLineEdit :param filters: filters by filename extension :type filters: str :param new: select existing or create new file. :type new: bool """ initdir = to_text_string(edit.text()) if osp.isdir(initdir): basedir = initdir else: basedir = osp.dirname(initdir) if not osp.isdir(basedir): basedir = getcwd_or_home() if filters is None: filters = _("All files (*)") title = _("Select file") if new: filename, _selfilter = getsavefilename(self, title, basedir, filters) else: filename, _selfilter = getopenfilename(self, title, basedir, filters) if filename: edit.setText(filename) self.load_lines() # Push default object name if self.object_name is not None: path, fn = osp.split(filename) object_name = osp.splitext(fn)[0] self.object_name.edit.setText(object_name)
def translate_gettext(x): if not isinstance(x, str): x = x.encode("utf-8") y = lgettext(x) if isinstance(y, str): return y else: return to_text_string(y, "utf-8")
def select_directory(self, edit): """Select directory""" basedir = to_text_string(edit.text()) if not osp.isdir(basedir): basedir = getcwd_or_home() title = _("Select directory") directory = getexistingdirectory(self, title, basedir) if directory: edit.setText(directory)
def mimedata2url(source, extlist=None): """ Extract url list from MIME data extlist: for example ('.py', '.pyw') """ pathlist = [] if source.hasUrls(): for url in source.urls(): path = _process_mime_path(to_text_string(url.toString()), extlist) if path is not None: pathlist.append(path) elif source.hasText(): for rawpath in to_text_string(source.text()).splitlines(): path = _process_mime_path(rawpath, extlist) if path is not None: pathlist.append(path) else: raise ValueError("Unknown value") if pathlist: return pathlist
def select_file(self, edit, filters=None): """Select File""" basedir = osp.dirname(to_text_string(edit.text())) if not osp.isdir(basedir): basedir = getcwd_or_home() if filters is None: filters = _("All files (*)") title = _("Select file") filename, _selfilter = getopenfilename(self, title, basedir, filters) if filename: edit.setText(filename)
def set_root_path(self, root_path): """Set project root path.""" if self.name is None: self.name = osp.basename(root_path) self.root_path = to_text_string(root_path) config_path = self.__get_project_config_path() if osp.exists(config_path): self.load() else: if not osp.isdir(self.root_path): os.mkdir(self.root_path) self.save()
def is_valid(self): """Return True if all widget contents are valid""" for lineedit in self.lineedits: if lineedit in self.validate_data and lineedit.isEnabled(): validator, invalid_msg = self.validate_data[lineedit] text = to_text_string(lineedit.text()) if not validator(text): QMessageBox.critical( self, self.get_name(), "%s:<br><b>%s</b>" % (invalid_msg, text), QMessageBox.Ok) return False return True
def _update_defaults(self, defaults, old_version, verbose=False): """Update defaults after a change in version""" old_defaults = self._load_old_defaults(old_version) for section, options in defaults: for option in options: new_value = options[option] try: old_value = old_defaults.get(section, option) except (cp.NoSectionError, cp.NoOptionError): old_value = None if old_value is None or \ to_text_string(new_value) != old_value: self._set(section, option, new_value, verbose)
def _write(self, fp): """ Private write method for Python 2 The one from configparser fails for non-ascii Windows accounts """ if self._defaults: fp.write("[%s]\n" % cp.DEFAULTSECT) for (key, value) in self._defaults.items(): fp.write("%s = %s\n" % (key, str(value).replace('\n', '\n\t'))) fp.write("\n") for section in self._sections: fp.write("[%s]\n" % section) for (key, value) in self._sections[section].items(): if key == "__name__": continue if (value is not None) or (self._optcre == self.OPTCRE): value = to_text_string(value) key = " = ".join((key, value.replace('\n', '\n\t'))) fp.write("%s\n" % (key)) fp.write("\n")
def select_directory(self, edit): """Select directory. :param edit: box to display the selected file. :type edit: QLineEdit """ basedir = to_text_string(edit.text()) if not osp.isdir(basedir): basedir = getcwd_or_home() title = _("Select directory") directory = getexistingdirectory(self, title, basedir) if directory: edit.setText(directory) self.load_lines() # Push default object name # if hasattr(self, 'object_name'): this is not enough because # after EasyDialog init, self.object_name is a built-in method. if self.object_name is not None: path, fn = osp.split(directory) object_name = osp.splitext(fn)[0] self.object_name.edit.setText(object_name)
def load_from_conf(self): """Load settings from configuration file""" for checkbox, (option, default) in list(self.checkboxes.items()): checkbox.setChecked(self.get_option(option, default)) # QAbstractButton works differently for PySide and PyQt if not API == 'pyside': checkbox.clicked.connect( lambda _foo, opt=option: self.has_been_modified(opt)) else: checkbox.clicked.connect( lambda opt=option: self.has_been_modified(opt)) for radiobutton, (option, default) in list(self.radiobuttons.items()): radiobutton.setChecked(self.get_option(option, default)) radiobutton.toggled.connect( lambda _foo, opt=option: self.has_been_modified(opt)) if radiobutton.restart_required: self.restart_options[option] = radiobutton.label_text for lineedit, (option, default) in list(self.lineedits.items()): lineedit.setText(self.get_option(option, default)) lineedit.textChanged.connect( lambda _foo, opt=option: self.has_been_modified(opt)) if lineedit.restart_required: self.restart_options[option] = lineedit.label_text for spinbox, (option, default) in list(self.spinboxes.items()): spinbox.setValue(self.get_option(option, default)) spinbox.valueChanged.connect( lambda _foo, opt=option: self.has_been_modified(opt)) for combobox, (option, default) in list(self.comboboxes.items()): value = self.get_option(option, default) for index in range(combobox.count()): data = from_qvariant(combobox.itemData(index), to_text_string) # For PyQt API v2, it is necessary to convert `data` to # unicode in case the original type was not a string, like an # integer for example (see qtpy.compat.from_qvariant): if to_text_string(data) == to_text_string(value): break combobox.setCurrentIndex(index) combobox.currentIndexChanged.connect( lambda _foo, opt=option: self.has_been_modified(opt)) if combobox.restart_required: self.restart_options[option] = combobox.label_text for (fontbox, sizebox), option in list(self.fontboxes.items()): font = self.get_font(option) fontbox.setCurrentFont(font) sizebox.setValue(font.pointSize()) if option is None: property = 'plugin_font' else: property = option fontbox.currentIndexChanged.connect( lambda _foo, opt=property: self.has_been_modified(opt)) sizebox.valueChanged.connect( lambda _foo, opt=property: self.has_been_modified(opt)) for clayout, (option, default) in list(self.coloredits.items()): property = to_qvariant(option) edit = clayout.lineedit btn = clayout.colorbtn edit.setText(self.get_option(option, default)) # QAbstractButton works differently for PySide and PyQt if not API == 'pyside': btn.clicked.connect( lambda _foo, opt=option: self.has_been_modified(opt)) else: btn.clicked.connect( lambda opt=option: self.has_been_modified(opt)) edit.textChanged.connect( lambda _foo, opt=option: self.has_been_modified(opt)) for (clayout, cb_bold, cb_italic), (option, default) in list(self.scedits.items()): edit = clayout.lineedit btn = clayout.colorbtn color, bold, italic = self.get_option(option, default) edit.setText(color) cb_bold.setChecked(bold) cb_italic.setChecked(italic) edit.textChanged.connect( lambda _foo, opt=option: self.has_been_modified(opt)) # QAbstractButton works differently for PySide and PyQt if not API == 'pyside': btn.clicked.connect( lambda _foo, opt=option: self.has_been_modified(opt)) cb_bold.clicked.connect( lambda _foo, opt=option: self.has_been_modified(opt)) cb_italic.clicked.connect( lambda _foo, opt=option: self.has_been_modified(opt)) else: btn.clicked.connect( lambda opt=option: self.has_been_modified(opt)) cb_bold.clicked.connect( lambda opt=option: self.has_been_modified(opt)) cb_italic.clicked.connect( lambda opt=option: self.has_been_modified(opt))
def translate_dumb(x): if not isinstance(x, str): return to_text_string(x, "utf-8") return x