def __init__(self, parent=None, is_half_star=False): QComboBox.__init__(self, parent) self.undo_stack = QUndoStack(self) self.undo, self.redo = self.undo_stack.undo, self.undo_stack.redo self.allow_undo = False self.is_half_star = is_half_star self._model = RatingModel(is_half_star=is_half_star, parent=self) self.setModel(self._model) self.delegate = QStyledItemDelegate(self) self.view().setItemDelegate(self.delegate) self.view().setStyleSheet('QListView { background: palette(window) }\nQListView::item { padding: 6px }') self.setMaxVisibleItems(self.count()) self.currentIndexChanged.connect(self.update_font)
def __init__(self, parent=None): QWidget.__init__(self, parent) self.setAcceptDrops(True) self.setMouseTracking(True) self.setFocusPolicy(Qt.ClickFocus) self.selection_state = SelectionState() self.undo_stack = u = QUndoStack() u.setUndoLimit(10) u.canUndoChanged.connect(self.emit_undo_redo_state) u.canRedoChanged.connect(self.emit_undo_redo_state) self.original_image_data = None self.is_valid = False self.original_image_format = None self.current_image = None self.current_scaled_pixmap = None self.last_canvas_size = None self.target = QRectF(0, 0, 0, 0) self.undo_action = a = self.undo_stack.createUndoAction( self, _('Undo') + ' ') a.setIcon(QIcon(I('edit-undo.png'))) self.redo_action = a = self.undo_stack.createRedoAction( self, _('Redo') + ' ') a.setIcon(QIcon(I('edit-redo.png')))
def __init__(self, parent=None, is_half_star=False): QComboBox.__init__(self, parent) self.addItem(_('Not rated')) if is_half_star: [self.addItem(stars(x, True)) for x in range(1, 11)] else: [self.addItem(stars(x)) for x in (2, 4, 6, 8, 10)] self.rating_font = QFont(rating_font()) self.undo_stack = QUndoStack(self) self.undo, self.redo = self.undo_stack.undo, self.undo_stack.redo self.allow_undo = False self.is_half_star = is_half_star self.delegate = RatingItemDelegate(self) self.view().setItemDelegate(self.delegate) self.view().setStyleSheet('QListView { background: palette(window) }\nQListView::item { padding: 6px }') self.setMaxVisibleItems(self.count()) self.currentIndexChanged.connect(self.update_font)
def __init__(self, parent=None, is_half_star=False): QComboBox.__init__(self, parent) self.undo_stack = QUndoStack(self) self.undo, self.redo = self.undo_stack.undo, self.undo_stack.redo self.allow_undo = False self.is_half_star = is_half_star self._model = RatingModel(is_half_star=is_half_star, parent=self) self.setModel(self._model) self.delegate = QStyledItemDelegate(self) self.view().setItemDelegate(self.delegate) self.view().setStyleSheet("QListView { background: palette(window) }\nQListView::item { padding: 6px }") self.setMaxVisibleItems(self.count()) self.currentIndexChanged.connect(self.update_font)
class RatingEditor(QComboBox): def __init__(self, parent=None, is_half_star=False): QComboBox.__init__(self, parent) self.undo_stack = QUndoStack(self) self.undo, self.redo = self.undo_stack.undo, self.undo_stack.redo self.allow_undo = False self.is_half_star = is_half_star self._model = RatingModel(is_half_star=is_half_star, parent=self) self.setModel(self._model) self.delegate = QStyledItemDelegate(self) self.view().setItemDelegate(self.delegate) self.view().setStyleSheet( 'QListView { background: palette(window) }\nQListView::item { padding: 6px }' ) self.setMaxVisibleItems(self.count()) self.currentIndexChanged.connect(self.update_font) @property def null_text(self): return self._model.null_text @null_text.setter def null_text(self, val): self._model.null_text = val self._model.dataChanged.emit(self._model.index(0, 0), self._model.index(0, 0)) def update_font(self): if self.currentIndex() == 0: self.setFont(QApplication.instance().font()) else: self.setFont(self._model.rating_font) def clear_to_undefined(self): self.setCurrentIndex(0) @property def rating_value(self): ' An integer from 0 to 10 ' ans = self.currentIndex() if not self.is_half_star: ans *= 2 return ans @rating_value.setter def rating_value(self, val): val = max(0, min(int(val or 0), 10)) if self.allow_undo: cmd = UndoCommand(self, val) self.undo_stack.push(cmd) else: self.undo_stack.clear() if not self.is_half_star: val //= 2 self.setCurrentIndex(val) def keyPressEvent(self, ev): if ev == QKeySequence.Undo: self.undo() return ev.accept() if ev == QKeySequence.Redo: self.redo() return ev.accept() k = ev.key() num = {getattr(Qt, 'Key_%d' % i): i for i in range(6)}.get(k) if num is None: return QComboBox.keyPressEvent(self, ev) ev.accept() if self.is_half_star: num *= 2 self.setCurrentIndex(num) @staticmethod def test(): q = RatingEditor(is_half_star=True) q.rating_value = 7 return q
class RatingEditor(QComboBox): def __init__(self, parent=None, is_half_star=False): QComboBox.__init__(self, parent) self.undo_stack = QUndoStack(self) self.undo, self.redo = self.undo_stack.undo, self.undo_stack.redo self.allow_undo = False self.is_half_star = is_half_star self._model = RatingModel(is_half_star=is_half_star, parent=self) self.setModel(self._model) self.delegate = QStyledItemDelegate(self) self.view().setItemDelegate(self.delegate) self.view().setStyleSheet("QListView { background: palette(window) }\nQListView::item { padding: 6px }") self.setMaxVisibleItems(self.count()) self.currentIndexChanged.connect(self.update_font) @property def null_text(self): return self._model.null_text @null_text.setter def null_text(self, val): self._model.null_text = val self._model.dataChanged.emit(self._model.index(0, 0), self._model.index(0, 0)) def update_font(self): if self.currentIndex() == 0: self.setFont(QApplication.instance().font()) else: self.setFont(self._model.rating_font) def clear_to_undefined(self): self.setCurrentIndex(0) @property def rating_value(self): " An integer from 0 to 10 " ans = self.currentIndex() if not self.is_half_star: ans *= 2 return ans @rating_value.setter def rating_value(self, val): val = max(0, min(int(val or 0), 10)) if self.allow_undo: cmd = UndoCommand(self, val) self.undo_stack.push(cmd) else: self.undo_stack.clear() if not self.is_half_star: val //= 2 self.setCurrentIndex(val) def keyPressEvent(self, ev): if ev == QKeySequence.Undo: self.undo() return ev.accept() if ev == QKeySequence.Redo: self.redo() return ev.accept() k = ev.key() num = {getattr(Qt, "Key_%d" % i): i for i in range(6)}.get(k) if num is None: return QComboBox.keyPressEvent(self, ev) ev.accept() if self.is_half_star: num *= 2 self.setCurrentIndex(num)