Ejemplo n.º 1
0
 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))
Ejemplo n.º 2
0
 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))
Ejemplo n.º 3
0
    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)
Ejemplo n.º 4
0
 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")
Ejemplo n.º 5
0
 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)
Ejemplo n.º 6
0
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
Ejemplo n.º 7
0
 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)
Ejemplo n.º 8
0
 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()
Ejemplo n.º 9
0
 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
Ejemplo n.º 10
0
 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)
Ejemplo n.º 11
0
 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")
Ejemplo n.º 12
0
    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)
Ejemplo n.º 13
0
    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))
Ejemplo n.º 14
0
 def translate_dumb(x):
     if not isinstance(x, str):
         return to_text_string(x, "utf-8")
     return x