OnMonitorCornersItem::cornersActions OnMonitorCornersItem::getMode(const QPointF &pos, int *corner) { *corner = -1; if (polygon().count() != 4) return NoAction; QPainterPath mouseArea; qreal size = 12; if (getView()) size /= m_view->matrix().m11(); mouseArea.addRect(pos.x() - size / 2, pos.y() - size / 2, size, size); for (int i = 0; i < 4; ++i) { if (mouseArea.contains(polygon().at(i))) { *corner = i; return Corner; } } if (KdenliveSettings::onmonitoreffects_cornersshowcontrols()) { if (mouseArea.contains(getCentroid())) return Move; for (int i = 0; i < 4; ++i) { int j = (i + 1) % 4; if (mouseArea.contains(QLineF(polygon().at(i), polygon().at(j)).pointAt(.5))) { *corner = i; return MoveSide; } } } return NoAction; }
void LensItem::hoverMoveEvent(QGraphicsSceneHoverEvent *event) { QPainterPath p; p.addEllipse(rect()); if (isSelected()) { if (p.contains(event->pos())) qApp->changeOverrideCursor(QCursor(Qt::SizeAllCursor)); else { QRectF br = boundingRect(); double siz = 6.0 / scaling; if (QRectF(br.x(), br.y(), siz, siz).contains(event->pos())) qApp->changeOverrideCursor(QCursor(Qt::SizeFDiagCursor)); else if (QRectF(br.x() + br.width(), br.y(), -siz, siz).contains(event->pos())) qApp->changeOverrideCursor(QCursor(Qt::SizeBDiagCursor)); else if (QRectF(br.x() + br.width(), br.y() + br.height(), -siz, -siz).contains(event->pos())) qApp->changeOverrideCursor(QCursor(Qt::SizeFDiagCursor)); else if (QRectF(br.x(), br.y() + br.height(), siz, -siz).contains(event->pos())) qApp->changeOverrideCursor(QCursor(Qt::SizeBDiagCursor)); else qApp->changeOverrideCursor(QCursor(Qt::ArrowCursor)); } } else qApp->changeOverrideCursor(QCursor(Qt::ArrowCursor)); }
/** * Checks if the given point is inside this closed polygon. If this * polyline is not closed (\see setClosed), false is returned. */ bool RPolyline::contains(const RVector& point, bool borderIsInside, double tolerance) const { if (!isGeometricallyClosed(tolerance)) { return false; } // check if point is on polyline: if (isOnShape(point, true, tolerance)) { return borderIsInside; } if (hasArcSegments()) { QPainterPath pp = toPainterPath(); return pp.contains(QPointF(point.x, point.y)); } int nvert = vertices.size(); int i, j; bool c = false; for (i=0, j=nvert-1; i<nvert; j=i++) { if (((vertices[i].y>point.y) != (vertices[j].y>point.y)) && (point.x < (vertices[j].x-vertices[i].x) * (point.y-vertices[i].y) / (vertices[j].y-vertices[i].y) + vertices[i].x) ) { c = !c; } } return c; }
void KisGradientPainterTest::testFindShapedExtremums() { QPolygonF selectionPolygon; selectionPolygon << QPointF(100, 100); selectionPolygon << QPointF(200, 120); selectionPolygon << QPointF(170, 140); selectionPolygon << QPointF(200, 180); selectionPolygon << QPointF(30, 220); QPolygonF selectionErasePolygon; selectionErasePolygon << QPointF(101, 101); selectionErasePolygon << QPointF(190, 120); selectionErasePolygon << QPointF(160, 140); selectionErasePolygon << QPointF(200, 180); selectionErasePolygon << QPointF(30, 220); QPainterPath path; path.addPolygon(selectionPolygon); path.closeSubpath(); path.addPolygon(selectionErasePolygon); path.closeSubpath(); QPointF center = KisPolygonalGradientShapeStrategy::testingCalculatePathCenter( 4, path, 2.0, true); dbgKrita << ppVar(center); QVERIFY(path.contains(center)); }
qreal PortHandler::circularPortId(const QPointF &location, const QStringList &types) const { for (int circularPortNumber = 0; circularPortNumber < mCircularPorts.count(); circularPortNumber++) { const StatCircular * const circularPort = mCircularPorts.at(circularPortNumber); if (!types.contains(circularPort->type())) { continue; } QPainterPathStroker ps; ps.setWidth(kvadratik); QPainterPath path; StatCircular::CircularPort circular = transformPortForNodeSize(circularPort); path.addEllipse({circular.x, circular.y}, circular.rx, circular.ry); path = ps.createStroke(path); if (path.contains(location)) { return circularPortNumber + mPointPorts.size() + mLinePorts.size() + (pointByCircularPortAngle(circularPortNumber, location) / 360.0); } } return nonexistentPortId; }
qreal PortHandler::linePortId(const QPointF &location, const QStringList &types) const { for (int linePortNumber = 0; linePortNumber < mLinePorts.count(); linePortNumber++) { const StatLine * const linePort = mLinePorts.at(linePortNumber); if (!types.contains(linePort->type())) { continue; } QPainterPathStroker ps; ps.setWidth(kvadratik - 5); QPainterPath path; const QLineF line = transformPortForNodeSize(linePort); path.moveTo(line.p1()); path.lineTo(line.p2()); path = ps.createStroke(path); if (path.contains(location)) { return linePortNumber + mPointPorts.size() + qMin(QLineF(line.p1(), location).length() / line.length() , mMaximumFractionPartValue); } } return nonexistentPortId; }
void ItemScene::determinePointsInSelection() { QRectF controlRect = this->selectionPathItem->path().controlPointRect(); QPainterPath path = this->selectionPathItem->path(); QImage sourceImage = this->imageFileItem->pixmap().toImage(); QImage extractedImage(controlRect.width(), controlRect.height(), QImage::Format_RGB888); for(int yPos = controlRect.topLeft().y(); yPos < controlRect.topLeft().y() + controlRect.height(); yPos++) { for(int xPos = controlRect.topLeft().x(); xPos < controlRect.topLeft().x() + controlRect.width(); xPos++) { if(path.contains(QPointF(xPos, yPos))) { extractedImage.setPixel( xPos - (controlRect.topRight().x() - controlRect.width()), yPos - (controlRect.bottomLeft().y() - controlRect.height()),sourceImage.pixel(xPos, yPos)); } else extractedImage.setPixel( xPos - (controlRect.topRight().x() - controlRect.width()), yPos - (controlRect.bottomLeft().y() - controlRect.height()), QRgb(qRgb(255,255,255))); } } this->imageFileItem->setPixmap(QPixmap::fromImage(extractedImage)); this->imageFileItem->setPos(this->sceneRect().center().operator -=(QPointF(extractedImage.width()/2, extractedImage.height()/2))); this->imageFileItem->update(); this->removeItem(this->selectionPathItem); for(QList<SelectionMarker*>::iterator iterator = this->selectionMarkers->begin(); iterator != this->selectionMarkers->end(); iterator++) { this->removeItem( (*iterator)); } }
/** * Checks if the given point is inside this closed polygon. If this * polyline is not closed (\see setClosed), false is returned. */ bool RPolyline::contains(const RVector& point) const { if (!closed) { return false; } QPainterPath pp = toPainterPath(); return pp.contains(QPointF(point.x, point.y)); }
rectActions OnMonitorRectItem::getMode(const QPointF &pos) { // Item mapped coordinates QPolygonF pol(rect().normalized()); QPainterPath top(pol.at(0)); top.lineTo(pol.at(1)); QPainterPath bottom(pol.at(2)); bottom.lineTo(pol.at(3)); QPainterPath left(pol.at(0)); left.lineTo(pol.at(3)); QPainterPath right(pol.at(1)); right.lineTo(pol.at(2)); QPainterPath mouseArea; qreal xsize = 12; qreal ysize = 12; if (getView()) { xsize /= m_view->matrix().m11(); ysize /= m_view->matrix().m22(); } mouseArea.addRect(pos.x() - xsize / 2, pos.y() - ysize / 2, xsize, ysize); // Check for collisions between the mouse and the borders if (mouseArea.contains(pol.at(0))) return ResizeTopLeft; else if (mouseArea.contains(pol.at(2))) return ResizeBottomRight; else if (mouseArea.contains(pol.at(1))) return ResizeTopRight; else if (mouseArea.contains(pol.at(3))) return ResizeBottomLeft; else if (top.intersects(mouseArea)) return ResizeTop; else if (bottom.intersects(mouseArea)) return ResizeBottom; else if (right.intersects(mouseArea)) return ResizeRight; else if (left.intersects(mouseArea)) return ResizeLeft; else if (rect().normalized().contains(pos)) return Move; else return NoAction; }
void LensItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event) { QPainterPath p; p.addEllipse(rect()); if ((p.contains(event->pos())) && (isSelected())) qApp->changeOverrideCursor(QCursor(Qt::SizeAllCursor)); else qApp->changeOverrideCursor(QCursor(Qt::ArrowCursor)); }
void BounceEdgeBlock::executeNextStep(ExecutionThread& executionThread) const { Sprite* sprite = executionThread.getSprite(); if(sprite != NULL) { SpriteView* s = new SpriteView(sprite); QSize screensize = sprite->getProgramModel()->getScreen()->getSize(); QPainterPath horedges; horedges.addRect(-50, 0, screensize.width() + 100, screensize.height()); QPainterPath veredges; veredges.addRect(0, -50, screensize.width(), screensize.height() + 100); if (!horedges.contains(s->mapToScene(s->shape()))) sprite->setRotation(-sprite->getRotation()); if (!veredges.contains(s->mapToScene(s->shape()))) sprite->setRotation(-sprite->getRotation() - 180); } executionThread.endExecution(NULL); }
static bool intersect_path(const QGraphicsItem *item, const QRectF &exposeRect, Qt::ItemSelectionMode mode, const QTransform &deviceTransform, const void *intersectData) { const QPainterPath scenePath = *static_cast<const QPainterPath *>(intersectData); QRectF brect = item->boundingRect(); _q_adjustRect(&brect); // ### Add test for this (without making things slower?) Q_UNUSED(exposeRect); bool keep = true; const QGraphicsItemPrivate *itemd = QGraphicsItemPrivate::get(item); if (itemd->itemIsUntransformable()) { // Untransformable items; map the scene rect to item coordinates. const QTransform transform = item->deviceTransform(deviceTransform); QPainterPath itemPath = (deviceTransform * transform.inverted()).map(scenePath); if (mode == Qt::ContainsItemShape || mode == Qt::ContainsItemBoundingRect) keep = itemPath.contains(brect); else keep = itemPath.intersects(brect); if (keep && (mode == Qt::ContainsItemShape || mode == Qt::IntersectsItemShape)) keep = QGraphicsSceneIndexPrivate::itemCollidesWithPath(item, itemPath, mode); } else { Q_ASSERT(!itemd->dirtySceneTransform); const QRectF itemSceneBoundingRect = itemd->sceneTransformTranslateOnly ? brect.translated(itemd->sceneTransform.dx(), itemd->sceneTransform.dy()) : itemd->sceneTransform.mapRect(brect); if (mode == Qt::ContainsItemShape || mode == Qt::ContainsItemBoundingRect) keep = scenePath.contains(itemSceneBoundingRect); else keep = scenePath.intersects(itemSceneBoundingRect); if (keep && (mode == Qt::ContainsItemShape || mode == Qt::IntersectsItemShape)) { QPainterPath itemPath = itemd->sceneTransformTranslateOnly ? scenePath.translated(-itemd->sceneTransform.dx(), -itemd->sceneTransform.dy()) : itemd->sceneTransform.inverted().map(scenePath); keep = QGraphicsSceneIndexPrivate::itemCollidesWithPath(item, itemPath, mode); } } return keep; }
/*! \internal Checks if item collides with the path and mode, but also checks that if it doesn't collide, maybe its frame rect will. */ bool QGraphicsSceneIndexPrivate::itemCollidesWithPath(const QGraphicsItem *item, const QPainterPath &path, Qt::ItemSelectionMode mode) { if (item->collidesWithPath(path, mode)) return true; if (item->isWidget()) { // Check if this is a window, and if its frame rect collides. const QGraphicsWidget *widget = static_cast<const QGraphicsWidget *>(item); if (widget->isWindow()) { QRectF frameRect = widget->windowFrameRect(); QPainterPath framePath; framePath.addRect(frameRect); bool intersects = path.intersects(frameRect); if (mode == Qt::IntersectsItemShape || mode == Qt::IntersectsItemBoundingRect) return intersects || path.contains(frameRect.topLeft()) || framePath.contains(path.elementAt(0)); return !intersects && path.contains(frameRect.topLeft()); } } return false; }
void GradientRangeEditor::mousePressEvent( QMouseEvent * event ) { switch( event->button() ) { case Qt::LeftButton: { { QPainterPath path; path.addEllipse( QRectF( QPointF( _startPoint.x() * width(), _startPoint.y() * height() ) - QPointF( 12, 12 ), QSizeF( 24, 24 ) ) ); if( path.contains( event->pos() ) ) { movedPoint = startMoved; break; } } { QPainterPath path; path.addEllipse( QRectF( QPointF( _endPoint.x() * width(), _endPoint.y() * height() ) - QPointF( 12, 12 ), QSizeF( 24, 24 ) ) ); if( path.contains( event->pos() ) ) { movedPoint = endMoved; break; } } break; } default: break; } }
int GradientStopEditor::stopFromPoint( const QPoint &point ) { int countStops = values.size(); for( int i = 0; i < countStops; i++ ) { QPainterPath path; path.addEllipse( QRectF( getStopPosition( i ) - QPointF( 6, 6 ), QSizeF( 12, 12 ) ) ); if( path.contains( point ) ) return i; } return -1; }
void MapEditorWidget::mousePressEvent ( QMouseEvent * event ){ if(popup){ return; } QPointF clickPos = transform.inverted().map(QPointF(event->pos())); int index = -1; for (int i=0; i<points.size(); ++i) { QPainterPath path; path.addEllipse(pointBoundingRect(i)); if (path.contains(event->pos())) { index = i; break; } } if(plotArea.contains(event->pos()) == false && index == -1){ return; } if (event->button() == Qt::LeftButton) { if (index == -1) { int pos = 0; // Insert sort for x or y for (int i=0; i<points.size(); ++i){ if (points.at(i).x() > clickPos.x()) { pos = i; break; } } points.insert(pos, clickPos); currentIndex = pos; firePointChange(); } else { currentIndex = index; } } else if (event->button() == Qt::RightButton) { if (index >= 0) { if(points.size() > 1){ points.remove(index); firePointChange(); } } } }
/** * Return index of closest segment. * * @param point The point which is to be tested for closeness. * * @return Index of the line segment closest to the \a point passed; * -1 if no line segment is closer to passed in \a point. */ int AssociationLine::closestSegmentIndex(const QPointF &point, qreal delta) const { QPainterPathStroker stroker; stroker.setWidth(delta); for(int i = 1; i < m_points.size(); ++i) { QLineF segment(m_points[i-1], m_points[i]); QPainterPath path; path.moveTo(segment.p1()); path.lineTo(segment.p2()); path = stroker.createStroke(path); if (path.contains(point)) { return i-1; } } return -1; }
int KPrIrisWipeEffectStrategyBase::findMaxScaling( const KPrPageEffect::Data &data ) { const int width = data.m_widget->width(); const int height = data.m_widget->height(); QPainterPath widget; widget.addRect( 0, 0, width, height ); int pathMaxMeasure; int maxMeasure; //We find whether the screen is taller or wider so that we can start searching //from a closer point if( width > height ) { pathMaxMeasure = m_shape.boundingRect().width(); maxMeasure = width; } else { pathMaxMeasure = m_shape.boundingRect().height(); maxMeasure = height; } //We now search from the previous point and incressing over and over till the shape fills //the widget given int halfWidth = width / 2; int halfHeight = height / 2; QPainterPath path; while( !path.contains( widget ) ) { QTransform matrix; matrix.translate( halfWidth, halfHeight ); double maxScaling = (double) maxMeasure / (double) pathMaxMeasure; matrix.scale( maxScaling, maxScaling ); path = matrix.map( m_shape ); maxMeasure += 5;//we don't need to be very precise } return maxMeasure; }
void MapEditorWidget::mouseDoubleClickEvent ( QMouseEvent * event ){ //QPointF clickPos = transform.inverted().map(QPointF(event->pos())); int index = -1; if(popup){ /* there is already a point being edited */ return; } for (int i=0; i<points.size(); ++i) { QPainterPath path; path.addEllipse(pointBoundingRect(i)); if (path.contains(event->pos())) { index = i; break; } } if(index != -1){ popup = new MapEditorPopup(event->pos(),points[index],index,this); popup->show(); if(plotArea.contains(popup->geometry()) == false){ QRectF united = plotArea.united(popup->geometry()); QPoint move(0,0); if(united.bottomRight() != plotArea.bottomRight()){ if(united.bottomRight().x()!=plotArea.bottomRight().x()){ move.rx() -= popup->width()+pointSize.width(); } move.ry() -= united.bottomRight().y()-plotArea.bottomRight().y(); } if(united.topLeft() != plotArea.topLeft()){ move.rx() -= united.topLeft().x()-plotArea.topLeft().x(); move.ry() -= united.topLeft().y()-plotArea.topLeft().y(); } popup->move(popup->pos()+move); } connect(popup,SIGNAL(editingFinished()),this,SLOT(updatePointFromPopup())); }else{ QWidget::mouseDoubleClickEvent(event); } }
bool HoverPoints::eventFilter(QObject *object, QEvent *event) { if (object == m_widget && m_enabled) { switch (event->type()) { case QEvent::MouseButtonPress: { if (!m_fingerPointMapping.isEmpty()) return true; QMouseEvent *me = (QMouseEvent *) event; QPointF clickPos = me->pos(); int index = -1; for (int i=0; i<m_points.size(); ++i) { QPainterPath path; if (m_shape == CircleShape) path.addEllipse(pointBoundingRect(i)); else path.addRect(pointBoundingRect(i)); if (path.contains(clickPos)) { index = i; break; } } if (me->button() == Qt::LeftButton) { if (index == -1) { if (!m_editable) return false; int pos = 0; // Insert sort for x or y if (m_sortType == XSort) { for (int i=0; i<m_points.size(); ++i) if (m_points.at(i).x() > clickPos.x()) { pos = i; break; } } else if (m_sortType == YSort) { for (int i=0; i<m_points.size(); ++i) if (m_points.at(i).y() > clickPos.y()) { pos = i; break; } } m_points.insert(pos, clickPos); m_locks.insert(pos, 0); m_currentIndex = pos; firePointChange(); } else { m_currentIndex = index; } return true; } else if (me->button() == Qt::RightButton) { if (index >= 0 && m_editable) { if ((m_points.size() - 1) < m_minCountPoints) return true; if (m_locks[index] == 0) { m_locks.remove(index); m_points.remove(index); } firePointChange(); return true; } } } break; case QEvent::MouseButtonRelease: if (!m_fingerPointMapping.isEmpty()) return true; m_currentIndex = -1; break; case QEvent::MouseMove: if (!m_fingerPointMapping.isEmpty()) return true; if (m_currentIndex >= 0) movePoint(m_currentIndex, ((QMouseEvent *)event)->pos()); break; case QEvent::TouchBegin: case QEvent::TouchUpdate: { const QTouchEvent *const touchEvent = static_cast<const QTouchEvent*>(event); const QList<QTouchEvent::TouchPoint> points = touchEvent->touchPoints(); const qreal pointSize = qMax(m_pointSize.width(), m_pointSize.height()); Q_FOREACH (const QTouchEvent::TouchPoint &touchPoint, points) { const int id = touchPoint.id(); switch (touchPoint.state()) { case Qt::TouchPointPressed: { // find the point, move it QSet<int> activePoints = QSet<int>::fromList(m_fingerPointMapping.values()); int activePoint = -1; qreal distance = -1; const int pointsCount = m_points.size(); const int activePointCount = activePoints.size(); if (pointsCount == 2 && activePointCount == 1) { // only two points activePoint = activePoints.contains(0) ? 1 : 0; } else { for (int i=0; i<pointsCount; ++i) { if (activePoints.contains(i)) continue; qreal d = QLineF(touchPoint.pos(), m_points.at(i)).length(); if ((distance < 0 && d < 12 * pointSize) || d < distance) { distance = d; activePoint = i; } } } if (activePoint != -1) { m_fingerPointMapping.insert(touchPoint.id(), activePoint); movePoint(activePoint, touchPoint.pos()); } } break; case Qt::TouchPointReleased: { // move the point and release QHash<int,int>::iterator it = m_fingerPointMapping.find(id); movePoint(it.value(), touchPoint.pos()); m_fingerPointMapping.erase(it); } break; case Qt::TouchPointMoved: { // move the point const int pointIdx = m_fingerPointMapping.value(id, -1); if (pointIdx >= 0) // do we track this point? movePoint(pointIdx, touchPoint.pos()); } break; default: break; } } if (m_fingerPointMapping.isEmpty()) { event->ignore(); return false; } else { return true; } } break; case QEvent::TouchEnd: if (m_fingerPointMapping.isEmpty()) { event->ignore(); return false; } return true; break; case QEvent::Resize: { QResizeEvent *e = (QResizeEvent *) event; if (e->oldSize().width() == 0 || e->oldSize().height() == 0) break; qreal stretch_x = e->size().width() / qreal(e->oldSize().width()); qreal stretch_y = e->size().height() / qreal(e->oldSize().height()); for (int i=0; i<m_points.size(); ++i) { QPointF p = m_points[i]; movePoint(i, QPointF(p.x() * stretch_x, p.y() * stretch_y), false); } firePointChange(); break; } case QEvent::Paint: { QWidget *that_widget = m_widget; m_widget = 0; QApplication::sendEvent(object, event); m_widget = that_widget; paintPoints(); return true; } default: break; } } return false; }
bool PathCutPlugin::run(ScribusDoc* doc, QString) { QString vers = QString(qVersion()).left(5); if (vers < "4.3.3") { QMessageBox::information(doc->scMW(), tr("Qt Version too old"), tr("This plugin requires at least version 4.3.3 of the Qt library")); return true; } ScribusDoc* currDoc = doc; if (currDoc == 0) currDoc = ScCore->primaryMainWindow()->doc; if (currDoc->m_Selection->count() > 1) { PageItem *Item1 = currDoc->m_Selection->itemAt(0); PageItem *Item2 = currDoc->m_Selection->itemAt(1); if (Item1->itemType() != PageItem::PolyLine) { Item1 = currDoc->m_Selection->itemAt(1); Item2 = currDoc->m_Selection->itemAt(0); } FPointArray path = Item1->PoLine; QPainterPathStroker stroke; stroke.setWidth(Item1->lineWidth()); QPainterPath cutter = stroke.createStroke(path.toQPainterPath(false)); QMatrix ms; ms.translate(Item1->xPos() - Item2->xPos(), Item1->yPos() - Item2->yPos()); ms.rotate(Item1->rotation()); cutter = ms.map(cutter); path.map(ms); FPoint start = path.point(0); FPoint end = path.point(path.size()-2); QMatrix mm; mm.rotate(Item2->rotation()); QPainterPath objekt = mm.map(Item2->PoLine.toQPainterPath(true)); if ((objekt.contains(QPointF(start.x(), start.y()))) || (objekt.contains(QPointF(end.x(), end.y())))) { QMessageBox::information(doc->scMW(), tr("Error"), tr("The cutting line must cross the polygon and\nboth end points must lie outside of the polygon")); return true; } QPainterPath result = objekt.subtracted(cutter); FPointArray points; points.fromQPainterPath(result); Item2->PoLine = points; Item2->Frame = false; Item2->ClipEdited = true; Item2->FrameType = 3; currDoc->AdjustItemSize(Item2); Item2->OldB2 = Item2->width(); Item2->OldH2 = Item2->height(); Item2->updateClip(); Item2->ContourLine = Item2->PoLine.copy(); currDoc->m_Selection->clear(); currDoc->m_Selection->addItem(Item1); currDoc->itemSelection_DeleteItem(); currDoc->m_Selection->clear(); currDoc->m_Selection->addItem(Item2); currDoc->itemSelection_SplitItems(); currDoc->changed(); } return true; }
bool HoverPoints::eventFilter( QObject* object, QEvent* hoverEvent ) { // If the selected object is the transfer object and the widget is enabled. if( object != _colorMapWidget || !_enabled || !_editable ) return false; auto& controlPoints = _colorMap.getControlPoints( _channel ); if( controlPoints.empty( )) return false; const float w = _colorMapWidget->width(); const float h = _colorMapWidget->height(); // Detect the event type. switch ( hoverEvent->type()) { case QEvent::MouseButtonPress: { QMouseEvent* mouseEvent = (QMouseEvent*)hoverEvent; QPointF clickPosition = mouseEvent->pos(); QPointF selectedPoint = emptyPoint; for( size_t i = 0; i < controlPoints.size(); ++i ) { // Select the shape of the bounding rectangle of the volume // whether it is circle of rectangle. QPainterPath touchSurface; const auto& cp = controlPoints[ i ]; const QPointF point( cp.getX() / 256.0f * w, ( 1.0f - cp.getY( )) * h ); touchSurface.addEllipse( point, controlPointSize, controlPointSize ); // If the mouse event was applied in this boundary of the point, // set the index to that of the selected point. if( touchSurface.contains( clickPosition )) { selectedPoint = { cp.getX(), cp.getY() }; break; } } // If the Qt::LeftButton is clicked where there are no points, // insert a new point, and if the Qt::LeftButton is clicked where // a point already exists, then move the HoverPoint. if( mouseEvent->button() == Qt::LeftButton ) { // If there is no point where the mouse is clicked, then create // a new point. if( selectedPoint == emptyPoint ) { lexis::render::ControlPoint cp( (float)clickPosition.x() / w * 256.0f, 1.0f - (float)clickPosition.y() / h ); // Insert a new point at this position. _colorMap.addControlPoint( cp, _channel ); _selectedPoint = QPointF( cp.getX(), cp.getY( )); // Select the point // Update the system. emit pointsChanged(); } else // If there is a specific point that is clicked, get it. _selectedPoint = selectedPoint; // We have created or selected a point. return true; } // If the Qt::RightButton is selcted where there is a point, then // delete this point and update the system. else if( mouseEvent->button() == Qt::RightButton ) { // If there is a specified point that is selected based on // the index and the widget is editible. if( selectedPoint != emptyPoint ) { const float selectedX = selectedPoint.x(); // If it is the last point or the first point, do not remove if( selectedX == controlPoints[ 0 ].getX() || selectedX == controlPoints[ controlPoints.size() - 1 ].getX( )) { return true; } _colorMap.removeControlPoint( selectedX, _channel ); // Update the system. emit pointsChanged(); // We have deleted a point. return true; } } } break; case QEvent::MouseButtonRelease: { _selectedPoint = emptyPoint; } break; case QEvent::MouseMove: { // If there is a point selected with a specific index, move it. if( _selectedPoint == emptyPoint ) return false; const auto& pos = ((QMouseEvent*)hoverEvent )->pos(); const float y = std::max( std::min( 1.0f, 1.0f - pos.y() / h ), 0.0f ); const float selectedX = _selectedPoint.x(); // If it is the last point or the first point if( selectedX == controlPoints[ 0 ].getX() || selectedX == controlPoints[ controlPoints.size() - 1 ].getX( )) { // Update the control point only on y direction _colorMap.addControlPoint( { selectedX, y }, _channel ); emit pointsChanged(); return true; } const float x = pos.x() / w * 256.0f; // If position reaches or passes the boundaries if( x <= controlPoints[ 0 ].getX( ) || x >= controlPoints[ controlPoints.size() - 1 ].getX( )) { return true; } // If there is previously a control point in the position for( size_t i = 0; i < controlPoints.size(); ++i ) { if( controlPoints[ i ].getX() == x && _selectedPoint.x() != x ) return true; } _colorMap.removeControlPoint( selectedX, _channel ); _colorMap.addControlPoint( { x, y }, _channel ); _selectedPoint = { x, y }; // Update the system. emit pointsChanged(); return true; } break; case QEvent::Resize: // Resize the points. { // Update the system. emit pointsChanged(); break; } case QEvent::Paint: // Render the points on the widget. { QWidget* tfWidget = _colorMapWidget; _colorMapWidget = 0; QApplication::sendEvent( object, hoverEvent ); _colorMapWidget = tfWidget; // Paing all the points on the widget. paintPoints(); return true; } default: break; } // No event is selected. return false; }
void findTriangle(QVector <BCofRegionPoint> &RegionPoint, QVector<QPoint> *vertex, QVector<int> *triList, int width) { int left, right, up, bottom; QPoint vertex1,vertex2,vertex3; BCofRegionPoint tmp; // QFile file("Vertex.txt"); // if (!file.open(QIODevice::ReadWrite | QIODevice::Text)) // return; // QTextStream out(&file); for(int i = 0; i < triList->size() / 3; ++i) { QPainterPath triangle; vertex1 = vertex->at(triList->at(i * 3)); vertex2 = vertex->at(triList->at(i * 3 + 1)); vertex3 = vertex->at(triList->at(i * 3 + 2)); //out << i <<":"<<triList->at(i*3)<<" "<<triList->at(i*3 + 1)<<" "<<triList->at(i*3 + 2)<<"\n"; //file.flush(); triangle.moveTo(vertex1); triangle.lineTo(vertex2); triangle.lineTo(vertex3); //triangle.lineTo(vertex1); tmp.seq = i; tmp.v1 = 1.; tmp.v2 = 0.; tmp.v3 = 0.; RegionPoint.push_back(tmp); tmp.v1 = 0.; tmp.v2 = 1.; tmp.v3 = 0.; RegionPoint.push_back(tmp); tmp.v1 = 0.; tmp.v2 = 0.; tmp.v3 = 1.; RegionPoint.push_back(tmp); left = MIN(MIN(vertex1.x(),vertex2.x()),vertex3.x()); right = MAX(MAX(vertex1.x(),vertex2.x()),vertex3.x()); up = MIN(MIN(vertex1.y(),vertex2.y()),vertex3.y()); bottom = MAX(MAX(vertex1.y(),vertex2.y()),vertex3.y()); cv::Mat rp(bottom-up+1,right-left+1,CV_8UC3); rp.setTo(0); std::ofstream of("qt.txt"); int count = 0; for(int n = up; n <= bottom; ++n) { for(int m = left; m <= right; ++m) { if(triangle.contains(QPoint(m,n))) { tmp.seq = i; barycenter(tmp, m, n, vertex1,vertex2,vertex3); RegionPoint.push_back(tmp); rp.at<cv::Vec3b>(n-up,m-left) = cv::Vec3b(255,255,255); of<<m<<" "<<n<<";"; count++; } } } of<<std::endl<<count; of.close(); cv::imwrite("rp.png",rp); } // file.close(); }
bool HoverPoints::eventFilter( QObject* object, QEvent* hoverEvent ) { // If the selected object is the transfer object and the widget is enabled. if( object == _tfWidget && _enabled ) { // Detect the event type. switch ( hoverEvent->type()) { case QEvent::MouseButtonPress: { if( !_fingerPointMapping.isEmpty()) return true; QMouseEvent* mouseEvent = (QMouseEvent*)hoverEvent; QPointF clickPosition = mouseEvent->pos(); int index = -1; for( int32_t i = 0; i < _tfPoints.size(); ++i ) { // Select the shape of the bounding rectangle of the volume // whether it is circle of rectangle. QPainterPath touchSurface; if( _pointShape == CIRCLE_POINT ) touchSurface.addEllipse( _pointBoundingRectangle( i )); else touchSurface.addRect( _pointBoundingRectangle( i )); // If the mouse event was applied in this boundary of the point, // set the index to that of the selected point. if (touchSurface.contains( clickPosition )) { index = i; break; } } // If the Qt::LeftButton is clicked where there are no points, // insert a new point, and if the Qt::LeftButton is clicked where // a point already exists, then move the HoverPoint. if( mouseEvent->button() == Qt::LeftButton ) { // If there is no point where the mouse is clicked, then create // a new point. if( index == -1 ) { // If the widget (point) is not editible, return. if ( !_editable ) return false; // TODO: Insert sort for x or y int position = 0; if( _sortType == X_SORT ) { for( int32_t i = 0; i < _tfPoints.size(); ++i ) { if( _tfPoints.at(i).x() > clickPosition.x()) { position = i; break; } } } else if( _sortType == Y_SORT ) { for ( int32_t i = 0; i < _tfPoints.size(); ++i ) { if( _tfPoints.at(i).y() > clickPosition.y()) { position = i; break; } } } // Insert a new point at this position. _tfPoints.insert( position, clickPosition ); _locks.insert( position, 0 ); _currentPointIndex = position; // Update the system. firePointChange(); } else // If there is a specific point that is clicked, get it. { _currentPointIndex = index; } // We have created or selected a point. return true; } // If the Qt::RightButton is selcted where there is a point, then // delete this point and update the system. else if( mouseEvent->button() == Qt::RightButton ) { // If there is a specified point that is selected based on // the index and the widget is editible. if( index >= 0 && _editable ) { if( _locks[index] == 0 ) { // Remove the point from the list. _locks.remove( index ); _tfPoints.remove( index ); } // Update the system. firePointChange(); // We have deleted a point. return true; } } } break; case QEvent::MouseButtonRelease: { if( !_fingerPointMapping.isEmpty()) return true; _currentPointIndex = -1; } break; case QEvent::MouseMove: { // If there is no point selected, do nothing. if( !_fingerPointMapping.isEmpty()) return true; // If there is a point selected with a specific index, move it. if( _currentPointIndex >= 0 ) _movePoint( _currentPointIndex, ((QMouseEvent*)hoverEvent )->pos()); } break; case QEvent::TouchBegin: /// Events for the rendeirng widget. case QEvent::TouchUpdate: /// Events for the rendeirng widget. { const QTouchEvent* const touchEvent = static_cast<const QTouchEvent*>( hoverEvent ); const QList<QTouchEvent::TouchPoint> touchPoints = touchEvent->touchPoints(); const qreal pointSize = qMax( _pointSize.width(), _pointSize.height() ); foreach ( const QTouchEvent::TouchPoint &touchPoint, touchPoints ) { const int touchId = touchPoint.id(); switch ( touchPoint.state()) { case Qt::TouchPointPressed: { // Find the point and then move it QSet<int> activePoints = QSet<int>::fromList( _fingerPointMapping.values()); int activePoint = -1; qreal distance = -1; const int pointsCount = _tfPoints.size(); const int activePointCount = activePoints.size(); // You are allowed to only have two points on the rendering // widget. if( pointsCount == 2 && activePointCount == 1 ) activePoint = activePoints.contains(0) ? 1 : 0; else { for( int32_t i = 0; i < pointsCount; ++i ) { if( activePoints.contains( i )) continue; qreal d = QLineF( touchPoint.pos(), _tfPoints.at(i)).length(); if(( distance < 0 && d < 12 * pointSize) || d < distance ) { distance = d; activePoint = i; } } } if( activePoint != -1 ) { _fingerPointMapping.insert( touchPoint.id(), activePoint ); _movePoint( activePoint, touchPoint.pos()); } } break; case Qt::TouchPointReleased: /// Events for the rendeirng widget. { // Move the point and release QHash<int,int>::iterator it = _fingerPointMapping.find( touchId ); _movePoint( it.value(), touchPoint.pos()); _fingerPointMapping.erase( it ); } break; case Qt::TouchPointMoved: /// Events for the rendeirng widget. { // Move the point const int pointIdx = _fingerPointMapping.value( touchId, -1 ); if( pointIdx >= 0 ) _movePoint(pointIdx, touchPoint.pos()); } break; default: break; } } if( _fingerPointMapping.isEmpty()) { hoverEvent->ignore(); return false; } else { return true; } } break; case QEvent::TouchEnd: if( _fingerPointMapping.isEmpty()) { hoverEvent->ignore(); return false; } return true; break; case QEvent::Resize: // Resize the points. { QResizeEvent* resizeEvent = (QResizeEvent *) hoverEvent; const int oldPointWidth = resizeEvent->oldSize().width(); const int oldPointHeight = resizeEvent->oldSize().height(); if( oldPointWidth == 0 || oldPointHeight == 0 ) break; const int newPointWidth = resizeEvent->size().width(); const int newPointHeight = resizeEvent->size().height(); const qreal scaleX = newPointWidth / qreal( oldPointWidth ); const qreal scaleY = newPointHeight / qreal( oldPointHeight ); // Update the size of all the points in the transfer function widget. for( int32_t i = 0; i < _tfPoints.size(); ++i ) { QPointF p = _tfPoints[i]; _movePoint( i, QPointF(p.x() * scaleX, p.y() * scaleY), false ); } // Update the system. firePointChange(); break; } case QEvent::Paint: // Render the points on the widget. { QWidget* tfWidget = _tfWidget; _tfWidget = 0; QApplication::sendEvent( object, hoverEvent ); _tfWidget = tfWidget; // Paing all the points on the widget. paintPoints(); return true; } default: break; } } // No event is selected. return false; }
bool Breakpoints::eventFilter(QObject *object, QEvent *event) { if( object != m_widget ) { return false; } switch (event->type()) { case QEvent::MouseButtonPress: { QMouseEvent *me = (QMouseEvent *) event; QPointF clickPos = me->pos(); for(m_selectedBreakpoint = m_model->GetBreakpoints().begin(); m_selectedBreakpoint != m_model->GetBreakpoints().end(); ++m_selectedBreakpoint) { QPainterPath path; path.addEllipse(pointBoundingRect(m_selectedBreakpoint)); if (path.contains(clickPos)) { break; } } if (me->button() == Qt::LeftButton) { if (m_selectedBreakpoint == m_model->GetBreakpoints().end() ) { double px, py; CalculatePercent(clickPos.x(), clickPos.y(), px, py); ElVis::Color c; c.SetAlpha(py); c.SetGreen(1.0); c.SetRed(1.0); c.SetBlue(1.0); m_selectedBreakpoint = m_model->InsertBreakpoint(px, c); } } else if (me->button() == Qt::RightButton) { if( m_selectedBreakpoint != m_model->GetBreakpoints().end() ) { m_model->RemoveBreakpoint(m_selectedBreakpoint); m_selectedBreakpoint = m_model->GetBreakpoints().end(); } } OnSelectedPointChanged(m_selectedBreakpoint); OnBreakpointsChanged(); return true; } break; case QEvent::MouseButtonRelease: break; case QEvent::MouseMove: if (m_selectedBreakpoint != m_model->GetBreakpoints().end() ) { const QPointF& point = ((QMouseEvent *)event)->pos(); double px, py; CalculatePercent(point.x(), point.y(), px, py); ElVisFloat key = px; while( m_model->GetBreakpoints().find(key) != m_model->GetBreakpoints().end()) { key += .00001; } ElVis::Color c = (*m_selectedBreakpoint).second.Col; c.SetAlpha(py); m_model->RemoveBreakpoint(m_selectedBreakpoint); m_selectedBreakpoint = m_model->InsertBreakpoint(key, c); OnBreakpointsChanged(); } break; case QEvent::Resize: { //QResizeEvent *e = (QResizeEvent *) event; //if (e->oldSize().width() == 0 || e->oldSize().height() == 0) // break; //qreal stretch_x = e->size().width() / qreal(e->oldSize().width()); //qreal stretch_y = e->size().height() / qreal(e->oldSize().height()); //for (int i=0; i<m_points.size(); ++i) //{ // QPointF p = m_points[i]; // movePoint(i, QPointF(p.x() * stretch_x, p.y() * stretch_y), false); //} //update(); break; } case QEvent::Paint: { ColorMapRect* that_widget = m_widget; m_widget = 0; QApplication::sendEvent(object, event); m_widget = that_widget; paintPoints(); return true; } default: break; } return false; }
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> const Geometry* GeometryLayer::geometryAt(const QPoint& point) { for (int i = 0; i < _geometries.size(); i++) { switch ( _geometries.at(i)->type() ) { case Geometry::Line: { Line* line = dynamic_cast<Line*>(_geometries.at(i)); if ( !line ) continue; QString eq; bool found = Math::pointLiesOnLine(point, line->screenStart(), line->screenEnd(), &eq); if ( found ) return _geometries.at(i); } break; case Geometry::d_Epicenter: { Epicenter* epicenter = dynamic_cast<Epicenter*>(_geometries.at(i)); if ( !epicenter ) continue; //! Enlarge path's ellipse so it can contain at least 3 times //! the epicenter's screen size (taking the pen width in account)... QPainterPath path; path.addEllipse(epicenter->screenPosition().x() - epicenter->screenSize().width() * 2, epicenter->screenPosition().y() - epicenter->screenSize().height() * 2, epicenter->screenSize().width() * 3, epicenter->screenSize().height() * 3); if ( path.contains(point) ) return _geometries.at(i); } break; default: { //! Over-scanning a lil'bit to ensure tooltips are displayed //! when mouse is genuinely at the point which is relative to //! the closeness of the converted lat/lon into screen coordinates. //! Therefore we use a factor 2 since we've displaced the point //! coordinates by its width and height... QPainterPath path; path.addEllipse(_geometries.at(i)->screenPosition().toPoint().x() - _geometries.at(i)->size().width(), _geometries.at(i)->screenPosition().toPoint().y() - _geometries.at(i)->size().height(), _geometries.at(i)->size().width() * 2, _geometries.at(i)->size().height() * 2); if ( path.contains(point) ) return _geometries.at(i); } break; } } return NULL; }
bool HoverPoints::eventFilter(QObject *object, QEvent *event) { if (object == m_widget && m_enabled) { switch (event->type()) { case QEvent::MouseButtonPress: { QMouseEvent *me = (QMouseEvent *) event; QPointF clickPos = me->pos(); int index = -1; for (int i = 0; i < m_points.size(); ++i) { QPainterPath path; if (m_shape == CircleShape) { path.addEllipse(pointBoundingRect(i)); } else { if (m_shape == RectangleShape) { path.addRect(pointBoundingRect(i)); } } if (path.contains(clickPos)) { index = i; break; } } if (me->button() == Qt::LeftButton) { if (index == -1) { if (!m_editable) { return false; } int pos = 0; // Insert sort for x or y if (m_sortType == XSort) { for (int i = 0; i < m_points.size(); ++i) { if (m_points.at(i).x() > clickPos.x()) { pos = i; break; } } } else if (m_sortType == YSort) { for (int i = 0; i < m_points.size(); ++i) { if (m_points.at(i).y() > clickPos.y()) { pos = i; break; } } } m_points.insert(pos, clickPos); m_locks.insert(pos, 0); m_currentIndex = pos; firePointChange(); } else { m_currentIndex = index; } return true; } else if (me->button() == Qt::RightButton) { if (index >= 0 && m_editable) { if (m_locks[index] == 0) { m_locks.remove(index); m_points.remove(index); } firePointChange(); return true; } } } break; case QEvent::MouseButtonRelease: m_currentIndex = -1; break; case QEvent::MouseMove: if (m_currentIndex >= 0) { movePoint(m_currentIndex, ((QMouseEvent *)event)->pos()); } break; case QEvent::Resize: { if (m_widget->isVisible()) { QResizeEvent *e = (QResizeEvent *) event; double stretch_x = e->size().width() / double(e->oldSize().width()); double stretch_y = e->size().height() / double(e->oldSize().height()); for (int i = 0; i < m_points.size(); ++i) { QPointF p = m_points[i]; movePoint(i, QPointF(p.x() * stretch_x, p.y() * stretch_y), false); } firePointChange(); m_width = e->size().width(); m_height = e->size().height(); } break; } case QEvent::Show: { if (m_width != m_widget->width() || m_height != m_widget->height()) { if (m_width == -1 && m_height == -1) { m_width = m_widget->width(); m_height = m_widget->height(); } double stretch_x = m_widget->width() / double(m_width); double stretch_y = m_widget->height() / double(m_height); for (int i = 0; i < m_points.size(); ++i) { QPointF p = m_points[i]; movePoint(i, QPointF(p.x() * stretch_x, p.y() * stretch_y), false); } firePointChange(); m_width = m_widget->width(); m_height = m_widget->height(); } break; } case QEvent::Paint: { QWidget *that_widget = m_widget; m_widget = 0; QApplication::sendEvent(object, event); m_widget = that_widget; paintPoints(); return true; } default: break; } } return false; }
void ZoneDefaultInteraction::onProximityUpdate(QPointF const& mouse_pos, InteractionState& interaction) { m_screenMousePos = mouse_pos; QTransform const to_screen(m_rContext.imageView().imageToWidget()); QTransform const from_screen(m_rContext.imageView().widgetToImage()); QPointF const image_mouse_pos(from_screen.map(mouse_pos)); m_ptrNearestVertex.reset(); m_ptrNearestVertexSpline.reset(); m_nearestSegment = SplineSegment(); m_ptrNearestSegmentSpline.reset(); Proximity best_vertex_proximity; Proximity best_segment_proximity; bool has_zone_under_mouse = false; BOOST_FOREACH(EditableZoneSet::Zone const& zone, m_rContext.zones()) { EditableSpline::Ptr const& spline = zone.spline(); if (!has_zone_under_mouse) { QPainterPath path; path.setFillRule(Qt::WindingFill); path.addPolygon(spline->toPolygon()); has_zone_under_mouse = path.contains(image_mouse_pos); } // Process vertices. for (SplineVertex::Ptr vert(spline->firstVertex()); vert; vert = vert->next(SplineVertex::NO_LOOP)) { Proximity const proximity(mouse_pos, to_screen.map(vert->point())); if (proximity < best_vertex_proximity) { m_ptrNearestVertex = vert; m_ptrNearestVertexSpline = spline; best_vertex_proximity = proximity; } } // Process segments. for (EditableSpline::SegmentIterator it(*spline); it.hasNext(); ) { SplineSegment const segment(it.next()); QLineF const line(to_screen.map(segment.toLine())); QPointF point_on_segment; Proximity const proximity(Proximity::pointAndLineSegment(mouse_pos, line, &point_on_segment)); if (proximity < best_segment_proximity) { m_nearestSegment = segment; m_ptrNearestSegmentSpline = spline; best_segment_proximity = proximity; m_screenPointOnSegment = point_on_segment; } } } interaction.updateProximity(m_vertexProximity, best_vertex_proximity, 1); interaction.updateProximity(m_segmentProximity, best_segment_proximity, 0); if (has_zone_under_mouse) { Proximity const zone_area_proximity(std::min(best_vertex_proximity, best_segment_proximity)); interaction.updateProximity(m_zoneAreaProximity, zone_area_proximity, -1, zone_area_proximity); } }
bool HoverPoints::eventFilter(QObject *object, QEvent *event) { if (object == m_widget && m_enabled) { switch (event->type()) { case QEvent::MouseButtonPress: { QMouseEvent *me = (QMouseEvent *) event; QPointF clickPos = me->pos(); int index = -1; for (int i=0; i<m_points.size(); ++i) { QPainterPath path; if (m_shape == CircleShape) path.addEllipse(pointBoundingRect(i)); else path.addRect(pointBoundingRect(i)); if (path.contains(clickPos)) { index = i; break; } } if (me->button() == Qt::LeftButton) { if (index == -1) { if (!m_editable) return false; int pos = 0; // Insert sort for x or y if (m_sortType == XSort) { for (int i=0; i<m_points.size(); ++i) if (m_points.at(i).x() > clickPos.x()) { pos = i; break; } } else if (m_sortType == YSort) { for (int i=0; i<m_points.size(); ++i) if (m_points.at(i).y() > clickPos.y()) { pos = i; break; } } m_points.insert(pos, clickPos); m_locks.insert(pos, 0); m_currentIndex = pos; firePointChange(); } else { m_currentIndex = index; } return true; } else if (me->button() == Qt::RightButton) { if (index >= 0 && m_editable) { if (m_locks[index] == 0) { m_locks.remove(index); m_points.remove(index); } firePointChange(); return true; } } } break; case QEvent::MouseButtonRelease: m_currentIndex = -1; break; case QEvent::MouseMove: if (m_currentIndex >= 0) movePoint(m_currentIndex, ((QMouseEvent *)event)->pos()); break; case QEvent::Resize: { QResizeEvent *e = (QResizeEvent *) event; if (e->oldSize().width() == 0 || e->oldSize().height() == 0) break; qreal stretch_x = e->size().width() / qreal(e->oldSize().width()); qreal stretch_y = e->size().height() / qreal(e->oldSize().height()); for (int i=0; i<m_points.size(); ++i) { QPointF p = m_points[i]; movePoint(i, QPointF(p.x() * stretch_x, p.y() * stretch_y), false); } firePointChange(); break; } case QEvent::Paint: { QWidget *that_widget = m_widget; m_widget = 0; QApplication::sendEvent(object, event); m_widget = that_widget; paintPoints(); #ifdef QT_OPENGL_SUPPORT ArthurFrame *af = qobject_cast<ArthurFrame *>(that_widget); if (af && af->usesOpenGL()) af->glWidget()->swapBuffers(); #endif return true; } default: break; } } return false; }