QWidget* QGraphicsViewAdapter::getWidgetAt(const QPoint& pos) { QWidget* childAt = _graphicsView->childAt(pos); if(childAt) { return childAt; } QGraphicsItem* item = _graphicsView->itemAt(pos); if(item && item->contains(item->mapFromScene(pos))) { QGraphicsProxyWidget* p = dynamic_cast<QGraphicsProxyWidget*>(item); if(p) { childAt = p->widget(); QWidget* c; while( (c = childAt->childAt(childAt->mapFromGlobal(pos)))!=0 ) { childAt = c; } return childAt; } } return NULL; }
QVariant SCgObject::itemChange(GraphicsItemChange change, const QVariant &value) { // item selection changed if (change == QGraphicsItem::ItemSelectedHasChanged) { if (isSelected()) { mColor = scg_cfg_get_value_color(scg_key_element_color_selected);//QColor(255, 128, 64); //setCursor(QCursor(Qt::SizeAllCursor)); } else { mColor = scg_cfg_get_value_color(scg_key_element_color_normal);//QColor(0, 0, 0); //setCursor(QCursor(Qt::ArrowCursor)); } } // move to correct position automaticly if (change == QGraphicsItem::ItemParentChange && scene()) { // we need to set this flag to prevent processing ItemPositionHasChanged, // because item position at this moment not actual mParentChanging = true; QGraphicsItem* newParent = value.value<QGraphicsItem*>(); if(newParent) setPos(newParent->mapFromScene(scenePos())); else setPos(scenePos()); } if (change == QGraphicsItem::ItemParentHasChanged) { // now item position has valid value mParentChanging = false; updateConnected(); } // Change stacking order if (scene() && change == QGraphicsItem::ItemSelectedHasChanged && isSelected() && scene()->selectedItems().size() == 1) { QGraphicsItem* top = this; QList<QGraphicsItem*> lst = scene()->items(); foreach(QGraphicsItem* it, lst) { if(it != this && it->type() == type() && it->parentItem() == parentItem()) { it->stackBefore(top); top = it; } } }
void CalendarGraphicsItem::mouseReleaseEvent(QGraphicsSceneMouseEvent * event) { QGraphicsItem * item; for(int i = 0; i < 42; ++i) { item = _items[QString("day%1").arg(i)]; if(item && item->contains(item->mapFromScene(event->scenePos()))) { QDate firstMonthDay = QDate(_selectedDay.year(), _selectedDay.month(), 1); QDate firstCalendarDay = firstMonthDay.addDays(firstMonthDay.dayOfWeek() * -1); if(firstMonthDay.dayOfWeek() < 2) firstCalendarDay = firstCalendarDay.addDays(-7); setSelectedDay(firstCalendarDay.addDays(i)); return; } } }
void AbstractGroupPrivate::removeChild(QGraphicsWidget *child) { QPointF newPos = child->scenePos(); QGraphicsItem *parent = q->parentItem(); QTransform t(child->itemTransform(parent)); if (t.m11() != 0) { qreal angle = (t.m12() > 0 ? acos(t.m11()) : -acos(t.m11())); QTransform at; QSizeF size(child->size()); at.translate(size.width() / 2, size.height() / 2); at.rotateRadians(angle); at.translate(-size.width() / 2, -size.height() / 2); child->setTransform(at); } child->setParentItem(parent); child->setPos(parent->mapFromScene(newPos)); child->disconnect(q); }
void View::MoveToLayer(QGraphicsItem* pItem, ViewLayerId viewLayerId) { QGraphicsItem* newParent = m_pNullLayer; if (viewLayerId == ViewLayerId_Null) newParent = m_pNullLayer; else if (viewLayerId == ViewLayerId_Main) newParent = m_pMainLayer; else if (viewLayerId == ViewLayerId_System) newParent = m_pSystemLayer; else if (viewLayerId == ViewLayerId_Overlay) newParent = m_pOverlayLayer; else if (viewLayerId == ViewLayerId_Underlay) newParent = m_pUnderlayLayer; if (pItem->scene() != NULL) { /* * Maintain the current position of * the item relative to the scene. */ QPointF pos = pItem->scenePos(); pos = newParent->mapFromScene(pos); pItem->setPos(pos); } pItem->setParentItem(newParent); }
void CustomGraphicsScene::mouseReleaseEvent(QGraphicsSceneMouseEvent* _event) { // // Обработку производим только после перемещения мыши // if (m_afterMoving) { // // 1. Перемещение карточки на связь // QList<Shape*> selected = selectedShapes(); CardShape* selectedCard = nullptr; ArrowFlow* selectedFlow = nullptr; if (selected.size() == 2) { if (dynamic_cast<CardShape*>(selected.first())) { selectedCard = dynamic_cast<CardShape*>(selected.first()); selectedFlow = dynamic_cast<ArrowFlow*>(selected.last()); } else { selectedCard = dynamic_cast<CardShape*>(selected.last()); selectedFlow = dynamic_cast<ArrowFlow*>(selected.first()); } // // Если это действительно перемещение карточки на связь // if (selectedCard != nullptr && selectedFlow != nullptr && selectedFlow->startShape() != selectedCard && selectedFlow->endShape() != selectedCard) { // // Изымаем карточку из сцены // takeShape(selectedCard); // // Определяем элементы к которым теперь будет присоединена карточка // CardShape* previousCard = dynamic_cast<CardShape*>(selectedFlow->startShape()); CardShape* nextCard = dynamic_cast<CardShape*>(selectedFlow->endShape()); // // Заменяем старую связь на новые // removeShape(selectedFlow); appendShape(new ArrowFlow(previousCard, selectedCard)); if (hasCards(selectedCard)) { appendShape(new ArrowFlow(lastCard(selectedCard), nextCard)); } else { appendShape(new ArrowFlow(selectedCard, nextCard)); } // // Меняем порядок следования фигур // insertShape(selectedCard, previousCard); // // Если предыдущая карточка вложена в группирующий элемент // QGraphicsItem* previousParentItem = previousCard->parentItem(); QGraphicsItem* nextParentItem = nextCard->parentItem(); bool handled = false; if (previousParentItem != nullptr) { const QRectF selectedCardRect = selectedCard->mapToScene(selectedCard->boundingRect()).boundingRect(); const QRectF parentCardRect = previousParentItem->mapToScene(previousParentItem->boundingRect()).boundingRect(); // // ... и если текущая карточка тоже помещена внутрь группирующего элемента // if (parentCardRect.contains(selectedCardRect)) { // // ... поместим её внутрь // const QPointF lastPos = selectedCard->scenePos(); selectedCard->setParentItem(previousParentItem); selectedCard->setPos(previousParentItem->mapFromScene(lastPos)); handled = true; } } // // Если следующая карточка вложена в группирующий элемент // if (!handled && nextParentItem != nullptr) { const QRectF selectedCardRect = selectedCard->mapToScene(selectedCard->boundingRect()).boundingRect(); const QRectF parentCardRect = nextParentItem->mapToScene(nextParentItem->boundingRect()).boundingRect(); // // ... и если текущая карточка тоже помещена внутрь группирующего элемента // if (parentCardRect.contains(selectedCardRect)) { // // ... поместим её внутрь // const QPointF lastPos = selectedCard->scenePos(); selectedCard->setParentItem(nextParentItem); selectedCard->setPos(nextParentItem->mapFromScene(lastPos)); handled = true; } } // // Если не удалось вложить, то убираем родителя у элемента // if (!handled && selectedCard->parentItem() != nullptr) { const QPointF lastPos = selectedCard->scenePos(); selectedCard->setParentItem(nullptr); selectedCard->setPos(lastPos); } } } // // 2. Обработка вложения и вытаскивания элементов из групп сцен и папок // selected = selectedShapes(); if (selected.size() == 1) { selectedCard = dynamic_cast<CardShape*>(selected.first()); if (selectedCard != nullptr) { // // Определим, есть ли группирующий элемент, помеченный на вложение // CardShape* parentCard = nullptr; for (Shape* shape : shapes()) { if (CardShape* card = dynamic_cast<CardShape*>(shape)) { if (card->isOnInstertionState()) { parentCard = card; break; } } } // // Если это перемещение карточки внутри своего родителя, просто снимем режим выделения // if (parentCard != nullptr && selectedCard->parentItem() == parentCard) { parentCard->setOnInstertionState(false); } // // В противном случае // else { // // Вложение // if (parentCard != nullptr) { // // Изымаем карточку из сцены // takeShape(selectedCard); // // Если у группирующего элемента есть дети, то связываем с последней карточкой // if (hasCards(parentCard)) { // // Определяем элементы к которым теперь будет присоединена карточка // CardShape* previousCard = dynamic_cast<CardShape*>(lastCard(parentCard)); Flow* previousCardFlow = cardFlow(previousCard, CARD_ON_FLOW_START); if (previousCardFlow != nullptr) { CardShape* nextCard = dynamic_cast<CardShape*>(previousCardFlow->endShape()); // // Заменяем старую связь на новые // removeShape(previousCardFlow); if (hasCards(selectedCard)) { appendShape(new ArrowFlow(lastCard(selectedCard), nextCard)); } else { appendShape(new ArrowFlow(selectedCard, nextCard)); } } appendShape(new ArrowFlow(previousCard, selectedCard)); // // Меняем порядок следования фигур // insertShape(selectedCard, previousCard); } // // Если детей нет, то связываем непосредственно с группирующим элементом // else { // // Определяем элементы к которым теперь будет присоединена карточка // CardShape* previousCard = parentCard; Flow* previousCardFlow = cardFlow(previousCard, CARD_ON_FLOW_START); if (previousCardFlow != nullptr) { CardShape* nextCard = dynamic_cast<CardShape*>(previousCardFlow->endShape()); // // Заменяем старую связь на новые // removeShape(previousCardFlow); if (hasCards(selectedCard)) { appendShape(new ArrowFlow(lastCard(selectedCard), nextCard)); } else { appendShape(new ArrowFlow(selectedCard, nextCard)); } } appendShape(new ArrowFlow(previousCard, selectedCard)); // // Меняем порядок следования фигур // insertShape(selectedCard, previousCard); } // // Назначаем нового родителя // const QPointF lastPos = selectedCard->scenePos(); selectedCard->setParentItem(parentCard); selectedCard->setPos(parentCard->mapFromScene(lastPos)); parentCard->setOnInstertionState(false); } // // Вытаскивание - соединяем с последней карточкой в сценарии // else if (selectedCard->parentItem() != nullptr) { // // Изымаем карточку из сцены // takeShape(selectedCard); // // Определяем элемент к которому теперь будет присоединена карточка // CardShape* previousCard = dynamic_cast<CardShape*>(lastCard()); // // Добавляем связь // appendShape(new ArrowFlow(previousCard, selectedCard)); // // Меняем порядок следования фигур // insertShape(selectedCard, previousCard); // // Убираем родителя // const QPointF lastPos = selectedCard->scenePos(); selectedCard->setParentItem(nullptr); selectedCard->setPos(lastPos); } } } } } update(); QGraphicsScene::mouseReleaseEvent(_event); m_afterMoving = false; }
void GraphicsNodeView::leftMouseButtonPress(QMouseEvent *event) { QGraphicsView::mousePressEvent(event); // GUI logic: if we click on a socket, we need to handle // the event appropriately QGraphicsItem *item = itemAt(event->pos()); if (!item) { setDragMode(QGraphicsView::RubberBandDrag); QGraphicsView::mousePressEvent(event); return; } switch (item->type()) { case GraphicsNodeItemTypes::TypeSocket: { QPointF scenePos = mapToScene(event->pos()); QPointF itemPos = item->mapFromScene(scenePos); GraphicsNodeSocket *sock = static_cast<GraphicsNodeSocket *>(item); if (sock->isInSocketCircle(itemPos)) { viewport()->setCursor(Qt::ClosedHandCursor); // initialize a new drag mode event _drag_event = new EdgeDragEvent(); auto edges = sock->get_edges(); if (edges.size() == 1) { // get the first edge for (auto e : edges) { _tmp_edge = e; break; } _drag_event->e = _tmp_edge; if (sock->socket_type() == Port::Direction::IN) { _drag_event->e->disconnect_sink(); _drag_event->e->set_stop(mapToScene(event->pos())); _drag_event->mode = EdgeDragEvent::move_to_sink; } else { _drag_event->e->disconnect_source(); _drag_event->e->set_start(mapToScene(event->pos())); _drag_event->mode = EdgeDragEvent::move_to_source; } } else { GraphicsNodeScene *gscene = dynamic_cast<GraphicsNodeScene *>(scene()); _drag_event->e = gscene->make_edge(); if (sock->socket_type() == Port::Direction::IN) { _drag_event->e->set_start(mapToScene(event->pos())); _drag_event->e->connect_sink(sock); _drag_event->mode = EdgeDragEvent::connect_to_source; } else { _drag_event->e->connect_source(sock); _drag_event->e->set_stop(mapToScene(event->pos())); _drag_event->mode = EdgeDragEvent::connect_to_sink; } } event->ignore(); } else { QGraphicsView::mousePressEvent(event); } break; } case GraphicsNodeItemTypes::TypeNode: { QPointF scenePos = mapToScene(event->pos()); QPointF itemPos = item->mapFromScene(scenePos); GraphicsNode *node = static_cast<GraphicsNode *>(item); if (itemPos.x() > (node->width() - 10) && (itemPos.y() > (node->height() - 10))) { setViewportUpdateMode( QGraphicsView::BoundingRectViewportUpdate); _resize_event = new NodeResizeEvent(); _resize_event->node = node; _resize_event->orig_width = node->width(); _resize_event->orig_height = node->height(); _resize_event->pos = event->pos(); event->ignore(); } else { QGraphicsView::mousePressEvent(event); } break; } default: QGraphicsView::mousePressEvent(event); } }