def headerData(self, section, orientation, role=Qt.DisplayRole): """Set header data""" if role != Qt.DisplayRole: return to_qvariant() labels = self.xlabels if orientation == Qt.Horizontal else self.ylabels if labels is None: return to_qvariant(int(section)) else: return to_qvariant(labels[section])
def headerData(self, section, orientation, role=Qt.DisplayRole): """Overriding method headerData""" if role != Qt.DisplayRole: return to_qvariant() i_column = int(section) if orientation == Qt.Horizontal: headers = (_("File"), _("Line"), _("Condition"), "") return to_qvariant( headers[i_column] ) else: return to_qvariant()
def data(self, index, role=Qt.DisplayRole): """Cell content""" if not index.isValid(): return to_qvariant() if role == Qt.DisplayRole or role == Qt.EditRole: column = index.column() row = index.row() if column == 0: return to_qvariant(to_text_string(self.df_index[row])) else: value = self.get_value(row, column-1) if isinstance(value, float): try: return to_qvariant(self._format % value) except (ValueError, TypeError): # may happen if format = '%d' and value = NaN; # see issue 4139 return to_qvariant(DEFAULT_FORMAT % value) else: try: return to_qvariant(to_text_string(value)) except UnicodeDecodeError: return to_qvariant(encoding.to_unicode(value)) elif role == Qt.BackgroundColorRole: return to_qvariant(self.get_bgcolor(index)) elif role == Qt.FontRole: return to_qvariant(get_font(font_size_delta=DEFAULT_SMALL_DELTA)) return to_qvariant()
def headerData(self, section, orientation, role=Qt.DisplayRole): """Overriding method headerData""" if role != Qt.DisplayRole: return to_qvariant() i_column = int(section) if orientation == Qt.Horizontal: headers = (_("Module"), _(" Required "), _(" Installed "), _("Provided features")) return to_qvariant( headers[i_column] ) else: return to_qvariant()
def data(self, index, role=Qt.DisplayRole): """Return a model data element""" if not index.isValid(): return to_qvariant() if role == Qt.DisplayRole: return self._display_data(index) elif role == Qt.BackgroundColorRole: return to_qvariant(get_color(self._data[index.row()][index.column()], .2)) elif role == Qt.TextAlignmentRole: return to_qvariant(int(Qt.AlignRight|Qt.AlignVCenter)) return to_qvariant()
def create_combobox(self, text, choices, option, default=NoDefault, tip=None, restart=False): """choices: couples (name, key)""" label = QLabel(text) combobox = QComboBox() if tip is not None: combobox.setToolTip(tip) for name, key in choices: if not (name is None and key is None): combobox.addItem(name, to_qvariant(key)) # Insert separators count = 0 for index, item in enumerate(choices): name, key = item if name is None and key is None: combobox.insertSeparator(index + count) count += 1 self.comboboxes[combobox] = (option, default) layout = QHBoxLayout() layout.addWidget(label) layout.addWidget(combobox) layout.addStretch(1) layout.setContentsMargins(0, 0, 0, 0) widget = QWidget(self) widget.label = label widget.combobox = combobox widget.setLayout(layout) combobox.restart_required = restart combobox.label_text = text return widget
def create_action(parent, text, shortcut=None, icon=None, tip=None, toggled=None, triggered=None, data=None, menurole=None, context=Qt.WindowShortcut): """Create a QAction""" action = QAction(text, parent) if triggered is not None: action.triggered.connect(triggered) if toggled is not None: action.toggled.connect(toggled) action.setCheckable(True) if icon is not None: if is_text_string(icon): icon = get_icon(icon) action.setIcon(icon) if shortcut is not None: action.setShortcut(shortcut) if tip is not None: action.setToolTip(tip) action.setStatusTip(tip) if data is not None: action.setData(to_qvariant(data)) if menurole is not None: action.setMenuRole(menurole) #TODO: Hard-code all shortcuts and choose context=Qt.WidgetShortcut # (this will avoid calling shortcuts from another dockwidget # since the context thing doesn't work quite well with these widgets) action.setShortcutContext(context) return action
def data(self, index, role=Qt.DisplayRole): """Cell content""" if not index.isValid(): return to_qvariant() value = self.get_value(index) if is_binary_string(value): try: value = to_text_string(value, 'utf8') except: pass if role == Qt.DisplayRole: if value is np.ma.masked: return '' else: try: return to_qvariant(self._format % value) except TypeError: self.readonly = True return repr(value) elif role == Qt.TextAlignmentRole: return to_qvariant(int(Qt.AlignCenter|Qt.AlignVCenter)) elif role == Qt.BackgroundColorRole and self.bgcolor_enabled \ and value is not np.ma.masked: try: hue = (self.hue0 + self.dhue * (float(self.vmax) - self.color_func(value)) / (float(self.vmax) - self.vmin)) hue = float(np.abs(hue)) color = QColor.fromHsvF(hue, self.sat, self.val, self.alp) return to_qvariant(color) except TypeError: return to_qvariant() elif role == Qt.FontRole: return to_qvariant(get_font(font_size_delta=DEFAULT_SMALL_DELTA)) return to_qvariant()
def data(self, index, role=Qt.DisplayRole): """Qt Override.""" row = index.row() if not index.isValid() or not (0 <= row < len(self.shortcuts)): return to_qvariant() shortcut = self.shortcuts[row] key = shortcut.key column = index.column() if role == Qt.DisplayRole: if column == CONTEXT: return to_qvariant(shortcut.context) elif column == NAME: color = self.text_color if self._parent == QApplication.focusWidget(): if self.current_index().row() == row: color = self.text_color_highlight else: color = self.text_color text = self.rich_text[row] text = '<p style="color:{0}">{1}</p>'.format(color, text) return to_qvariant(text) elif column == SEQUENCE: text = QKeySequence(key).toString(QKeySequence.NativeText) return to_qvariant(text) elif column == SEARCH_SCORE: # Treating search scores as a table column simplifies the # sorting once a score for a specific string in the finder # has been defined. This column however should always remain # hidden. return to_qvariant(self.scores[row]) elif role == Qt.TextAlignmentRole: return to_qvariant(int(Qt.AlignHCenter | Qt.AlignVCenter)) return to_qvariant()
def highlight_current_line(self): """Highlight current line""" selection = TextDecoration(self.textCursor()) selection.format.setProperty(QTextFormat.FullWidthSelection, to_qvariant(True)) selection.format.setBackground(self.currentline_color) selection.cursor.clearSelection() self.set_extra_selections('current_line', [selection]) self.update_extra_selections()
def data(self, index, role=Qt.DisplayRole): """Override Qt method""" if not index.isValid() or not 0 <= index.row() < len(self._rows): return to_qvariant() row = index.row() column = index.column() name, state = self.row(row) if role == Qt.DisplayRole or role == Qt.EditRole: if column == 0: return to_qvariant(name) elif role == Qt.CheckStateRole: if column == 0: if state: return Qt.Checked else: return Qt.Unchecked if column == 1: return to_qvariant(state) return to_qvariant()
def data(self, index, role=Qt.DisplayRole): """Return data at table index""" if not index.isValid(): return to_qvariant() dep = self.dependencies[index.row()] if role == Qt.DisplayRole: if index.column() == 0: value = self.get_value(index) return to_qvariant(value) else: value = self.get_value(index) return to_qvariant(value) elif role == Qt.TextAlignmentRole: return to_qvariant(int(Qt.AlignLeft|Qt.AlignVCenter)) elif role == Qt.BackgroundColorRole: from spyder.dependencies import Dependency status = dep.get_status() if status == Dependency.NOK: color = QColor(Qt.red) color.setAlphaF(.25) return to_qvariant(color)
def headerData(self, section, orientation, role=Qt.DisplayRole): """Set header data""" if role != Qt.DisplayRole: return to_qvariant() if orientation == Qt.Horizontal: if section == 0: return 'Index' elif section == 1 and PY2: # Get rid of possible BOM utf-8 data present at the # beginning of a file, which gets attached to the first # column header when headers are present in the first # row. # Fixes Issue 2514 try: header = to_text_string(self.df_header[0], encoding='utf-8-sig') except: header = to_text_string(self.df_header[0]) return to_qvariant(header) elif isinstance(self.df_header[section-1], TEXT_TYPES): # Get the proper encoding of the text in the header. # Fixes Issue 3896 if not PY2: try: header = self.df_header[section-1].encode('utf-8') coding = 'utf-8-sig' except: header = self.df_header[section-1].encode('utf-8') coding = encoding.get_coding(header) else: header = self.df_header[section-1] coding = encoding.get_coding(header) return to_qvariant(to_text_string(header, encoding=coding)) else: return to_qvariant(to_text_string(self.df_header[section-1])) else: return to_qvariant()
def headerData(self, section, orientation, role=Qt.DisplayRole): """Set header data""" if role != Qt.DisplayRole: return to_qvariant() if orientation == Qt.Horizontal: if section == 0: return 'Index' elif section == 1 and PY2: # Get rid of possible BOM utf-8 data present at the # beginning of a file, which gets attached to the first # column header when headers are present in the first # row. # Fixes Issue 2514 try: header = to_text_string(self.df_header[0], encoding='utf-8-sig') except: header = to_text_string(self.df_header[0]) return to_qvariant(header) else: return to_qvariant(to_text_string(self.df_header[section-1])) else: return to_qvariant()
def createEditor(self, parent, option, index): """Create editor widget""" model = index.model() value = model.get_value(index) if model._data.dtype.name == "bool": value = not value model.setData(index, to_qvariant(value)) return elif value is not np.ma.masked: editor = QLineEdit(parent) editor.setFont(get_font(font_size_delta=DEFAULT_SMALL_DELTA)) editor.setAlignment(Qt.AlignCenter) if is_number(self.dtype): editor.setValidator(QDoubleValidator(editor)) editor.returnPressed.connect(self.commitAndCloseEditor) return editor
def headerData(self, section, orientation, role=Qt.DisplayRole): """Qt Override.""" if role == Qt.TextAlignmentRole: if orientation == Qt.Horizontal: return to_qvariant(int(Qt.AlignHCenter | Qt.AlignVCenter)) return to_qvariant(int(Qt.AlignRight | Qt.AlignVCenter)) if role != Qt.DisplayRole: return to_qvariant() if orientation == Qt.Horizontal: if section == CONTEXT: return to_qvariant(_("Context")) elif section == NAME: return to_qvariant(_("Name")) elif section == SEQUENCE: return to_qvariant(_("Shortcut")) elif section == SEARCH_SCORE: return to_qvariant(_("Score")) return to_qvariant()
def headerData(self, section, orientation, role=Qt.DisplayRole): """Qt Override.""" if role == Qt.TextAlignmentRole: if orientation == Qt.Horizontal: return to_qvariant(int(Qt.AlignHCenter | Qt.AlignVCenter)) return to_qvariant(int(Qt.AlignRight | Qt.AlignVCenter)) if role != Qt.DisplayRole: return to_qvariant() if orientation == Qt.Horizontal: if section == LANGUAGE: return to_qvariant(_("Language")) elif section == ADDR: return to_qvariant(_("Address")) elif section == CMD: return to_qvariant(_("Command to execute")) return to_qvariant()
def headerData(self, section, orientation, role=Qt.DisplayRole): """Override Qt method""" if role == Qt.TextAlignmentRole: if orientation == Qt.Horizontal: return to_qvariant(int(Qt.AlignHCenter | Qt.AlignVCenter)) return to_qvariant(int(Qt.AlignRight | Qt.AlignVCenter)) elif role == Qt.DisplayRole and orientation == Qt.Horizontal: if section == C.COL_PACKAGE_TYPE: return to_qvariant(_("T")) if section == C.COL_NAME: return to_qvariant(_("Name")) elif section == C.COL_VERSION: return to_qvariant(_("Version")) elif section == C.COL_DESCRIPTION: return to_qvariant(_("Description")) elif section == C.COL_STATUS: return to_qvariant(_("Status")) else: return to_qvariant()
def create_action(parent, text, shortcut=None, icon=None, tip=None, toggled=None, triggered=None, data=None, menurole=None, context=Qt.WindowShortcut): """Create a QAction""" action = QAction(text, parent) if triggered is not None: action.triggered.connect(triggered) if toggled is not None: action.toggled.connect(toggled) action.setCheckable(True) if icon is not None: if is_text_string(icon): icon = get_icon(icon) action.setIcon(icon) if tip is not None: action.setToolTip(tip) action.setStatusTip(tip) if data is not None: action.setData(to_qvariant(data)) if menurole is not None: action.setMenuRole(menurole) # Workround for Mac because setting context=Qt.WidgetShortcut # there doesn't have any effect if sys.platform == 'darwin': action._shown_shortcut = None if context == Qt.WidgetShortcut: if shortcut is not None: action._shown_shortcut = shortcut else: # This is going to be filled by # main.register_shortcut action._shown_shortcut = 'missing' else: if shortcut is not None: action.setShortcut(shortcut) action.setShortcutContext(context) else: if shortcut is not None: action.setShortcut(shortcut) action.setShortcutContext(context) return action
def highlight_current_cell(self): """Highlight current cell""" if self.cell_separators is None or \ not self.highlight_current_cell_enabled: return cursor, whole_file_selected, whole_screen_selected =\ self.select_current_cell_in_visible_portion() selection = TextDecoration(cursor) selection.format.setProperty(QTextFormat.FullWidthSelection, to_qvariant(True)) selection.format.setBackground(self.currentcell_color) if whole_file_selected: self.clear_extra_selections('current_cell') elif whole_screen_selected: if self.has_cell_separators: self.set_extra_selections('current_cell', [selection]) self.update_extra_selections() else: self.clear_extra_selections('current_cell') else: self.set_extra_selections('current_cell', [selection]) self.update_extra_selections()
def data(self, index, role=Qt.DisplayRole): """Return data at table index""" if not index.isValid(): return to_qvariant() if role == Qt.DisplayRole: if index.column() == 0: value = osp.basename(self.get_value(index)) return to_qvariant(value) else: value = self.get_value(index) return to_qvariant(value) elif role == Qt.TextAlignmentRole: return to_qvariant(int(Qt.AlignLeft|Qt.AlignVCenter)) elif role == Qt.ToolTipRole: if index.column() == 0: value = self.get_value(index) return to_qvariant(value) else: return to_qvariant()
def data(self, index, role=Qt.DisplayRole): """Qt Override.""" row = index.row() if not index.isValid() or not (0 <= row < len(self.servers)): return to_qvariant() server = self.servers[row] column = index.column() if role == Qt.DisplayRole: if column == LANGUAGE: return to_qvariant(server.language) elif column == ADDR: text = '{0}:{1}'.format(server.host, server.port) return to_qvariant(text) elif column == CMD: text = ' <tt style="color:{0}">{{0}} {{1}}</tt>' text = text.format(self.text_color) if server.external: text = ' <tt>External server</tt>' return to_qvariant(text.format(server.cmd, server.args)) elif role == Qt.TextAlignmentRole: return to_qvariant(int(Qt.AlignHCenter | Qt.AlignVCenter)) return to_qvariant()
def data(self, index, role=Qt.DisplayRole): """Override Qt method""" if not index.isValid() or not 0 <= index.row() < len(self._rows): return to_qvariant() row = index.row() column = index.column() P = self._palette if self._rows[row] == row: action = C.ACTION_NONE type_ = u'' name = u'' description = u'' version = u'-' status = -1 # url = u'' # license_ = u'' i = False r = False u = False d = False # action_version = None else: action = self._rows[row][C.COL_ACTION] type_ = self._rows[row][C.COL_PACKAGE_TYPE] name = self._rows[row][C.COL_NAME] description = self._rows[row][C.COL_DESCRIPTION] version = self._rows[row][C.COL_VERSION] status = self._rows[row][C.COL_STATUS] # url = self._rows[row][C.COL_URL] # license_ = self._rows[row][C.COL_LICENSE] i = self._rows[row][C.COL_INSTALL] r = self._rows[row][C.COL_REMOVE] u = self._rows[row][C.COL_UPGRADE] d = self._rows[row][C.COL_DOWNGRADE] # action_version = self._rows[row][C.COL_ACTION_VERSION] is_upgradable = self.is_upgradable(self.index(row, C.COL_VERSION)) # if is_upgradable: # version += C.UPGRADE_SYMBOL if role == Qt.DisplayRole: if column == C.COL_PACKAGE_TYPE: return to_qvariant(type_) if column == C.COL_NAME: return to_qvariant(name) elif column == C.COL_VERSION: return to_qvariant(version) elif column == C.COL_STATUS: return to_qvariant(status) elif column == C.COL_DESCRIPTION: return to_qvariant(description) elif column == C.COL_ACTION: return to_qvariant(action) elif role == Qt.BackgroundRole: if action == C.ACTION_REMOVE: return to_qvariant(P['background.remove']) elif action == C.ACTION_INSTALL: return to_qvariant(P['background.install']) elif action == C.ACTION_UPGRADE: return to_qvariant(P['background.upgrade']) elif action == C.ACTION_DOWNGRADE: return to_qvariant(P['background.downgrade']) elif role == Qt.TextAlignmentRole: if column in [C.COL_NAME, C.COL_DESCRIPTION]: return to_qvariant(int(Qt.AlignLeft | Qt.AlignVCenter)) elif column in [C.COL_VERSION] and is_upgradable: return to_qvariant(int(Qt.AlignLeft | Qt.AlignVCenter)) # else: # return to_qvariant(int(Qt.AlignHCenter | Qt.AlignVCenter)) elif role == Qt.DecorationRole: if column == C.COL_ACTION: if action == C.ACTION_NONE: if status == C.NOT_INSTALLED: return to_qvariant(P['icon.action.not_installed']) elif status in [C.UPGRADABLE, C.MIXGRADABLE]: return to_qvariant(P['icon.action.installed']) elif status in [C.INSTALLED, C.DOWNGRADABLE, C.MIXGRADABLE]: return to_qvariant(P['icon.action.installed']) elif action == C.ACTION_INSTALL: return to_qvariant(P['icon.action.add']) elif action == C.ACTION_REMOVE: return to_qvariant(P['icon.action.remove']) elif action == C.ACTION_UPGRADE: return to_qvariant(P['icon.action.upgrade']) elif action == C.ACTION_DOWNGRADE: return to_qvariant(P['icon.action.downgrade']) else: return to_qvariant() elif column == C.COL_PACKAGE_TYPE: if type_ == C.CONDA_PACKAGE: return to_qvariant(P['icon.anaconda']) elif type_ == C.PIP_PACKAGE: return to_qvariant(P['icon.python']) else: return to_qvariant() elif column == C.COL_INSTALL: if status == C.NOT_INSTALLED: if i: return to_qvariant(P['icon.add.pressed']) else: return to_qvariant(P['icon.add.active']) elif (status == C.INSTALLED or status == C.UPGRADABLE or status == C.DOWNGRADABLE or status == C.MIXGRADABLE): if r: return to_qvariant(P['icon.remove.pressed']) else: return to_qvariant(P['icon.remove.active']) else: return to_qvariant(P['icon.add.inactive']) elif column == C.COL_REMOVE: if (status == C.INSTALLED or status == C.UPGRADABLE or status == C.DOWNGRADABLE or status == C.MIXGRADABLE): if r: return to_qvariant(P['icon.remove.pressed']) else: return to_qvariant(P['icon.remove.active']) else: return to_qvariant(P['icon.remove.inactive']) elif column == C.COL_UPGRADE: if status == C.UPGRADABLE or \ status == C.MIXGRADABLE: if u: return to_qvariant(P['icon.upgrade.pressed']) else: return to_qvariant(P['icon.upgrade.active']) else: return to_qvariant(P['icon.upgrade.inactive']) elif column == C.COL_DOWNGRADE: if status == C.DOWNGRADABLE or \ status == C.MIXGRADABLE: if d: return to_qvariant(P['icon.downgrade.pressed']) else: return to_qvariant(P['icon.downgrade.active']) else: return to_qvariant(P['icon.downgrade.inactive']) elif column == C.COL_VERSION: if is_upgradable: return to_qvariant(P['icon.upgrade.arrow']) else: return to_qvariant(P['spacer']) elif role == Qt.ToolTipRole: if column == C.COL_INSTALL and status == C.NOT_INSTALLED: return to_qvariant(_('Install package')) elif column == C.COL_INSTALL and (status == C.INSTALLED or status == C.UPGRADABLE or status == C.DOWNGRADABLE or status == C.MIXGRADABLE): return to_qvariant(_('Remove package')) elif column == C.COL_UPGRADE and (status == C.INSTALLED or status == C.UPGRADABLE or status == C.MIXGRADABLE): return to_qvariant(_('Upgrade package')) elif column == C.COL_DOWNGRADE and (status == C.INSTALLED or status == C.DOWNGRADABLE or status == C.MIXGRADABLE): return to_qvariant(_('Downgrade package')) elif column == C.COL_PACKAGE_TYPE: if type_ == C.CONDA_PACKAGE: return to_qvariant(_('Conda package')) elif type_ == C.PIP_PACKAGE: return to_qvariant(_('Python package')) elif column == C.COL_VERSION: if is_upgradable: return to_qvariant(_('Update available')) elif role == Qt.ForegroundRole: palette = QPalette() if column in [C.COL_NAME, C.COL_DESCRIPTION]: if status in [C.INSTALLED, C.UPGRADABLE, C.DOWNGRADABLE, C.MIXGRADABLE]: color = palette.color(QPalette.WindowText) return to_qvariant(color) elif status in [C.NOT_INSTALLED]: color = palette.color(QPalette.Mid) color = P['foreground.not.installed'] return to_qvariant(color) elif column in [C.COL_VERSION]: if is_upgradable: return to_qvariant(P['foreground.upgrade']) elif role == Qt.SizeHintRole: if column in [C.ACTION_COLUMNS] + [C.COL_PACKAGE_TYPE]: return to_qvariant(QSize(24, 24)) return to_qvariant()
def _display_data(self, index): """Return a data element""" return to_qvariant(self._data[index.row()][index.column()])
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 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 data(self, index, role=Qt.DisplayRole): """Override Qt method""" if not index.isValid() or not 0 <= index.row() < len(self._rows): return to_qvariant() row = index.row() column = index.column() # Carefull here with the order, this has to be adjusted manually if self._rows[row] == row: name, unlink, link, fetch = [u'', u'', u'', u''] else: name, unlink, link, fetch = self._rows[row] if role == Qt.DisplayRole: if column == 0: return to_qvariant(name) elif column == 1: return to_qvariant(unlink) elif column == 2: return to_qvariant(link) elif column == 3: return to_qvariant(fetch) elif role == Qt.TextAlignmentRole: if column in [0]: return to_qvariant(int(Qt.AlignLeft | Qt.AlignVCenter)) elif column in [1, 2, 3]: return to_qvariant(int(Qt.AlignHCenter | Qt.AlignVCenter)) elif role == Qt.ForegroundRole: return to_qvariant() elif role == Qt.FontRole: font = QFont() if row in self._bold_rows: font.setBold(True) return to_qvariant(font) else: font.setBold(False) return to_qvariant(font) return to_qvariant()
def headerData(self, section, orientation, role=Qt.DisplayRole): """Override Qt method""" if role == Qt.TextAlignmentRole: if orientation == Qt.Horizontal: return to_qvariant(int(Qt.AlignHCenter | Qt.AlignVCenter)) return to_qvariant(int(Qt.AlignRight | Qt.AlignVCenter)) elif role == Qt.ToolTipRole: column = section if column == const.INSTALL: return to_qvariant(_('Install/Remove package')) elif column == const.REMOVE: return to_qvariant(_('Remove package')) elif column == const.UPGRADE: return to_qvariant(_('Upgrade package')) elif column == const.DOWNGRADE: return to_qvariant(_('Downgrade package')) if orientation == Qt.Horizontal: if section == const.NAME: return to_qvariant(_("Name")) elif section == const.VERSION: return to_qvariant(_("Version")) elif section == const.DESCRIPTION: return to_qvariant(_("Description")) elif section == const.STATUS: return to_qvariant(_("Status")) elif section == const.INSTALL: return to_qvariant(_("I/R")) elif section == const.REMOVE: return to_qvariant(_("R")) elif section == const.UPGRADE: return to_qvariant(_("U")) elif section == const.DOWNGRADE: return to_qvariant(_("D")) else: return to_qvariant()
def headerData(self, section, orientation, role=Qt.DisplayRole): """Override Qt method""" if role == Qt.TextAlignmentRole: if orientation == Qt.Horizontal: return to_qvariant(int(Qt.AlignHCenter | Qt.AlignVCenter)) return to_qvariant(int(Qt.AlignRight | Qt.AlignVCenter)) elif role == Qt.ToolTipRole: column = section if column == C.COL_PACKAGE_TYPE: return to_qvariant(_('Package type: Conda, Pip')) elif column == C.COL_INSTALL: return to_qvariant(_('Install/Remove package')) elif column == C.COL_REMOVE: return to_qvariant(_('Remove package')) elif column == C.COL_UPGRADE: return to_qvariant(_('Upgrade package')) elif column == C.COL_DOWNGRADE: return to_qvariant(_('Downgrade package')) if orientation == Qt.Horizontal: if section == C.COL_PACKAGE_TYPE: return to_qvariant(_("T")) if section == C.COL_NAME: return to_qvariant(_("Name")) elif section == C.COL_VERSION: return to_qvariant(_("Version")) elif section == C.COL_DESCRIPTION: return to_qvariant(_("Description")) elif section == C.COL_STATUS: return to_qvariant(_("Status")) else: return to_qvariant()
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 data(self, index, role=Qt.DisplayRole): """Override Qt method""" if not index.isValid() or not 0 <= index.row() < len(self._rows): return to_qvariant() row = index.row() column = index.column() # Carefull here with the order, this has to be adjusted manually if self._rows[row] == row: [name, description, version, status, url, license_, i, r, u, d] =\ [u'', u'', '-', -1, u'', u'', False, False, False, False] else: [name, description, version, status, url, license_, i, r, u, d] = self._rows[row] if role == Qt.DisplayRole: if column == const.NAME: return to_qvariant(name) elif column == const.VERSION: return to_qvariant(version) elif column == const.STATUS: return to_qvariant(status) elif column == const.DESCRIPTION: return to_qvariant(description) elif role == Qt.TextAlignmentRole: if column in [const.NAME, const.DESCRIPTION]: return to_qvariant(int(Qt.AlignLeft | Qt.AlignVCenter)) else: return to_qvariant(int(Qt.AlignHCenter | Qt.AlignVCenter)) elif role == Qt.DecorationRole: if column == const.INSTALL: if status == const.NOT_INSTALLED: if i: return to_qvariant(self._icons['add.pressed']) else: return to_qvariant(self._icons['add.active']) elif (status == const.INSTALLED or status == const.UPGRADABLE or status == const.DOWNGRADABLE or status == const.MIXGRADABLE): if r: return to_qvariant(self._icons['remove.pressed']) else: return to_qvariant(self._icons['remove.active']) else: return to_qvariant(self._icons['add.inactive']) elif column == const.REMOVE: if (status == const.INSTALLED or status == const.UPGRADABLE or status == const.DOWNGRADABLE or status == const.MIXGRADABLE): if r: return to_qvariant(self._icons['remove.pressed']) else: return to_qvariant(self._icons['remove.active']) else: return to_qvariant(self._icons['remove.inactive']) elif column == const.UPGRADE: if status == const.UPGRADABLE or \ status == const.MIXGRADABLE: if u: return to_qvariant(self._icons['upgrade.pressed']) else: return to_qvariant(self._icons['upgrade.active']) else: return to_qvariant(self._icons['upgrade.inactive']) elif column == const.DOWNGRADE: if status == const.DOWNGRADABLE or \ status == const.MIXGRADABLE: if d: return to_qvariant(self._icons['downgrade.pressed']) else: return to_qvariant(self._icons['downgrade.active']) else: return to_qvariant(self._icons['downgrade.inactive']) elif role == Qt.ToolTipRole: if column == const.INSTALL and status == const.NOT_INSTALLED: return to_qvariant(_('Install package')) elif column == const.INSTALL and (status == const.INSTALLED or status == const.UPGRADABLE or status == const.DOWNGRADABLE or status == const.MIXGRADABLE): return to_qvariant(_('Remove package')) elif column == const.UPGRADE and (status == const.INSTALLED or status == const.UPGRADABLE or status == const.MIXGRADABLE): return to_qvariant(_('Upgrade package')) elif column == const.DOWNGRADE and (status == const.INSTALLED or status == const.DOWNGRADABLE or status == const.MIXGRADABLE): return to_qvariant(_('Downgrade package')) elif role == Qt.ForegroundRole: palette = QPalette() if column in [const.NAME, const.DESCRIPTION, const.VERSION]: if status in [const.INSTALLED, const.UPGRADABLE, const.DOWNGRADABLE, const.MIXGRADABLE]: color = palette.color(QPalette.WindowText) return to_qvariant(color) elif status in [const.NOT_INSTALLED, const.NOT_INSTALLABLE]: color = palette.color(QPalette.Mid) return to_qvariant(color) return to_qvariant()
def set_item_user_text(item, text): """Set QTreeWidgetItem user role string""" item.setData(0, Qt.UserRole, to_qvariant(text))
def data(self, index, role=Qt.DisplayRole): """Override Qt method""" if not index.isValid() or not 0 <= index.row() < len(self._rows): return to_qvariant() row = index.row() column = index.column() P = self._palette if self._rows[row] == row: action = C.ACTION_NONE type_ = u'' name = u'' description = u'' version = u'-' status = -1 # url = u'' # license_ = u'' i = False r = False u = False d = False # action_version = None else: action = self._rows[row][C.COL_ACTION] type_ = self._rows[row][C.COL_PACKAGE_TYPE] name = self._rows[row][C.COL_NAME] description = self._rows[row][C.COL_DESCRIPTION] version = self._rows[row][C.COL_VERSION] status = self._rows[row][C.COL_STATUS] # url = self._rows[row][C.COL_URL] # license_ = self._rows[row][C.COL_LICENSE] i = self._rows[row][C.COL_INSTALL] r = self._rows[row][C.COL_REMOVE] u = self._rows[row][C.COL_UPGRADE] d = self._rows[row][C.COL_DOWNGRADE] # action_version = self._rows[row][C.COL_ACTION_VERSION] is_upgradable = self.is_upgradable(self.index(row, C.COL_VERSION)) # if is_upgradable: # version += C.UPGRADE_SYMBOL if role == Qt.DisplayRole: if column == C.COL_PACKAGE_TYPE: return to_qvariant(type_) if column == C.COL_NAME: return to_qvariant(name) elif column == C.COL_VERSION: return to_qvariant(version) elif column == C.COL_STATUS: return to_qvariant(status) elif column == C.COL_DESCRIPTION: return to_qvariant(description) elif column == C.COL_ACTION: return to_qvariant(action) elif role == Qt.BackgroundRole: if action == C.ACTION_REMOVE: return to_qvariant(P['background.remove']) elif action == C.ACTION_INSTALL: return to_qvariant(P['background.install']) elif action == C.ACTION_UPGRADE: return to_qvariant(P['background.upgrade']) elif action == C.ACTION_DOWNGRADE: return to_qvariant(P['background.downgrade']) elif role == Qt.TextAlignmentRole: if column in [C.COL_NAME, C.COL_DESCRIPTION]: return to_qvariant(int(Qt.AlignLeft | Qt.AlignVCenter)) elif column in [C.COL_VERSION] and is_upgradable: return to_qvariant(int(Qt.AlignLeft | Qt.AlignVCenter)) else: return to_qvariant(int(Qt.AlignHCenter | Qt.AlignVCenter)) elif role == Qt.DecorationRole: if column == C.COL_ACTION: if action == C.ACTION_NONE: if status == C.NOT_INSTALLED: return to_qvariant(P['icon.action.not_installed']) elif status in [C.UPGRADABLE, C.MIXGRADABLE]: return to_qvariant(P['icon.action.installed']) elif status in [ C.INSTALLED, C.DOWNGRADABLE, C.MIXGRADABLE ]: return to_qvariant(P['icon.action.installed']) elif action == C.ACTION_INSTALL: return to_qvariant(P['icon.action.add']) elif action == C.ACTION_REMOVE: return to_qvariant(P['icon.action.remove']) elif action == C.ACTION_UPGRADE: return to_qvariant(P['icon.action.upgrade']) elif action == C.ACTION_DOWNGRADE: return to_qvariant(P['icon.action.downgrade']) else: return to_qvariant() elif column == C.COL_PACKAGE_TYPE: if type_ == C.CONDA_PACKAGE: return to_qvariant(P['icon.anaconda']) elif type_ == C.PIP_PACKAGE: return to_qvariant(P['icon.python']) else: return to_qvariant() elif column == C.COL_INSTALL: if status == C.NOT_INSTALLED: if i: return to_qvariant(P['icon.add.pressed']) else: return to_qvariant(P['icon.add.active']) elif (status == C.INSTALLED or status == C.UPGRADABLE or status == C.DOWNGRADABLE or status == C.MIXGRADABLE): if r: return to_qvariant(P['icon.remove.pressed']) else: return to_qvariant(P['icon.remove.active']) else: return to_qvariant(P['icon.add.inactive']) elif column == C.COL_REMOVE: if (status == C.INSTALLED or status == C.UPGRADABLE or status == C.DOWNGRADABLE or status == C.MIXGRADABLE): if r: return to_qvariant(P['icon.remove.pressed']) else: return to_qvariant(P['icon.remove.active']) else: return to_qvariant(P['icon.remove.inactive']) elif column == C.COL_UPGRADE: if status == C.UPGRADABLE or \ status == C.MIXGRADABLE: if u: return to_qvariant(P['icon.upgrade.pressed']) else: return to_qvariant(P['icon.upgrade.active']) else: return to_qvariant(P['icon.upgrade.inactive']) elif column == C.COL_DOWNGRADE: if status == C.DOWNGRADABLE or \ status == C.MIXGRADABLE: if d: return to_qvariant(P['icon.downgrade.pressed']) else: return to_qvariant(P['icon.downgrade.active']) else: return to_qvariant(P['icon.downgrade.inactive']) elif column == C.COL_VERSION and is_upgradable: return to_qvariant(P['icon.upgrade.arrow']) elif role == Qt.ToolTipRole: if column == C.COL_INSTALL and status == C.NOT_INSTALLED: return to_qvariant(_('Install package')) elif column == C.COL_INSTALL and (status == C.INSTALLED or status == C.UPGRADABLE or status == C.DOWNGRADABLE or status == C.MIXGRADABLE): return to_qvariant(_('Remove package')) elif column == C.COL_UPGRADE and (status == C.INSTALLED or status == C.UPGRADABLE or status == C.MIXGRADABLE): return to_qvariant(_('Upgrade package')) elif column == C.COL_DOWNGRADE and (status == C.INSTALLED or status == C.DOWNGRADABLE or status == C.MIXGRADABLE): return to_qvariant(_('Downgrade package')) elif column == C.COL_PACKAGE_TYPE: if type_ == C.CONDA_PACKAGE: return to_qvariant(_('Conda package')) elif type_ == C.PIP_PACKAGE: return to_qvariant(_('Python package')) elif column == C.COL_VERSION: if is_upgradable: return to_qvariant(_('Update available')) elif role == Qt.ForegroundRole: palette = QPalette() if column in [C.COL_NAME, C.COL_DESCRIPTION]: if status in [ C.INSTALLED, C.UPGRADABLE, C.DOWNGRADABLE, C.MIXGRADABLE ]: color = palette.color(QPalette.WindowText) return to_qvariant(color) elif status in [C.NOT_INSTALLED]: color = palette.color(QPalette.Mid) color = P['foreground.not.installed'] return to_qvariant(color) elif column in [C.COL_VERSION]: if is_upgradable: return to_qvariant(P['foreground.upgrade']) elif role == Qt.SizeHintRole: if column in [C.ACTION_COLUMNS] + [C.COL_PACKAGE_TYPE]: return to_qvariant(QSize(24, 24)) return to_qvariant()