def mousePressEvent(self, event): #print "FixedRatioRectItem_cutom::mousePressEvent" if event.button() & Qt.RightButton != 0: w=self._rect.width() h=self._rect.height() centre_x=self._rect.center().x() centre_y=self._rect.center().y() ratio=self._rect.width()/self._rect.height() self._resize = True self._resize_start = event.scenePos() self._resize_start_rect = QRectF(self._rect) self._upper_half_clicked = (event.scenePos().y() < self._resize_start_rect.center().y()) self._left_half_clicked = (event.scenePos().x() < self._resize_start_rect.center().x()) event.accept() #new_w=self._resize_start_rect.width() #new_h=new_w/ratio #x = centre_x - new_w/2 #y = centre_y - new_h/2 #rect = QRectF(QPointF(x,y), QSizeF(new_w, new_h)).normalized() #self._updateRect(rect) #self.updateModel() #event.accept() else: BaseItem.mousePressEvent(self, event)
def mouseMoveEvent(self, event): #print "FixedRatioRectItem_cutom::mouseMoveEvent" if self._resize: diff = event.scenePos() - self._resize_start ratio=self._rect.width()/self._rect.height() centre_x=self._rect.center().x() centre_y=self._rect.center().y() changed=False if self._left_half_clicked: new_w = self._resize_start_rect.width() - diff.x() if (new_w>10): w = new_w h = w/ratio changed=True else: new_w = self._resize_start_rect.width() + diff.x() if (new_w>10): w = new_w h = w/ratio changed=True if self._upper_half_clicked: new_h=self._resize_start_rect.height() - diff.y() if (new_h>10): h = new_h w = ratio*h changed=True else: new_h=self._resize_start_rect.height() + diff.y() if (new_h>10): h = new_h w = ratio*h changed=True if (changed): x = centre_x - w/2 y = centre_y - h/2 if (x+w<=self._max_w and y+h<=self._max_h and x>=0 and y>=0): rect = QRectF(QPointF(x,y), QSizeF(w, h)).normalized() self._updateRect(rect) self.updateModel() event.accept() else: currentRect=self._rect BaseItem.mouseMoveEvent(self, event) accepted=True if (self._rect.x()<0 or self._rect.y()<0): accepted=False elif (self._rect.x()+self._rect.width()>self._max_w or self._rect.y()+self._rect.height()>self._max_h): accepted=False if not(accepted): self._updateRect(currentRect) self.updateModel() event.accept()
def __init__(self, model_item=None, prefix="", parent=None, color=Qt.black, label="", radius=5): BaseItem.__init__(self, model_item, prefix, parent) self._point = None self._radius=radius self._userColor=color self._userLabel=label self.setColor(self._userColor) self.setRadius(self._radius) self._text_item.setHtml(self._userLabel) self.updatePoint()
def keyPressEvent(self, event): BaseItem.keyPressEvent(self, event) step = 1 if event.modifiers() & Qt.ShiftModifier: step = 5 ds = {Qt.Key_Left: (-step, 0), Qt.Key_Right: (step, 0), Qt.Key_Up: (0, -step), Qt.Key_Down: (0, step) }.get(event.key(), None) if ds is not None: self.moveBy(*ds) event.accept()
def paint(self, painter, option, widget=None): BaseItem.paint(self, painter, option, widget) pen = self.pen() if self.isSelected(): pen.setStyle(Qt.DashLine) painter.setPen(pen) # bb=self.boundingRect() # painter.drawEllipse(bb) # painter.drawRect(bb) r = self._radius r_4=r/4 r_2=2*r_4 hr=QRectF(-r, -r_4, 2 * r, r_2) vr=QRectF(-r_4, -r, r_2, 2 * r) # print r,hr,vr painter.drawRect(hr) painter.drawRect(vr)
def keyPressEvent(self, event): BaseItem.keyPressEvent(self, event) step = 1 if event.modifiers() & Qt.ShiftModifier: step = 5 ds = {Qt.Key_Left: (-step, 0), Qt.Key_Right: (step, 0), Qt.Key_Up: (0, -step), Qt.Key_Down: (0, step), }.get(event.key(), None) if ds is not None: if event.modifiers() & Qt.ControlModifier: centre_x=self._rect.center().x()#self._rect.x()+self._rect.width()/2 centre_y=self._rect.center().y()#self._rect.y()+self._rect.height()/2 ratio=self._rect.width()/self._rect.height() changed=False if (ds[0]==0): # change of height newh=self._rect.height() + ds[1]*-2 if (newh>10): h = newh w = ratio*h changed=True else: # change of width neww=self._rect.width() + ds[0]*2 if (neww>10): w = neww h = w/ratio changed=True if (changed): x = centre_x - w/2 y = centre_y - h/2 if (x>=0 and y>=0 and x+w<=self._max_w and y+h<=self._max_h): rect = QRectF(QPointF(x,y), QSizeF(w, h)).normalized() self._updateRect(rect) else: rect = self._rect.adjusted(*(ds + ds)) if (rect.x()>=0 and rect.y()>=0 and rect.x()+rect.width()<=self._max_w and rect.y()+rect.height()<=self._max_h): self._updateRect(rect) self.updateModel() event.accept()