def __init__(self, tolerance=None, allow_tail=True, colors=False, parent=None): self._parent = parent QStyledItemDelegate.__init__(self, parent) try: self._tolerance = int(tolerance) except (ValueError, TypeError): self._tolerance = self.DEFAULT_TOLERANCE self._allow_tail = allow_tail self._colors = colors self._color = None self._validator = QDoubleValidator() self._validator.setLocale(QLocale().system())
def _float_field( self, min_val: float = 0.0, max_val: float = 1.0, decimals: int = 1, placeholder: str = "", tooltip: str = "", ) -> QLineEdit: result = QLineEdit() # TODO derive the max width from the text repr of min_val, max_val. result.setMaximumWidth(self._FIELD_WIDTH) result.setAlignment(Qt.AlignRight) validator = QDoubleValidator(min_val, max_val, decimals) result.setValidator(validator) if placeholder: result.setPlaceholderText(placeholder) if tooltip: result.setToolTip(tooltip) return result
def create_validator(self, value, parent): if isinstance(value, bool): return QRegularExpressionValidator(self.bool_exp, parent) if isinstance(value, float): return QDoubleValidator(parent) if isinstance(value, int): return QIntValidator(parent) if isinstance(value, QByteArray): return QRegularExpressionValidator(self.byteArray_exp, parent) if isinstance(value, QColor): return QRegularExpressionValidator(self.color_exp, parent) if isinstance(value, QDate): return QRegularExpressionValidator(self.date_exp, parent) if isinstance(value, QDateTime): return QRegularExpressionValidator(self.dateTime_exp, parent) if isinstance(value, QTime): return QRegularExpressionValidator(self.time_exp, parent) if isinstance(value, QPoint): return QRegularExpressionValidator(self.point_exp, parent) if isinstance(value, QRect): return QRegularExpressionValidator(self.rect_exp, parent) if isinstance(value, QSize): return QRegularExpressionValidator(self.size_exp, parent) return None
def __init__(self): super(self.__class__, self).__init__() Ui_Img.__init__(self) self.setupUi(self) self.bookId = "" self.epsId = 0 self.curIndex = 0 self.setWindowTitle("IMG") self.setWindowModality(QtCore.Qt.ApplicationModal) self.resize(800, 900) self.checkBox.setChecked(True) self.index = 0 self.comboBox.setCurrentIndex(self.index) validator = QIntValidator(0, 9999999) self.heighEdit.setValidator(validator) self.widthEdit.setValidator(validator) exp = QDoubleValidator(0.1, 64, 1) exp.setNotation(exp.StandardNotation) self.scaleEdit.setValidator(exp) # self.setWindowFlags(Qt.FramelessWindowHint) self.graphicsView.setFrameStyle(QFrame.NoFrame) self.graphicsView.setObjectName("graphicsView") self.graphicsView.setBackgroundBrush(QColor(Qt.white)) self.graphicsView.setCursor(Qt.OpenHandCursor) self.graphicsView.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff) self.graphicsView.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff) self.graphicsView.setRenderHints(QPainter.Antialiasing | QPainter.SmoothPixmapTransform) self.graphicsView.setCacheMode(self.graphicsView.CacheBackground) self.graphicsView.setViewportUpdateMode(self.graphicsView.SmartViewportUpdate) self.graphicsItem = QGraphicsPixmapItem() self.graphicsItem.setFlags(QGraphicsPixmapItem.ItemIsFocusable | QGraphicsPixmapItem.ItemIsMovable) self.setContextMenuPolicy(Qt.CustomContextMenu) self.customContextMenuRequested.connect(self.CopyPicture) self.graphicsScene = QGraphicsScene(self) # 场景 self.graphicsView.setScene(self.graphicsScene) self.graphicsScene.addItem(self.graphicsItem) self.graphicsView.setMinimumSize(10, 10) self.pixMap = QPixmap("Loading") self.graphicsItem.setPixmap(self.pixMap) # self.radioButton.setChecked(True) self.isStripModel = False # self.radioButton.installEventFilter(self) # self.radioButton_2.installEventFilter(self) self.graphicsView.installEventFilter(self) self.graphicsView.setWindowFlag(Qt.FramelessWindowHint) # tta有BUG,暂时屏蔽 TODO # self.ttaModel.setEnabled(False) self.data = b"" self.waifu2xData = b"" self._delta = 0.1 self.scaleCnt = 0 self.backStatus = "" self.format = ""
class FloatDelegate(QStyledItemDelegate): DEFAULT_TOLERANCE = 6 def __init__(self, tolerance=None, allow_tail=True, colors=False, parent=None): self._parent = parent QStyledItemDelegate.__init__(self, parent) try: self._tolerance = int(tolerance) except (ValueError, TypeError): self._tolerance = self.DEFAULT_TOLERANCE self._allow_tail = allow_tail self._colors = colors self._color = None self._validator = QDoubleValidator() self._validator.setLocale(QLocale().system()) def formatFloatLong(self, value): precision = self._tolerance decimal_places = -decimal.Decimal( str(value).rstrip('0')).as_tuple().exponent if self._allow_tail and (decimal_places > self._tolerance): precision = decimal_places return QLocale().toString(value, 'f', precision) def displayText(self, value, locale): try: amount = float(value) except ValueError: amount = 0.0 if amount > 0: self._color = CustomColor.LightGreen elif amount < 0: self._color = CustomColor.LightRed return self.formatFloatLong(amount) # this is required when edit operation is called from QTableView def createEditor(self, aParent, option, index): float_editor = QLineEdit(aParent) float_editor.setValidator(self._validator) return float_editor def setEditorData(self, editor, index): try: amount = float(index.model().data(index, Qt.EditRole)) except (ValueError, TypeError): amount = 0.0 # QLocale().toString works in a bit weird way with float formatting - garbage appears after 5-6 decimal digits # if too long precision is specified for short number. So we need to be more precise setting precision. decimal_places = -decimal.Decimal( str(amount).rstrip('0')).as_tuple().exponent editor.setText(QLocale().toString(amount, 'f', decimal_places)) def setModelData(self, editor, model, index): value = QLocale().toDouble(editor.text())[0] model.setData(index, value) def initStyleOption(self, option, index): super().initStyleOption(option, index) option.displayAlignment = Qt.AlignRight if self._colors: option.backgroundBrush = QBrush(self._color) def paint(self, painter, option, index): super().paint(painter, option, index) if type( self._parent ) == QTreeView: # Extra code for tree views - to draw grid lines painter.save() pen = painter.pen() pen.setWidth(1) pen.setStyle(Qt.DotLine) pen.setColor(Qt.GlobalColor.lightGray) painter.setPen(pen) painter.drawRect(option.rect) painter.restore()
def __init__(self, model: SettingsModel, parent=None): super(SetQuotaDiskWidget, self).__init__(parent) self._model = model self.setAccessibleName("InfoBox") self.title = QLabel() self.title.setText("Spazio di archiviazione") self.title.setAccessibleName("Title2") self.sottotitolo = QLabel() self.sottotitolo.setAccessibleName('Sottotitolo') self.sottotitolo.setText( "Cambia lo spazio di archiviazione destinato alla cartella sincronizzata" ) # Barra riempimento disco self.progress_label = QLabel() self.progress_label.setText("Spazio occupato:") self.disk_progress = QProgressBar() self.disk_progress.setFormat("") self.disk_quota = QLabel() # Modifica spazio dedicato self.spaceLabel = QLabel(" ") self.dedicated_space = QLineEdit() self.dedicated_space.setValidator(QDoubleValidator()) self.sizes_box = QComboBox() self.sizes_box.wheelEvent = lambda event: None _path_size = bitmath.parse_string(model.convert_size(model.get_size())) _disk_free = bitmath.parse_string( model.convert_size(model.get_free_disk())) self.populate_size_box(_path_size, _disk_free) self.change_quota_button = QPushButton("Cambia quota disco") self.change_quota_button.setMaximumWidth(150) self.change_quota_button.clicked.connect( self.Sl_dedicated_space_changed) self.dedicated_space.returnPressed.connect( self.Sl_dedicated_space_changed) self.buttonLayout = QHBoxLayout() self.buttonLayout.addWidget(self.spaceLabel) self.buttonLayout.addWidget(self.change_quota_button) self.buttonLayout.addWidget(self.spaceLabel) set_space_layout = QHBoxLayout() set_space_layout.addWidget(self.dedicated_space) set_space_layout.addWidget(self.sizes_box) quota_layout = QHBoxLayout() quota_layout.setAlignment(Qt.AlignLeft) quota_layout.addWidget(self.progress_label) quota_layout.addWidget(self.disk_quota) # layout disk_layout = QVBoxLayout() disk_layout.setAlignment(Qt.AlignLeft) disk_layout.addWidget(self.title) disk_layout.addWidget(self.sottotitolo) disk_layout.addWidget(self.spaceLabel) disk_layout.addLayout(quota_layout) disk_layout.addWidget(self.disk_progress) disk_layout.addWidget(self.spaceLabel) disk_layout.addLayout(set_space_layout) disk_layout.addLayout(self.buttonLayout) self.setLayout(disk_layout) self.Sl_model_changed()