def setData(self, index, value, role=Qt.EditRole, change_type=None): """Cell content change""" column = index.column() row = index.row() if change_type is not None: try: value = self.data(index, role=Qt.DisplayRole) val = from_qvariant(value, str) if change_type is bool: val = bool_false_check(val) self.df.iloc[row, column - 1] = change_type(val) except ValueError: self.df.iloc[row, column - 1] = change_type('0') else: val = from_qvariant(value, str) current_value = self.get_value(row, column-1) if isinstance(current_value, bool): val = bool_false_check(val) supported_types = (bool,) + REAL_NUMBER_TYPES + COMPLEX_NUMBER_TYPES if (isinstance(current_value, supported_types) or is_text_string(current_value)): try: self.df.iloc[row, column-1] = current_value.__class__(val) except ValueError as e: QMessageBox.critical(self.dialog, "Error", "Value error: %s" % str(e)) return False else: QMessageBox.critical(self.dialog, "Error", "The type of the cell is not a supported " "type") return False self.max_min_col_update() return True
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 textedit, (option, _default) in list(self.textedits.items()): self.set_option(option, to_text_string(textedit.toPlainText())) 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 setData(self, index, value, role=Qt.EditRole): """Qt Override.""" if index.isValid() and 0 <= index.row() < len(self.shortcuts): shortcut = self.shortcuts[index.row()] column = index.column() text = from_qvariant(value, str) if column == SEQUENCE: shortcut.key = text self.dataChanged.emit(index, index) return True return False
def setData(self, index, value, role): """Override Qt method""" row = index.row() name, state = self.row(row) if role == Qt.CheckStateRole: self.set_row(row, [name, not state]) self._parent.setCurrentIndex(index) self._parent.setFocus() self.dataChanged.emit(index, index) return True elif role == Qt.EditRole: self.set_row(row, [from_qvariant(value, to_text_string), state]) self.dataChanged.emit(index, index) return True return True
def display_to_value(value, default_value, ignore_errors=True): """Convert back to value""" from qtpy.compat import from_qvariant value = from_qvariant(value, to_text_string) try: np_dtype = get_numpy_dtype(default_value) if isinstance(default_value, bool): # We must test for boolean before NumPy data types # because `bool` class derives from `int` class try: value = bool(float(value)) except ValueError: value = value.lower() == "true" elif np_dtype is not None: if 'complex' in str(type(default_value)): value = np_dtype(complex(value)) else: value = np_dtype(value) elif is_binary_string(default_value): value = to_binary_string(value, 'utf8') elif is_text_string(default_value): value = to_text_string(value) elif isinstance(default_value, complex): value = complex(value) elif isinstance(default_value, float): value = float(value) elif isinstance(default_value, int): try: value = int(value) except ValueError: value = float(value) elif isinstance(default_value, datetime.datetime): value = datestr_to_datetime(value) elif isinstance(default_value, datetime.date): value = datestr_to_datetime(value).date() elif isinstance(default_value, datetime.timedelta): value = str_to_timedelta(value) elif ignore_errors: value = try_to_eval(value) else: value = eval(value) except (ValueError, SyntaxError): if ignore_errors: value = try_to_eval(value) else: return default_value return value
def setData(self, index, value, role=Qt.EditRole): """Cell content change""" if not index.isValid() or self.readonly: return False i = index.row() j = index.column() value = from_qvariant(value, str) dtype = self._data.dtype.name if dtype == "bool": try: val = bool(float(value)) except ValueError: val = value.lower() == "true" elif dtype.startswith("string") or dtype.startswith("bytes"): val = to_binary_string(value, 'utf8') elif dtype.startswith("unicode") or dtype.startswith("str"): val = to_text_string(value) else: if value.lower().startswith('e') or value.lower().endswith('e'): return False try: val = complex(value) if not val.imag: val = val.real except ValueError as e: QMessageBox.critical(self.dialog, "Error", "Value error: %s" % str(e)) return False try: self.test_array[0] = val # will raise an Exception eventually except OverflowError as e: print(type(e.message)) QMessageBox.critical(self.dialog, "Error", "Overflow error: %s" % e.message) return False # Add change to self.changes self.changes[(i, j)] = val self.dataChanged.emit(index, index) if not is_string(val): if val > self.vmax: self.vmax = val if val < self.vmin: self.vmin = val return True
def _save_lang(self): """ Get selected language setting and save to language configuration file. """ for combobox, (option, _default) in list(self.comboboxes.items()): if option == 'interface_language': data = combobox.itemData(combobox.currentIndex()) value = from_qvariant(data, to_text_string) break try: save_lang_conf(value) self.set_option('interface_language', value) except Exception: QMessageBox.critical( self, _("Error"), _("We're sorry but the following error occurred while trying " "to set your selected language:<br><br>" "<tt>{}</tt>").format(traceback.format_exc()), QMessageBox.Ok) return
def __init__(self, name, line, parent, preceding): if preceding is None: QTreeWidgetItem.__init__(self, parent, QTreeWidgetItem.Type) else: if preceding is not parent: # Preceding must be either the same as item's parent # or have the same parent as item while preceding.parent() is not parent: preceding = preceding.parent() if preceding is None: break if preceding is None: QTreeWidgetItem.__init__(self, parent, QTreeWidgetItem.Type) else: QTreeWidgetItem.__init__(self, parent, preceding, QTreeWidgetItem.Type) self.setText(0, name) parent_text = from_qvariant(parent.data(0, Qt.UserRole), to_text_string) set_item_user_text(self, parent_text+'/'+name) self.line = line
def delete_layout(self): """ """ names, order, active = self.names, self.order, self.order name = from_qvariant(self.table.selectionModel().currentIndex().data(), to_text_string) if name in names: index = names.index(name) # In case nothing has focus in the table if index != -1: order.remove(name) names[index] = None if name in active: active.remove(name) self.names, self.order, self.active = names, order, active self.table.model().set_data(order, active) index = self.table.model().index(0, 0) self.table.setCurrentIndex(index) self.table.setFocus() self.selection_changed(None, None) if len(order) == 0: self.button_move_up.setDisabled(True) self.button_move_down.setDisabled(True) self.button_delete.setDisabled(True)
def keybinding(attr): """Return keybinding""" ks = getattr(QKeySequence, attr) return from_qvariant(QKeySequence.keyBindings(ks)[0], str)
def setEditorData(self, editor, index): """Set editor widget's data""" text = from_qvariant(index.model().data(index, Qt.DisplayRole), str) editor.setText(text)
def load_from_conf(self): """Load settings from configuration file.""" for checkbox, (sec, option, default) in list(self.checkboxes.items()): checkbox.setChecked(self.get_option(option, default, section=sec)) checkbox.clicked[bool].connect(lambda _, opt=option, sect=sec: self .has_been_modified(sect, opt)) if checkbox.restart_required: self.restart_options[(sec, option)] = checkbox.text() for radiobutton, (sec, option, default) in list(self.radiobuttons.items()): radiobutton.setChecked( self.get_option(option, default, section=sec)) radiobutton.toggled.connect(lambda _foo, opt=option, sect=sec: self .has_been_modified(sect, opt)) if radiobutton.restart_required: self.restart_options[(sec, option)] = radiobutton.label_text for lineedit, (sec, option, default) in list(self.lineedits.items()): data = self.get_option(option, default, section=sec) if getattr(lineedit, 'content_type', None) == list: data = ', '.join(data) lineedit.setText(data) lineedit.textChanged.connect(lambda _, opt=option, sect=sec: self. has_been_modified(sect, opt)) if lineedit.restart_required: self.restart_options[(sec, option)] = lineedit.label_text for textedit, (sec, option, default) in list(self.textedits.items()): data = self.get_option(option, default, section=sec) if getattr(textedit, 'content_type', None) == list: data = ', '.join(data) elif getattr(textedit, 'content_type', None) == dict: data = to_text_string(data) textedit.setPlainText(data) textedit.textChanged.connect( lambda opt=option, sect=sec: self.has_been_modified(sect, opt)) if textedit.restart_required: self.restart_options[(sec, option)] = textedit.label_text for spinbox, (sec, option, default) in list(self.spinboxes.items()): spinbox.setValue(self.get_option(option, default, section=sec)) spinbox.valueChanged.connect(lambda _foo, opt=option, sect=sec: self.has_been_modified(sect, opt)) for combobox, (sec, option, default) in list(self.comboboxes.items()): value = self.get_option(option, default, section=sec) 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 else: if combobox.count() == 0: index = None if index: combobox.setCurrentIndex(index) combobox.currentIndexChanged.connect( lambda _foo, opt=option, sect=sec: self.has_been_modified( sect, opt)) if combobox.restart_required: self.restart_options[(sec, option)] = combobox.label_text for (fontbox, sizebox), option in list(self.fontboxes.items()): rich_font = True if "rich" in option.lower() else False font = self.get_font(rich_font) 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( self.CONF_SECTION, opt)) sizebox.valueChanged.connect( lambda _foo, opt=property: self.has_been_modified( self.CONF_SECTION, opt)) for clayout, (sec, option, default) in list(self.coloredits.items()): property = to_qvariant(option) edit = clayout.lineedit btn = clayout.colorbtn edit.setText(self.get_option(option, default, section=sec)) # QAbstractButton works differently for PySide and PyQt if not API == 'pyside': btn.clicked.connect(lambda _foo, opt=option, sect=sec: self. has_been_modified(sect, opt)) else: btn.clicked.connect(lambda opt=option, sect=sec: self. has_been_modified(sect, opt)) edit.textChanged.connect(lambda _foo, opt=option, sect=sec: self. has_been_modified(sect, opt)) for (clayout, cb_bold, cb_italic), (sec, option, default) in list(self.scedits.items()): edit = clayout.lineedit btn = clayout.colorbtn options = self.get_option(option, default, section=sec) if options: color, bold, italic = options edit.setText(color) cb_bold.setChecked(bold) cb_italic.setChecked(italic) edit.textChanged.connect(lambda _foo, opt=option, sect=sec: self. has_been_modified(sect, opt)) btn.clicked[bool].connect(lambda _foo, opt=option, sect=sec: self. has_been_modified(sect, opt)) cb_bold.clicked[bool].connect(lambda _foo, opt=option, sect=sec: self.has_been_modified(sect, opt)) cb_italic.clicked[bool].connect(lambda _foo, opt=option, sect=sec: self.has_been_modified(sect, opt))
def save_to_conf(self): """Save settings to configuration file""" for checkbox, (sec, option, _default) in list(self.checkboxes.items()): if (sec, option) in self.changed_options: value = checkbox.isChecked() self.set_option(option, value, section=sec, recursive_notification=False) for radiobutton, (sec, option, _default) in list(self.radiobuttons.items()): if (sec, option) in self.changed_options: self.set_option(option, radiobutton.isChecked(), section=sec, recursive_notification=False) for lineedit, (sec, option, _default) in list(self.lineedits.items()): if (sec, option) in self.changed_options: data = lineedit.text() content_type = getattr(lineedit, 'content_type', None) if content_type == list: data = [item.strip() for item in data.split(',')] else: data = to_text_string(data) self.set_option(option, data, section=sec, recursive_notification=False) for textedit, (sec, option, _default) in list(self.textedits.items()): if (sec, option) in self.changed_options: data = textedit.toPlainText() content_type = getattr(textedit, 'content_type', None) if content_type == dict: if data: data = ast.literal_eval(data) else: data = textedit.content_type() elif content_type in (tuple, list): data = [item.strip() for item in data.split(',')] else: data = to_text_string(data) self.set_option(option, data, section=sec, recursive_notification=False) for spinbox, (sec, option, _default) in list(self.spinboxes.items()): if (sec, option) in self.changed_options: self.set_option(option, spinbox.value(), section=sec, recursive_notification=False) for combobox, (sec, option, _default) in list(self.comboboxes.items()): if (sec, option) in self.changed_options: data = combobox.itemData(combobox.currentIndex()) self.set_option(option, from_qvariant(data, to_text_string), section=sec, recursive_notification=False) for (fontbox, sizebox), option in list(self.fontboxes.items()): if (self.CONF_SECTION, option) in self.changed_options: font = fontbox.currentFont() font.setPointSize(sizebox.value()) self.set_font(font, option) for clayout, (sec, option, _default) in list(self.coloredits.items()): if (sec, option) in self.changed_options: self.set_option(option, to_text_string(clayout.lineedit.text()), section=sec, recursive_notification=False) for (clayout, cb_bold, cb_italic), (sec, option, _default) in list(self.scedits.items()): if (sec, option) in self.changed_options: color = to_text_string(clayout.lineedit.text()) bold = cb_bold.isChecked() italic = cb_italic.isChecked() self.set_option(option, (color, bold, italic), section=sec, recursive_notification=False)
def setEditorData(self, editor, index): """Set editor widget's data""" text = from_qvariant(index.model().data(index, Qt.DisplayRole), str) editor.setText(text)
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 textedit, (option, default) in list(self.textedits.items()): textedit.setPlainText(self.get_option(option, default)) textedit.textChanged.connect(lambda opt=option: self.has_been_modified(opt)) if textedit.restart_required: self.restart_options[option] = textedit.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 else: if combobox.count() == 0: index = None if index: 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 get_item_user_text(item): """Get QTreeWidgetItem user role string""" return from_qvariant(item.data(0, Qt.UserRole), to_text_string)
def keybinding(attr): """Return keybinding""" ks = getattr(QKeySequence, attr) return from_qvariant(QKeySequence.keyBindings(ks)[0], str)
def get_item_user_text(item): """Get QTreeWidgetItem user role string""" return from_qvariant(item.data(0, Qt.UserRole), to_text_string)