Exemplo n.º 1
0
//===============================================================
bool MouseElementDeleter::eventFilter(QObject *widget, QEvent *e) {
  QMouseEvent *qMouseEv = (QMouseEvent *) e;

  if(qMouseEv != NULL) {
    node tmpNode;
    edge tmpEdge;
    ElementType type;
    GlMainWidget *glMainWidget = (GlMainWidget *) widget;

    if(e->type() == QEvent::MouseMove) {
      if (glMainWidget->doSelect(qMouseEv->x(), qMouseEv->y(), type, tmpNode, tmpEdge)) {
        glMainWidget->setCursor(QCursor(QPixmap(":/i_del.png")));
      }
      else {
        glMainWidget->setCursor(Qt::ArrowCursor);
      }

      return false;
    }
    else if (e->type() == QEvent::MouseButtonPress && qMouseEv->button()==Qt::LeftButton) {
      if (glMainWidget->doSelect(qMouseEv->x(), qMouseEv->y(), type, tmpNode, tmpEdge)) {
        Observable::holdObservers();
        Graph* graph = glMainWidget->getGraph();
        // allow to undo
        graph->push();

        switch(type) {
        case NODE:
          graph->delNode(tmpNode);
          break;

        case EDGE:
          graph->delEdge(tmpEdge);
          break;
        }

        glMainWidget->redraw();
        Observable::unholdObservers();
        return true;
      }
    }
  }

  return false;
}
Exemplo n.º 2
0
//========================================================================================
bool MouseSelectionEditor::eventFilter(QObject *widget, QEvent *e) {
  QMouseEvent *qMouseEv = static_cast<QMouseEvent *>(e);
  GlMainWidget *glMainWidget = static_cast<GlMainWidget *>(widget);

  if (e->type() == QEvent::MouseButtonPress) {

    initProxies(glMainWidget);
    computeFFD(glMainWidget);

    int H;
    H = glMainWidget->height();
    editCenter = centerRect.getCenter();
    editCenter[2] = 0;
    editCenter[1] = glMainWidget->screenToViewport(H) - editCenter[1];
    // editCenter[0] = W - editCenter[0];
    editPosition[0] = qMouseEv->x();
    editPosition[1] = qMouseEv->y();
    editPosition[2] = 0;
    editLayoutCenter = _layoutCenter;

    vector<SelectedEntity> select;

    switch (qMouseEv->buttons()) {
    case Qt::LeftButton: {
      // first ensure that something is selected
      bool hasSelection = _selection->hasNonDefaultValuatedNodes(_graph) ||
                          _selection->hasNonDefaultValuatedEdges(_graph);

      if (!hasSelection ||
          (!glMainWidget->pickGlEntities(int(editPosition[0]) - 3, int(editPosition[1]) - 3, 6, 6,
                                         select, layer))) {
        // event occurs outside the selection rectangle
        // so from now we delegate the job to a MouseSelector object
        // which should intercept the event
        operation = NONE;
        glMainWidget->setCursor(QCursor(Qt::CrossCursor));

        return false;
      }

      glMainWidget->setCursor(QCursor(Qt::PointingHandCursor));

      int shapeId = -1;
      bool advShape = false;

      for (unsigned int i = 0; (i < select.size()) && (shapeId == -1); ++i) {
        for (int j = 0; j < 8; ++j) {
          if (select[i].getSimpleEntity() == &_controls[j]) {
            shapeId = i;
          }
        }

        for (int j = 0; j < 6; ++j) {
          if (select[i].getSimpleEntity() == &_advControls[j]) {
            advShape = true;
            shapeId = i;
          }
        }
      }

      if (shapeId != -1) {
        if (!advShape) {
          static_cast<GlCircle *>(select[shapeId].getSimpleEntity())
              ->setFillColor(Color(40, 255, 40, 200));
          static_cast<GlCircle *>(select[shapeId].getSimpleEntity())
              ->setOutlineColor(Color(20, 128, 20, 200));
        }

        getOperation(select[shapeId].getSimpleEntity());

        switch (operation) {
        case ALIGN_TOP:
        case ALIGN_BOTTOM:
        case ALIGN_LEFT:
        case ALIGN_RIGHT:
        case ALIGN_VERTICALLY:
        case ALIGN_HORIZONTALLY:
          mAlign(operation, glMainWidget);
          return true;

        case ROTATE_Z:
        case ROTATE_XY:
        case NONE:
        case STRETCH_X:
        case STRETCH_Y:
        case STRETCH_XY:
        case TRANSLATE:
        default:
          break;
        }
      } else {
        if (qMouseEv->modifiers() &
#if defined(__APPLE__)
            Qt::AltModifier
#else
            Qt::ControlModifier
#endif
        ) {
          operation = ROTATE_XY;
          glMainWidget->setCursor(QCursor(Qt::PointingHandCursor));
        } else {
          operation = TRANSLATE;
          glMainWidget->setCursor(QCursor(Qt::SizeAllCursor));
        }
      }

      mode = COORD_AND_SIZE;

      if (qMouseEv->modifiers() & Qt::ShiftModifier)
        mode = COORD;

      if (qMouseEv->modifiers() &
#if defined(__APPLE__)
          Qt::AltModifier
#else
          Qt::ControlModifier
#endif
      )
        mode = SIZE;

      initEdition();
      break;
    }

    case Qt::MidButton:
      undoEdition();
      glMainWidget->setCursor(QCursor(Qt::ArrowCursor));
      break;

    default:
      return false;
    }

    glMainWidget->redraw();
    return true;
  }

  if (e->type() == QEvent::MouseButtonRelease && qMouseEv->button() == Qt::LeftButton &&
      operation != NONE) {
    stopEdition();

    // restore colors
    for (unsigned int i = 0; i < 8; ++i) {
      _controls[i].setFillColor(Color(255, 40, 40, 200));
      _controls[i].setOutlineColor(Color(128, 20, 20, 200));
    }

    glMainWidget->setCursor(QCursor(Qt::ArrowCursor));
    glMainWidget->draw();
    return true;
  }

  if (e->type() == QEvent::KeyPress) {
    // first ensure that something is selected
    bool hasSelection = _selection->hasNonDefaultValuatedNodes(_graph) ||
                        _selection->hasNonDefaultValuatedEdges(_graph);

    if (hasSelection) {
      switch (static_cast<QKeyEvent *>(e)->key()) {
      case Qt::Key_Left:
        mMouseTranslate(editPosition[0] - 1, editPosition[1], glMainWidget);
        break;

      case Qt::Key_Right:
        mMouseTranslate(editPosition[0] + 1, editPosition[1], glMainWidget);
        break;

      case Qt::Key_Up:
        mMouseTranslate(editPosition[0], editPosition[1] - 1, glMainWidget);
        break;

      case Qt::Key_Down:
        mMouseTranslate(editPosition[0], editPosition[1] + 1, glMainWidget);
        break;
      }
    }
  }

  if (e->type() == QEvent::MouseMove && qMouseEv->buttons() & Qt::LeftButton && operation != NONE) {
    int newX = qMouseEv->x();
    int newY = qMouseEv->y();

    switch (operation) {
    case STRETCH_X:
    case STRETCH_Y:
    case STRETCH_XY:
      mMouseStretchAxis(newX, newY, glMainWidget);
      return true;

    case ROTATE_Z:
    case ROTATE_XY:
      mMouseRotate(newX, newY, glMainWidget);
      return true;

    case TRANSLATE:
      mMouseTranslate(newX, newY, glMainWidget);
      return true;

    case NONE:
    case ALIGN_TOP:
    case ALIGN_BOTTOM:
    case ALIGN_LEFT:
    case ALIGN_RIGHT:
    case ALIGN_VERTICALLY:
    case ALIGN_HORIZONTALLY:
    default:
      qWarning() << "[Error] : " << __FUNCTION__ << " should not have been called" << endl;
      break;
    }
  }

  return false;
}
Exemplo n.º 3
0
bool MouseEdgeBuilder::eventFilter(QObject *widget, QEvent *e) {

    GlMainWidget *glMainWidget = static_cast<GlMainWidget *>(widget);

    if (e->type() == QEvent::MouseButtonPress) {
        QMouseEvent * qMouseEv = static_cast<QMouseEvent *>(e);

        SelectedEntity selectedEntity;
        GlGraphInputData *inputData=glMainWidget->getScene()->getGlGraphComposite()->getInputData();
        Graph * _graph = inputData->getGraph();

        LayoutProperty* mLayout = inputData->getElementLayout();

        if (qMouseEv->buttons()==Qt::LeftButton) {
            if (!_started) {
                bool result=glMainWidget->pickNodesEdges(qMouseEv->x(), qMouseEv->y(), selectedEntity);

                if (result && (selectedEntity.getEntityType() == SelectedEntity::NODE_SELECTED)) {
                    _started=true;
                    initObserver(_graph);
                    _source=node(selectedEntity.getComplexEntityId());
                    _curPos=_startPos=mLayout->getNodeValue(_source);
                    return true;
                }

                return false;
            }
            else {
                bool result = glMainWidget->pickNodesEdges(qMouseEv->x(),qMouseEv->y(),selectedEntity);

                if (result && (selectedEntity.getEntityType() == SelectedEntity::NODE_SELECTED)) {
                    Observable::holdObservers();
                    _started=false;
                    clearObserver();
                    // allow to undo
                    _graph->push();
                    addLink(widget,_source,node(selectedEntity.getComplexEntityId()));
                    Observable::unholdObservers();

                }
                else {
                    Coord point(glMainWidget->width() - qMouseEv->x(), qMouseEv->y(), 0);
                    _bends.push_back(glMainWidget->getScene()->getGraphCamera().viewportTo3DWorld(glMainWidget->screenToViewport(point)));
                    glMainWidget->redraw();
                }
            }

            return true;
        }

        if (qMouseEv->buttons()==Qt::MidButton) {
            _bends.clear();
            _started=false;
            _source=node();
            clearObserver();
            glMainWidget->draw();
            return true;
        }
    }

    if  (e->type() == QEvent::MouseMove) {
        QMouseEvent * qMouseEv = static_cast<QMouseEvent *>(e);

        if (!_started) {
            SelectedEntity selectedEntity;
            bool hoveringOverNode = glMainWidget->pickNodesEdges(qMouseEv->x(), qMouseEv->y(), selectedEntity) && selectedEntity.getEntityType() == SelectedEntity::NODE_SELECTED;

            if (!hoveringOverNode) {
                glMainWidget->setCursor(QCursor(Qt::ArrowCursor));
                return false;
            }
            else {
                glMainWidget->setCursor(QCursor(Qt::CrossCursor));
            }
        }
        else {
            SelectedEntity selectedEntity;

            if(glMainWidget->pickNodesEdges(qMouseEv->x(), qMouseEv->y(), selectedEntity) && selectedEntity.getEntityType() == SelectedEntity::NODE_SELECTED) {
                glMainWidget->setCursor(QCursor(Qt::CrossCursor));
            }
            else {
                glMainWidget->setCursor(QCursor(Qt::ArrowCursor));
            }

            Coord point(glMainWidget->width() - qMouseEv->x(), qMouseEv->y(), 0);
            point = glMainWidget->getScene()->getGraphCamera().viewportTo3DWorld(glMainWidget->screenToViewport(point));
            _curPos.set(point[0], point[1], point[2]);
            glMainWidget->redraw();
        }

        return true;
    }

    return false;
}
//========================================================================================
bool MouseEdgeBendEditor::eventFilter(QObject *widget, QEvent *e) {

  QMouseEvent * qMouseEv = (QMouseEvent *) e;

  if(qMouseEv == NULL)
    return false;

  // Double click to create a new control point
  if(e->type() == QEvent::MouseButtonDblClick &&  qMouseEv->button() == Qt::LeftButton && haveSelection(glMainWidget)) {
    _operation = NEW_OP;
    mMouseCreate(qMouseEv->x(), qMouseEv->y(), glMainWidget);
    return true;
  }

  if (e->type() == QEvent::MouseButtonPress) {
    if(!glMainWidget)
      glMainWidget = (GlMainWidget *) widget;

    initProxies(glMainWidget);
    bool hasSelection = haveSelection(glMainWidget);

    editPosition[0] = qMouseEv->x();
    editPosition[1] = qMouseEv->y();
    editPosition[2] = 0;

    switch(qMouseEv->buttons()) {
    case Qt::LeftButton : {

      if (!hasSelection) {
        // event occurs outside the selection rectangle
        // so from now we delegate the job to a MouseEdgeSelector object
        // which should intercept the event
        _operation = NONE_OP;
      }
      else {

        bool entityIsSelected = glMainWidget->pickGlEntities((int)editPosition[0] - 3, (int)editPosition[1] - 3, 6, 6, select, layer);

        if(!entityIsSelected) {
          // We have click outside an entity
          _operation = NONE_OP;
        }
        else {
          selectedEntity=circleString->findKey(select[0].getSimpleEntity());

          if (qMouseEv->modifiers() &
#if defined(__APPLE__)
              Qt::AltModifier
#else
              Qt::ControlModifier
#endif
             ) {
            _operation = DELETE_OP;
            mMouseDelete();
          }
          else {
            _graph->push();
            _operation = TRANSLATE_OP;
            glMainWidget->setCursor(QCursor(Qt::SizeAllCursor));
            mode = COORD;
          }

          return true;
        }
      }

      break;
    }

    default: {
      return false;
    }
    }

    glMainWidget->redraw();
  }

  if (e->type() == QEvent::MouseButtonRelease &&
      qMouseEv->button() == Qt::LeftButton &&
      _operation != NONE_OP) {
    GlMainWidget *glMainWidget = (GlMainWidget *) widget;

    if(selectedEntity=="targetTriangle") {
      SelectedEntity selectedEntity;

      if (glMainWidget->pickNodesEdges(qMouseEv->x(), qMouseEv->y(), selectedEntity) && selectedEntity.getEntityType() == SelectedEntity::NODE_SELECTED) {
        glMainWidget->getScene()->getGlGraphComposite()->getGraph()->setEnds(mEdge,glMainWidget->getScene()->getGlGraphComposite()->getGraph()->ends(mEdge).first,node(selectedEntity.getComplexEntityId()));
      }
    }
    else if(selectedEntity=="sourceCircle") {
      SelectedEntity selectedEntity;

      if (glMainWidget->pickNodesEdges(qMouseEv->x(), qMouseEv->y(), selectedEntity) && selectedEntity.getEntityType() == SelectedEntity::NODE_SELECTED) {
        glMainWidget->getScene()->getGlGraphComposite()->getGraph()->setEnds(mEdge,node(selectedEntity.getComplexEntityId()),glMainWidget->getScene()->getGlGraphComposite()->getGraph()->ends(mEdge).second);
      }
    }

    selectedEntity="";

    _operation = NONE_OP;
    glMainWidget->setCursor(QCursor(Qt::PointingHandCursor));
    glMainWidget->redraw();
    return true;
  }

  if(e->type() == QEvent::MouseButtonPress) {
    vector<SelectedEntity> selectedEntities;
    glMainWidget->pickGlEntities(qMouseEv->x(), qMouseEv->y(), selectedEntities);

    if(!selectedEntities.empty())
      if(selectedEntities[0].getEntityType()==SelectedEntity::SIMPLE_ENTITY_SELECTED)
        if(selectedEntities[0].getSimpleEntity()==edgeEntity) {
          mouseButtonPressOnEdge=true;
          return true;
        }
  }

  if(e->type() == QEvent::MouseButtonRelease) {
    vector<SelectedEntity> selectedEntities;
    glMainWidget->pickGlEntities(qMouseEv->x(), qMouseEv->y(), selectedEntities);

    if(!selectedEntities.empty())
      if(selectedEntities[0].getEntityType()==SelectedEntity::SIMPLE_ENTITY_SELECTED)
        if(selectedEntities[0].getSimpleEntity()==edgeEntity && mouseButtonPressOnEdge) {
          mouseButtonPressOnEdge=false;
          return true;
        }

    mouseButtonPressOnEdge=false;
  }

  if  (e->type() == QEvent::MouseMove) {
    if(qMouseEv->buttons() == Qt::LeftButton &&
        _operation != NONE_OP) {
      GlMainWidget *glMainWidget = (GlMainWidget *) widget;

      switch (_operation) {
      case TRANSLATE_OP:
        mMouseTranslate(qMouseEv->x(), qMouseEv->y(), glMainWidget);
        return true;

      default:
        return false;
      }
    }
    else if(qMouseEv->buttons() == Qt::NoButton) {
      SelectedEntity selectedEntity;
      GlMainWidget *g = (GlMainWidget *) widget;

      if (g->pickNodesEdges(qMouseEv->x(), qMouseEv->y(), selectedEntity) && selectedEntity.getEntityType() == SelectedEntity::NODE_SELECTED) {
        g->setCursor(Qt::CrossCursor);
      }
      else {
        g->setCursor(Qt::ArrowCursor);
      }

      return false;
    }
  }

  return false;
}