void MainWindow::resizeItem() { for (int i = 0; i < 10; i++) { QGraphicsRectItem* theFrame = item1; QPointF P1(100 - (7.0 * i)/3.0, 50 -(5.0*i)/3.0); graphicsSheet->addPoint("P1", P1); graphicsSheet->addPoint("pos", theFrame->pos()); QPointF P2i = QPointF(theFrame->rect().width(), theFrame->rect().height()); QPointF P2 = theFrame->mapToScene(P2i); qDebug() << "P1:" << P1; qDebug() << "P2:" << P2; graphicsSheet->addPoint("P2", P2); QTransform t; t.rotate(-theFrame->rotation()); t.translate(-P1.x(), -P1.y()); QPointF P2t = t.map(P2); // qDebug() << "P2t:" << P2t; // OK /*************************/ QPointF P3(P2t.x()/2, P2t.y()/2); // qDebug() << "P3:" << P3; QTransform t2; t2.translate(P1.x(), P1.y()); t2.rotate(theFrame->rotation()); QPointF P3t = t2.map(P3); //// qDebug() << "P3t:" << P3t; // OK graphicsSheet->addPoint("P3t", P3t); QTransform t3; t3.translate(P3t.x(), P3t.y()); t3.rotate(-theFrame->rotation()); t3.translate(-(P3t.x()), -(P3t.y())); QPointF newPos = t3.map(P1); QSizeF newSize = QSizeF(P2t.x(), P2t.y()); graphicsSheet->addPoint("newPos", newPos); RectItem* item1 = new RectItem(QRectF(newPos.x(), newPos.y(), newSize.width(), newSize.height())); qreal angle = 30; QPointF center2 = QPointF(item1->rect().width() / 2, item1->rect().height() / 2); item1->setTransformOriginPoint(center2); item1->setRotation(angle); graphicsSheet->scene()->addItem(item1); } }
void SymbolDataEditor::rememberChanges() { if (scene->items().isEmpty()) return; if (itemToChange == Item::InPoint) { QGraphicsEllipseItem* inPointItem = static_cast<QGraphicsEllipseItem *>(scene->items(Qt::AscendingOrder).at(Item::InPoint)); inPoint = inPointItem->rect().center(); } if (itemToChange == Item::OutPoint) { QGraphicsEllipseItem* outPointItem = static_cast<QGraphicsEllipseItem *>(scene->items(Qt::AscendingOrder).at(Item::OutPoint)); outPoint = outPointItem->rect().center(); } if (itemToChange == Item::LimitsRect) { QGraphicsRectItem* limitsRectItem = static_cast<QGraphicsRectItem *>(scene->items(Qt::AscendingOrder).at(Item::LimitsRect)); limits = limitsRectItem->rect(); } correctLimits(); }
QList<Manifold *> Element::detectCollidingObjects(void) { collidingItemsList = scene()->collidingItems(this); QList<Manifold *> mList = * new QList<Manifold *>(); for (int j = 0; j < collidingItemsList.size(); j++) { mList.append(new Manifold()); } int i = 0; // While detect colliding items while (!collidingItemsList.isEmpty()) { // Pop the first item from the list QGraphicsRectItem * rectB = dynamic_cast<QGraphicsRectItem*>(collidingItemsList.takeFirst()); mList[i]->second_object = dynamic_cast<Object2d*>(rectB); // Vector between two colliding objects A and B QPointF dist_AB = rectB->mapToScene(rectB->rect().center()) - this->mapToScene(this->rect().center()); // x overlap between two colliding objects qreal overlap_x = std::min<qreal>(mapToScene(rect().topLeft()).x() + rect().width(), rectB->mapToScene(rectB->rect().topLeft()).x() + rectB->rect().width()) - std::max<qreal>(mapToScene(rect().topLeft()).x(), rectB->mapToScene(rectB->rect().topLeft()).x()); if (overlap_x > 0) { // y overlap between two colliding objects qreal overlap_y = std::min<qreal>(mapToScene(rect().topLeft()).y() + rect().height(), rectB->mapToScene(rectB->rect().topLeft()).y() + rectB->rect().height()) - std::max<qreal>(mapToScene(rect().topLeft()).y(), rectB->mapToScene(rectB->rect().topLeft()).y()); if (overlap_y > 0) { if (overlap_y > overlap_x) { if (dist_AB.x() < 0) { mList[i]->normal_x = -1; mList[i]->normal_y = 0; } else { mList[i]->normal_x = 1; mList[i]->normal_y = 0; } mList[i]->penetration = overlap_x; } else { if (dist_AB.y() < 0) { mList[i]->normal_x = 0; mList[i]->normal_y = -1; } else { mList[i]->normal_x = 0; mList[i]->normal_y = 1; } mList[i]->penetration = overlap_y; } } } ++i; } return mList; }
void GraphiItemCtrl::OnDrawEvent() { switch(i_CurrentType) { case ITM_TYPE_LINE: { QGraphicsLineItem* pLine = o_ItemScene.addLine(o_PointA.x(),o_PointA.y(), o_PointB.x(), o_PointB.y(),o_CurrentPen); int iKey = GetNextItemKey(); QLineF oLine = pLine->line(); map_Lines.insert(iKey,oLine); map_LineItems.insert(iKey, pLine); pLine->setData(CUSTDATA_KEY,iKey); emit NotifyLineAdded(iKey,oLine); break; } case ITM_TYPE_RECTANGLE: { double dWidth = o_PointB.x() - o_PointA.x(); double dHeight = o_PointB.y() - o_PointA.y(); QGraphicsRectItem* pRect = o_ItemScene.addRect(o_PointA.x(),o_PointA.y(), dWidth ,dHeight,o_CurrentPen); int iKey = GetNextItemKey(); QRectF oRect = pRect->rect(); map_Rects.insert(iKey, oRect); pRect->setData(CUSTDATA_KEY,iKey); emit NotifyRectAdded(iKey,oRect); break; } case ITM_TYPE_ELIPSE: { double dWidth = o_PointB.x() - o_PointA.x(); double dHeight = o_PointB.y() - o_PointA.y(); QGraphicsEllipseItem* pElipse = o_ItemScene.addEllipse(o_PointA.x(),o_PointA.y(), dWidth ,dHeight,o_CurrentPen); int iKey = GetNextItemKey(); pElipse->setData(CUSTDATA_KEY, iKey); QRectF oRect = pElipse->rect(); map_Elipses.insert(iKey,oRect); emit NotifyElipseAdded(iKey,oRect); break; } } }
void SymbolDataEditor::moveItem(const QPoint pos) { if (itemToChange == Item::NoItem || scene->items().isEmpty()) return; if (itemToChange == Item::InPoint || itemToChange == Item::OutPoint) { QPointF itemPos = mapToScene(pos); QGraphicsEllipseItem* item = static_cast<QGraphicsEllipseItem *>(scene->items(Qt::AscendingOrder).at(itemToChange)); item->setRect(itemPos.x() - pointWidth / 2, itemPos.y() - pointWidth / 2, pointWidth, pointWidth); } if (itemToChange == Item::LimitsRect) { QGraphicsRectItem* item = static_cast<QGraphicsRectItem *>(scene->items(Qt::AscendingOrder).at(itemToChange)); QPointF scenePos = mapToScene(pos); QRectF newRect = item->rect(); if (sideToChange & Side::Top) newRect.setTop(scenePos.y()); if (sideToChange & Side::Bottom) newRect.setBottom(scenePos.y()); if (sideToChange & Side::Left) newRect.setLeft(scenePos.x()); if (sideToChange & Side::Right) newRect.setRight(scenePos.x()); if (sideToChange == Side::AllSides) { newRect.moveCenter(scenePos + dLimitsCenter); } item->setRect(newRect); } }
void SymbolDataEditor::calculateSideToChange(QPoint pos) { sideToChange = Side::NoSide; if (itemToChange != Item::LimitsRect || scene->items().isEmpty()) return; QGraphicsRectItem* limitsItem = static_cast<QGraphicsRectItem *>(scene->items(Qt::AscendingOrder).at(itemToChange)); QRectF limitsRect = limitsItem->rect(); QPoint topLeft = mapFromScene(limitsRect.topLeft()); QPoint bottomRight = mapFromScene(limitsRect.bottomRight()); if (QRectF(topLeft, bottomRight).adjusted(-10,-10,10,10).contains(pos)) { if (abs(topLeft.x() - pos.x()) < 10) sideToChange = static_cast<Side>(sideToChange | Side::Left); if (abs(topLeft.y() - pos.y()) < 10) sideToChange = static_cast<Side>(sideToChange | Side::Top); if (abs(bottomRight.x() - pos.x()) < 10) sideToChange = static_cast<Side>(sideToChange | Side::Right); if (abs(bottomRight.y() - pos.y()) < 10) sideToChange = static_cast<Side>(sideToChange | Side::Bottom); } if (sideToChange & Side::Left && sideToChange & Side::Right) sideToChange = Side::Left; if (sideToChange & Side::Top && sideToChange & Side::Bottom) sideToChange = Side::Top; if (sideToChange == Side::NoSide && limitsRect.contains(mapToScene(pos))) { sideToChange = Side::AllSides; dLimitsCenter = limitsRect.center() - mapToScene(pos); } }
bool ZipplXmlReader::read( QIODevice *dev ) { setDevice( dev ); bool res = true; bool metaMode = false; mCurrParent = 0; mCurrSpot = 0; QGraphicsScene *scene = mGraphWidget->scene(); int spotID = 0; while (!atEnd()) { readNext(); if( isStartElement() ) { qDebug() << "XML name: " << name(); if( name() == "presentation") { // presentation mode: debug & presentation QString mode = attributes().value("mode").toString(); if( !mode.isEmpty() ) mMode = mode; mPath = attributes().value("path").toString(); if( !mPath.endsWith('/') ) mPath += "/"; qreal dx = qrealAttrib("width") / -2.0; qreal dy = qrealAttrib("height") / -2.0; QRectF rect( dx, dy, -2.0*dx, -2.0*dy ); scene->setSceneRect( rect ); } else if( name() == "meta" ) { metaMode = true; } else if( name() == "title" && metaMode ) { mPresentationTitle = readElementText(); } else if( name() == "description" && metaMode ) { mPresentationDescr = readElementText(); } else if( name() == "date" && metaMode ) { mPresentationDate = readElementText(); } else if( name() == "name" && metaMode ) { mAuthorName = readElementText(); } else if( name() == "email" && metaMode ) { mAuthorEmail = readElementText(); } else if( name() == "tocentry" ) { if( mCurrSpot ) { mCurrSpot->setData( TOCENTRY, readElementText() ); } } else if( name() == "spot" ) { if( mCurrParent != 0 ) { qDebug() << "Strange: Current Parent should be zero here!"; } QGraphicsRectItem *rectItem = new QGraphicsRectItem( ); rectItem->setPen( pen( rectItem->pen(), QColor("#aeaeae") )); mCurrParent = rectItem; mCurrSpot = rectItem; mCurrParent->setData( ID, QVariant( spotID++ )); mCurrParent->setPos( position() ); rectItem->setBrush( brush( rectItem->brush() ) ); scene->addItem( mCurrParent ); mSpots.append( mCurrParent ); // Prepare the hidden items list GraphicsItemList list; mHiddenItems.insert( mCurrParent, list ); } else if( name() == "hidden" ) { QGraphicsRectItem *rectItem = new QGraphicsRectItem( mCurrParent, scene ); rectItem->setPen( QPen( QColor( 240, 240, 240 ))); // append this hidden item to the list of hiddens of the parent spot. GraphicsItemList list = mHiddenItems[mCurrSpot]; list.append( rectItem ); mHiddenItems[mCurrSpot] = list; mCurrParent = rectItem; mCurrParent->setData( ID, QVariant( spotID++ )); } else if( name() == "rect" ) { if( mCurrParent ) { // within a spot qDebug() << "Creating a rectangle!"; QGraphicsRectItem *rectItem = new QGraphicsRectItem( mCurrParent, scene ); qreal width = qrealAttrib( "width" ); qreal height = qrealAttrib( "height" ); QPointF pos = position(); if( width > 0 && height > 0 ) { rectItem->setRect( pos.x(), pos.y(), width, height ); } else { rectItem->setPos( pos ); } rectItem->setPen( pen( rectItem->pen() ) ); mCurrParent = rectItem; } } else if( name() == "circle" ) { QPointF pos = position(); QGraphicsEllipseItem *ellipse = new QGraphicsEllipseItem( mCurrParent, scene ); // ellipse->setBrush( getBrush() ); qreal r = 2.0 * qrealAttrib( "r" ); QRectF rect( pos, QSizeF( r, r ) ); ellipse->setPen( pen( ellipse->pen() ) ); ellipse->setRect( rect ); } else if( name() == "text" ) { QGraphicsSimpleTextItem *textItem = new QGraphicsSimpleTextItem( mCurrParent, scene ); QString font = attributes().value("font").toString(); QString size = attributes().value("size").toString(); QFont currFont = textItem->font(); if( !font.isEmpty() ) { currFont.setFamily( font ); textItem->setFont( currFont ); } if( !size.isEmpty() ) { currFont.setPointSize( size.toInt() ); textItem->setFont( currFont ); } textItem->setPos( position() ); // set the brush QBrush b( textItem->brush() ); b.setColor( color() ); textItem->setBrush( b ); QString text = readElementText(); textItem->setText( text ); } else if( name() == "image" ) { if( handleImg( scene ) ) { } } } else if( isEndElement() ) { qDebug( ) << "XML CLOSE: " << name().toString(); if( name() == "spot" || name() == "toc" ) { QRectF rect = mCurrParent->childrenBoundingRect(); rect.setX(0); rect.setY(0); qgraphicsitem_cast<QGraphicsRectItem*>(mCurrParent)->setRect( rect); mCurrParent = 0; } else if( name() == "rect" ) { QGraphicsRectItem *item = qgraphicsitem_cast<QGraphicsRectItem*>(mCurrParent); if( item->rect().isEmpty() ) item->setRect( mCurrParent->childrenBoundingRect() ); mCurrParent = mCurrParent->parentItem(); } else if( name() == "hidden") { mCurrParent->setOpacity( 0.0 ); mCurrParent = mCurrParent->parentItem(); } else if( name() == "meta" ) { metaMode = false; } } } createToc( scene ); return res; }