/**
 * @brief Receives a mouse press event.
 * @param event The event to handle.
 */
void TilesetView::mousePressEvent(QMouseEvent* event) {

  if (model == nullptr) {
    return;
  }

  if (state == State::NORMAL) {

    QList<QGraphicsItem*> items_under_mouse = items(
          QRect(event->pos(), QSize(1, 1)),
          Qt::IntersectsItemBoundingRect  // Pick transparent items too.
    );
    QGraphicsItem* item = items_under_mouse.empty() ? nullptr : items_under_mouse.first();

    const bool control_or_shift = (event->modifiers() & (Qt::ControlModifier | Qt::ShiftModifier));

    bool keep_selected = false;
    if (control_or_shift) {
      // If ctrl or shift is pressed, keep the existing selection.
      keep_selected = true;
    }
    else if (item != nullptr && item->isSelected()) {
      // When clicking an already selected item, keep the existing selection too.
      keep_selected = true;
    }
    if (!keep_selected) {
      scene->clearSelection();
    }

    if (event->button() == Qt::LeftButton) {
      if (item != nullptr &&
          item->isSelected() &&
          model->get_selection_count() == 1 &&
          !is_read_only()) {
        // Clicking on an already selected item: allow to move it.
        start_state_moving_pattern(event->pos());
      }
      else {
        // Otherwise initialize a selection rectangle.
        initially_selected_items = scene->selectedItems();
        start_state_drawing_rectangle(event->pos());
      }
    }
    else {
      if (item != nullptr && !item->isSelected()) {
        // Select the right-clicked item.
        item->setSelected(true);
      }
    }
  }
}
void ULLineEditor::mousePressEvent ( QGraphicsSceneMouseEvent * mouseEvent )
{
	if ( itemAt ( mouseEvent->scenePos() ) )
	{
		QGraphicsItem * it =itemAt ( mouseEvent->scenePos() );
		it->isSelected() ? it->setSelected ( false ) : it->setSelected ( true );
	}
	else
	{
		mouseSelectBegin = mouseEvent->scenePos();
		mouseSelect = true;
		mouseSelectRect->setPos ( mouseEvent->pos() );
		addItem ( mouseSelectRect );
	}
}
Exemple #3
0
void SCgSelectSubGraph::select(SCgObject *obj)
{
    obj->setSelected(true);

    // select parent
    //if (obj->parentItem() != 0 && SCgObject::isSCgObjectType(obj->parentItem()->type()))
        //select(static_cast<SCgObject*>(obj->parentItem()));

    switch(obj->type())
    {
    case SCgBus::Type:
        {
            SCgBus *bus = static_cast<SCgBus*>(obj);
            if (bus->owner() != 0 && !bus->owner()->isSelected())
                select(bus->owner());
        }
        break;

    case SCgNode::Type:
        {
            SCgNode *node = static_cast<SCgNode*>(obj);
            if (node->bus() != 0 && !node->bus()->isSelected())
                select(node->bus());
        }
        break;

    case SCgPair::Type:
        {
            SCgPair *pair = static_cast<SCgPair*>(obj);
            if (pair->beginObject() != 0 && !pair->beginObject()->isSelected())
                select(pair->beginObject());

            if (pair->endObject() != 0 && !pair->endObject()->isSelected())
                select(pair->endObject());
        }
        break;

    case SCgContour::Type:
        {
            SCgContour *contour = static_cast<SCgContour*>(obj);
            QList<QGraphicsItem*> items = contour->childItems();
            QGraphicsItem *item = 0;
            foreach(item, items)
            {
                // skip not sc.g-objects and selected objects
                if (!SCgObject::isSCgObjectType(item->type()) || item->isSelected())
                    continue;

                select(static_cast<SCgObject*>(item));
            }
        }
        break;
    }

    SCgObject::SCgObjectList connected = obj->connectedObjects();
    SCgObject *c_obj = 0;
    foreach(c_obj, connected)
    {
        // skip selected objects
        if (c_obj->isSelected())
            continue;

        select(c_obj);
    }
}
void EnhancedGraphicsScene::mouseMoveEvent(QGraphicsSceneMouseEvent * mouseEvent)
{
    static int cnt = 0;
    QPointF mousePosition = mouseEvent->scenePos();
    QGraphicsItem* pItem = itemAt(mousePosition.x(), mousePosition.y(), QTransform());

    if (pItem == 0)
    {
        if(!rot && !selectedItems().isEmpty())
        {
            QGraphicsScene::mouseMoveEvent(mouseEvent);
            return;
        }
    }
    else if (pItem->isSelected() == false && !rot && !selectedItems().isEmpty())
    {
        QGraphicsScene::mouseMoveEvent(mouseEvent);
        return;
    }

    // We want to move the items only if the mouse is clicked and not in rubberband
    if((rubberBandOn == false) && clicked)
    {
        if(rot)
        {
            QLog_Info(LOG_ID_INFO, "entering rotation code");
            // we must find the most east point of all selected object as well
            // as the most west point. With this, we determine if we turn around
            // one or the other
            QList<QGraphicsItem*> selectionList = selectedItems();
            QGraphicsItemGroup *group = createItemGroup(selectionList);

            // Get the bounding rect of the group to determine the rotation
            if(firstRot)
            {
                // Calculate X coordinate of the center of the top segment, we need to separate cases if there is only
                // one selected object, and to use this object's bounding rect. Otherwise, the bounding rect of the group
                // won't fit the object
                qreal center = 0;
                if(selectionList.size() == 1)
                {
                    center = (selectionList.first()->mapToScene(0,0).x() + selectionList.first()->mapToScene(selectionList.first()->boundingRect().topRight()).x()) / 2;
                }
                else
                    center = group->boundingRect().center().x();

                if(mouseEvent->scenePos().x() < center)
                {
                    originPoint = findRotationPivot(selectionList, false);
                    previousRot = 0;
                }
                else
                {
                    originPoint = findRotationPivot(selectionList, true);
                    previousRot = 0;
                }

                // Check the rotation offset of the position of the mouse
                qreal a1 = mouseEvent->scenePos().x() - originPoint.x();
                qreal a2 = mouseEvent->scenePos().y() - originPoint.y();
                previousAngle = qAtan2(a2, a1);

                firstRot = false;
            }

            qreal a1 = mouseEvent->scenePos().x() - originPoint.x();
            qreal a2 = mouseEvent->scenePos().y() - originPoint.y();
            qreal angle = qAtan2(a2, a1);

            QTransform trans;
            trans.translate(originPoint.x(), originPoint.y())
                    .rotate(-previousRot)
                    .rotate(( (angle - previousAngle) * 180 / 3.14))
                    .translate(-originPoint.x(), -originPoint.y());

            previousRot = ((angle - previousAngle) * 180 / 3.14);

            group->setTransform(trans, true);

            // group must be destroyed so that we can interact with the items
            destroyItemGroup(group);
        }

        if(cnt++ % 6 == 0)
        {
            cnt = 0;
            refreshItemPositionsOnNetwork();
        }
        // In case of a rotation, we don't want the graphics items
        // to get the mouse move event, so we return without
        // calling mouse move event of qgraphicsscene
        if(rot) return;
    }

    QGraphicsScene::mouseMoveEvent(mouseEvent);
}
/**
 * @brief Receives a mouse release event.
 * @param event The event to handle.
 */
void TilesetView::mouseReleaseEvent(QMouseEvent* event) {

  if (model == nullptr) {
    return;
  }

  bool do_selection = false;
  if (state == State::DRAWING_RECTANGLE) {
    // If the rectangle is empty, consider it was a click and not a drag.
    // In this case we simply select the clicked item.
    do_selection = current_area_item->rect().isEmpty();
    end_state_drawing_rectangle();
  }
  else if (state == State::MOVING_PATTERN) {
    end_state_moving_pattern();
  }

  if (do_selection) {
    if (event->button() == Qt::LeftButton || event->button() == Qt::RightButton) {

      // Left or right button: possibly change the selection.
      QList<QGraphicsItem*> items_under_mouse = items(
            QRect(event->pos(), QSize(1, 1)),
            Qt::IntersectsItemBoundingRect  // Pick transparent items too.
            );
      QGraphicsItem* item = items_under_mouse.empty() ? nullptr : items_under_mouse.first();

      const bool control_or_shift = (event->modifiers() & (Qt::ControlModifier | Qt::ShiftModifier));

      bool keep_selected = false;
      if (control_or_shift) {
        // If ctrl or shift is pressed, keep the existing selection.
        keep_selected = true;
      }
      else if (item != nullptr && item->isSelected()) {
        // When clicking an already selected item, keep the existing selection too.
        keep_selected = true;
      }

      if (!keep_selected) {
        scene->clearSelection();
      }

      if (event->button() == Qt::LeftButton) {

        if (item != nullptr) {

          if (control_or_shift) {
            // Left-clicking an item while pressing control or shift: toggle it.
            item->setSelected(!item->isSelected());
          }
          else {
            if (!item->isSelected()) {
              // Select the item.
              item->setSelected(true);
            }
          }
        }
      }
    }
  }
}