1 Replies - 166 Views - Last Post: 19 February 2019 - 08:15 AM Rate Topic: -----

#1 trispade   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 20-February 13

PyQt, need someone to check my code in my resizer and offer suggestion

Posted 19 February 2019 - 07:59 AM

The code bellow is a resizer class which is responsible for placing a resizing handle and handling scaling for Regions of interest (QGraphicsRectItems) in other parts of my code. It is based on Resizer. Right now my code is almost working correctly. The issue I am having is that resizing allows the ROI to slightly go out of my Scene Rectangle.

I believe the problem lies in the else statements. I can't figure out how to move the resizer back into the scene rect and then change the new_pos to be that value

Problem Area:
            if self.scenePos().x()+offset.x() < scene_rect.right():
                new_pos.setX(
                    min(scene_rect.right() - self.boundingRect().right()/2,
                        max(new_pos.x(), self.boundingRect().left())))
            else:
                new_pos.setX(min(self.pos().x(), new_pos.x()))

            if self.scenePos().y()+offset.y() < scene_rect.bottom():
                new_pos.setY(
                    min(scene_rect.bottom() - self.boundingRect().bottom() / 2,
                        max(new_pos.y(), self.boundingRect().top())))
            else:
                new_pos.setY(min(self.pos().y(), new_pos.y()))





from PyQt5.QtWidgets import QGraphicsItem, QGraphicsObject
from PyQt5.QtCore import pyqtSignal, QPointF, QRectF, Qt
from PyQt5.QtGui import QPen, QPainter
from helpers.pylog import PyLog


class Resizer(QGraphicsObject):

    resizeSignal = pyqtSignal(QPointF)

    def __init__(self, rect=QRectF(0, 0, 10, 10), parent=None):
        super(Resizer, self).__init__(parent)
        self.setParentItem(parent)
        self.__log = PyLog.instance().log
        self.setFlag(QGraphicsItem.ItemIsMovable, True)
        self.setFlag(QGraphicsItem.ItemIsSelectable, True)
        self.setFlag(QGraphicsItem.ItemSendsGeometryChanges, True)
        self.setCursor(Qt.SizeFDiagCursor)
        self.rect = rect
        self.hide()

    def boundingRect(self):
        return self.rect

    def paint(self, painter, option, widget=None):
        pen = QPen(0)
        pen.setStyle(Qt.SolidLine)
        pen.setColor(Qt.red)
        painter.setPen(pen)
        painter.setBrush(Qt.red)
        painter.setRenderHint(QPainter.Antialiasing, True)
        painter.drawEllipse(self.rect)
        self.update()

    def itemChange(self, change, value):
        if change == QGraphicsItem.ItemPositionchange and self.isSelected() and self.scene():
            scene_rect = self.scene().sceneRect()
            new_pos = value
            offset = QPointF(self.boundingRect().right()/2, self.boundingRect().bottom()/2)

            if self.scenePos().x()+offset.x() < scene_rect.right():
                new_pos.setX(
                    min(scene_rect.right() - self.boundingRect().right()/2,
                        max(new_pos.x(), self.boundingRect().left())))
            else:
                new_pos.setX(min(self.pos().x(), new_pos.x()))

            if self.scenePos().y()+offset.y() < scene_rect.bottom():
                new_pos.setY(
                    min(scene_rect.bottom() - self.boundingRect().bottom() / 2,
                        max(new_pos.y(), self.boundingRect().top())))
            else:
                new_pos.setY(min(self.pos().y(), new_pos.y()))

            self.resizeSignal.emit(value - self.pos())
            return new_pos
        return value



Is This A Good Question/Topic? 0
  • +

Replies To: PyQt, need someone to check my code in my resizer and offer suggestion

#2 trispade   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 20-February 13

Re: PyQt, need someone to check my code in my resizer and offer suggestion

Posted 19 February 2019 - 08:15 AM

Here is an image of the problem:
Attached Image

What it should look like:
Attached Image

This post has been edited by trispade: 19 February 2019 - 08:20 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1