void GraphicsScene::drawEdge(QPointF &lastPos, QPointF &pos, QPoint &ids) { QGraphicsLineItem *edge = new QGraphicsLineItem(lastPos.x() + (cidadeWidth / 2.0),lastPos.y() + (cidadeHeigth / 2.0), pos.x() + (cidadeWidth / 2.0),pos.y() + (cidadeHeigth / 2.0)); edge->setPen(QPen(Qt::black, 1, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin)); edge->setData(1, 3); edge->setData(2, ids); this->addItem(edge); }
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; } } }
RaceViewer::RaceViewer(const QList<GeoCoordinate> &racePoints, QWidget *parent) : QDialog(parent), mPoints(racePoints) { const int ratio = 300 * 1000; scene = new QGraphicsScene(this); view = new ResizableView(this); view->setScene(scene); view->scale(ratio, -ratio); QPointF prev = racePoints.at(0).projection(); QPointF cur; int j = 1; while (j < mPoints.size()) { cur = mPoints.at(j).projection(); if (prev != cur) { QGraphicsLineItem* segment; segment = scene->addLine(prev.x(), prev.y(), cur.x(), cur.y()); segment->setData(0, j - 1); prev = cur; j++; } else { mPoints.removeAt(j); } } qDebug() << j; QPointF first = racePoints.at(0).projection(); marker = new ExtensibleEllipseItem; marker->setRect(first.x(), first.y(), 4 / 10000.0, 4 / 10000.0); marker->moveBy(-2 / 10000.0, -2 / 10000.0); marker->setFlag(QGraphicsItem::ItemIsMovable, true); marker->setBrush(QColor(255, 0, 0, 127)); marker->setZValue(100); scene->addItem(marker); QPushButton* okButton = new QPushButton(tr("Ok")); connect(okButton, SIGNAL(clicked()), this, SLOT(accept())); QPushButton* cancelButton = new QPushButton(tr("Annuler")); connect(cancelButton, SIGNAL(clicked()), this, SLOT(reject())); QHBoxLayout* viewerLayout = new QHBoxLayout; viewerLayout->addWidget(view); QHBoxLayout* buttonLayout = new QHBoxLayout; buttonLayout->addWidget(cancelButton); buttonLayout->addWidget(okButton); QVBoxLayout* topLayout = new QVBoxLayout; topLayout->addLayout(viewerLayout); topLayout->addLayout(buttonLayout); setLayout(topLayout); }
void StitcherView::mouseReleaseEvent( QMouseEvent * event){ ImageView::mouseReleaseEvent(event); if(mode == Line && event->button() & Qt::LeftButton){ setMode(Default); QGraphicsLineItem * line = new QGraphicsLineItem(QLineF(mapToScene(lineOrigin),mapToScene(lineEnd))); line->setData(0,QString("Helper")); line->setZValue(11); QPen pen = line->pen(); pen.setColor(Qt::white); pen.setStyle(Qt::SolidLine); line->setPen(pen); graphicsScene->addItem(line); }else if(mode == Circle && event->button() & Qt::LeftButton){ setMode(Default); QPointF lineOriginF = mapToScene(lineOrigin); QPointF lineEndF = mapToScene(lineEnd); QPointF circleCenter = (lineOriginF+lineEndF)/2; qreal circleRadius = sqrt((lineOriginF-lineEndF).x()* (lineOriginF-lineEndF).x()+ (lineOriginF-lineEndF).y()* (lineOriginF-lineEndF).y())/2; if(QApplication::keyboardModifiers() & Qt::ShiftModifier){ circleCenter = mapFromScene(QPointF(0,0)); circleRadius = sqrt((circleCenter-lineEnd).x()* (circleCenter-lineEnd).x()+ (circleCenter-lineEnd).y()* (circleCenter-lineEnd).y()); circleCenter = QPointF(0,0); } QGraphicsEllipseItem * circle = new QGraphicsEllipseItem(QRect(circleCenter.x()-circleRadius,circleCenter.y()-circleRadius,circleRadius*2,circleRadius*2)); circle->setData(0,QString("Helper")); circle->setZValue(11); QPen pen = circle->pen(); pen.setColor(Qt::white); pen.setStyle(Qt::SolidLine); circle->setPen(pen); graphicsScene->addItem(circle); }else if(mode == AddPoint && event->button() & Qt::LeftButton){ QList<QGraphicsItem *> it = items(event->pos()); for(int i = 0; i < it.size(); i++){ if(ImageItem * item = qgraphicsitem_cast<ImageItem *>(it.at(i))){ item->addControlPoint(item->mapFromScene(mapToScene(event->pos()))); } } }else if(mode == DeletePoint && event->button() & Qt::LeftButton){ QList<QGraphicsItem *> it = items(event->pos()); for(int i = 0; i < it.size(); i++){ if(ImageItem * item = qgraphicsitem_cast<ImageItem *>(it.at(i))){ item->deleteControlPoint(item->mapFromScene(mapToScene(event->pos()))); } } }else if(mode == DeleteGuide && event->button() & Qt::LeftButton){ QList<QGraphicsItem *> it = items(event->pos()); QPointF pos = mapToScene(event->pos()); /* 10 px tolerance radius, delete the closest */ for(int i = 0; i < it.size(); i++){ if(QString("Helper") == it[i]->data(0)){ QGraphicsEllipseItem * elipse = qgraphicsitem_cast<QGraphicsEllipseItem *>(it[i]); if(elipse){ // Check if click position close to the line QPointF origin = elipse->rect().center(); qreal radius = elipse->rect().height()/2; QPointF d = origin-pos; if(abs(sqrt(d.x()*d.x()+d.y()*d.y())-radius) < 10){ delete elipse; } } QGraphicsLineItem * line = qgraphicsitem_cast<QGraphicsLineItem *>(it[i]); if(line){ delete line; } } } } }