Esempio n. 1
1
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_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());
                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();
#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;
}
Esempio n. 2
0
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;
}
Esempio n. 3
0
void HoverPoints::movePoint(int index, const QPointF &point, bool emitUpdate)
{
    m_points[index] = bound_point(point, boundingRect(), m_locks.at(index));
    if (emitUpdate)
        firePointChange();
}
Esempio n. 4
0
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;
}
Esempio n. 5
0
void MapEditorWidget::mouseReleaseEvent ( QMouseEvent * ){
  currentIndex = -1;
  updateTransform();
  firePointChange();
}