def __init__(self, parent=None, dragger_steps=None, slider_range=None, *args, **kwargs): if dragger_steps is None: dragger_steps = INT_SLIDER_DRAG_STEPS if slider_range is None: slider_range = [-100, 100] super(Slider, self).__init__(parent=parent, **kwargs) self._slider_range = slider_range self._dragger_steps = dragger_steps self._is_float = False self._default_value = 0 self._prev_value = 0 self._delta_value = 0 self._start_drag_pos = QPointF() self._real_start_drag_pos = QPointF() self._draggers = None if dcc.is_maya(): self._left_button = Qt.MidButton self._mid_button = Qt.LeftButton else: self._left_button = Qt.LeftButton self._mid_button = Qt.MidButton self.setFocusPolicy(Qt.StrongFocus) self.setOrientation(Qt.Horizontal) self.setRange(self._slider_range[0], self._slider_range[1])
def mouseMoveEvent(self, event): if not self._knop_pressed: return delta_pos = QPointF(event.pos() - self._last_pos) delta_pos += 0.5 * (QPointF(event.pos()) - self._knop_bounds.center()) from_center_to_knop = self._knop_bounds.center( ) + delta_pos - self._bounds.center() radius = (self._bounds.width() - self._knop_bounds.width()) * 0.5 from_center_to_knop.setX( self.constraint(from_center_to_knop.x(), -radius, radius)) from_center_to_knop.setY( self.constraint(from_center_to_knop.y(), -radius, radius)) self._knop_bounds.moveCenter(from_center_to_knop + self._bounds.center()) self._last_pos = event.pos() self.update() if radius == 0: return x = (self._knop_bounds.center().x() - self._bounds.center().x()) / radius y = (-self._knop_bounds.center().y() + self._bounds.center().y()) / radius if self._x != x: self._x = x self.xChanged.emit(self._x) if self._y != y: self._y = y self.yChanged.emit(self._y)
def vertical_line_to(path, cmd, data): current_pos = path.currentPosition() if cmd.islower(): target = current_pos + QPointF(0, data[0]) else: target = QPointF(current_pos.x(), data[0]) path.lineTo(target)
def __init__(self, parent=None, **kwargs): QGraphicsItem.__init__(self, parent=parent) self.enabled = kwargs.get('enabled', True) self.color = kwargs.get('color', QColor(31, 32, 33, 255)) self.disabled_color = kwargs.get('disabled_color', QColor(125, 125, 125, 255)) self.selected_color = kwargs.get('selected_color', QColor(30, 35, 40, 255)) self.disabled_border_width = kwargs.get('disabled_border_width', 1.5) self.selected_border_width = kwargs.get('selected_border_width', 1.5) self.disabled_border_color = kwargs.get('disabled_border_color', QColor(40, 40, 40, 255)) self.selected_border_color = kwargs.get('selected_border_color', QColor(250, 210, 90, 255)) self.disabled_shadow_color = kwargs.get('disabled_shadow_color', QColor(35, 35, 35, 60)) self.selected_shadow_color = kwargs.get('selected_shadow_color', QColor(105, 55, 0, 60)) self.disabled_border_style = kwargs.get('disabled_border_style', Qt.DashDotLine) self.selected_border_style = kwargs.get('selected_border_style', Qt.DashLine) self._border_width = kwargs.get('border_width', 1.5) self._shadow_color = kwargs.get('shadow_color', QColor(0, 0, 0, 60)) self._border_color = kwargs.get('border_color', QColor(10, 10, 10, 255)) self._border_type = kwargs.get('border_type', Qt.SolidLine) self._current_pos = QPointF(0, 0) self._new_pos = QPointF(0, 0) self._width = kwargs.get('width', 120) self._height = kwargs.get('height', 40) self._sizes = [0, 0, self._width, self._height, 7, 7] # [x, y, width, height, radius_x, radius_x] self._is_hovered = False self._render_effects = True
def horizontal_line_to(path, cmd, data): current_pos = path.currentPosition() if cmd.islower(): target = current_pos + QPointF(data[0], 0) else: target = QPointF(data[0], current_pos.y()) path.lineTo(target)
def addItems(self): itemCount = self.itemTable.rowCount() imageOffset = 150 leftMargin = 70 topMargin = 40 for i in range(itemCount): record = self.itemTable.record(i) id_ = record.value("id") file = record.value("file") item = record.value("itemtype") columnOffset = (i % 2) * 37 x = ((i % 2) * imageOffset) + leftMargin + columnOffset y = ((i / 2) * imageOffset) + topMargin image = ImageItem(id_, QPixmap(":/" + file)) image.setData(0, i) image.setPos(x, y) self.scene.addItem(image) label = self.scene.addText(item) label.setDefaultTextColor(QColor("#d7d6d5")) labelOffset = QPointF((120 - label.boundingRect().width()) / 2, 120.0) label.setPos(QPointF(x, y) + labelOffset)
def paint(self, painter, option, index): painter.setRenderHints(QPainter.Antialiasing | QPainter.SmoothPixmapTransform) model = index.model() view = self.parent() if view.hasFocus() and option.state & QStyle.State_MouseOver: painter.setPen(Qt.NoPen) painter.setBrush(Qt.gray) painter.drawRoundedRect(option.rect.adjusted(1, 1, -1, -1), self._ICON_MARGIN, self._ICON_MARGIN) pixmap = model.data(index, Qt.DecorationRole).pixmap(view.iconSize()) pm_rect = QRect( option.rect.topLeft() + QPoint(self._ICON_MARGIN + 1, self._ICON_MARGIN + 1), view.iconSize() - QSize(self._ICON_MARGIN * 2, self._ICON_MARGIN * 2)) painter.drawPixmap(pm_rect, pixmap) if option.state & QStyle.State_Selected: painter.setPen( QPen(Qt.red, 1.0, Qt.SolidLine, Qt.SquareCap, Qt.RoundJoin)) painter.setBrush(Qt.NoBrush) painter.drawRect(option.rect.adjusted(2, 2, -2, -2)) font = view.font() fm = QFontMetrics(font) text = os.path.splitext( os.path.basename(model.data(index, Qt.DisplayRole)))[0] text = fm.elidedText(text, Qt.ElideRight, view.iconSize().width() - 4) text_opt = QTextOption() text_opt.setAlignment(Qt.AlignHCenter) txt_rect = QRectF(QPointF(pm_rect.bottomLeft() + QPoint(0, 1)), QPointF(option.rect.bottomRight() - QPoint(4, 3))) painter.save() painter.setPen(Qt.NoPen) painter.setBrush(QColor(22, 22, 22, 220)) painter.drawRoundedRect(txt_rect.adjusted(-2, -2, 2, 2), 2, 2) painter.restore() painter.setPen(self.parent().palette().color(QPalette.WindowText)) painter.drawText(txt_rect, text, text_opt) font.setPointSize(8) fm = QFontMetrics(font) item = model.itemFromIndex(index) size_text = '%d x %d' % (item.size.width(), item.size.height()) size_rect = fm.boundingRect(option.rect, Qt.AlignLeft | Qt.AlignTop, size_text) size_rect.translate(4, 4) painter.save() painter.setPen(Qt.NoPen) painter.setBrush(QColor(22, 22, 22, 220)) painter.drawRoundedRect(size_rect.adjusted(-2, -2, 2, 2), 2, 2) painter.restore() painter.setFont(font) painter.drawText(size_rect, size_text)
def quad_to(path, cmd, data): new1st_pos = QPointF(data[0], data[1]) new_end_pos = QPointF(data[2], data[3]) path.quadTo(new1st_pos, new_end_pos) if cmd.islower(): current_pos = path.currentPosition() new1st_pos += current_pos new_end_pos += current_pos path.quadTo(new1st_pos, new_end_pos)
def cubic_to(path, cmd, data): new1st_pos = QPointF(data[0], data[1]) new2st_pos = QPointF(data[2], data[3]) new_end_pos = QPointF(data[4], data[5]) if cmd.islower(): current_pos = path.currentPosition() new1st_pos += current_pos new2st_pos += current_pos new_end_pos += current_pos path.cubicTo(new1st_pos, new2st_pos, new_end_pos)
def smooth_quad_to(path, cmd, data): elem_count = path.elementCount() prev_end_x, prevEndY = path.elementAt(elem_count - 1).x, path.elementAt(elem_count - 1).y prev1st_x, prev1stY = path.elementAt(elem_count - 2).x, path.elementAt(elem_count - 2).y new1st_pos = QPointF(2 * prev_end_x - prev1st_x, 2 * prevEndY - prev1stY) new_end_pos = QPointF(data[0], data[1]) if cmd.islower(): current_pos = path.currentPosition() new_end_pos += current_pos path.quadTo(new1st_pos, new_end_pos)
def pan(self, delta: QPoint) -> None: dx = QPointF(delta) / float(tdim) center = tileForCoordinate(self.latitude, self.longitude, self.zoom) - dx self.latitude = latitudeFromTile(center.y(), self.zoom) self.longitude = longitudeFromTile(center.x(), self.zoom) self.invalidate()
def __init__(self, parent=None): self._base_size = 300 self._bezier_dict = { 'bezier': [ QPoint(0, 0), QPoint(75, 0), QPoint(225, 300), QPoint(300, 300)], 'linear': [ QPointF(0.000000, 0.000000), QPointF(75.000000, 75.000000), QPointF(225.000000, 225.000000), QPointF(300.000000, 300.000000)] } super(FallofCurveWidget, self).__init__(parent=parent) self.setObjectName('Falloff Curve') self.show() self.refresh()
def get_points(self): points = list() for obj in self._point_objects: point = QPointF(obj.pos().x() + 5, obj.pos().y() + 5) points.append(point) return points
def tileForCoordinate(lat: float, lng: float, zoom: int) -> QPointF: radianLat = math.radians(lat) zn = float(1 << zoom) tx = (lng + 180.0) / 360.0 ty = 0.5 - math.log(math.tan(radianLat) + 1.0 / math.cos(radianLat)) / math.pi / 2.0 return QPointF(tx * zn, ty * zn)
def set_x(self, x): self._x = self.constraint(x, -1.0, 1.0) radius = (self._bounds.width() - self._knop_bounds.width()) * 0.5 self._knop_bounds.moveCenter( QPointF(self._bounds.center().x() + self._x * radius, self._knop_bounds.center().y())) self.update() self.xChanged.emit(self._x)
def set_y(self, y): self._y = self.constraint(y, -1.0, 1.0) radius = (self._bounds.width() - self._knop_bounds.width()) * 0.5 self._knop_bounds.moveCenter( QPointF(self._knop_bounds.center().x(), self._bounds.center().y() - self._y * radius)) self.update() self.yChanged.emit(self._x)
def calculate_start_angle(x1, y1, rx, ry, coordAngle, largeArcFlag, sweep_flag, x2, y2): def dotproduct(v1, v2): return sum((a * b for a, b in zip(v1, v2))) def length(v): return math.sqrt(dotproduct(v, v)) def angle(v1, v2): return math.acos(dotproduct(v1, v2) / (length(v1) * length(v2))) rotated_x1 = math.cos(math.radians(coordAngle)) * ( (x1 - x2) / 2) + math.sin(math.radians(coordAngle)) * ((y1 - y2) / 2) rotated_y1 = -math.sin(math.radians(coordAngle)) * ( (x1 - x2) / 2) + math.cos(math.radians(coordAngle)) * ((y1 - y2) / 2) delta = rotated_x1**2 / rx**2 + rotated_y1**2 / ry**2 if delta > 1: rx *= math.sqrt(delta) ry *= math.sqrt(delta) _a = (rx**2 * ry**2 - rx**2 * rotated_y1**2 - ry**2 * rotated_x1**2) _b = (rx**2 * rotated_y1**2 + ry**2 * rotated_x1**2) var = math.sqrt(_a / _b) if largeArcFlag == sweep_flag: var *= -1 ccx = var * (rx * rotated_y1 / ry) ccy = var * -(ry * rotated_x1 / rx) cx = math.cos(math.radians(coordAngle)) * ccx - math.sin( math.radians(coordAngle)) * ccy + (x1 + x2) / 2 cy = math.sin(math.radians(coordAngle)) * ccx + math.cos( math.radians(coordAngle)) * ccy + (y1 + y2) / 2 start_angle = math.degrees( angle([1, 0], [(rotated_x1 - ccx) / rx, (rotated_y1 - ccy) / ry])) start_angle_sign = 1 * (rotated_y1 - ccy) / ry - 0 * (rotated_x1 - ccx) / rx if start_angle_sign == 0: start_angle_sign = 1.0 start_angle_sign /= abs(start_angle_sign) start_angle *= start_angle_sign try: sweep_angle = math.degrees( angle([(rotated_x1 - ccx) / rx, (rotated_y1 - ccy) / ry], [(-rotated_x1 - ccx) / rx, (-rotated_y1 - ccy) / ry])) except ValueError: sweep_angle = 180.0 sweep_angle_sign = (rotated_x1 - ccx) / rx * (-rotated_y1 - ccy) / ry - \ (rotated_y1 - ccy) / ry * (-rotated_x1 - ccx) / rx if sweep_angle_sign == 0: sweep_angle_sign = 1.0 sweep_angle_sign /= abs(sweep_angle_sign) sweep_angle *= sweep_angle_sign if sweep_flag == 0 and sweep_angle > 0: sweep_angle -= 360 elif sweep_flag == 1 and sweep_angle < 0: sweep_angle += 360 rect = QRectF(0, 0, rx * 2, ry * 2) rect.moveCenter(QPointF(cx, cy)) return (start_angle, sweep_angle, rect)
def __init__(self, chart): super().__init__(chart) self.m_chart = chart self.m_text = "" self.m_textRect = QRectF() self.m_rect = QRectF() self.m_anchor = QPointF() self.m_font = QFont()
def _move_line_to_center(self, new_pos): if not self._line: return x_offset = self._rect.x() - self._rect.width() / 2 y_offset = self._rect.y() - self._rect.height() / 2 new_center_pos = QPointF(new_pos.x() - x_offset, new_pos.y() - y_offset) p1 = new_center_pos if self._is_point1 else self._line.line().p1() p2 = self._line.line().p2() if self._is_point1 else new_center_pos self._line.setLine(QLineF(p1, p2))
def drawBackground(self, painter, rect): """ Override draw background method to write out image as background """ painter.setRenderHint(QPainter.Antialiasing) painter.fillRect(rect, self._color) painter.setPen(QPen(Qt.black, 0.5, Qt.DashLine)) painter.setBrush(QColor(67, 255, 163)) if self._use_bg_image and not self._pixmap.isNull(): painter.drawPixmap(QPointF(0, 0), self._pixmap)
def paintEvent(self, event): painter = QPainter(self) painter.setRenderHint(QPainter.Antialiasing) painter.setRenderHint(QPainter.HighQualityAntialiasing) gradient = QRadialGradient(self._bounds.center(), self._bounds.width() * 0.5, self._bounds.center()) gradient.setFocalRadius(self._bounds.width() * 0.3) gradient.setCenterRadius(self._bounds.width() * 0.7) gradient.setColorAt(0, Qt.white) gradient.setColorAt(1, Qt.lightGray) painter.setPen(QPen(QBrush(Qt.gray), self._bounds.width() * 0.005)) painter.setBrush(QBrush(gradient)) painter.drawEllipse(self._bounds) painter.setPen(QPen(QBrush(Qt.gray), self._bounds.width() * 0.005)) painter.drawLine( QPointF(self._bounds.left(), self._bounds.center().y()), QPointF(self._bounds.center().x() - self._bounds.width() * 0.35, self._bounds.center().y())) painter.drawLine( QPointF(self._bounds.center().x() + self._bounds.width() * 0.35, self._bounds.center().y()), QPointF(self._bounds.right(), self._bounds.center().y())) painter.drawLine( QPointF(self._bounds.center().x(), self._bounds.top()), QPointF(self._bounds.center().x(), self._bounds.center().y() - self._bounds.width() * 0.35)) painter.drawLine( QPointF(self._bounds.center().x(), self._bounds.center().y() + self._bounds.width() * 0.35), QPointF(self._bounds.center().x(), self._bounds.bottom())) if not self.isEnabled(): return gradient = QRadialGradient(self._knop_bounds.center(), self._knop_bounds.width() * 0.5, self._knop_bounds.center()) gradient.setFocalRadius(self._knop_bounds.width() * 0.2) gradient.setCenterRadius(self._knop_bounds.width() * 0.5) gradient.setColorAt(0, Qt.gray) gradient.setColorAt(1, Qt.darkGray) painter.setPen(QPen(QBrush(Qt.darkGray), self._bounds.width() * 0.005)) painter.setBrush(QBrush(gradient)) painter.drawEllipse(self._knop_bounds)
def create_ellipse_path(data): path = QPainterPath() path.addEllipse(QPointF(data.get('cx'), data.get('cy')), data.get('rx'), data.get('ry')) if 'transform' in data: m = re.match('^matrix\\((.+)\\)$', data.get('transform')) if m: args = map(lambda x: float(x), m.group(1).split()) if len(args) == 6: transform = QTransform(*args) path *= transform return path
def __init__(self, items, images, parent=None): super().__init__(parent) self.informationWindows = [] self.itemTable = QSqlRelationalTableModel(self) self.itemTable.setTable(items) self.itemTable.setRelation(1, QSqlRelation(images, "itemid", "file")) self.itemTable.select() self.scene = QGraphicsScene(self) self.scene.setSceneRect(0, 0, 465, 365) self.setScene(self.scene) self.addItems() self.setMinimumSize(470, 370) self.setMaximumSize(470, 370) gradient = QLinearGradient(QPointF(0, 0), QPointF(0, 370)) gradient.setColorAt(0, QColor("#868482")) gradient.setColorAt(1, QColor("#5d5b59")) self.setBackgroundBrush(gradient)
def mousePressEvent(self, event): self._prev_value = self.value() self._start_drag_pos = event.pos() if event.button() == Qt.MidButton: if not self._draggers: self._draggers = SliderDraggers(parent=self, is_float=self._is_float, dragger_steps=self._dragger_steps) self._draggers.increment.connect(self.valueIncremented.emit) self._draggers.show() if self._is_float: self._draggers.move( self.mapToGlobal(QPoint(event.pos().x() - 1, event.pos().y() - self._draggers.height() / 2))) else: draggers_height = self._draggers.height() self._draggers.move( self.mapToGlobal( QPoint(event.pos().x() - 1, event.pos().y() - (self._draggers.height() - draggers_height / 6)))) elif event.button() == self._left_button and event.modifiers() not in \ [Qt.ControlModifier, Qt.ShiftModifier, Qt.ControlModifier | Qt.ShiftModifier]: buttons = Qt.MouseButtons(self._mid_button) mouse_event = QMouseEvent(event.type(), event.pos(), self._mid_button, buttons, event.modifiers()) super(Slider, self).mousePressEvent(mouse_event) elif event.modifiers() in [Qt.ControlModifier, Qt.ShiftModifier, Qt.ControlModifier | Qt.ShiftModifier]: style_slider = QStyleOptionSlider() style_slider.initFrom(self) style_slider.orientation = self.orientation() available = self.style().pixelMetric(QStyle.PM_SliderSpaceAvailable, style_slider, self) x_loc = QStyle.sliderPositionFromValue( self.minimum(), self.maximum(), super(Slider, self).value(), available) buttons = Qt.MouseButtons(self._mid_button) new_pos = QPointF() new_pos.setX(x_loc) mouse_event = QMouseEvent(event.type(), new_pos, self._mid_button, buttons, event.modifiers()) self._start_drag_pos = new_pos self._real_start_drag_pos = event.pos() super(Slider, self).mousePressEvent(mouse_event) self._delta_value = self.value() - self._prev_value self.setValue(self._prev_value) else: super(Slider, self).mousePressEvent(event)
def mouseMoveEvent(self, event): super(CurveNodeItem, self).mouseMoveEvent(event) curve_offset = -self.WIDTH * 0.5 scale_x = min(max(event.scenePos().x(), curve_offset), self.scene().base_size + curve_offset) scale_y = min(max(event.scenePos().y(), curve_offset), self.scene().base_size + curve_offset) if self._lock_x_pos: scale_x = self._lock_x_pos if self._snap is not False: scale_x = round((float(scale_x) / self._snap)) * self._snap scale_y = round((float(scale_y) / self._snap)) * self._snap self._new_pos = QPointF(scale_x, scale_y) self.setPos(self._new_pos) self.scene().update_curve() self.signals.curveUpdated.emit()
def printDocument(self, printer): loop = QEventLoop() result = False def printPreview(success): nonlocal result result = success loop.quit() self.m_page.print(printer, printPreview) loop.exec_() if not result: painter = QPainter() if painter.begin(printer): font = painter.font() font.setPixelSize(20) painter.setFont(font) painter.drawText(QPointF(10, 25), "Could not generate print preview.") painter.end()
def __init__(self, collapsed=False, parent=None): super(ExpandableArrow, self).__init__(parent=parent) self.setMaximumSize(24, 24) self.setMinimumWidth(24) self._arrow = None # Define vertical and horizontal arrow to avoid the deletion of its items during garbage collection # (this gc produces errors when trying to paint the arrow if we do not save them in a local variable self._arrow = None self._vertical = QPolygonF([QPointF(8.0, 7.0), QPointF(13.0, 12.0), QPointF(8.0, 17.0)]) self._horizontal = QPolygonF([QPointF(7.0, 8.0), QPointF(17.0, 8.0), QPointF(12.0, 13.0)]) self.setArrow(bool(collapsed))
def populateScene(self): self.scene = QGraphicsScene() image = QImage(":/qt4logo.png") # Populate scene xx = 0 nitems = 0 for i in range(-11000, 11000, 110): xx += 1 yy = 0 for j in range(-7000, 7000, 70): yy += 1 x = (i + 11000) / 22000.0 y = (j + 7000) / 14000.0 color = QColor( image.pixel(int(image.width() * x), int(image.height() * y))) item = Chip(color, xx, yy) item.setPos(QPointF(i, j)) self.scene.addItem(item) nitems += 1
def writeData(self, data): max_size = len(data) resolution = 4 if not self.m_buffer: for i in range(XYSeriesIODevice.sampleCount): self.m_buffer.append(QPointF(i, 0)) start = 0 available_samples = int(max_size) // resolution if available_samples < XYSeriesIODevice.sampleCount: start = XYSeriesIODevice.sampleCount - available_samples for s in range(0, start): self.m_buffer[s].setY(self.m_buffer[s + available_samples].y()) pos = 0 for s in range(start, XYSeriesIODevice.sampleCount): y = (1.0 * (data[pos] - 128)) / 128.0 self.m_buffer[s].setY(y) pos += resolution self.m_series.replace(self.m_buffer) return (XYSeriesIODevice.sampleCount - start) * resolution
def paint(self, painter, option, widget=None): path = QPainterPath() path.addRoundedRect(self.m_rect, 5, 5) anchor = self.mapFromParent(self.m_chart.mapToPosition(self.m_anchor)) if not self.m_rect.contains(anchor): point1 = QPointF() point2 = QPointF() # establish the position of the anchor point in relation to m_rect above = anchor.y() <= self.m_rect.top() aboveCenter = (anchor.y() > self.m_rect.top() and anchor.y() <= self.m_rect.center().y()) belowCenter = (anchor.y() > self.m_rect.center().y() and anchor.y() <= self.m_rect.bottom()) below = anchor.y() > self.m_rect.bottom() onLeft = anchor.x() <= self.m_rect.left() leftOfCenter = (anchor.x() > self.m_rect.left() and anchor.x() <= self.m_rect.center().x()) rightOfCenter = (anchor.x() > self.m_rect.center().x() and anchor.x() <= self.m_rect.right()) onRight = anchor.x() > self.m_rect.right() # get the nearest m_rect corner. x = (onRight + rightOfCenter) * self.m_rect.width() y = (below + belowCenter) * self.m_rect.height() cornerCase = ((above and onLeft) or (above and onRight) or (below and onLeft) or (below and onRight)) vertical = abs(anchor.x() - x) > abs(anchor.y() - y) x1 = (x + leftOfCenter * 10 - rightOfCenter * 20 + cornerCase * int(not vertical) * (onLeft * 10 - onRight * 20)) y1 = (y + aboveCenter * 10 - belowCenter * 20 + cornerCase * int(vertical) * (above * 10 - below * 20)) point1.setX(x1) point1.setY(y1) x2 = (x + leftOfCenter * 20 - rightOfCenter * 10 + cornerCase * int(not vertical) * (onLeft * 20 - onRight * 10)) y2 = (y + aboveCenter * 20 - belowCenter * 10 + cornerCase * int(vertical) * (above * 20 - below * 10)) point2.setX(x2) point2.setY(y2) path.moveTo(point1) path.lineTo(anchor) path.lineTo(point2) path = path.simplified() painter.setBrush(QColor(255, 255, 255)) painter.drawPath(path) painter.drawText(self.m_textRect, self.m_text)