#include "context_menu.h"
#include "griddesigner.h"

DesignerContextMenu::DesignerContextMenu(GridDesigner* designer, DesignerItem* item)
{
	_item = item;
	_designer = designer;
}

DesignerContextMenu::DesignerContextMenu(GridDesigner* designer, DesignerItemMap items)
{
	_item = NULL;
	_items = items;
	_designer = designer;
}

void DesignerContextMenu::exec(const QPoint& screenPos, const QPointF& scenePos)
{
	QMenu ctxMenu(_designer);

	if(_item) {
		switch(_item->type()) {
			case DesignerItem::_link_item:
			case DesignerItem::_gridnode_item:
			case DesignerItem::_comment_item:
			{
				ctxMenu.addAction(_designer->getUi()->actionDelete_Components);
				ctxMenu.addAction(_designer->getUi()->action_Properties);
				break;
			}
			case DesignerItem::_connection_item:
			{
				Connection* conn = (Connection*) _item;
				if(conn->pointNear(scenePos))
					ctxMenu.addAction(_designer->getUi()->actionRemove_Point);
				else
					ctxMenu.addAction(_designer->getUi()->actionAdd_Point);	
				ctxMenu.addAction(_designer->getUi()->actionDelete_Components);
				//ctxMenu.setShortcutEnabled(_designer->getUi()->actionDelete_Components->id(), false);
				break;
			}
		}

	}
	else if(!_items.empty()) {
		ctxMenu.addAction(_designer->getUi()->actionDelete_Components);
		//ctxMenu.setShortcutEnabled(_designer->getUi()->actionDelete_Components, false);
	}	

	if(!ctxMenu.actions().empty())
		ctxMenu.exec(screenPos);
}