Exemplo n.º 1
0
void lmcUserTreeWidget::contextMenuEvent(QContextMenuEvent* event) {
    QTreeWidget::contextMenuEvent(event);

    QTreeWidgetItem* item = itemAt(event->pos());
    QPoint pos = event->globalPos();
    if(item && event->reason() != QContextMenuEvent::Mouse) {
        QRect itemRect = visualItemRect(item);
        pos = itemRect.bottomLeft();
        pos.ry() += itemRect.height();
        pos = mapToGlobal(pos);
    }

    emit itemContextMenu(item, pos);
}
Exemplo n.º 2
0
bool QNodesEditor::eventFilter(QObject *o, QEvent *e)
{
    QGraphicsSceneMouseEvent* me = (QGraphicsSceneMouseEvent*)e;

    const QPen unconnectedPen(QColor(0x3498db), 3);
    const QPen errorPen(QColor(0xe74c3c), 3);
    const QPen connectedPen(QColor(0x2c3e50), 3);

    switch ((int) e->type())
    {
        case QEvent::GraphicsSceneContextMenu:
        {
            QGraphicsItem *item = itemAt(me->scenePos());
            if (!item)
            {
                emit contextMenu(me);
            }
            else if (item->type() == QNEPort::Type)
            {
                emit portContextMenu(me, (QNEPort*)item);
            }
            else if (item->type() == QNEBlock::Type)
            {
                emit blockContextMenu(me, (QNEBlock*)item);
            }
            else if (item->type() == QNEConnection::Type)
            {
                emit connectionContextMenu(me, (QNEConnection*)item);
            }
            else
            {
                emit itemContextMenu(me, item);
            }
        }
        break;
        case QEvent::GraphicsSceneMousePress:
        {

            switch ((int) me->button())
            {
                case Qt::LeftButton:
                {
                    QGraphicsItem *item = itemAt(me->scenePos());
                    if (item && item->type() == QNEPort::Type)
                    {
                        m_connection = new QNEConnection(0);
                        m_scene->addItem(m_connection);
                        m_connection->setPort1((QNEPort*) item);
                        m_connection->setPos2(me->scenePos());
                        m_connection->setPen(unconnectedPen);

                        return true;
                    }
                    else if (item && item->type() == QNEBlock::Type)
                    {
                        /* if (selBlock)
                    selBlock->setSelected(); */
                        // selBlock = (QNEBlock*) item;
                    }
                    break;
                }
//                case Qt::RightButton:
//                {
//                    QGraphicsItem *item = itemAt(me->scenePos());
//                    if (item && (item->type() == QNEConnection::Type || item->type() == QNEBlock::Type))
//                    {
//                        delete item;
//                        return true;
//                    }
//                    break;
//                }
            }
        }
        case QEvent::GraphicsSceneMouseMove:
        {
            if (m_connection)
            {
                m_connection->setPos2(me->scenePos());

                QGraphicsItem *item = itemAt(me->scenePos());
                if (item && item->type() == QNEPort::Type)
                {
                    QNEPort *port1 = m_connection->port1();
                    QNEPort *port2 = (QNEPort*) item;
                    if (port1->block() != port2->block() &&
                            port1->isOutput() != port2->isOutput() &&
                            port1->portType() == port2->portType() &&
                            (port1->portRate() == port2->portRate() || port1->portRate() == 0 || port2->portRate() == 0) &&
                            !port1->isConnected(port2))
                    {
                        m_connection->setPen(connectedPen);
                    }
                    else
                    {
                        m_connection->setPen(errorPen);
                    }
                }
                else
                {
                    m_connection->setPen(unconnectedPen);
                }

                return true;
            }
            break;
        }
        case QEvent::GraphicsSceneMouseRelease:
        {
            if (m_connection && me->button() == Qt::LeftButton)
            {
                QGraphicsItem *item = itemAt(me->scenePos());
                if (item && item->type() == QNEPort::Type)
                {
                    QNEPort *port1 = m_connection->port1();
                    QNEPort *port2 = (QNEPort*) item;

                    if (port1->block() != port2->block() &&
                            port1->isOutput() != port2->isOutput() &&
                            port1->portType() == port2->portType() &&
                            !port1->isConnected(port2))
                    {
                        port1->connectedSignal.execute(port2);
                        port2->connectedSignal.execute(port1);

                        m_connection->setPort2(port2);
                        m_connection->setPen(connectedPen);
                        m_connection = 0;
                        return true;
                    }
                }

                delete m_connection;
                m_connection = 0;
                return true;
            }
            break;
        }
    }
    return QObject::eventFilter(o, e);
}