def __init__(self, numberProvider, constr, on_corner_change, sizeProvider, corner=RectAnchors.LeftTop, style=None): NumberItem.__init__(self, numberProvider, constr, style) self.corner = corner self._change_trigger_ = on_corner_change self.scaleFactor = 1.0 self.sizeProvider = sizeProvider self.dragCount = QPointF()
def setRect(self, x, y, w, h): # if self.x == x and self.y == y and self.width == w and self.height == h: # return if w <= 0 or h <= 0: return min = self.constr(QPointF(x, y)) max = self.constr(QPointF(x + w, y + h)) self.size = QSizeF(max.x() - min.x(), max.y() - min.y()) self.arrows[0].setPos(0, self.height / 2) self.arrows[1].setPos(self.width, self.height / 2) self.arrows[2].setPos(self.width / 2, 0) self.arrows[3].setPos(self.width / 2, self.height) self.posHandle.setPos(self.size.width() / 2, self.size.height() / 2) x = min.x() y = min.y() # #force redraw by chaning position # if self.x == x and self.y == y: # self.setPos(x-1, y-1) # self.setPos(x, y) # else: self.setPos(x, y) self._properties_.changed.emit()
def applyDrag(self, items): if self.dragging and self.dir: bigDelta = self.pos - self.start delta = self.pos - self.posPrev delta = QPointF(delta.x(), 0) if self.dir == 1 else QPointF( 0, delta.y()) for item in self.dragging: item.dragMove(delta, bigDelta)
def dragMove(self, delta, totalDelta): d = delta if self.dragCount.x() == self.dragCount.y(): horizontal = abs(d.x()) > abs(d.y()) else: horizontal = abs(self.dragCount.x()) > abs(self.dragCount.y()) self.dragCount = self.dragCount + d drag = self.dragCount.x() if horizontal else self.dragCount.y() corner = None # treshhold = AnchoredNumberItem.dragStrength * self.scaleFactor #abs(totalDelta.x()) if horizontal else abs(totalDelta.y()) # # treshhold = treshhold * AnchoredNumberItem.dragStrength treshold = self.sizeProvider().width()/2 if horizontal else self.sizeProvider().height()/2 # print drag, treshold if horizontal and drag > treshold: corner = RectAnchors.toRight(self.corner) self.dragCount = QPointF() elif horizontal and drag < -treshold: corner = RectAnchors.toLeft(self.corner) self.dragCount = QPointF() elif not horizontal and drag > treshold: corner = RectAnchors.toBottom(self.corner) self.dragCount = QPointF() elif not horizontal and drag < -treshold: corner = RectAnchors.toUp(self.corner) self.dragCount = QPointF() if corner is not None: self.corner = corner # print "Corner changed", corner self._change_trigger_()
def __init__(self, imageUrl=None): QGraphicsScene.__init__(self) self.imgItem = None self.gridLines = [] self.gridXShift = 0 self.gridYShift = 0 self.posConstraint = PosConstraint(0) self.pressPos = None self.draggingItems = dict() self.recentPos = QPointF() if imageUrl: self.setImage(imageUrl) self.userItems = [] self.dragStyles = [DefaultDrag(), DuplicateDrag(self)] self.activeDragStyle = None self.dragItems = [] self._is_pick_enabled_ = True self.editedItem = None self.pressEvent = False self.dragStarted = False
def __init__(self, numberProvider, constr, style=None): ItemBase.__init__(self, style if style else makeStyle()) self.setFlag(QGraphicsItem.ItemIsSelectable, True) self.setFlag(QGraphicsItem.ItemSendsGeometryChanges, True) self.number = numberProvider.nextNumber() self.numberProvider = numberProvider self.size = 30 self.fontSize = 14 self.margin = 0 self.constr = constr self.startPoint = QPointF()
def __init__(self, initialPointProvider, on_drag, w=12, h=12, style=None): ItemBase.__init__(self, style if style else makeHandleStyle()) self.w = w self.h = h self.initProvider = initialPointProvider self.on_drag = on_drag self.setFlag(QGraphicsItem.ItemIsSelectable, True) # self.setFlag(QGraphicsItem.ITem) self.setAcceptHoverEvents(True) self.startPoint = QPointF() self.translation = QTransform() self.scale = QTransform()
class AnchoredNumberItem(NumberItem): dragStrength = 3 def __init__(self, numberProvider, constr, on_corner_change, sizeProvider, corner=RectAnchors.LeftTop, style=None): NumberItem.__init__(self, numberProvider, constr, style) self.corner = corner self._change_trigger_ = on_corner_change self.scaleFactor = 1.0 self.sizeProvider = sizeProvider self.dragCount = QPointF() def setSizeScale(self, scale): self.scaleFactor = 1.0 * scale def dragStart(self, startPoint): print 'clearing drag value' self.dragCount = QPointF() def dragMove(self, delta, totalDelta): d = delta if self.dragCount.x() == self.dragCount.y(): horizontal = abs(d.x()) > abs(d.y()) else: horizontal = abs(self.dragCount.x()) > abs(self.dragCount.y()) self.dragCount = self.dragCount + d drag = self.dragCount.x() if horizontal else self.dragCount.y() corner = None # treshhold = AnchoredNumberItem.dragStrength * self.scaleFactor #abs(totalDelta.x()) if horizontal else abs(totalDelta.y()) # # treshhold = treshhold * AnchoredNumberItem.dragStrength treshold = self.sizeProvider().width()/2 if horizontal else self.sizeProvider().height()/2 # print drag, treshold if horizontal and drag > treshold: corner = RectAnchors.toRight(self.corner) self.dragCount = QPointF() elif horizontal and drag < -treshold: corner = RectAnchors.toLeft(self.corner) self.dragCount = QPointF() elif not horizontal and drag > treshold: corner = RectAnchors.toBottom(self.corner) self.dragCount = QPointF() elif not horizontal and drag < -treshold: corner = RectAnchors.toUp(self.corner) self.dragCount = QPointF() if corner is not None: self.corner = corner # print "Corner changed", corner self._change_trigger_()
def positionOnRect(rect, anchor): if anchor == RectAnchors.LeftTop: return QPointF(rect.x(), rect.y()) elif anchor == RectAnchors.CenterTop: return QPointF(rect.x()+rect.width()/2, rect.y()) elif anchor == RectAnchors.RightTop: return QPointF(rect.x()+rect.width(), rect.y()) elif anchor == RectAnchors.RightCenter: return QPointF(rect.x()+rect.width(), rect.y()+rect.height()/2) elif anchor == RectAnchors.RightBottom: return QPointF(rect.x()+rect.width(), rect.y()+rect.height()) elif anchor == RectAnchors.CenterBottom: return QPointF(rect.x()+rect.width()/2, rect.y()+rect.height()) elif anchor == RectAnchors.LeftBottom: return QPointF(rect.x(), rect.y()+rect.height()) elif anchor == RectAnchors.LeftCenter: return QPointF(rect.x(), rect.y()+rect.height()/2) raise Exception('Anchor value should be 0-7')
def outDir(anchor): import numpy as np if anchor == RectAnchors.LeftTop: return QPointF(-np.sqrt(2)/2, -np.sqrt(2)/2) elif anchor == RectAnchors.CenterTop: return QPointF(0, -1) elif anchor == RectAnchors.RightTop: return QPointF(np.sqrt(2)/2, -np.sqrt(2)/2) elif anchor == RectAnchors.RightCenter: return QPointF(1, 0) elif anchor == RectAnchors.RightBottom: return QPointF(np.sqrt(2)/2, np.sqrt(2)/2) elif anchor == RectAnchors.CenterBottom: return QPointF(0, 1) elif anchor == RectAnchors.LeftBottom: return QPointF(-np.sqrt(2)/2, np.sqrt(2)/2) elif anchor == RectAnchors.LeftCenter: return QPointF(-1, 0) raise Exception('Anchor value should be 0-7')
def dragStart(self, startPoint): print 'clearing drag value' self.dragCount = QPointF()
def __init__(self): self.start = QPointF() self.pos = QPointF() self.posPrev = QPointF() self.startItemsPosition = dict()
def __call__(self, pnt): if self.spacing == 0: return pnt return QPointF(self.round(pnt.x(), self.xShift), self.round(pnt.y(), self.yShift))