def selectRubberband(self, rect: QRect, from_pt: QPointF, to_point: QPointF): """ Args: rect: Description from_pt: Description to_point: Description """ fset = self.manager.document.filter_set if self.FILTER_NAME not in fset: return rbr_last, fp_last, tp_last = self.last_rubberband_vals if rect.isNull() and rbr_last.isValid(): part_item = self.part_item part = part_item.part() # convert and normalize the drag rectangle from_pt_part_item = part_item.mapFromScene(fp_last) to_pt_part_item = part_item.mapFromScene(tp_last) # note QRectF.normalized doesn't seem to actually normalize a # rectangle near as I can tell no we have normaliz from_model_point: Vec3T = part_item.getModelPos(from_pt_part_item) to_model_point: Vec3T = part_item.getModelPos(to_pt_part_item) query_rect = (from_model_point[0], from_model_point[1], to_model_point[0], to_model_point[1]) query_rect = normalizeRect(query_rect) # print("Query rect", query_rect, # query_rect[0] < query_rect[2], query_rect[1] < query_rect[3]) res = part.getVirtualHelicesInArea(query_rect) doc = self.manager.document doc.clearAllSelected() doc.addVirtualHelicesToSelection(part, res) else: self.last_rubberband_vals = (rect, from_pt, to_point)
def selectRubberband(self, rect: QRect, from_pt: QPointF, to_point: QPointF): """ Args: rect: Description from_pt: Description to_point: Description """ fset = self.manager.document.filter_set if self.FILTER_NAME not in fset: return rbr_last, fp_last, tp_last = self.last_rubberband_vals if rect.isNull() and rbr_last.isValid(): part_item = self.part_item part = part_item.part() # convert and normalize the drag rectangle from_pt_part_item = part_item.mapFromScene(fp_last) to_pt_part_item = part_item.mapFromScene(tp_last) # note QRectF.normalized doesn't seem to actually normalize a # rectangle near as I can tell no we have normaliz from_model_point = part_item.getModelPos(from_pt_part_item) to_model_point = part_item.getModelPos(to_pt_part_item) query_rect = (from_model_point[0], from_model_point[1], to_model_point[0], to_model_point[1]) query_rect = normalizeRect(query_rect) # print("Query rect", query_rect, # query_rect[0] < query_rect[2], query_rect[1] < query_rect[3]) res = part.getVirtualHelicesInArea(query_rect) doc = self.manager.document doc.clearAllSelected() doc.addVirtualHelicesToSelection(part, res) else: self.last_rubberband_vals = (rect, from_pt, to_point)
class Convertor: def __init__(self, path, stitches_only=False): self.jef = jef.Pattern(path) self.stitches_only = stitches_only self.rect = QRect() def bounding_rect(self): if not self.rect.isNull(): return self.rect x, y = [], [] for coordinates in self.jef.coordinates: # i = map(lambda (op, i, j): i, coordinates) # j = map(lambda (op, i, j): j, coordinates) i = map(lambda op_i_j: op_i_j[1], coordinates) j = map(lambda op_i_j: op_i_j[2], coordinates) x.append(min(i)) x.append(max(i)) y.append(min(j)) y.append(max(j)) return QRect(min(x), -max(y), max(x) - min(x), max(y) - min(y)) def show(self, painter): painter.save() painter.translate(-self.bounding_rect().topLeft()) i = 0 for i in range(self.jef.threads): color = QColor(*self.jef.color_for_thread(i)) coordinates = self.jef.coordinates[i] if not self.stitches_only: pen = QPen(QColor(200, 200, 200)) painter.setPen(pen) for op, x, y in coordinates: painter.drawEllipse(x - 2, -y - 2, 4, 4) pen = QPen(color) painter.setPen(pen) mx, my = 0, 0 for op, x, y in coordinates: if op == "move": mx, my = x, y elif op == "stitch": painter.drawLine(mx, -my, x, -y) mx, my = x, y i += 1 painter.restore()
class Example(QWidget): def __init__(self): super(Example, self).__init__() self.initUI() def initUI(self): self.setFixedSize(400, 400) self.setWindowTitle('Colours') self.btnPaint = QPushButton("Paint", self) self.btnMove = QPushButton("Move x+1 y+1", self) self.rect = QRect() self.btnPaint.clicked.connect(self.onPaint) self.btnMove.clicked.connect(self.onMove) self.hlayout = QHBoxLayout(self) self.hlayout.addWidget(self.btnPaint) self.hlayout.addWidget(self.btnMove) self.hlayout.addStretch(1) self.show() def onPaint(self): if self.rect.isNull(): self.rect = QRect(10, 15, 80, 45) self.update() def onMove(self): if not self.rect.isNull(): self.rect.translate(QPoint(1, 1)) self.update() def paintEvent(self, e): qp = QPainter(self) qp.setPen(QColor("#d4d4d4")) qp.setBrush(QColor(200, 0, 0)) qp.drawRect(self.rect)
def __grabRect(self): """ Private method to grab the selected rectangle (i.e. do the snapshot). """ if self.__mode == SnapshotRegionGrabber.Ellipse: ell = QRegion(self.__selection, QRegion.Ellipse) if not ell.isEmpty(): self.__grabbing = True xOffset = self.__pixmap.rect().x() - ell.boundingRect().x() yOffset = self.__pixmap.rect().y() - ell.boundingRect().y() translatedEll = ell.translated(xOffset, yOffset) pixmap2 = QPixmap(ell.boundingRect().size()) pixmap2.fill(Qt.transparent) pt = QPainter() pt.begin(pixmap2) if pt.paintEngine().hasFeature(QPaintEngine.PorterDuff): pt.setRenderHints( QPainter.Antialiasing | QPainter.HighQualityAntialiasing | QPainter.SmoothPixmapTransform, True) pt.setBrush(Qt.black) pt.setPen(QPen(QBrush(Qt.black), 0.5)) pt.drawEllipse(translatedEll.boundingRect()) pt.setCompositionMode(QPainter.CompositionMode_SourceIn) else: pt.setClipRegion(translatedEll) pt.setCompositionMode(QPainter.CompositionMode_Source) pt.drawPixmap(pixmap2.rect(), self.__pixmap, ell.boundingRect()) pt.end() self.grabbed.emit(pixmap2) else: r = QRect(self.__selection) if not r.isNull() and r.isValid(): self.__grabbing = True self.grabbed.emit(self.__pixmap.copy(r))
class SnapshotRegionGrabber(QWidget): """ Class implementing a grabber widget for a rectangular snapshot region. @signal grabbed(QPixmap) emitted after the region was grabbed """ grabbed = pyqtSignal(QPixmap) StrokeMask = 0 FillMask = 1 Rectangle = 0 Ellipse = 1 def __init__(self, mode=Rectangle): """ Constructor @param mode region grabber mode (SnapshotRegionGrabber.Rectangle or SnapshotRegionGrabber.Ellipse) """ super(SnapshotRegionGrabber, self).__init__( None, Qt.X11BypassWindowManagerHint | Qt.WindowStaysOnTopHint | Qt.FramelessWindowHint | Qt.Tool) assert mode in [ SnapshotRegionGrabber.Rectangle, SnapshotRegionGrabber.Ellipse ] self.__mode = mode self.__selection = QRect() self.__mouseDown = False self.__newSelection = False self.__handleSize = 10 self.__mouseOverHandle = None self.__showHelp = True self.__grabbing = False self.__dragStartPoint = QPoint() self.__selectionBeforeDrag = QRect() self.__locale = QLocale() # naming conventions for handles # T top, B bottom, R Right, L left # 2 letters: a corner # 1 letter: the handle on the middle of the corresponding side self.__TLHandle = QRect(0, 0, self.__handleSize, self.__handleSize) self.__TRHandle = QRect(0, 0, self.__handleSize, self.__handleSize) self.__BLHandle = QRect(0, 0, self.__handleSize, self.__handleSize) self.__BRHandle = QRect(0, 0, self.__handleSize, self.__handleSize) self.__LHandle = QRect(0, 0, self.__handleSize, self.__handleSize) self.__THandle = QRect(0, 0, self.__handleSize, self.__handleSize) self.__RHandle = QRect(0, 0, self.__handleSize, self.__handleSize) self.__BHandle = QRect(0, 0, self.__handleSize, self.__handleSize) self.__handles = [ self.__TLHandle, self.__TRHandle, self.__BLHandle, self.__BRHandle, self.__LHandle, self.__THandle, self.__RHandle, self.__BHandle ] self.__helpTextRect = QRect() self.__helpText = self.tr( "Select a region using the mouse. To take the snapshot, press" " the Enter key or double click. Press Esc to quit.") self.__pixmap = QPixmap() self.setMouseTracking(True) QTimer.singleShot(200, self.__initialize) def __initialize(self): """ Private slot to initialize the rest of the widget. """ self.__desktop = QApplication.desktop() x = self.__desktop.x() y = self.__desktop.y() if qVersion() >= "5.0.0": self.__pixmap = QApplication.screens()[0].grabWindow( self.__desktop.winId(), x, y, self.__desktop.width(), self.__desktop.height()) else: self.__pixmap = QPixmap.grabWindow(self.__desktop.winId(), x, y, self.__desktop.width(), self.__desktop.height()) self.resize(self.__pixmap.size()) self.move(x, y) self.setCursor(Qt.CrossCursor) self.show() self.grabMouse() self.grabKeyboard() self.activateWindow() def paintEvent(self, evt): """ Protected method handling paint events. @param evt paint event (QPaintEvent) """ if self.__grabbing: # grabWindow() should just get the background return painter = QPainter(self) pal = QPalette(QToolTip.palette()) font = QToolTip.font() handleColor = pal.color(QPalette.Active, QPalette.Highlight) handleColor.setAlpha(160) overlayColor = QColor(0, 0, 0, 160) textColor = pal.color(QPalette.Active, QPalette.Text) textBackgroundColor = pal.color(QPalette.Active, QPalette.Base) painter.drawPixmap(0, 0, self.__pixmap) painter.setFont(font) r = QRect(self.__selection) if not self.__selection.isNull(): grey = QRegion(self.rect()) if self.__mode == SnapshotRegionGrabber.Ellipse: reg = QRegion(r, QRegion.Ellipse) else: reg = QRegion(r) grey = grey.subtracted(reg) painter.setClipRegion(grey) painter.setPen(Qt.NoPen) painter.setBrush(overlayColor) painter.drawRect(self.rect()) painter.setClipRect(self.rect()) drawRect(painter, r, handleColor) if self.__showHelp: painter.setPen(textColor) painter.setBrush(textBackgroundColor) self.__helpTextRect = painter.boundingRect( self.rect().adjusted(2, 2, -2, -2), Qt.TextWordWrap, self.__helpText).translated(-self.__desktop.x(), -self.__desktop.y()) self.__helpTextRect.adjust(-2, -2, 4, 2) drawRect(painter, self.__helpTextRect, textColor, textBackgroundColor) painter.drawText(self.__helpTextRect.adjusted(3, 3, -3, -3), Qt.TextWordWrap, self.__helpText) if self.__selection.isNull(): return # The grabbed region is everything which is covered by the drawn # rectangles (border included). This means that there is no 0px # selection, since a 0px wide rectangle will always be drawn as a line. txt = "{0}, {1} ({2} x {3})".format( self.__locale.toString(self.__selection.x()), self.__locale.toString(self.__selection.y()), self.__locale.toString(self.__selection.width()), self.__locale.toString(self.__selection.height())) textRect = painter.boundingRect(self.rect(), Qt.AlignLeft, txt) boundingRect = textRect.adjusted(-4, 0, 0, 0) if textRect.width() < r.width() - 2 * self.__handleSize and \ textRect.height() < r.height() - 2 * self.__handleSize and \ r.width() > 100 and \ r.height() > 100: # center, unsuitable for small selections boundingRect.moveCenter(r.center()) textRect.moveCenter(r.center()) elif r.y() - 3 > textRect.height() and \ r.x() + textRect.width() < self.rect().width(): # on top, left aligned boundingRect.moveBottomLeft(QPoint(r.x(), r.y() - 3)) textRect.moveBottomLeft(QPoint(r.x() + 2, r.y() - 3)) elif r.x() - 3 > textRect.width(): # left, top aligned boundingRect.moveTopRight(QPoint(r.x() - 3, r.y())) textRect.moveTopRight(QPoint(r.x() - 5, r.y())) elif r.bottom() + 3 + textRect.height() < self.rect().bottom() and \ r.right() > textRect.width(): # at bottom, right aligned boundingRect.moveTopRight(QPoint(r.right(), r.bottom() + 3)) textRect.moveTopRight(QPoint(r.right() - 2, r.bottom() + 3)) elif r.right() + textRect.width() + 3 < self.rect().width(): # right, bottom aligned boundingRect.moveBottomLeft(QPoint(r.right() + 3, r.bottom())) textRect.moveBottomLeft(QPoint(r.right() + 5, r.bottom())) # If the above didn't catch it, you are running on a very # tiny screen... drawRect(painter, boundingRect, textColor, textBackgroundColor) painter.drawText(textRect, Qt.AlignHCenter, txt) if (r.height() > self.__handleSize * 2 and r.width() > self.__handleSize * 2) or \ not self.__mouseDown: self.__updateHandles() painter.setPen(Qt.NoPen) painter.setBrush(handleColor) painter.setClipRegion( self.__handleMask(SnapshotRegionGrabber.StrokeMask)) painter.drawRect(self.rect()) handleColor.setAlpha(60) painter.setBrush(handleColor) painter.setClipRegion( self.__handleMask(SnapshotRegionGrabber.FillMask)) painter.drawRect(self.rect()) def resizeEvent(self, evt): """ Protected method to handle resize events. @param evt resize event (QResizeEvent) """ if self.__selection.isNull(): return r = QRect(self.__selection) r.setTopLeft(self.__limitPointToRect(r.topLeft(), self.rect())) r.setBottomRight(self.__limitPointToRect(r.bottomRight(), self.rect())) if r.width() <= 1 or r.height() <= 1: # This just results in ugly drawing... self.__selection = QRect() else: self.__selection = self.__normalizeSelection(r) def mousePressEvent(self, evt): """ Protected method to handle mouse button presses. @param evt mouse press event (QMouseEvent) """ self.__showHelp = not self.__helpTextRect.contains(evt.pos()) if evt.button() == Qt.LeftButton: self.__mouseDown = True self.__dragStartPoint = evt.pos() self.__selectionBeforeDrag = QRect(self.__selection) if not self.__selection.contains(evt.pos()): self.__newSelection = True self.__selection = QRect() else: self.setCursor(Qt.ClosedHandCursor) elif evt.button() == Qt.RightButton: self.__newSelection = False self.__selection = QRect() self.setCursor(Qt.CrossCursor) self.update() def mouseMoveEvent(self, evt): """ Protected method to handle mouse movements. @param evt mouse move event (QMouseEvent) """ shouldShowHelp = not self.__helpTextRect.contains(evt.pos()) if shouldShowHelp != self.__showHelp: self.__showHelp = shouldShowHelp self.update() if self.__mouseDown: if self.__newSelection: p = evt.pos() r = self.rect() self.__selection = self.__normalizeSelection( QRect(self.__dragStartPoint, self.__limitPointToRect(p, r))) elif self.__mouseOverHandle is None: # moving the whole selection r = self.rect().normalized() s = self.__selectionBeforeDrag.normalized() p = s.topLeft() + evt.pos() - self.__dragStartPoint r.setBottomRight(r.bottomRight() - QPoint(s.width(), s.height()) + QPoint(1, 1)) if not r.isNull() and r.isValid(): self.__selection.moveTo(self.__limitPointToRect(p, r)) else: # dragging a handle r = QRect(self.__selectionBeforeDrag) offset = evt.pos() - self.__dragStartPoint if self.__mouseOverHandle in \ [self.__TLHandle, self.__THandle, self.__TRHandle]: r.setTop(r.top() + offset.y()) if self.__mouseOverHandle in \ [self.__TLHandle, self.__LHandle, self.__BLHandle]: r.setLeft(r.left() + offset.x()) if self.__mouseOverHandle in \ [self.__BLHandle, self.__BHandle, self.__BRHandle]: r.setBottom(r.bottom() + offset.y()) if self.__mouseOverHandle in \ [self.__TRHandle, self.__RHandle, self.__BRHandle]: r.setRight(r.right() + offset.x()) r.setTopLeft(self.__limitPointToRect(r.topLeft(), self.rect())) r.setBottomRight( self.__limitPointToRect(r.bottomRight(), self.rect())) self.__selection = self.__normalizeSelection(r) self.update() else: if self.__selection.isNull(): return found = False for r in self.__handles: if r.contains(evt.pos()): self.__mouseOverHandle = r found = True break if not found: self.__mouseOverHandle = None if self.__selection.contains(evt.pos()): self.setCursor(Qt.OpenHandCursor) else: self.setCursor(Qt.CrossCursor) else: if self.__mouseOverHandle in [ self.__TLHandle, self.__BRHandle ]: self.setCursor(Qt.SizeFDiagCursor) elif self.__mouseOverHandle in [ self.__TRHandle, self.__BLHandle ]: self.setCursor(Qt.SizeBDiagCursor) elif self.__mouseOverHandle in [ self.__LHandle, self.__RHandle ]: self.setCursor(Qt.SizeHorCursor) elif self.__mouseOverHandle in [ self.__THandle, self.__BHandle ]: self.setCursor(Qt.SizeVerCursor) def mouseReleaseEvent(self, evt): """ Protected method to handle mouse button releases. @param evt mouse release event (QMouseEvent) """ self.__mouseDown = False self.__newSelection = False if self.__mouseOverHandle is None and \ self.__selection.contains(evt.pos()): self.setCursor(Qt.OpenHandCursor) self.update() def mouseDoubleClickEvent(self, evt): """ Protected method to handle mouse double clicks. @param evt mouse double click event (QMouseEvent) """ self.__grabRect() def keyPressEvent(self, evt): """ Protected method to handle key presses. @param evt key press event (QKeyEvent) """ if evt.key() == Qt.Key_Escape: self.grabbed.emit(QPixmap()) elif evt.key() in [Qt.Key_Enter, Qt.Key_Return]: self.__grabRect() else: evt.ignore() def __updateHandles(self): """ Private method to update the handles. """ r = QRect(self.__selection) s2 = self.__handleSize // 2 self.__TLHandle.moveTopLeft(r.topLeft()) self.__TRHandle.moveTopRight(r.topRight()) self.__BLHandle.moveBottomLeft(r.bottomLeft()) self.__BRHandle.moveBottomRight(r.bottomRight()) self.__LHandle.moveTopLeft(QPoint(r.x(), r.y() + r.height() // 2 - s2)) self.__THandle.moveTopLeft(QPoint(r.x() + r.width() // 2 - s2, r.y())) self.__RHandle.moveTopRight( QPoint(r.right(), r.y() + r.height() // 2 - s2)) self.__BHandle.moveBottomLeft( QPoint(r.x() + r.width() // 2 - s2, r.bottom())) def __handleMask(self, maskType): """ Private method to calculate the handle mask. @param maskType type of the mask to be used (SnapshotRegionGrabber.FillMask or SnapshotRegionGrabber.StrokeMask) @return calculated mask (QRegion) """ mask = QRegion() for rect in self.__handles: if maskType == SnapshotRegionGrabber.StrokeMask: r = QRegion(rect) mask += r.subtracted(QRegion(rect.adjusted(1, 1, -1, -1))) else: mask += QRegion(rect.adjusted(1, 1, -1, -1)) return mask def __limitPointToRect(self, point, rect): """ Private method to limit the given point to the given rectangle. @param point point to be limited (QPoint) @param rect rectangle the point shall be limited to (QRect) @return limited point (QPoint) """ q = QPoint() if point.x() < rect.x(): q.setX(rect.x()) elif point.x() < rect.right(): q.setX(point.x()) else: q.setX(rect.right()) if point.y() < rect.y(): q.setY(rect.y()) elif point.y() < rect.bottom(): q.setY(point.y()) else: q.setY(rect.bottom()) return q def __normalizeSelection(self, sel): """ Private method to normalize the given selection. @param sel selection to be normalized (QRect) @return normalized selection (QRect) """ rect = QRect(sel) if rect.width() <= 0: left = rect.left() width = rect.width() rect.setLeft(left + width - 1) rect.setRight(left) if rect.height() <= 0: top = rect.top() height = rect.height() rect.setTop(top + height - 1) rect.setBottom(top) return rect def __grabRect(self): """ Private method to grab the selected rectangle (i.e. do the snapshot). """ if self.__mode == SnapshotRegionGrabber.Ellipse: ell = QRegion(self.__selection, QRegion.Ellipse) if not ell.isEmpty(): self.__grabbing = True xOffset = self.__pixmap.rect().x() - ell.boundingRect().x() yOffset = self.__pixmap.rect().y() - ell.boundingRect().y() translatedEll = ell.translated(xOffset, yOffset) pixmap2 = QPixmap(ell.boundingRect().size()) pixmap2.fill(Qt.transparent) pt = QPainter() pt.begin(pixmap2) if pt.paintEngine().hasFeature(QPaintEngine.PorterDuff): pt.setRenderHints( QPainter.Antialiasing | QPainter.HighQualityAntialiasing | QPainter.SmoothPixmapTransform, True) pt.setBrush(Qt.black) pt.setPen(QPen(QBrush(Qt.black), 0.5)) pt.drawEllipse(translatedEll.boundingRect()) pt.setCompositionMode(QPainter.CompositionMode_SourceIn) else: pt.setClipRegion(translatedEll) pt.setCompositionMode(QPainter.CompositionMode_Source) pt.drawPixmap(pixmap2.rect(), self.__pixmap, ell.boundingRect()) pt.end() self.grabbed.emit(pixmap2) else: r = QRect(self.__selection) if not r.isNull() and r.isValid(): self.__grabbing = True self.grabbed.emit(self.__pixmap.copy(r))
class SnapshotRegionGrabber(QWidget): """ Class implementing a grabber widget for a rectangular snapshot region. @signal grabbed(QPixmap) emitted after the region was grabbed """ grabbed = pyqtSignal(QPixmap) StrokeMask = 0 FillMask = 1 Rectangle = 0 Ellipse = 1 def __init__(self, mode=Rectangle): """ Constructor @param mode region grabber mode (SnapshotRegionGrabber.Rectangle or SnapshotRegionGrabber.Ellipse) """ super(SnapshotRegionGrabber, self).__init__( None, Qt.X11BypassWindowManagerHint | Qt.WindowStaysOnTopHint | Qt.FramelessWindowHint | Qt.Tool) assert mode in [SnapshotRegionGrabber.Rectangle, SnapshotRegionGrabber.Ellipse] self.__mode = mode self.__selection = QRect() self.__mouseDown = False self.__newSelection = False self.__handleSize = 10 self.__mouseOverHandle = None self.__showHelp = True self.__grabbing = False self.__dragStartPoint = QPoint() self.__selectionBeforeDrag = QRect() # naming conventions for handles # T top, B bottom, R Right, L left # 2 letters: a corner # 1 letter: the handle on the middle of the corresponding side self.__TLHandle = QRect(0, 0, self.__handleSize, self.__handleSize) self.__TRHandle = QRect(0, 0, self.__handleSize, self.__handleSize) self.__BLHandle = QRect(0, 0, self.__handleSize, self.__handleSize) self.__BRHandle = QRect(0, 0, self.__handleSize, self.__handleSize) self.__LHandle = QRect(0, 0, self.__handleSize, self.__handleSize) self.__THandle = QRect(0, 0, self.__handleSize, self.__handleSize) self.__RHandle = QRect(0, 0, self.__handleSize, self.__handleSize) self.__BHandle = QRect(0, 0, self.__handleSize, self.__handleSize) self.__handles = [self.__TLHandle, self.__TRHandle, self.__BLHandle, self.__BRHandle, self.__LHandle, self.__THandle, self.__RHandle, self.__BHandle] self.__helpTextRect = QRect() self.__helpText = self.tr( "Select a region using the mouse. To take the snapshot, press" " the Enter key or double click. Press Esc to quit.") self.__pixmap = QPixmap() self.setMouseTracking(True) QTimer.singleShot(200, self.__initialize) def __initialize(self): """ Private slot to initialize the rest of the widget. """ self.__desktop = QApplication.desktop() x = self.__desktop.x() y = self.__desktop.y() if qVersion() >= "5.0.0": self.__pixmap = QApplication.screens()[0].grabWindow( self.__desktop.winId(), x, y, self.__desktop.width(), self.__desktop.height()) else: self.__pixmap = QPixmap.grabWindow( self.__desktop.winId(), x, y, self.__desktop.width(), self.__desktop.height()) self.resize(self.__pixmap.size()) self.move(x, y) self.setCursor(Qt.CrossCursor) self.show() self.grabMouse() self.grabKeyboard() def paintEvent(self, evt): """ Protected method handling paint events. @param evt paint event (QPaintEvent) """ if self.__grabbing: # grabWindow() should just get the background return painter = QPainter(self) pal = QPalette(QToolTip.palette()) font = QToolTip.font() handleColor = pal.color(QPalette.Active, QPalette.Highlight) handleColor.setAlpha(160) overlayColor = QColor(0, 0, 0, 160) textColor = pal.color(QPalette.Active, QPalette.Text) textBackgroundColor = pal.color(QPalette.Active, QPalette.Base) painter.drawPixmap(0, 0, self.__pixmap) painter.setFont(font) r = QRect(self.__selection) if not self.__selection.isNull(): grey = QRegion(self.rect()) if self.__mode == SnapshotRegionGrabber.Ellipse: reg = QRegion(r, QRegion.Ellipse) else: reg = QRegion(r) grey = grey.subtracted(reg) painter.setClipRegion(grey) painter.setPen(Qt.NoPen) painter.setBrush(overlayColor) painter.drawRect(self.rect()) painter.setClipRect(self.rect()) drawRect(painter, r, handleColor) if self.__showHelp: painter.setPen(textColor) painter.setBrush(textBackgroundColor) self.__helpTextRect = painter.boundingRect( self.rect().adjusted(2, 2, -2, -2), Qt.TextWordWrap, self.__helpText).translated( -self.__desktop.x(), -self.__desktop.y()) self.__helpTextRect.adjust(-2, -2, 4, 2) drawRect(painter, self.__helpTextRect, textColor, textBackgroundColor) painter.drawText( self.__helpTextRect.adjusted(3, 3, -3, -3), Qt.TextWordWrap, self.__helpText) if self.__selection.isNull(): return # The grabbed region is everything which is covered by the drawn # rectangles (border included). This means that there is no 0px # selection, since a 0px wide rectangle will always be drawn as a line. txt = "{0:n}, {1:n} ({2:n} x {3:n})".format( self.__selection.x(), self.__selection.y(), self.__selection.width(), self.__selection.height()) textRect = painter.boundingRect(self.rect(), Qt.AlignLeft, txt) boundingRect = textRect.adjusted(-4, 0, 0, 0) if textRect.width() < r.width() - 2 * self.__handleSize and \ textRect.height() < r.height() - 2 * self.__handleSize and \ r.width() > 100 and \ r.height() > 100: # center, unsuitable for small selections boundingRect.moveCenter(r.center()) textRect.moveCenter(r.center()) elif r.y() - 3 > textRect.height() and \ r.x() + textRect.width() < self.rect().width(): # on top, left aligned boundingRect.moveBottomLeft(QPoint(r.x(), r.y() - 3)) textRect.moveBottomLeft(QPoint(r.x() + 2, r.y() - 3)) elif r.x() - 3 > textRect.width(): # left, top aligned boundingRect.moveTopRight(QPoint(r.x() - 3, r.y())) textRect.moveTopRight(QPoint(r.x() - 5, r.y())) elif r.bottom() + 3 + textRect.height() < self.rect().bottom() and \ r.right() > textRect.width(): # at bottom, right aligned boundingRect.moveTopRight(QPoint(r.right(), r.bottom() + 3)) textRect.moveTopRight(QPoint(r.right() - 2, r.bottom() + 3)) elif r.right() + textRect.width() + 3 < self.rect().width(): # right, bottom aligned boundingRect.moveBottomLeft(QPoint(r.right() + 3, r.bottom())) textRect.moveBottomLeft(QPoint(r.right() + 5, r.bottom())) # If the above didn't catch it, you are running on a very # tiny screen... drawRect(painter, boundingRect, textColor, textBackgroundColor) painter.drawText(textRect, Qt.AlignHCenter, txt) if (r.height() > self.__handleSize * 2 and r.width() > self.__handleSize * 2) or \ not self.__mouseDown: self.__updateHandles() painter.setPen(Qt.NoPen) painter.setBrush(handleColor) painter.setClipRegion( self.__handleMask(SnapshotRegionGrabber.StrokeMask)) painter.drawRect(self.rect()) handleColor.setAlpha(60) painter.setBrush(handleColor) painter.setClipRegion( self.__handleMask(SnapshotRegionGrabber.FillMask)) painter.drawRect(self.rect()) def resizeEvent(self, evt): """ Protected method to handle resize events. @param evt resize event (QResizeEvent) """ if self.__selection.isNull(): return r = QRect(self.__selection) r.setTopLeft(self.__limitPointToRect(r.topLeft(), self.rect())) r.setBottomRight(self.__limitPointToRect(r.bottomRight(), self.rect())) if r.width() <= 1 or r.height() <= 1: # This just results in ugly drawing... self.__selection = QRect() else: self.__selection = self.__normalizeSelection(r) def mousePressEvent(self, evt): """ Protected method to handle mouse button presses. @param evt mouse press event (QMouseEvent) """ self.__showHelp = not self.__helpTextRect.contains(evt.pos()) if evt.button() == Qt.LeftButton: self.__mouseDown = True self.__dragStartPoint = evt.pos() self.__selectionBeforeDrag = QRect(self.__selection) if not self.__selection.contains(evt.pos()): self.__newSelection = True self.__selection = QRect() else: self.setCursor(Qt.ClosedHandCursor) elif evt.button() == Qt.RightButton: self.__newSelection = False self.__selection = QRect() self.setCursor(Qt.CrossCursor) self.update() def mouseMoveEvent(self, evt): """ Protected method to handle mouse movements. @param evt mouse move event (QMouseEvent) """ shouldShowHelp = not self.__helpTextRect.contains(evt.pos()) if shouldShowHelp != self.__showHelp: self.__showHelp = shouldShowHelp self.update() if self.__mouseDown: if self.__newSelection: p = evt.pos() r = self.rect() self.__selection = self.__normalizeSelection( QRect(self.__dragStartPoint, self.__limitPointToRect(p, r))) elif self.__mouseOverHandle is None: # moving the whole selection r = self.rect().normalized() s = self.__selectionBeforeDrag.normalized() p = s.topLeft() + evt.pos() - self.__dragStartPoint r.setBottomRight( r.bottomRight() - QPoint(s.width(), s.height()) + QPoint(1, 1)) if not r.isNull() and r.isValid(): self.__selection.moveTo(self.__limitPointToRect(p, r)) else: # dragging a handle r = QRect(self.__selectionBeforeDrag) offset = evt.pos() - self.__dragStartPoint if self.__mouseOverHandle in \ [self.__TLHandle, self.__THandle, self.__TRHandle]: r.setTop(r.top() + offset.y()) if self.__mouseOverHandle in \ [self.__TLHandle, self.__LHandle, self.__BLHandle]: r.setLeft(r.left() + offset.x()) if self.__mouseOverHandle in \ [self.__BLHandle, self.__BHandle, self.__BRHandle]: r.setBottom(r.bottom() + offset.y()) if self.__mouseOverHandle in \ [self.__TRHandle, self.__RHandle, self.__BRHandle]: r.setRight(r.right() + offset.x()) r.setTopLeft(self.__limitPointToRect(r.topLeft(), self.rect())) r.setBottomRight( self.__limitPointToRect(r.bottomRight(), self.rect())) self.__selection = self.__normalizeSelection(r) self.update() else: if self.__selection.isNull(): return found = False for r in self.__handles: if r.contains(evt.pos()): self.__mouseOverHandle = r found = True break if not found: self.__mouseOverHandle = None if self.__selection.contains(evt.pos()): self.setCursor(Qt.OpenHandCursor) else: self.setCursor(Qt.CrossCursor) else: if self.__mouseOverHandle in [self.__TLHandle, self.__BRHandle]: self.setCursor(Qt.SizeFDiagCursor) elif self.__mouseOverHandle in [self.__TRHandle, self.__BLHandle]: self.setCursor(Qt.SizeBDiagCursor) elif self.__mouseOverHandle in [self.__LHandle, self.__RHandle]: self.setCursor(Qt.SizeHorCursor) elif self.__mouseOverHandle in [self.__THandle, self.__BHandle]: self.setCursor(Qt.SizeVerCursor) def mouseReleaseEvent(self, evt): """ Protected method to handle mouse button releases. @param evt mouse release event (QMouseEvent) """ self.__mouseDown = False self.__newSelection = False if self.__mouseOverHandle is None and \ self.__selection.contains(evt.pos()): self.setCursor(Qt.OpenHandCursor) self.update() def mouseDoubleClickEvent(self, evt): """ Protected method to handle mouse double clicks. @param evt mouse double click event (QMouseEvent) """ self.__grabRect() def keyPressEvent(self, evt): """ Protected method to handle key presses. @param evt key press event (QKeyEvent) """ if evt.key() == Qt.Key_Escape: self.grabbed.emit(QPixmap()) elif evt.key() in [Qt.Key_Enter, Qt.Key_Return]: self.__grabRect() else: evt.ignore() def __updateHandles(self): """ Private method to update the handles. """ r = QRect(self.__selection) s2 = self.__handleSize // 2 self.__TLHandle.moveTopLeft(r.topLeft()) self.__TRHandle.moveTopRight(r.topRight()) self.__BLHandle.moveBottomLeft(r.bottomLeft()) self.__BRHandle.moveBottomRight(r.bottomRight()) self.__LHandle.moveTopLeft(QPoint(r.x(), r.y() + r.height() // 2 - s2)) self.__THandle.moveTopLeft(QPoint(r.x() + r.width() // 2 - s2, r.y())) self.__RHandle.moveTopRight( QPoint(r.right(), r.y() + r.height() // 2 - s2)) self.__BHandle.moveBottomLeft( QPoint(r.x() + r.width() // 2 - s2, r.bottom())) def __handleMask(self, maskType): """ Private method to calculate the handle mask. @param maskType type of the mask to be used (SnapshotRegionGrabber.FillMask or SnapshotRegionGrabber.StrokeMask) @return calculated mask (QRegion) """ mask = QRegion() for rect in self.__handles: if maskType == SnapshotRegionGrabber.StrokeMask: r = QRegion(rect) mask += r.subtracted(QRegion(rect.adjusted(1, 1, -1, -1))) else: mask += QRegion(rect.adjusted(1, 1, -1, -1)) return mask def __limitPointToRect(self, point, rect): """ Private method to limit the given point to the given rectangle. @param point point to be limited (QPoint) @param rect rectangle the point shall be limited to (QRect) @return limited point (QPoint) """ q = QPoint() if point.x() < rect.x(): q.setX(rect.x()) elif point.x() < rect.right(): q.setX(point.x()) else: q.setX(rect.right()) if point.y() < rect.y(): q.setY(rect.y()) elif point.y() < rect.bottom(): q.setY(point.y()) else: q.setY(rect.bottom()) return q def __normalizeSelection(self, sel): """ Private method to normalize the given selection. @param sel selection to be normalized (QRect) @return normalized selection (QRect) """ rect = QRect(sel) if rect.width() <= 0: left = rect.left() width = rect.width() rect.setLeft(left + width - 1) rect.setRight(left) if rect.height() <= 0: top = rect.top() height = rect.height() rect.setTop(top + height - 1) rect.setBottom(top) return rect def __grabRect(self): """ Private method to grab the selected rectangle (i.e. do the snapshot). """ if self.__mode == SnapshotRegionGrabber.Ellipse: ell = QRegion(self.__selection, QRegion.Ellipse) if not ell.isEmpty(): self.__grabbing = True xOffset = self.__pixmap.rect().x() - ell.boundingRect().x() yOffset = self.__pixmap.rect().y() - ell.boundingRect().y() translatedEll = ell.translated(xOffset, yOffset) pixmap2 = QPixmap(ell.boundingRect().size()) pixmap2.fill(Qt.transparent) pt = QPainter() pt.begin(pixmap2) if pt.paintEngine().hasFeature(QPaintEngine.PorterDuff): pt.setRenderHints( QPainter.Antialiasing | QPainter.HighQualityAntialiasing | QPainter.SmoothPixmapTransform, True) pt.setBrush(Qt.black) pt.setPen(QPen(QBrush(Qt.black), 0.5)) pt.drawEllipse(translatedEll.boundingRect()) pt.setCompositionMode(QPainter.CompositionMode_SourceIn) else: pt.setClipRegion(translatedEll) pt.setCompositionMode(QPainter.CompositionMode_Source) pt.drawPixmap(pixmap2.rect(), self.__pixmap, ell.boundingRect()) pt.end() self.grabbed.emit(pixmap2) else: r = QRect(self.__selection) if not r.isNull() and r.isValid(): self.__grabbing = True self.grabbed.emit(self.__pixmap.copy(r))
class Example(QWidget): def __init__(self, vertex_x, vertex_y): #takes dimensions of the demo video super(Example, self).__init__() self.title = "Truck Front User Interface" self.top = 150 self.left = 150 self.width = 1000 self.height = 800 self.flag = 200 self.centerX = 0 self.centerY = 0 self.ratio_x = vertex_x # su anda kullanilmiyo self.ratio_y = vertex_y # su anda kullanilmiyo self.initUI() self.rectObjArray = [] def initUI(self): self.setFixedSize(900, 800) #sets the window size self.setWindowTitle(self.title) self.setGeometry(self.top, self.left, self.width, self.height) self.label = QLabel(self) #creates a label for the truck image self.label.setPixmap(QPixmap('truck_black_front.png')) self.label.setGeometry(380, 500, 800, 400) self.hlayout = QHBoxLayout(self) self.hlayout.addStretch(1) self.show() def delRectObj(self): #deletes the car object self.rectObjArray.clear() #koordinatlarin bulundugu array i temizler def createRectObj(self, tupleArray): # creates a car object for i in tupleArray: #tuple contains -1 if algorithm couldnt detect car if i[0] == -1 or i[1] == -1: continue # continue without creating object if there is no detection rect = QRect( i[0] + 75, i[1] + 100, 50, 100) #(int x, int y, int width, int height) creates car object self.rectObjArray.append(rect) #add the created objcet to an array self.update() def onPaint(self): #kullanilmiyo self.rect = QRect( self.centerX - 50, self.centerY - 25, 100, 50) #850,280,100,50 #(int x, int y, int width, int height) self.update() def onMove(self, cX, cY, priorCX, priorCY): #kullanilmiyo newCX = float(480 * cX) / self.ratio_x newPriorCx = float(480 * priorCX) / self.ratio_x if not self.rect.isNull(): self.rect.translate(QPoint(newCX - newPriorCx, cY - priorCY)) self.update() self.show() def setCenterX(self, data): #kullanilmiyo self.centerX = data #print('x', self.centerX) def setCenterY(self, data): #kullanilmiyo self.centerX = data #print('y', self.centerY) def paintEvent( self, event ): #draws the regions around the truck and change their colors green to red if there is any car in the region painter = QPainter(self) painter.setPen(QPen(Qt.black, 3, Qt.DashLine)) painter.setBrush( QBrush(QColor.fromRgb(0, 255, 0, 100), Qt.SolidPattern) ) #fromRgb(int r, int g, int b, int a = 255) a=alpha-chanel->transperancy painter.drawRect(460, 635, 100, 165) # sagalt 530, 635, 100, 165 painter.drawRect(300, 635, 100, 165) # solalt 370, 635, 100, 165 painter.drawRect(300, 535, 130, 96) # ustsol 370,535, 130, 96 painter.drawRect(435, 535, 130, 96) # ustsag 505, 535, 130, 96 qp = QPainter(self) #change colors for rect in self.rectObjArray: if (rect.x() <= 565 and rect.x() >= 170 and rect.y() <= 735 and rect.y() >= 535): qp.setPen(QPen(Qt.black, 3, Qt.DashLine)) qp.setBrush(QColor(200, 0, 0)) qp.drawRect(rect) else: qp.setPen(QPen(Qt.black, 3, Qt.DashLine)) qp.setBrush(QBrush(QColor.fromRgb(0, 255, 0), Qt.SolidPattern)) qp.drawRect(rect)
class Example(QWidget): def __init__(self, vertex_x, vertex_y): super(Example, self).__init__() self.title = "Truck Interface" self.top = 150 self.left = 150 self.width = 1000 self.height = 800 self.flag = 200 self.centerX = 0 self.centerY = 0 self.ratio_x = vertex_x self.ratio_y = vertex_y # su anda kullanilmiyo self.initUI() self.rectObjArray = [] def initUI(self): self.setFixedSize(1000, 800) self.setWindowTitle(self.title) self.setGeometry(self.top, self.left, self.width, self.height) self.label = QLabel(self) self.label.setPixmap(QPixmap('truck_black.png')) # find how to resize self.label.setGeometry(250, 200, 800, 400) #800,400 #width,height print(self.label.width()) #self.rect = QRect() #self.onPaint() program her basladiginda car olmayabilir self.hlayout = QHBoxLayout(self) self.hlayout.addStretch(1) self.show() def delRectObj(self): self.rectObjArray.clear() def createRectObj(self, tupleArray): for i in tupleArray: if i[0] == -1 or i[1] == -1: continue rect = QRect( i[0], i[1], 100, 50) # 850,280,100,50 #(int x, int y, int width, int height) self.rectObjArray.append(rect) self.update() def onPaint(self): self.rect = QRect( self.centerX - 50, self.centerY - 25, 100, 50) #850,280,100,50 #(int x, int y, int width, int height) self.update() def onMove(self, cX, cY, priorCX, priorCY): newCX = float(480 * cX) / self.ratio_x newPriorCx = float(480 * priorCX) / self.ratio_x if not self.rect.isNull(): self.rect.translate(QPoint(newCX - newPriorCx, cY - priorCY)) self.update() self.show() def setCenterX(self, data): self.centerX = data #print('x', self.centerX) def setCenterY(self, data): self.centerX = data #print('y', self.centerY) def paintEvent(self, event): #change the opacity of regions painter = QPainter(self) painter.setPen(QPen(Qt.black, 3, Qt.DashLine)) painter.setBrush( QBrush(QColor.fromRgb(0, 255, 0, 100), Qt.SolidPattern) ) #fromRgb(int r, int g, int b, int a = 255) a=alpha-chanel->transperancy painter.drawRect(200, 280, 70, 120) #soldikust #1100 #480 painter.drawRect(200, 404, 70, 120) #soldikalt painter.drawRect(275, 280, 130, 60) #yanust1 painter.drawRect(410, 280, 130, 60) #yanust2 painter.drawRect(545, 280, 130, 60) #yanust3 painter.drawRect(680, 280, 70, 120) #sagdikust painter.drawRect(680, 404, 70, 120) #sagdikalt painter.drawRect(275, 460, 130, 63) # yanalt1 painter.drawRect(410, 460, 130, 63) # yanalt2 painter.drawRect(545, 460, 130, 63) # yanalt3 qp = QPainter(self) for rect in self.rectObjArray: if (rect.x() <= 750 and rect.x() >= 130 and rect.y() <= 550 and rect.y() >= 270): qp.setPen(QPen(Qt.black, 3, Qt.DashLine)) qp.setBrush(QColor(200, 0, 0)) qp.drawRect(rect) else: qp.setPen(QPen(Qt.black, 3, Qt.DashLine)) qp.setBrush(QBrush(QColor.fromRgb(0, 255, 0), Qt.SolidPattern)) qp.drawRect(rect)
class Monitor(QMainWindow): '''Sets up a GUI for finding the potentials using a finite difference method. Geometric conditions are requried to be known otherwise the software will fail to compute the required matrices. The current implementation in code will allow for variation in dielectric constants for different material. However drawing the geometry on screen proves to be difficult. ''' def __init__(self, *kwargs): super().__init__() self.size_policy = QSizePolicy.Expanding self.font = QFont() self.font.setPointSize(12) self.showMaximized() self.setWindowIcon(QIcon('RSIL_Logo.png')) self.setWindowTitle('Finite difference solver') # self.showMaximized() self.setGeometry(100, 100, 1000, 500) self.base_geometry = QRect() self.init() self.menu_bar() def init(self): '''Setting up the interface for basic information and drawing the geometry ''' self.x_total = QLineEdit(self) self.x_total.setFont(self.font) self.x_total.setSizePolicy(self.size_policy, self.size_policy) self.x_total.setText('0') self.x_total.setToolTip( '''Enter the total size in the horizontal\ndirection of the area in question in microns''' ) self.y_total = QLineEdit(self) self.y_total.setFont(self.font) self.y_total.setSizePolicy(self.size_policy, self.size_policy) self.y_total.setText('0') self.y_total.setToolTip( '''Enter the total size in the vertical direction of the area in question in microns''' ) self.x_nodes = QLineEdit(self) self.x_nodes.setFont(self.font) self.x_nodes.setSizePolicy(self.size_policy, self.size_policy) self.x_nodes.setText('0') self.x_nodes.setToolTip( 'Number of nodes to evaluate in the horizontal direction') self.y_nodes = QLineEdit(self) self.y_nodes.setFont(self.font) self.y_nodes.setSizePolicy(self.size_policy, self.size_policy) self.y_nodes.setText('0') self.y_nodes.setToolTip('Number of nodes in the vertical direction') self.terminal_number = QLineEdit(self) self.terminal_number.setFont(self.font) self.terminal_number.setSizePolicy(self.size_policy, self.size_policy) self.terminal_number.setText('0') self.terminal_number.setToolTip( '''Number of terminals used as boundary conditions.\nGeometry and potential will be entered in coming steps''' ) #Labels for the geometry entry sections self.x_total_label = QLabel(self) self.x_total_label.setFont(self.font) self.x_total_label.setSizePolicy(self.size_policy, self.size_policy) self.x_total_label.setText('x-Direction(um):') self.y_total_label = QLabel(self) self.y_total_label.setFont(self.font) self.y_total_label.setSizePolicy(self.size_policy, self.size_policy) self.y_total_label.setText('y-Direction(um):') self.x_nodes_label = QLabel(self) self.x_nodes_label.setFont(self.font) self.x_nodes_label.setSizePolicy(self.size_policy, self.size_policy) self.x_nodes_label.setText('# nodes in x:') self.y_nodes_label = QLabel(self) self.y_nodes_label.setFont(self.font) self.y_nodes_label.setSizePolicy(self.size_policy, self.size_policy) self.y_nodes_label.setText('# nodes in y:') self.terminal_number_label = QLabel(self) self.terminal_number_label.setFont(self.font) self.terminal_number_label.setSizePolicy(self.size_policy, self.size_policy) self.terminal_number_label.setText('# of terminals:') self.geometry_entry = QPushButton('Terminal Geometry', self) self.geometry_entry.setSizePolicy(self.size_policy, self.size_policy) self.geometry_entry.setFont(self.font) self.geometry_entry.setToolTip( 'Launches window to enter geometric conditions of terminals') self.geometry_entry.clicked.connect(self.geom_enter) self.geometry_entry.setEnabled(True) self.calculate = QPushButton('Calculate', self) self.calculate.setFont(self.font) self.calculate.setSizePolicy(self.size_policy, self.size_policy) self.calculate.setToolTip( 'Calculates the potential at each node and graphs') self.calculate.clicked.connect(self.calculation) self.calculate.setDisabled(True) #create the basic layout member self.layout = QHBoxLayout() #create a QWidget to use add all the basic information into basic_info_ = QWidget() #create a layout for the basic info to be laid out in and put it there basic_layout = QGridLayout() basic_layout.addWidget(self.x_total_label, 0, 0) basic_layout.addWidget(self.x_total, 0, 1) basic_layout.addWidget(self.y_total_label, 1, 0) basic_layout.addWidget(self.y_total, 1, 1) basic_layout.addWidget(self.x_nodes_label, 2, 0) basic_layout.addWidget(self.x_nodes, 2, 1) basic_layout.addWidget(self.y_nodes_label, 3, 0) basic_layout.addWidget(self.y_nodes, 3, 1) basic_layout.addWidget(self.terminal_number_label, 4, 0) basic_layout.addWidget(self.terminal_number, 4, 1) basic_info_.setLayout(basic_layout) #setting up a frame for the drawing rectangle, will be blank #until the drawing begins self.geometry_visualization = QFrame(self) self.geometry_visualization.setFrameShape(QFrame.StyledPanel) #splitting the window horizontally and add the basic info and blank #area for the geometry to be drawn in geom_splitter = QSplitter(Qt.Horizontal) geom_splitter.addWidget(basic_info_) geom_splitter.addWidget(self.geometry_visualization) geom_splitter.setSizes([250, 500]) #set up a widget to add the buttons to control the execution controls = QWidget() #create a layout and add the buttons to it controls_layout = QHBoxLayout() controls_layout.addWidget(self.geometry_entry) controls_layout.addWidget(self.calculate) controls.setLayout(controls_layout) #add a vertical splitter and add the top splitter and the #bottom widget together and add them to the entire layout controls_spliter = QSplitter(Qt.Vertical) controls_spliter.addWidget(geom_splitter) controls_spliter.addWidget(controls) self.layout.addWidget(controls_spliter) #do some manipulation to get the layouts to play nicely # self.setLayout(self.layout) layout = QWidget() layout.setLayout(self.layout) self.setCentralWidget(layout) self.show() def menu_bar(self): '''Create the menu bar for the main window will include Name: Shortcut: Function called: File: New CTRL+N new_invoice_begin Open CTRL+O existing_invoice_open Save CTRL+S save_invoice Print CTRL+P print_invoice Quit ALT+F4 exit_system ''' self.menuFile = self.menuBar().addMenu("&File") self.actionNew = QAction('&New', self) self.actionNew.setShortcut('Ctrl+N') # self.actionNew.triggered.connect(self.new_invoice_begin) self.actionQuit = QAction('&Exit', self) # self.actionQuit.triggered.connect(self.exit_system) self.actionQuit.setShortcut('Alt+F4') self.menuFile.addActions([self.actionNew, self.actionQuit]) def geom_enter(self): '''Used when entering the geometry of the terminals''' #first make sure all the values are filled with correct values #and through and error if they are not # try: self.total_x_ = float(self.x_total.text()) self.total_y_ = float(self.y_total.text()) self.nodes_x = int(self.x_nodes.text()) self.nodes_y = int(self.y_nodes.text()) self.number = int(self.terminal_number.text()) #draw the base rectangular geomtery and apply a scaling factor to #the sizes if need be x = self.frameGeometry().width( ) - self.geometry_visualization.frameGeometry().width() self.base_geometry = QRect(x, 55, self.scalar(self.total_x_) * self.total_x_, self.scalar(self.total_y_) * self.total_y_) self.update() #create a dictionary to store the information passed to the upcoming #widget to be stored in to later draw and analyze the geometry self.boundary_conditions = {} for i in range(self.number): self.boundary_conditions[i] = [] self.win = PopUp(self.number) self.win.show() self.calculation_data = self.win.information self.calculate.setEnabled(True) # except: # error_data=QMessageBox(self) # error_data.setText('Please enter numeric values in all fields') # error_data.setWindowTitle('Numeric Error') # error_data.exec() def scalar(self, lenght): '''Determine the proper scaling to adjust the size to be visible by taking the largest dimension of the input geometry and scaling it to fit the entire area fo the provided region''' if self.total_x_ or self.total_y_ > 100: return 1 def paintEvent(self, event): QMainWindow.paintEvent(self, event) if not self.base_geometry.isNull(): painter = QPainter(self) pen = QPen(Qt.black, 1) painter.setPen(pen) painter.drawRect(self.base_geometry) def calculation(self): '''Call the calculation method and run it to get the results''' self.calculate.setDisabled(True) self.geometry_entry.setDisabled(True) a = time.time() self.values = Voltage_Calculation(self.nodes_x, self.nodes_y, self.total_x_, self.total_y_, self.calculation_data) print(time.time() - a) self.plot = PlotCanvas(self.values.x_nodes, self.values.y_nodes, self.values.vector) self.calculate.setEnabled(True) self.geometry_entry.setEnabled(True)
class DrawRectangle(QDialog): def __init__(self, *args, **kwargs): super(DrawRectangle, self).__init__(*args, **kwargs) self.pressed = False self.moved = False self.rectArea = QRect() # 矩形框 self.rectPen = QPen(QColor(22, 168, 250)) self.rectPen.setWidth(1) self.setMouseTracking(True) self.setWindowFlags(Qt.FramelessWindowHint) self.setAttribute(Qt.WA_TranslucentBackground, True) self.showMaximized() # 通过绑定自定义右键菜单来监听右键点击,实现重新画区域或者退出,或者弹出菜单 self.setContextMenuPolicy(Qt.CustomContextMenu) self.customContextMenuRequested.connect(self.onRightClicked) def onRightClicked(self, _): if not self.rectArea.isNull(): self.rectArea.setWidth(0) self.rectArea.setHeight(0) # clear return self.update() self.close() def mousePressEvent(self, event): if event.buttons() == Qt.LeftButton: self.pressed = True # if not self.rectArea.isNull(): # self.moved = self.rectArea.contains( # event.pos()) # 设置画好的窗口可整体移动 # return self.rectArea.setTopLeft(event.pos()) # 设置左上角位置 def mouseReleaseEvent(self, event): self.pressed = False self.moved = False super(DrawRectangle, self).mouseReleaseEvent(event) def mouseMoveEvent(self, event): pos = event.pos() # if self.moved and not self.rectArea.isNull(): # 表示移动整体画好的矩形框 # # 重新设置矩形框的位置 # self.rectArea.setX(pos.x()) # self.rectArea.setY(pos.y()) # return self.update() if not self.pressed: if not self.rectArea.isNull(): if self.rectArea.contains(pos): # 设置鼠标形状为十字箭头 return self.setCursor(Qt.SizeAllCursor) # 设置鼠标为普通形状 return self.setCursor(Qt.ArrowCursor) self.rectArea.setBottomRight(pos) # 设置右下角位置 self.update() def paintEvent(self, event): painter = QPainter(self) if self.rectArea.isNull(): # 窗口背景全部半透明颜色 return painter.fillRect(self.rect(), QColor(0, 0, 0, 50)) # 画外围背景,去掉中间绘制的矩形框 path = QPainterPath() path.setFillRule(Qt.OddEvenFill) path.addRect(QRectF(self.rect())) path.addRect(QRectF(self.rectArea)) painter.fillPath(path, QColor(0, 0, 0, 50)) # 画矩形 painter.save() painter.setPen(self.rectPen) painter.drawRect(self.rectArea) # 画矩形框 # 背景用白色透明遮住,防止鼠标穿透到桌面 painter.fillRect(self.rectArea, QColor(255, 255, 255, 1)) painter.restore()