def __init__(self, scene, parent=None): try: if DEBUG: print("In __init__ of GraphicsScene") super().__init__(parent) self.scene = scene # settings self.gridSize = 20 self.gridSquares = 5 self._color_background = QColor("#393939") self._color_light = QColor("#2f2f2f") self._color_dark = QColor("#292929") self._pen_light = QPen(self._color_light) self._pen_light.setWidth(1) self._pen_dark = QPen(self._color_dark) self._pen_dark.setWidth(2) self.setBackgroundBrush(self._color_background) except Exception as ex: print("Exception caught in GraphicsScene - __init__()") print(ex) handleError(ex)
def drawBackground(self, painter, rect): try: if DEBUG: print("drawing background") super().drawBackground(painter, rect) # here we create our grid left = int(math.floor(rect.left())) right = int(math.ceil(rect.right())) top = int(math.floor(rect.top())) bottom = int(math.ceil(rect.bottom())) first_left = left - (left % self.gridSize) first_top = top - (top % self.gridSize) # compute all lines to be drawn lines_light, lines_dark = [], [] for x in range(first_left, right, self.gridSize): if (x % (self.gridSize*self.gridSquares) != 0): lines_light.append(QLine(x, top, x, bottom)) else: lines_dark.append(QLine(x, top, x, bottom)) for y in range(first_top, bottom, self.gridSize): if (y % (self.gridSize*self.gridSquares) != 0): lines_light.append(QLine(left, y, right, y)) else: lines_dark.append(QLine(left, y, right, y)) # draw the lines painter.setPen(self._pen_light) painter.drawLines(*lines_light) painter.setPen(self._pen_dark) painter.drawLines(*lines_dark) if DEBUG: print("finished drawing backgroud") except Exception as ex: print("Exception caught in GraphicsScene - drawBackground()") print(ex) handleError(ex)
def setEditingFlag(self, value): try: self.node.scene.grScene.views()[0].editingFlag = value except Exception as ex: print("Exception caught in ContentWidget - seEditingFlag()") print(ex) handleError(ex)
def categoryClicked(self): if DEBUG: print("slot in ContentWidget - categoryClicked()") if DEBUG: print(self.node.category) try: if self.node.category.cat_id == "": if DEBUG: print("id is empty string") cat_id = QUuid() cat_id = cat_id.createUuid() cat_id = cat_id.toString() self.node.category.cat_id = cat_id if DEBUG: print(self.node.category.cat_id) try: self.catClicked.emit(self.node.category) # emitting signal up to Editor Widget if DEBUG: print("signal emitted") except Exception as ex: print("Exception caught in ContentWidget - categoryClicked()") print(ex) else: if DEBUG: print("id exists") if DEBUG: print(self.node.category.cat_id) try: self.catClicked.emit(self.node.category) # emitting signal up to Editor Widget if DEBUG: print("signal emitted") except Exception as ex: print("Exception caught in ContentWidget - categoryClicked()") print(ex) except Exception as ex: print("Exception caught in ContentWidget - categoryClicked()") print(ex) handleError(ex)
def initUI(self): try: self.layout = QVBoxLayout() self.layout.setContentsMargins(0, 0, 0, 0) self.setLayout(self.layout) # self.wdg_label = QLabel("Category") self.layout.addWidget(self.wdg_label) # add child button # self.addChild = QPushButton("Add child") # self.layout.addWidget(self.addChild) # connecting label to allow signals to be sent to slot self.wdg_label.templateLabel.catClicked.connect(self.categoryClicked) self.wdg_label.patternLabel.catClicked.connect(self.categoryClicked) self.wdg_label.thatLabel.catClicked.connect(self.categoryClicked) # self.addChild.clicked.connect(self.addChildClicked) # self.layout.addWidget(QLabel("What Ryan Hears:")) # self.layout.addWidget(QDMTextEdit("")) # self.layout.addWidget(QLabel("What Ryan Says:")) # self.layout.addWidget(QDMTextEdit("")) except Exception as ex: print("Exception caught in ContentWidget - initUI()") print(ex) handleError(ex)
def __init__(self, socket, socket_type=1): if DEBUG: print("In Graphics Socket constructor") try: self.socket = socket super().__init__(socket.node.grNode) self.radius = 6.0 self.outline_width = 1.0 self._colors = [ QColor("#FFFF7700"), QColor("#FF52e220"), QColor("#FF0056a6"), QColor("#FFa86db1"), QColor("#FFb54747"), QColor("#FFdbe220"), ] self._color_background = self._colors[socket_type] self._color_outline = QColor("#FF000000") self._pen = QPen(self._color_outline) self._pen.setWidthF(self.outline_width) self._brush = QBrush(self._color_background) except Exception as ex: print("Exception caught in GraphicsSocket - __init__()") print(ex) handleError(ex)
def remove(self): try: if DEBUG: print("> Removing Node", self) if DEBUG: print(" - remove all edges from sockets") for socket in (self.inputs + self.outputs): if socket.hasEdge(): if DEBUG: print(" - removing from socket:", socket, "edge:", socket.edge) socket.edge.remove() if DEBUG: print(" - remove grNode") self.scene.grScene.removeItem(self.grNode) self.grNode = None if DEBUG: print(" - remove node from the scene") self.scene.removeNode(self) if DEBUG: print(" - everything was done.") except Exception as ex: print("Exception caught in Node - remove()") print(ex) handleError(ex)
def __init__(self, edge, parent=None): if DEBUG: print("In QDMGraphicsEdge constructor") try: super().__init__(parent) self.edge = edge self._color = QColor("#f5fc1e") self._color_selected = QColor("#00ff00") self._pen = QPen(self._color) self._pen_selected = QPen(self._color_selected) self._pen_dragging = QPen(self._color) self._pen_dragging.setStyle(Qt.DashLine) self._pen.setWidthF(2.0) self._pen_selected.setWidthF(2.0) self._pen_dragging.setWidthF(2.0) self.setFlag(QGraphicsItem.ItemIsSelectable) self.setZValue(-1) self.posSource = [0, 0] self.posDestination = [200, 100] except Exception as ex: print("Exception caught in GraphicsEdge - __init__()") print(ex) handleError(ex)
def width(self, value): try: self.rect.setWidth(value) except Exception as ex: print("Exception caught in GraphicsNode - width() @setter") print(ex) handleError(ex)
def wheelEvent(self, event): try: # calculate our zoom Factor zoomOutFactor = 1 / self.zoomInFactor # calculate zoom if event.angleDelta().y() > 0: zoomFactor = self.zoomInFactor self.zoom += self.zoomStep else: zoomFactor = zoomOutFactor self.zoom -= self.zoomStep clamped = False if self.zoom < self.zoomRange[0]: self.zoom, clamped = self.zoomRange[0], True if self.zoom > self.zoomRange[1]: self.zoom, clamped = self.zoomRange[1], True # set scene scale if not clamped or self.zoomClamp is False: self.scale(zoomFactor, zoomFactor) except Exception as ex: print("Exception caught in GraphicsView - wheelEvent()") print(ex) handleError(ex)
def onFileExport(self): # Check for uncompiled changes retval = None if self.editSpace.up_to_date is False: if DEBUG: print("Code is not compiled. Compile before export.") retval = handleCompileMsg() if retval == QMessageBox.Cancel: return if retval == QMessageBox.Yes: if DEBUG: print('Compiling code before exporting') try: self.onCompile() except Exception as ex: print('Couldn\'t compile before exporting') return try: fname, filter = QFileDialog.getSaveFileName(self, 'Export to file') if fname == "": if DEBUG: print("Cancel clicked") return Storage.exportAIML(fname, self.editSpace.aiml) # save as an aiml file # Display Dialog exportSuccessful() except Exception as ex: print("Exception caught trying to export") print(ex) handleError(ex)
def pos(self): try: return self.grNode.pos() # QPointF except Exception as ex: print("Exception caught in Node - pos() @property") print(ex) handleError(ex)
def addEdge(self, edge): try: self.edges.append(edge) except Exception as ex: print("Exception caught in Scene - addEdge()") print(ex) handleError(ex)
def title(self): try: return self._title except Exception as ex: print("Exception caught in GraphicsNode - title() @property") print(ex) handleError(ex)
def addNode(self, node): try: self.nodes.append(node) except Exception as ex: print("Exception caught in Scene - addNode()") print(ex) handleError(ex)
def height(self, value): try: self.rect.setheight(value) except Exception as ex: print("Exception caught in GraphicsNode - height() @setter") print(ex) handleError(ex)
def interactiveResize(self, mousePos): try: """ Perform shape interactive resize. """ rect = QRectF(self.rect) self.prepareGeometryChange() if self.handleSelected: fromX = self.mousePressRect.right() fromY = self.mousePressRect.bottom() toX = fromX + mousePos.x() - self.mousePressPos.x() toY = fromY + mousePos.y() - self.mousePressPos.y() rect.setRight(toX) rect.setBottom(toY) self.rect = rect self.setContentGeo() self.node.updateSocketPos() # self.setRect(self.rect) self.handle = QRectF(self.rect.right() - self.handleSize, self.rect.bottom() - self.handleSize, self.handleSize, self.handleSize) except: print("Exception caught in GraphicsNode - interactiveResize()") print(ex) handleError(ex)
def height(self): try: return self.rect.height() except Exception as ex: print("Exception caught in GraphicsNode - height() @property") print(ex) handleError(ex)
def setPos(self, x, y): try: self.grNode.setPos(x, y) except Exception as ex: print("Exception caught in Node - setPos()") print(ex) handleError(ex)
def onFileImport(self): try: fname, filter = QFileDialog.getOpenFileName(self, "Import File") if fname == "": if DEBUG: print("Cancel was clicked") return yoffset = -4000 if DEBUG: print("fname: " + fname) self.filename = os.path.splitext(fname)[ 0] # removing extension from path name aiml = Storage.importAIML(self.filename) # import the aiml file numCats = 0 if DEBUG: print(f"aiml tags:\n{aiml.tags}") topics = [] for cat in aiml.tags: if cat.type == "topic": topics.append(cat) continue self.catCreated.emit(cat) numCats = numCats + 1 for cat in topics: self.catCreated.emit(cat) numCats = numCats + 1 if DEBUG: print("Finished creating " + str(numCats) + " categories") if DEBUG: print("file import successful") self.onCompile() if DEBUG: print("Compile after import sucessful!") except Exception as ex: handleError(ex) print(ex)
def addHasBeenModifiedListener(self, callback): if DEBUG: print("In addHasBeenModifiedListener()") try: self._has_been_modified_listeners.append(callback) except Exception as ex: print("Exception caught in Scene - addHasBeenModifiedListener()") print(ex) handleError(ex)
def has_been_modified(self): if DEBUG: print("In has_been_modified() @property") try: return self._has_been_modified except Exception as ex: print("Exception caught in Scene - has_been_modified @property") print(ex) handleError(ex)
def removeEdge(self, edge): try: if DEBUG: print("In removeEdge()") self.edges.remove(edge) except Exception as ex: print("Exception caught in Scene - removeEdge()") print(ex) handleError(ex)
def removeNode(self, node): try: if DEBUG: print("In removeNode()") self.nodes.remove(node) except Exception as ex: print("Exception caught in Scene - removeNode()") print(ex) handleError(ex)
def boundingRect(self): if DEBUG: print("In boundingRect() GraphicsEdge") try: return self.shape().boundingRect() except Exception as ex: print("Exception caught in GraphicsEdge - boundingRect()") print(ex) handleError(ex)
def setContentGeo(self): try: self.content.setGeometry(self.edge_size, self.title_height + self.edge_size, self.rect.width() - 2*self.edge_size, self.rect.height() - 2*self.edge_size-self.title_height) except Exception as ex: print("Exception caught in GraphicsNode - setContentGeo()") print(ex) handleError(ex)
def title(self, value): try: self._title = value self.title_item.setPlainText(self._title) except Exception as ex: print("Exception caught in GraphicsNode - title() @setter") print(ex) handleError(ex)
def rightMouseButtonRelease(self, event): try: super().mouseReleaseEvent(event) except Exception as ex: print( "Exception caught in GraphicsView - rightMouseButtonRelease()") print(ex) handleError(ex)
def setDestination(self, x, y): if DEBUG: print("In setDestination()") try: self.posDestination = [x, y] except Exception as ex: print("Exception caught in GraphicsEdge - setDestination()") print(ex) handleError(ex)
def shape(self): if DEBUG: print("In shape()") try: return self.calcPath() except Exception as ex: print("Exception caught in GraphicsEdge - shape()") print(ex) handleError(ex)