def is_overlap(rect1: QRectF, rect2: QRectF): if rect1.x() + rect1.width() > rect2.x() and\ rect2.x() + rect2.width() > rect1.x() and\ rect1.y() + rect1.height() > rect2.y() and\ rect2.y() + rect2.height() > rect1.y(): return True else: return False
def updateBoundingRect(self): margin = self.keySize * 0.5 boundingRect = QRectF(0, 0, 0, 0) for key in self.keyList: rect = key.shape().boundingRect() rect.translate(key.pos()) boundingRect = boundingRect.united(rect) boundingRect.setX(boundingRect.x() - margin) boundingRect.setY(boundingRect.y() - margin) boundingRect.setWidth(boundingRect.width() + margin) boundingRect.setHeight(boundingRect.height() + margin) self.keyBoundRect = boundingRect
def setPixmap(self, pixmap): '''Set the array to be viewed. Args: array (numpy array): the array to be viewed This will remove the previous array but maintain the previous scaling as well as the panned position. ''' self.pixmap = pixmap self.pixmapItem.setPixmap(self.pixmap) # Constrain scene to be the boundary of the pixmap pad = 5 r = self.pixmapItem.boundingRect() r = QRectF(r.left()-pad,r.top()-pad,r.width()+2*pad,r.height()+2*pad) self.setSceneRect(r)
class EmbDetailsDialog(QDialog): """ Subclass of `QDialog`_ Class implementing the Details dialog. .. sphinx_generate_methods_summary:: EmbDetailsDialog """ def __init__(self, theScene, parent=None): """ Default class constructor. :param `theScene`: TOWRITE :type `theScene`: `QGraphicsScene`_ :param `parent`: Pointer to a parent widget instance. :type `parent`: `QWidget`_ """ super(EmbDetailsDialog, self).__init__(parent) self.setMinimumSize(750, 550) self.getInfo() self.mainWidget = self.createMainWidget() self.buttonBox = QDialogButtonBox(QDialogButtonBox.Ok) self.buttonBox.accepted.connect(self.accept) vboxLayoutMain = QVBoxLayout(self) vboxLayoutMain.addWidget(self.mainWidget) vboxLayoutMain.addWidget(self.buttonBox) self.setLayout(vboxLayoutMain) self.setWindowTitle(self.tr("Embroidery Design Details")) QApplication.setOverrideCursor(Qt.ArrowCursor) self.stitchesTotal = int() self.stitchesReal = int() self.stitchesJump = int() self.stitchesTrim = int() self.colorTotal = int self.colorChanges = int() self.boundingRect = QRectF() def __del__(self): """Class destructor""" QApplication.restoreOverrideCursor() def getInfo(self): """TOWRITE""" # TODO: generate a temporary pattern from the scene data. # TODO: grab this information from the pattern self.stitchesTotal = 5 # TODO: embStitchList_count(pattern->stitchList, TOTAL) self.stitchesReal = 4 # TODO: embStitchList_count(pattern->stitchList, NORMAL) self.stitchesJump = 3 # TODO: embStitchList_count(pattern->stitchList, JUMP) self.stitchesTrim = 2 # TODO: embStitchList_count(pattern->stitchList, TRIM) self.colorTotal = 1 # TODO: embThreadList_count(pattern->threadList, TOTAL) self.colorChanges = 0 # TODO: embThreadList_count(pattern->threadList, CHANGES) self.boundingRect.setRect(0, 0, 50, 100) # TODO: embPattern_calcBoundingBox(pattern) def createMainWidget(self): """ TOWRITE :return: TOWRITE :rtype: `QScrollArea`_ """ widget = QWidget(self) # Misc groupBoxMisc = QGroupBox(self.tr("General Information"), widget) labelStitchesTotal = QLabel(self.tr("Total Stitches:"), self) labelStitchesReal = QLabel(self.tr("Real Stitches:"), self) labelStitchesJump = QLabel(self.tr("Jump Stitches:"), self) labelStitchesTrim = QLabel(self.tr("Trim Stitches:"), self) labelColorTotal = QLabel(self.tr("Total Colors:"), self) labelColorChanges = QLabel(self.tr("Color Changes:"), self) labelRectLeft = QLabel(self.tr("Left:"), self) labelRectTop = QLabel(self.tr("Top:"), self) labelRectRight = QLabel(self.tr("Right:"), self) labelRectBottom = QLabel(self.tr("Bottom:"), self) labelRectWidth = QLabel(self.tr("Width:"), self) labelRectHeight = QLabel(self.tr("Height:"), self) fieldStitchesTotal = QLabel(u'%s' % (self.stitchesTotal), self) fieldStitchesReal = QLabel(u'%s' % (self.stitchesReal), self) fieldStitchesJump = QLabel(u'%s' % (self.stitchesJump), self) fieldStitchesTrim = QLabel(u'%s' % (self.stitchesTrim), self) fieldColorTotal = QLabel(u'%s' % (self.colorTotal), self) fieldColorChanges = QLabel(u'%s' % (self.colorChanges), self) fieldRectLeft = QLabel(u'%s' % (str(self.boundingRect.left()) + " mm"), self) fieldRectTop = QLabel(u'%s' % (str(self.boundingRect.top()) + " mm"), self) fieldRectRight = QLabel(u'%s' % (str(self.boundingRect.right()) + " mm"), self) fieldRectBottom = QLabel(u'%s' % (str(self.boundingRect.bottom()) + " mm"), self) fieldRectWidth = QLabel(u'%s' % (str(self.boundingRect.width()) + " mm"), self) fieldRectHeight = QLabel(u'%s' % (str(self.boundingRect.height()) + " mm"), self) gridLayoutMisc = QGridLayout(groupBoxMisc) gridLayoutMisc.addWidget(labelStitchesTotal, 0, 0, Qt.AlignLeft) gridLayoutMisc.addWidget(labelStitchesReal, 1, 0, Qt.AlignLeft) gridLayoutMisc.addWidget(labelStitchesJump, 2, 0, Qt.AlignLeft) gridLayoutMisc.addWidget(labelStitchesTrim, 3, 0, Qt.AlignLeft) gridLayoutMisc.addWidget(labelColorTotal, 4, 0, Qt.AlignLeft) gridLayoutMisc.addWidget(labelColorChanges, 5, 0, Qt.AlignLeft) gridLayoutMisc.addWidget(labelRectLeft, 6, 0, Qt.AlignLeft) gridLayoutMisc.addWidget(labelRectTop, 7, 0, Qt.AlignLeft) gridLayoutMisc.addWidget(labelRectRight, 8, 0, Qt.AlignLeft) gridLayoutMisc.addWidget(labelRectBottom, 9, 0, Qt.AlignLeft) gridLayoutMisc.addWidget(labelRectWidth, 10, 0, Qt.AlignLeft) gridLayoutMisc.addWidget(labelRectHeight, 11, 0, Qt.AlignLeft) gridLayoutMisc.addWidget(fieldStitchesTotal, 0, 1, Qt.AlignLeft) gridLayoutMisc.addWidget(fieldStitchesReal, 1, 1, Qt.AlignLeft) gridLayoutMisc.addWidget(fieldStitchesJump, 2, 1, Qt.AlignLeft) gridLayoutMisc.addWidget(fieldStitchesTrim, 3, 1, Qt.AlignLeft) gridLayoutMisc.addWidget(fieldColorTotal, 4, 1, Qt.AlignLeft) gridLayoutMisc.addWidget(fieldColorChanges, 5, 1, Qt.AlignLeft) gridLayoutMisc.addWidget(fieldRectLeft, 6, 1, Qt.AlignLeft) gridLayoutMisc.addWidget(fieldRectTop, 7, 1, Qt.AlignLeft) gridLayoutMisc.addWidget(fieldRectRight, 8, 1, Qt.AlignLeft) gridLayoutMisc.addWidget(fieldRectBottom, 9, 1, Qt.AlignLeft) gridLayoutMisc.addWidget(fieldRectWidth, 10, 1, Qt.AlignLeft) gridLayoutMisc.addWidget(fieldRectHeight, 11, 1, Qt.AlignLeft) gridLayoutMisc.setColumnStretch(1,1) groupBoxMisc.setLayout(gridLayoutMisc) # TODO: Color Histogram # Stitch Distribution # groupBoxDist = QGroupBox(self.tr("Stitch Distribution"), widget) # TODO: Stitch Distribution Histogram # Widget Layout vboxLayoutMain = QVBoxLayout(widget) vboxLayoutMain.addWidget(groupBoxMisc) # vboxLayoutMain.addWidget(groupBoxDist) vboxLayoutMain.addStretch(1) widget.setLayout(vboxLayoutMain) scrollArea = QScrollArea(self) scrollArea.setWidgetResizable(True) scrollArea.setWidget(widget) return scrollArea
class EmbDetailsDialog(QDialog): """ Subclass of `QDialog`_ Class implementing the Details dialog. .. sphinx_generate_methods_summary:: EmbDetailsDialog """ def __init__(self, theScene, parent=None): """ Default class constructor. :param `theScene`: TOWRITE :type `theScene`: `QGraphicsScene`_ :param `parent`: Pointer to a parent widget instance. :type `parent`: `QWidget`_ """ super(EmbDetailsDialog, self).__init__(parent) self.setMinimumSize(750, 550) self.getInfo() self.mainWidget = self.createMainWidget() self.buttonBox = QDialogButtonBox(QDialogButtonBox.Ok) self.buttonBox.accepted.connect(self.accept) vboxLayoutMain = QVBoxLayout(self) vboxLayoutMain.addWidget(self.mainWidget) vboxLayoutMain.addWidget(self.buttonBox) self.setLayout(vboxLayoutMain) self.setWindowTitle(self.tr("Embroidery Design Details")) QApplication.setOverrideCursor(Qt.ArrowCursor) self.stitchesTotal = int() self.stitchesReal = int() self.stitchesJump = int() self.stitchesTrim = int() self.colorTotal = int self.colorChanges = int() self.boundingRect = QRectF() def __del__(self): """Class destructor""" QApplication.restoreOverrideCursor() def getInfo(self): """TOWRITE""" # TODO: generate a temporary pattern from the scene data. # TODO: grab this information from the pattern self.stitchesTotal = 5 # TODO: embStitchList_count(pattern->stitchList, TOTAL) self.stitchesReal = 4 # TODO: embStitchList_count(pattern->stitchList, NORMAL) self.stitchesJump = 3 # TODO: embStitchList_count(pattern->stitchList, JUMP) self.stitchesTrim = 2 # TODO: embStitchList_count(pattern->stitchList, TRIM) self.colorTotal = 1 # TODO: embThreadList_count(pattern->threadList, TOTAL) self.colorChanges = 0 # TODO: embThreadList_count(pattern->threadList, CHANGES) self.boundingRect.setRect( 0, 0, 50, 100) # TODO: embPattern_calcBoundingBox(pattern) def createMainWidget(self): """ TOWRITE :return: TOWRITE :rtype: `QScrollArea`_ """ widget = QWidget(self) # Misc groupBoxMisc = QGroupBox(self.tr("General Information"), widget) labelStitchesTotal = QLabel(self.tr("Total Stitches:"), self) labelStitchesReal = QLabel(self.tr("Real Stitches:"), self) labelStitchesJump = QLabel(self.tr("Jump Stitches:"), self) labelStitchesTrim = QLabel(self.tr("Trim Stitches:"), self) labelColorTotal = QLabel(self.tr("Total Colors:"), self) labelColorChanges = QLabel(self.tr("Color Changes:"), self) labelRectLeft = QLabel(self.tr("Left:"), self) labelRectTop = QLabel(self.tr("Top:"), self) labelRectRight = QLabel(self.tr("Right:"), self) labelRectBottom = QLabel(self.tr("Bottom:"), self) labelRectWidth = QLabel(self.tr("Width:"), self) labelRectHeight = QLabel(self.tr("Height:"), self) fieldStitchesTotal = QLabel(u'%s' % (self.stitchesTotal), self) fieldStitchesReal = QLabel(u'%s' % (self.stitchesReal), self) fieldStitchesJump = QLabel(u'%s' % (self.stitchesJump), self) fieldStitchesTrim = QLabel(u'%s' % (self.stitchesTrim), self) fieldColorTotal = QLabel(u'%s' % (self.colorTotal), self) fieldColorChanges = QLabel(u'%s' % (self.colorChanges), self) fieldRectLeft = QLabel(u'%s' % (str(self.boundingRect.left()) + " mm"), self) fieldRectTop = QLabel(u'%s' % (str(self.boundingRect.top()) + " mm"), self) fieldRectRight = QLabel( u'%s' % (str(self.boundingRect.right()) + " mm"), self) fieldRectBottom = QLabel( u'%s' % (str(self.boundingRect.bottom()) + " mm"), self) fieldRectWidth = QLabel( u'%s' % (str(self.boundingRect.width()) + " mm"), self) fieldRectHeight = QLabel( u'%s' % (str(self.boundingRect.height()) + " mm"), self) gridLayoutMisc = QGridLayout(groupBoxMisc) gridLayoutMisc.addWidget(labelStitchesTotal, 0, 0, Qt.AlignLeft) gridLayoutMisc.addWidget(labelStitchesReal, 1, 0, Qt.AlignLeft) gridLayoutMisc.addWidget(labelStitchesJump, 2, 0, Qt.AlignLeft) gridLayoutMisc.addWidget(labelStitchesTrim, 3, 0, Qt.AlignLeft) gridLayoutMisc.addWidget(labelColorTotal, 4, 0, Qt.AlignLeft) gridLayoutMisc.addWidget(labelColorChanges, 5, 0, Qt.AlignLeft) gridLayoutMisc.addWidget(labelRectLeft, 6, 0, Qt.AlignLeft) gridLayoutMisc.addWidget(labelRectTop, 7, 0, Qt.AlignLeft) gridLayoutMisc.addWidget(labelRectRight, 8, 0, Qt.AlignLeft) gridLayoutMisc.addWidget(labelRectBottom, 9, 0, Qt.AlignLeft) gridLayoutMisc.addWidget(labelRectWidth, 10, 0, Qt.AlignLeft) gridLayoutMisc.addWidget(labelRectHeight, 11, 0, Qt.AlignLeft) gridLayoutMisc.addWidget(fieldStitchesTotal, 0, 1, Qt.AlignLeft) gridLayoutMisc.addWidget(fieldStitchesReal, 1, 1, Qt.AlignLeft) gridLayoutMisc.addWidget(fieldStitchesJump, 2, 1, Qt.AlignLeft) gridLayoutMisc.addWidget(fieldStitchesTrim, 3, 1, Qt.AlignLeft) gridLayoutMisc.addWidget(fieldColorTotal, 4, 1, Qt.AlignLeft) gridLayoutMisc.addWidget(fieldColorChanges, 5, 1, Qt.AlignLeft) gridLayoutMisc.addWidget(fieldRectLeft, 6, 1, Qt.AlignLeft) gridLayoutMisc.addWidget(fieldRectTop, 7, 1, Qt.AlignLeft) gridLayoutMisc.addWidget(fieldRectRight, 8, 1, Qt.AlignLeft) gridLayoutMisc.addWidget(fieldRectBottom, 9, 1, Qt.AlignLeft) gridLayoutMisc.addWidget(fieldRectWidth, 10, 1, Qt.AlignLeft) gridLayoutMisc.addWidget(fieldRectHeight, 11, 1, Qt.AlignLeft) gridLayoutMisc.setColumnStretch(1, 1) groupBoxMisc.setLayout(gridLayoutMisc) # TODO: Color Histogram # Stitch Distribution # groupBoxDist = QGroupBox(self.tr("Stitch Distribution"), widget) # TODO: Stitch Distribution Histogram # Widget Layout vboxLayoutMain = QVBoxLayout(widget) vboxLayoutMain.addWidget(groupBoxMisc) # vboxLayoutMain.addWidget(groupBoxDist) vboxLayoutMain.addStretch(1) widget.setLayout(vboxLayoutMain) scrollArea = QScrollArea(self) scrollArea.setWidgetResizable(True) scrollArea.setWidget(widget) return scrollArea
class Axis(): # Rect holds the bounding box for the view of the graph rect = None # A list of the objects that axes has in the scene sceneObjects = None # Determines whether the axis text is drawn axisTextEnabled = True # Tick marks numXAxisMajor = 5 numYAxisMajor = 5 # Creates and draws the axes objects for the current view # Initially zoomed on a 2x2 view centered at 0,0 def __init__(self,graphicsView): self.scene = graphicsView.scene() self.gView = graphicsView self.rect = Rect(-1,1,2,2) self.update() # Updates the transform so the axes draw the given rect def setAxisView(self, rect): self.rect = rect self.update() # Should be called any time the view is scaled or updated def update(self): # Update the scaling based on the viewport size self.xScale = self.gView.size().width()/self.rect.width() self.yScale = self.gView.size().height()/self.rect.height() if self.sceneObjects is not None: for o in self.sceneObjects: self.scene.removeItem(o) self.sceneObjects = [] r = self.rect # Add the lines # Is x = 0 in the current view? if r.left() < 0 and r.right() > 0: self.sceneObjects.append(self.scene.addLine(0,r.bottom(),0,r.top())) yaLoc = 0 # Otherwise, add the line in the middle of the view else: self.sceneObjects.append(self.scene.addLine(r.center().x(),r.bottom(),r.center().x(),r.top())) yaLoc = r.center().x() # Is y = 0 in the current view? Note that top and bottom are reversed, due to -1 transform if r.top() < 0 and r.bottom() > 0: self.sceneObjects.append(self.scene.addLine(r.left(),0,r.right(),0)) xaLoc = 0 # Otherwise, add the line in the middle of the view else: self.sceneObjects.append(self.scene.addLine(r.left(),r.center().y(),r.right(),r.center().y())) xaLoc = r.center().y() # Draw the tick marks xaMajSpacing = 0 mult = 1.0 while xaMajSpacing == 0: xaMajSpacing = math.floor(mult*r.width()*1.0/(self.numXAxisMajor+1))/mult mult *= 10.0 yaMajSpacing = 0 mult = 1.0 while yaMajSpacing == 0: yaMajSpacing = math.floor(mult*r.height()*1.0/(self.numYAxisMajor+1))/mult mult *= 10.0 x = -xaMajSpacing + yaLoc while x > r.left(): self.sceneObjects.append(self.scene.addLine(x, xaLoc - 5/self.yScale, x, xaLoc + 5/self.yScale)) if self.axisTextEnabled: self.addText(str(x), x, xaLoc - 6/self.yScale, alignTop = True) x -= xaMajSpacing x = xaMajSpacing + yaLoc while x < r.right(): self.sceneObjects.append(self.scene.addLine(x, xaLoc - 5/self.yScale, x, xaLoc + 5/self.yScale)) if self.axisTextEnabled: self.addText(str(x), x, xaLoc - 6/self.yScale, alignTop = True) x += xaMajSpacing y = -yaMajSpacing + xaLoc while y > r.top(): self.sceneObjects.append(self.scene.addLine(-5/self.xScale + yaLoc, y, 5/self.xScale + yaLoc, y)) if self.axisTextEnabled: self.addText(str(y), yaLoc + 7.5/self.xScale, y, alignLeft = True) y -= yaMajSpacing y = yaMajSpacing + xaLoc while y < r.bottom(): self.sceneObjects.append(self.scene.addLine(-5/self.xScale + yaLoc, y, 5/self.xScale + yaLoc, y)) if self.axisTextEnabled: self.addText(str(y), yaLoc + 7.5/self.xScale, y, alignLeft = True) y += yaMajSpacing # Adds text centered at x and y def addText(self, text, x, y, alignLeft = False, alignTop = False): t = self.scene.addText(text) # Undo transform t.setTransform(t.transform().scale(1/self.xScale,-1/self.yScale)) tR = t.sceneBoundingRect() if not alignLeft: xLoc = x - tR.width()/2 else: xLoc = x if not alignTop: yLoc = y + tR.height()/2 else: yLoc = y r = self.rect if xLoc < r.left(): xLoc = r.left() elif xLoc + tR.width() > r.right(): xLoc = r.right() - tR.width() if yLoc < r.top(): yLoc = r.top() elif yLoc - tR.height() > r.bottom(): yLoc = r.bottom() - tR.height() t.setPos(xLoc, yLoc) self.sceneObjects.append(t) # Get the actual pixel coordinates of the given position def getPixelCoords(self, pos): p = [(pos[0] - self.rect.left()) * self.xScale, (pos[1] - self.rect.top()) * self.yScale] return p # Returns the correct transform to use in the GraphWindow QGraphicsView def getTransform(self): self.update() t = QtGui.QTransform(1,0,0,-1,0,0) t.scale(self.xScale,self.yScale) return t # Returns the center of the view (call on the QGraphicsView centerOn) def getCenter(self): return self.rect.center()