void Canvas::removeCurrentItem() { currentScene->shapes.removeAt(currentScene->shapes.indexOf(currentShape())); delete currentShape(); currentScene->edge()->hide(); currentScene->update(); }
void Canvas::makeCurrentByName(QString name) { AbstractShape * tmp; for(auto & i : currentScene->shapes) { if(i->getName() == name) { tmp = i; break; } } if(tmp != currentScene->items().first()) { currentScene->removeItem(tmp); currentScene->addItem(tmp); AbstractShape * item = currentShape(); currentScene->edge()->setPen(currentPen); QRectF tmp = item->boundingRect(); currentScene->edge()->draw(tmp.topLeft().x(), tmp.topLeft().y(), tmp.bottomRight().x(), tmp.bottomRight().y()); currentScene->edge()->show(); currentScene->update(); edgeLocker = false; } }
Ref<Inspector::Protocol::Runtime::StructureDescription> StructureShape::inspectorRepresentation() { auto base = Inspector::Protocol::Runtime::StructureDescription::create().release(); Ref<Inspector::Protocol::Runtime::StructureDescription> currentObject = base.copyRef(); RefPtr<StructureShape> currentShape(this); while (currentShape) { auto fields = Inspector::Protocol::Array<String>::create(); auto optionalFields = Inspector::Protocol::Array<String>::create(); for (auto field : currentShape->m_fields) fields->addItem(field.get()); for (auto field : currentShape->m_optionalFields) optionalFields->addItem(field.get()); currentObject->setFields(&fields.get()); currentObject->setOptionalFields(&optionalFields.get()); currentObject->setConstructorName(currentShape->m_constructorName); currentObject->setIsImprecise(currentShape->m_isInDictionaryMode); if (currentShape->m_proto) { auto nextObject = Inspector::Protocol::Runtime::StructureDescription::create().release(); currentObject->setPrototypeStructure(&nextObject.get()); currentObject = WTF::move(nextObject); } currentShape = currentShape->m_proto; } return WTF::move(base); }
void GLWidget::paintGL() { //Clear target buffer and depth buffer glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); //First move the selected shape to last in the list //(The posh solution would be to allow the user to change the //order) if(!mSelectedShape.isNull()) { mShapes.remove(mSelectedShape); mShapes.push_back(mSelectedShape); } //Draw every shape //(This is what a foreach loop looks like in C++) for(std::list<shape_ptr>::iterator it = mShapes.begin(); it != mShapes.end(); it++) { shape_ptr currentShape(*it); bool shapeSelected = (currentShape == mSelectedShape); currentShape->draw(shapeSelected); } }
void Canvas::mouseReleaseEvent(QMouseEvent *) { if(currentShape()->type() != LineSegment::LineSegmentType::Type && !currentScene->shapes.isEmpty()) { AbstractShape * item = currentShape(); currentScene->edge()->setPen(currentPen); QRectF tmp = item->boundingRect(); currentScene->edge()->draw(tmp.topLeft().x(), tmp.topLeft().y(), tmp.bottomRight().x(), tmp.bottomRight().y()); currentScene->edge()->show(); currentScene->update(); edgeLocker = false; } isMoved = false; if(enableFill) { enableFill = false; currentScene->currentShape()->setPen(currentPen); if(currentShape()->type() == Triangle::TriangleType::Type && ((Triangle *)currentShape())->normalizeFlag) { currentScene->currentShape()->normalize(startX(), startY(), startX() - rect().topLeft().x()); } else { currentScene->currentShape()->draw(startX(), startY(), endX(), endY()); } currentScene->update(); } else _normalize = NormalizeNone; if(buttonPressed) { _direction = None; _normalize = NormalizeNone; isMoved = false; shapeDrawn = true; buttonPressed = false; shapeSet = false; enableResize = true; } }
void GLWidget::mousePressEvent(QMouseEvent* event) { Qt::MouseButton button = event->button(); if (button==Qt::LeftButton) { mClickLocationX = 2*event->x(); mClickLocationY = 2*event->y(); //See if the user clicked on a shape //(Shapes "on top" are last in the list so we iterate in reverse) for(std::list<shape_ptr>::reverse_iterator it = mShapes.rbegin(); it != mShapes.rend(); ++it) { shape_ptr currentShape(*it); //Check if a shape has been clicked on //(note that "y" is different between QT and openGL) if(currentShape->inside(mClickLocationX, 2*height()-mClickLocationY)) { mSelectedShape = nSelectedShape = currentShape; updateGL(); return; } else { nSelectedShape.clear(); } } //Nothing has been clicked on so deselect (set to null) mSelectedShape.clear(); updateGL(); } }
void Canvas::addShape(AbstractShape *shape) { if(!shapeDrawn) { AbstractShape * undrawnShape = currentShape(); currentScene->removeItem(undrawnShape); delete undrawnShape; undrawnShape = nullptr; } if(shape->type() == LineSegment::LineSegmentType::Type) { currentScene->lines.append((LineSegment *)shape); } else { currentScene->shapes.append(shape); emit addName(shape->getName()); } currentScene->addItem(shape); shapeDrawn = false; shapeSet = true; }
void Canvas::mouseMoveEvent(QMouseEvent *event) { if(!enableFill) { if(!isMoved) { AbstractShape * item = currentShape(); QPointF p = event->pos(); QRectF r = item->boundingRect(); QPointF tl = mapFromScene(r.topLeft()); QPointF br = mapFromScene(r.bottomRight()); QPointF tr = mapFromScene(r.topRight()); QPointF bl = mapFromScene(r.bottomLeft()); checkIfProperRect(tl, br, tr, bl); if(enableResize && shapeDrawn) { //checkIfProperRect(tl, br); if(item != nullptr && item->type() != AbstractShape::AbstractType::Type + 1) { if(belongToFirstCorners(p, tl, br)) { setCursor(Qt::SizeBDiagCursor); } /*else if(belongToSecondCorners(p, tl, br)) { setCursor(Qt::SizeFDiagCursor); }*/ else if(((p.x() <= tl.x() && p.x() > tl.x() - 3) && p.y() > tl.y() - 3 && p.y() < br.y()) || ((p.x() > br.x() - 3 && p.x() <= br.x()) && p.y() > tl.y() - 3 && p.y() < br.y())) { setCursor(Qt::SizeHorCursor); } else if(((p.y() > br.y() - 3 && p.y() <= br.y()) && p.x() > tl.x() - 3 && p.x() < br.x()) || (p.y() <= tl.y() && p.y() > tl.y() - 3) && p.x() > tl.x() - 3 && p.x() < br.x()) { setCursor(Qt::SizeVerCursor); } else if(p.x() >= tl.x() && p.x() <= br.x() && p.y() >= tl.y() && p.y() <= br.y()) { setCursor(Qt::SizeAllCursor); } else { setCursor(Qt::CrossCursor); } if(event->buttons() & Qt::LeftButton) { QPointF tl = mapFromScene(r.topLeft()); QPointF br = mapFromScene(r.bottomRight()); QPointF point = mapToScene(event->pos()); switch(_direction) { case Left : { if(sceneLocker(point)) { if(tl.x() > br.x()) { _endX = point.x(); } else { _startX = point.x(); } } break; } case Right : { if(sceneLocker(point)) { if(tl.x() > br.x()) { _startX = point.x(); } else { _endX = point.x(); } } break; } case Top: { if(sceneLocker(point)) { if(tl.y() > br.y()) { _endY = point.y(); } else { _startY = point.y(); } } break; } case Bottom : { if(sceneLocker(point)) { if(tl.y() > br.y()) { _startY = point.y(); } else { _endY = point.y(); } } break; } } item->draw(startX(), startY(), endX(), endY()); currentScene->edge()->draw(startX(), startY(), endX(), endY()); currentScene->update(); switch(_normalize) { case NormalizeLeft: { _startX = point.x(); _startY = point.y(); item->normalize(point.x(), point.y(), point.x() - rect().topLeft().x()); QRectF tmp = item->boundingRect(); currentScene->edge()->draw(tmp.topLeft().x(), tmp.topLeft().y(), tmp.bottomRight().x(), tmp.bottomRight().y()); _startX = tmp.topLeft().x(); _startY = tmp.topLeft().y(); _endX = tmp.bottomRight().x(); _endY = tmp.bottomRight().y(); if(item->type() == Triangle::TriangleType::Type) { ((Triangle*)item)->normalizeFlag = true; _startX = point.x(); _startY = point.y(); } break; } } } } } QPointF point = mapToScene(event->pos()); if(event->buttons() & Qt::LeftButton && _direction == None && _normalize == NormalizeNone) { preventOverBoundingOnDraw(point, _endX, _endY); if(shapeSet) { currentScene->currentShape()->setPen(currentPen); currentScene->currentShape()->draw(startX(), startY(), endX(), endY()); currentScene->update(); edgeLocker = true; } } } else if((event->buttons() & Qt::LeftButton) && isMoved && currentScene->currentShape()->type() != LineSegment::LineSegmentType::Type && (!currentScene->shapes.isEmpty() || !currentScene->lines.isEmpty())) { QRectF r = currentShape()->boundingRect(); QPoint tmp = QPoint(event->pos().x() - coordinatesIterationMove.x(), event->pos().y() - coordinatesIterationMove.y()); preventOverBoundingOnMove(r, tmp); currentScene->currentShape()->setPen(currentPen); if(currentShape()->type() == Triangle::TriangleType::Type && ((Triangle *)currentShape())->normalizeFlag) { currentScene->currentShape()->normalize(startX(), startY(), startX() - rect().topLeft().x()); } else { currentScene->currentShape()->draw(startX(), startY(), endX(), endY()); } if(!linesEnds.isEmpty()) { for(auto i: linePointIndexes) { preventOverBoundingOnDraw(linesEnds[linePointIndexes.indexOf(i)], linesEnds[linePointIndexes.indexOf(i)].rx(), linesEnds[linePointIndexes.indexOf(i)].ry()); linesEnds[linePointIndexes.indexOf(i)] += tmp; currentScene->lines.at(i)->draw(linesStarts[linePointIndexes.indexOf(i)].x(), linesStarts[linePointIndexes.indexOf(i)].y(), linesEnds[linePointIndexes.indexOf(i)].x(), linesEnds[linePointIndexes.indexOf(i)].y()); } } currentScene->edge()->hide(); currentScene->update(); coordinatesIterationMove = event->pos(); } } else { AbstractShape * tmp = currentShape(); if(tmp->isUnderMouse()) { setCursor(Qt::PointingHandCursor); } else { setCursor(Qt::CrossCursor); } } }
void Canvas::mousePressEvent(QMouseEvent *event) { if(event->buttons() & Qt::LeftButton) { if(enableFill) { AbstractShape * tmp = currentShape(); if(tmp->isUnderMouse()) { tmp->setBrush(QBrush(color())); currentScene->update(); } } else { QPointF point = mapToScene(event->pos()); _startX = point.x(); _startY = point.y(); QGraphicsItem * item = currentShape(); QPointF p = event->pos(); QRectF r = item->boundingRect(); QPointF tl = mapFromScene(r.topLeft()); QPointF br = mapFromScene(r.bottomRight()); QPointF tr = mapFromScene(r.topRight()); QPointF bl = mapFromScene(r.bottomLeft()); checkIfProperRect(tl, br, tr, bl); if(belongToFirstCorners(p, tl, br)) { _direction = None; _normalize = NormalizeLeft; } /*else if(belongToSecondCorners(p, tl, br)) { _direction = None; _normalize = NormalizeRight; }*/ else if((p.x() <= tl.x() && p.x() > tl.x() - 3) && p.y() > tl.y() - 3 && p.y() < br.y()) { _direction = Left; _startY = r.topLeft().y(); } else if((p.x() > br.x() - 3 && p.x() <= br.x()) && p.y() > tl.y() - 3 && p.y() < br.y()) { _direction = Right; _startX = r.topLeft().x(); _startY = r.topLeft().y(); _endY = r.bottomRight().y(); } else if((p.y() <= tl.y() && p.y() > tl.y() - 3) && p.x() > tl.x() - 3 && p.x() < br.x()) { _direction = Top; _startX = r.topLeft().x(); } else if((p.y() > br.y() - 3 && p.y() <= br.y()) && p.x() > tl.x() - 3 && p.x() < br.x()) { _direction = Bottom; _startX = r.topLeft().x(); _startY = r.topLeft().y(); _endX = r.bottomRight().x(); } else if(p.x() >= tl.x() && p.x() <= br.x() && p.y() >= tl.y() && p.y() <= br.y()) { _direction = None; isMoved = true; _startX = item->boundingRect().topLeft().x(); _startY = item->boundingRect().topLeft().y(); _endX = item->boundingRect().bottomRight().x(); _endY = item->boundingRect().bottomRight().y(); coordinatesIterationMove = event->pos(); linesEnds.clear(); linesStarts.clear(); linePointIndexes.clear(); for(auto & i : currentScene->lines) { if(i->boundingRect().intersects(currentShape()->boundingRect())) { QPointF tl = i->boundingRect().topLeft(); QPointF br = i->boundingRect().bottomRight(); if(currentShape()->boundingRect().contains(br) && !currentShape()->boundingRect().contains(tl)) { linesEnds.append(br.toPoint()); linesStarts.append(tl.toPoint()); linePointIndexes.append(currentScene->lines.indexOf(i)); } else if(currentShape()->boundingRect().contains(tl) && !currentShape()->boundingRect().contains(br)) { linesEnds.append(tl.toPoint()); linesStarts.append(br.toPoint()); linePointIndexes.append(currentScene->lines.indexOf(i)); } } } } else { _direction = None; currentScene->edge()->hide(); if(point.x() > sceneRect().topLeft().x() && point.y() > sceneRect().topLeft().y() && point.x() < sceneRect().bottomRight().x() && point.y() < sceneRect().bottomRight().y()) { enableResize = false; buttonPressed = true; if(shapeDrawn) { notifyObservers(); } } } } } }
void Canvas::updateScene() { currentShape()->setPen(currentPen); currentShape()->draw(startX(), startY(), endX(), endY()); currentScene->update(); }