void Layout::add(KoShape *shape) { Q_ASSERT(!m_children.contains(shape)); TreeShape *tree = dynamic_cast<TreeShape*>(shape); if (tree) { tree->setZIndex(m_container->zIndex()+1); if (tree->nextShape()) { Q_ASSERT(m_children.contains(tree->nextShape())); int pos = m_children.indexOf(tree->nextShape()); if (pos != 0) { TreeShape *prev = dynamic_cast<TreeShape*>(m_children[pos-1]); prev->setNextShape(shape); } m_children.insert(pos, shape); } else { if (!m_children.isEmpty()) { TreeShape *prev = dynamic_cast<TreeShape*>(m_children.last()); prev->setNextShape(shape); } m_children.append(shape); } tree->setStructure(tree->structure()); } KoConnectionShape *connector = dynamic_cast<KoConnectionShape*>(shape); if (connector) { Q_ASSERT(!m_connectors.contains(connector)); m_connectors.append(shape); connector->setType(m_connectionType); } scheduleRelayout(); }
void ConnectionTool::mousePressEvent(KoPointerEvent * event) { if (!m_currentShape) { return; } KoShape * hitShape = findShapeAtPosition(event->point); int hitHandle = handleAtPoint(m_currentShape, event->point); if (m_editMode == EditConnection && hitHandle >= 0) { // create connection handle change strategy m_currentStrategy = new KoPathConnectionPointStrategy(this, dynamic_cast<KoConnectionShape*>(m_currentShape), hitHandle); } else if (m_editMode == EditConnectionPoint) { if (hitHandle >= KoConnectionPoint::FirstCustomConnectionPoint) { // start moving custom connection point m_currentStrategy = new MoveConnectionPointStrategy(m_currentShape, hitHandle, this); } } else if (m_editMode == CreateConnection) { // create new connection shape, connect it to the active connection point // and start editing the new connection // create the new connection shape KoShapeFactoryBase *factory = KoShapeRegistry::instance()->value("KoConnectionShape"); KoShape *shape = factory->createDefaultShape(canvas()->shapeController()->resourceManager()); KoConnectionShape * connectionShape = dynamic_cast<KoConnectionShape*>(shape); if (!connectionShape) { delete shape; resetEditMode(); return; } //set connection type connectionShape->setType(m_connectionType); // get the position of the connection point we start our connection from QPointF cp = m_currentShape->shapeToDocument(m_currentShape->connectionPoint(m_activeHandle).position); // move both handles to that point connectionShape->moveHandle(0, cp); connectionShape->moveHandle(1, cp); // connect the first handle of the connection shape to our connection point if (!connectionShape->connectFirst(m_currentShape, m_activeHandle)) { delete shape; resetEditMode(); return; } //add connector label connectionShape->createTextShape(canvas()->shapeController()->resourceManager()); connectionShape->setPlainText(""); // create the connection edit strategy from the path tool m_currentStrategy = new KoPathConnectionPointStrategy(this, connectionShape, 1); if (!m_currentStrategy) { delete shape; resetEditMode(); return; } // update our handle data setEditMode(m_editMode, shape, 1); // add connection shape to the shape manager so it gets painted canvas()->shapeManager()->addShape(connectionShape); } else { // pressing on a shape in idle mode switches to corresponding edit mode if (hitShape) { if (dynamic_cast<KoConnectionShape*>(hitShape)) { int hitHandle = handleAtPoint(hitShape, event->point); setEditMode(EditConnection, hitShape, hitHandle); if (hitHandle >= 0) { // start editing connection shape m_currentStrategy = new KoPathConnectionPointStrategy(this, dynamic_cast<KoConnectionShape*>(m_currentShape), m_activeHandle); } } } else { resetEditMode(); } } }