void FrameAttachmentPoint::showContextMenu(QPoint point)
{
    //Show menus with all attached frames and submenu where to attach it to
    clearActionReceivers();

    WindowManager* windowManager = Carbon::get()->getWindowManager();
    QMenu menu(windowManager->getMainWindow());
    QMenu* submenu;
    QAction* action;

    //For all attached frames, add frame menu
    int frame = 0;
    for (auto it = mAttachedFrames.begin(); it != mAttachedFrames.end(); it++) 
    {
        submenu = menu.addMenu((*it)->getCaption());
     
        //for all possible targets, add commands
        {
            NumberedActionReceiver* receiver;
            
            //Copy command
            receiver = new NumberedActionReceiver(frame, COMMAND_COPY);
            mActionReceivers.push_back(receiver);
            connect(receiver, SIGNAL(actionReceived(int,int)), this, SLOT(onContextMenuClick(int,int)));
            action = submenu->addAction(QIcon(":copy"), "Copy", receiver, SLOT(receiveAction()));
            action->setToolTip("Create a copy of the plugin.");
            if (!PluginFactory::getFactory().canCreatePlugin((*it)->getClassId())) //Disable copy if plugin cant be created anymore
                action->setEnabled(false);

            //Delete command
            receiver = new NumberedActionReceiver(frame, COMMAND_DELETE);
            mActionReceivers.push_back(receiver);
            connect(receiver, SIGNAL(actionReceived(int,int)), this, SLOT(onContextMenuClick(int,int)));
            action = submenu->addAction(QIcon(":delete"), "Delete", receiver, SLOT(receiveAction()));
            action->setToolTip("Delete the plugin.");
        }

        submenu->addSeparator();

        //for all possible targets, add target frames
        int target = COMMAND_MAX_ID;
        for (auto tar_it = windowManager->getFrameAttachmentPoints().begin(); tar_it != windowManager->getFrameAttachmentPoints().end(); tar_it++)
        {
            NumberedActionReceiver* receiver = new NumberedActionReceiver(frame, target);
            mActionReceivers.push_back(receiver);

            connect(receiver, SIGNAL(actionReceived(int,int)), this, SLOT(onContextMenuClick(int,int)));

            if (tar_it->second == this)
                action = submenu->addAction(QIcon(":accept"), tar_it->second->getName(), receiver, SLOT(receiveAction()));
            else
                action = submenu->addAction(tar_it->second->getName(), receiver, SLOT(receiveAction()));
            
            target++;
        }
        frame++;
    }

    menu.exec(mAttachmentPoint->mapToGlobal(point));
}
void FrameAttachmentPoint::onContextMenuClick(int frame, int command)
{
    if (frame >= 0 && frame < mAttachedFrames.size())
    {
        auto framePos = mAttachedFrames.begin();
        for (int i = 0; i < frame; i++) framePos++;

        //Check for command
        if (command == COMMAND_COPY)
        {
            //Copy the plugin
            PluginManager* manager = Carbon::get()->getPluginManager();
            AbstractPlugin* plugin = manager->createPlugin(*(*framePos)->getPluginDefinition(), true);
        }
        else if (command == COMMAND_DELETE)
        {
            //Delete the plugin
            PluginManager* manager = Carbon::get()->getPluginManager();
            manager->deletePlugin((*framePos)->getPluginId(), true);
        }
        else if (command >= COMMAND_MAX_ID)
        {
            //Reattach the plugin
            int target = command - COMMAND_MAX_ID;
            WindowManager* manager = Carbon::get()->getWindowManager();
            if ((int)manager->getFrameAttachmentPoints().size() > target && target >= 0)
            {
                auto pos = manager->getFrameAttachmentPoints().begin(); 
                for (int i = 0; i < target; i++) pos++;

                manager->changeAttachment(*framePos, pos->second->getName());
            }
        }
    }
    else
    {
        LOG_ERROR() << "Illegal frame index " << frame;
    }
}