Files
ResInsight/Fwk/AppFwk/cafCommand/cafCmdFeatureMenuBuilder.cpp

210 lines
7.1 KiB
C++
Raw Normal View History

//##################################################################################################
//
// Custom Visualization Core library
// Copyright (C) 2011-2013 Ceetron AS
//
// This library may be used under the terms of either the GNU General Public License or
// the GNU Lesser General Public License as follows:
//
// GNU General Public License Usage
// This library is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This library is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// See the GNU General Public License at <<http://www.gnu.org/licenses/gpl.html>>
// for more details.
//
// GNU Lesser General Public License Usage
// This library is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// This library is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// See the GNU Lesser General Public License at <<http://www.gnu.org/licenses/lgpl-2.1.html>>
// for more details.
//
//##################################################################################################
#include "cafCmdFeatureMenuBuilder.h"
#include "cafCmdFeature.h"
#include "cafCmdFeatureManager.h"
#include "cafCmdSelectionHelper.h"
#include "cafFactory.h"
#include "cvfAssert.h"
#include "defaultfeatures/cafCmdDeleteItemFeature.h"
#include "defaultfeatures/cafCmdAddItemFeature.h"
#include <QAction>
#include <QMenu>
namespace caf
{
// typedef Factory<CmdFeature, std::string> CommandFeatureFactory;
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
CmdFeatureMenuBuilder::CmdFeatureMenuBuilder()
{
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
CmdFeatureMenuBuilder::~CmdFeatureMenuBuilder()
{
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
CmdFeatureMenuBuilder& CmdFeatureMenuBuilder::operator<<(const QString& commandId)
{
if (commandId == "Separator")
{
addSeparator();
}
else
{
addCmdFeature(commandId);
}
return *this;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
CmdFeatureMenuBuilder& CmdFeatureMenuBuilder::addCmdFeature(const QString commandId, const QString& uiText)
{
MenuItem i;
i.itemType = MenuItem::COMMAND;
i.itemName = commandId;
i.uiText = uiText;
m_items.push_back(i);
return *this;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
CmdFeatureMenuBuilder& CmdFeatureMenuBuilder::addCmdFeatureWithUserData(const QString commandId, const QString& uiText, const QVariant& userData)
{
MenuItem i;
i.itemType = MenuItem::COMMAND;
i.itemName = commandId;
i.uiText = uiText;
i.userData = userData;
m_items.push_back(i);
return *this;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
CmdFeatureMenuBuilder& CmdFeatureMenuBuilder::addSeparator()
{
MenuItem i;
i.itemType = MenuItem::SEPARATOR;
m_items.push_back(i);
return *this;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
CmdFeatureMenuBuilder& CmdFeatureMenuBuilder::subMenuStart(const QString& menuName)
{
MenuItem i;
i.itemType = MenuItem::SUBMENU_START;
i.itemName = menuName;
m_items.push_back(i);
return *this;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
CmdFeatureMenuBuilder& CmdFeatureMenuBuilder::subMenuEnd()
{
MenuItem i;
i.itemType = MenuItem::SUBMENU_END;
m_items.push_back(i);
return *this;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void CmdFeatureMenuBuilder::appendToMenu(QMenu* menu)
{
CVF_ASSERT(menu);
std::vector<QMenu*> menus = { menu };
for (int i = 0; i < m_items.size(); i++)
{
if (m_items[i].itemType == MenuItem::SEPARATOR)
{
menu->addSeparator();
}
else if (m_items[i].itemType == MenuItem::SUBMENU_START)
{
QMenu* subMenu = menus.back()->addMenu(m_items[i].itemName);
menus.push_back(subMenu);
}
else if (m_items[i].itemType == MenuItem::SUBMENU_END)
{
if (menus.size() > 1)
{
menus.pop_back();
}
}
else
{
CmdFeatureManager* commandManager = CmdFeatureManager::instance();
QMenu* currentMenu = menus.back();
caf::CmdFeature* feature = commandManager->getCommandFeature(m_items[i].itemName.toStdString());
CVF_ASSERT(feature);
if (feature->canFeatureBeExecuted())
{
const QAction* act;
if (!m_items[i].userData.isNull())
{
act = commandManager->actionWithUserData(m_items[i].itemName, m_items[i].uiText, m_items[i].userData);
}
else
{
act = commandManager->action(m_items[i].itemName);
}
CVF_ASSERT(act);
for (QAction* existingAct : currentMenu->actions())
{
// If action exist, continue to make sure the action is positioned at the first
// location of a command ID
if (existingAct == act) continue;
}
currentMenu->addAction(const_cast<QAction*>(act));
}
}
}
}
} // end namespace caf