def calc_error_palette(self) -> QPalette: """Palette with red background, used for widgets with invalid input.""" error_palette = QPalette(self.palette()) bg = error_palette.color(QPalette.Base) red = QColor(qc.Qt.red) red_bg = blend_colors(bg, red, 0.5) error_palette.setColor(QPalette.Base, red_bg) return error_palette
def __init__(self, parent, packages, data): super(CondaPackagesModel, self).__init__(parent) self._parent = parent self._packages = packages self._rows = data self._name_to_index = {r[C.COL_NAME]: i for i, r in enumerate(data)} palette = QPalette() self._palette = { 'icon.upgrade.active': get_icon('conda_upgrade_active.png'), 'icon.upgrade.inactive': get_icon('conda_upgrade_inactive.png'), 'icon.upgrade.pressed': get_icon('conda_upgrade_pressed.png'), 'icon.downgrade.active': get_icon('conda_downgrade_active.png'), 'icon.downgrade.inactive': get_icon('conda_downgrade_inactive.png'), 'icon.downgrade.pressed': get_icon('conda_downgrade_pressed.png'), 'icon.add.active': get_icon('conda_add_active.png'), 'icon.add.inactive': get_icon('conda_add_inactive.png'), 'icon.add.pressed': get_icon('conda_add_pressed.png'), 'icon.remove.active': get_icon('conda_remove_active.png'), 'icon.remove.inactive': get_icon('conda_remove_inactive.png'), 'icon.remove.pressed': get_icon('conda_remove_pressed.png'), 'icon.action.not_installed': get_icon('conda_action_not_installed.png'), 'icon.action.installed': get_icon('conda_action_installed.png'), 'icon.action.installed_upgradable': get_icon('conda_action_installed_upgradable.png'), 'icon.action.remove': get_icon('conda_action_remove.png'), 'icon.action.add': get_icon('conda_action_add.png'), 'icon.action.upgrade': get_icon('conda_action_upgrade.png'), 'icon.action.downgrade': get_icon('conda_action_downgrade.png'), 'icon.upgrade.arrow': get_icon('conda_upgrade_arrow.png'), 'spacer': get_icon('spacer.png'), 'icon.python': get_icon('python.png').pixmap(QSize(16, 16)), 'icon.anaconda': get_icon('anaconda.png').pixmap(QSize(16, 16)), 'background.remove': QColor(128, 0, 0, 50), 'background.install': QColor(0, 128, 0, 50), 'background.upgrade': QColor(0, 0, 128, 50), 'background.downgrade': QColor(128, 0, 128, 50), 'foreground.not.installed': palette.color(QPalette.Mid), 'foreground.upgrade': QColor(0, 0, 128, 255), }
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()
class StyleDialog(ExToolWindow): """A dialog for editing Qt palette and stylesheet. Edits and sets Qt's application wide palette and stylesheet. See http://doc.qt.io/qt-4.8/stylesheet.html, and http://doc.qt.io/qt-4.8/qpalette.html for documentation of the effects of various settings, and the style sheet syntax. """ palette_entries = OrderedDict(( ('basic', ( 'Window', 'WindowText', )), ('extended', ( 'Base', 'Text', 'ToolTipBase', 'ToolTipText', 'Button', 'ButtonText', )), ('full', ( 'Highlight', 'HighlightedText', 'BrightText', 'Light', 'Midlight', 'Mid', 'Dark', 'Shadow', 'Link', 'LinkVisited', 'NoRole', )), )) pairs = [ ('Window', 'WindowText'), ('Base', 'Text'), ('Button', 'ButtonText'), ('ToolTipBase', 'ToolTipText'), ('Highlight', 'HighlightedText'), ('Light', 'Midlight'), ('Mid', 'Dark'), ('Link', 'LinkVisited'), ] def __init__(self, plugin, ui, parent): super(StyleDialog, self).__init__(parent) self.ui = ui self.setWindowTitle(tr("Edit application styles")) self.plugin = plugin self._palette = QPalette() self._rows = {} i = 0 for entries in self.palette_entries.values(): for key in entries: self._rows[key] = i for pair in self.pairs: if key == pair[0]: break else: i += 1 self.create_controls() self.save_action = QtWidgets.QAction(self) self.save_action.setShortcut(QtGui.QKeySequence.Save) self.save_action.triggered.connect(self.save) self.addAction(self.save_action) self.load() def save(self): """Store the palette and stylesheet to the plugin's settings""" self.plugin.settings['_style'] = self.editor.toPlainText() self.plugin.settings['_palette'] = self._palette def apply(self): """Apply current palette and stylesheet application wide""" QApplication.setPalette(self._palette) QApplication.instance().setStyleSheet(self.editor.toPlainText()) def load(self): """Load palette and stylesheet from the plugin't settings""" self.editor.setPlainText( self.plugin.settings['_style'] or '', 'text/plain', 'utf8' ) palette = self.plugin.settings['_palette'] if palette is not None: self._palette = QPalette(palette) self.update_palette_controls() def _clear_styles(self): """Reset style inputs to default values""" self._palette = QPalette(self.style().standardPalette()) self.editor.clear() self.update_palette_controls() def create_controls(self): """Build the editor's controls""" # First set up the stylesheet editor: editor = api.CodeEdit() editor.backend.start(server.__file__) editor.panels.append(panels.LineNumberPanel()) editor.panels.append(panels.CheckerPanel()) editor.modes.append(AutoIndentMode()) editor.modes.append(modes.CaretLineHighlighterMode()) editor.modes.append(modes.CodeCompletionMode()) editor.modes.append(modes.ExtendedSelectionMode()) editor.modes.append(modes.SmartBackSpaceMode()) editor.modes.append(modes.OccurrencesHighlighterMode()) editor.modes.append(modes.SymbolMatcherMode()) editor.modes.append(modes.ZoomMode()) self.editor = editor # Create the pushbuttons for the button bar: self.btn_apply = QPushButton(tr("Apply")) self.btn_apply.clicked.connect(self.apply) self.btn_save = QPushButton(tr("Save")) self.btn_save.clicked.connect(self.save) self.btn_revert = QPushButton(tr("Revert")) self.btn_revert.clicked.connect(self.load) self.btn_clear = QPushButton(tr("Clear")) self.btn_clear.clicked.connect(self._clear_styles) self.hbox = QHBoxLayout() for w in [self.btn_apply, self.btn_save, self.btn_revert, self.btn_clear]: self.hbox.addWidget(w) # Create the group-box with the palette editor self.palette_box = self.create_palette_colors() # Lay out the various components: vbox = QVBoxLayout(self) vbox.addWidget(self.palette_box) vbox.addWidget(editor) vbox.addLayout(self.hbox) self.setLayout(vbox) def create_palette_colors(self): """Create the controls for editing the palette""" layout = QtWidgets.QGridLayout() # Create combobox simple/extended/full cbo = QComboBox() cbo.addItems(list(self.palette_entries.keys())) cbo.currentIndexChanged[str].connect(self._on_cbo_change) layout.addWidget(cbo, 0, 0) self.cbo_mode = cbo # Create pickers for all self.pickers = {} for key, entries in self.palette_entries.items(): self.pickers[key] = {} for subkey in entries: btn = ColorButton() self.pickers[key][subkey] = btn btn.colorChanged.connect( partial(self._on_color_pick, subkey)) label = QLabel(subkey) if key != 'basic': btn.hide() label.hide() row = 1 + self._rows[subkey] for pair in self.pairs: if subkey == pair[1]: layout.addWidget(label, row, 2) layout.addWidget(btn, row, 3) break else: layout.addWidget(label, row, 0) layout.addWidget(btn, row, 1) # Initialize colors: self.update_palette_controls() box = QGroupBox('Palette') box.setLayout(layout) return box def update_palette_controls(self): """Update palette controls from internal QPalette""" for key, entries in self.palette_entries.items(): for subkey in entries: color = self._palette.color( getattr(self._palette, subkey)) btn = self.pickers[key][subkey] with block_signals(btn): btn.color = color def _setvis_palette_entry(self, key, show): """Show/hide specific palette entries""" row = 1 + self._rows[key] layout = self.palette_box.layout() # Always show/hide pairs together: for i in range(4): item = layout.itemAtPosition(row, i) if item is not None: item.widget().setVisible(show) def _on_color_pick(self, key, color): """Callback when a palette color has been picked""" if self.cbo_mode.currentText() == 'basic': # Other values should be auto-generated self._palette = QPalette(self.pickers['basic']['Window'].color) self._palette.setColor( getattr(self._palette, key), color ) def _on_cbo_change(self, selection): """Callback for when palette-mode selection changes""" # Use all up-until selection (keys are ordered) included = list(self.palette_entries.keys()) included = included[:1 + included.index(selection)] for key, entries in self.palette_entries.items(): visible = key in included for subkey in entries: self._setvis_palette_entry(subkey, show=visible)
def data(self, index, role=Qt.DisplayRole): """Override Qt method.""" if not index.isValid() or not 0 <= index.row() < len(self._rows): return row = index.row() column = index.column() P = self._palette # Include the palete directly here. if self._rows[row] == row: action = C.ACTION_NONE type_ = u'' name = u'' description = u'' version = u'-' status = -1 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] is_upgradable = self.is_upgradable(self.index(row, C.COL_VERSION)) if role == Qt.DisplayRole: if column == C.COL_PACKAGE_TYPE: return type_ if column == C.COL_NAME: return name elif column == C.COL_VERSION: return version elif column == C.COL_STATUS: return status elif column == C.COL_DESCRIPTION: return description elif column == C.COL_ACTION: return action elif role == Qt.TextAlignmentRole: if column in [C.COL_NAME, C.COL_DESCRIPTION]: return int(Qt.AlignLeft | Qt.AlignVCenter) elif column in [C.COL_VERSION]: return int(Qt.AlignLeft | Qt.AlignVCenter) else: return int(Qt.AlignCenter) elif role == Qt.DecorationRole: if column == C.COL_ACTION: if action == C.ACTION_NONE: if status == C.NOT_INSTALLED: return P['icon.action.not_installed'] elif status in [C.UPGRADABLE, C.MIXGRADABLE]: return P['icon.action.installed'] elif status in [ C.INSTALLED, C.DOWNGRADABLE, C.MIXGRADABLE ]: return P['icon.action.installed'] elif action == C.ACTION_INSTALL: return P['icon.action.add'] elif action == C.ACTION_REMOVE: return P['icon.action.remove'] elif action == C.ACTION_UPGRADE: return P['icon.action.upgrade'] elif action == C.ACTION_UPDATE: return P['icon.action.upgrade'] elif action == C.ACTION_DOWNGRADE: return P['icon.action.downgrade'] else: return elif column == C.COL_PACKAGE_TYPE: if type_ == C.CONDA_PACKAGE: return P['icon.anaconda'] elif type_ == C.PIP_PACKAGE: return P['icon.python'] else: return elif column == C.COL_VERSION: if is_upgradable: return P['icon.upgrade.arrow'] else: return P['icon.spacer'] elif role == Qt.ToolTipRole: if column == C.COL_PACKAGE_TYPE: if type_ == C.CONDA_PACKAGE: return 'Conda package' elif type_ == C.PIP_PACKAGE: return 'Python package' elif column == C.COL_VERSION and is_upgradable: return '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) color = QColor('#000') return color elif status in [C.NOT_INSTALLED]: color = palette.color(QPalette.Mid) color = P['color.foreground.not.installed'] return color elif column in [C.COL_VERSION] and is_upgradable: return P['color.foreground.upgrade'] elif (role == Qt.SizeHintRole and column in [C.ACTION_COLUMNS, C.COL_PACKAGE_TYPE]): return P['size.icons'] return
def __init__(self, parent, packages, data): super(CondaPackagesModel, self).__init__(parent) self._parent = parent self._packages = packages self._rows = data self._name_to_index = {r[C.COL_NAME]: i for i, r in enumerate(data)} palette = QPalette() self._palette = { 'icon.upgrade.active': get_icon('conda_upgrade_active.png'), 'icon.upgrade.inactive': get_icon('conda_upgrade_inactive.png'), 'icon.upgrade.pressed': get_icon('conda_upgrade_pressed.png'), 'icon.downgrade.active': get_icon('conda_downgrade_active.png'), 'icon.downgrade.inactive': get_icon('conda_downgrade_inactive.png'), 'icon.downgrade.pressed': get_icon('conda_downgrade_pressed.png'), 'icon.add.active': get_icon('conda_add_active.png'), 'icon.add.inactive': get_icon('conda_add_inactive.png'), 'icon.add.pressed': get_icon('conda_add_pressed.png'), 'icon.remove.active': get_icon('conda_remove_active.png'), 'icon.remove.inactive': get_icon('conda_remove_inactive.png'), 'icon.remove.pressed': get_icon('conda_remove_pressed.png'), 'icon.action.not_installed': get_icon('conda_action_not_installed.png'), 'icon.action.installed': get_icon('conda_action_installed.png'), 'icon.action.installed_upgradable': get_icon('conda_action_installed_upgradable.png'), 'icon.action.remove': get_icon('conda_action_remove.png'), 'icon.action.add': get_icon('conda_action_add.png'), 'icon.action.upgrade': get_icon('conda_action_upgrade.png'), 'icon.action.downgrade': get_icon('conda_action_downgrade.png'), 'icon.upgrade.arrow': get_icon('conda_upgrade_arrow.png'), 'icon.python': get_icon('python.png').pixmap(QSize(16, 16)), 'icon.anaconda': get_icon('anaconda.png').pixmap(QSize(16, 16)), 'background.remove': QColor(128, 0, 0, 50), 'background.install': QColor(0, 128, 0, 50), 'background.upgrade': QColor(0, 0, 128, 50), 'background.downgrade': QColor(128, 0, 128, 50), 'foreground.not.installed': palette.color(QPalette.Mid), 'foreground.upgrade': QColor(0, 0, 128, 255), }
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()
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()