#2020 Move LAS files. Support for submenus and command features with custom name and user data

This commit is contained in:
Bjørn Erik Jensen
2017-11-19 01:12:39 +01:00
parent f032a3eb9f
commit 31a84181e6
25 changed files with 920 additions and 457 deletions

View File

@@ -61,6 +61,8 @@ set( PROJECT_FILES
cafCmdFeature.h
cafCmdFeatureManager.cpp
cafCmdFeatureManager.h
cafCmdFeatureMenuBuilder.cpp
cafCmdFeatureMenuBuilder.h
)

View File

@@ -68,13 +68,21 @@ CmdFeature::~CmdFeature()
//--------------------------------------------------------------------------------------------------
QAction* CmdFeature::action()
{
return this->action(QString(""));
return this->actionWithCustomText(QString(""));
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QAction* CmdFeature::action(QString customText)
QAction* CmdFeature::actionWithCustomText(const QString& customText)
{
return actionWithUserData(customText, QVariant());
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QAction* CmdFeature::actionWithUserData(const QString& customText, const QVariant& userData)
{
QAction* action = NULL;
@@ -88,6 +96,11 @@ QAction* CmdFeature::action(QString customText)
else
{
action = new QAction(this);
if (!userData.isNull())
{
action->setData(userData);
}
connect(action, SIGNAL(triggered(bool)), SLOT(actionTriggered(bool)));
m_customTextToActionMap[customText]= action;
}
@@ -170,4 +183,15 @@ void CmdFeature::disableModelChangeContribution()
m_triggerModelChange = false;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
const QVariant CmdFeature::userData() const
{
QAction* action = qobject_cast<QAction*>(sender());
if (!action) return QVariant();
return action->data();
}
} // end namespace caf

View File

@@ -77,7 +77,8 @@ public:
virtual ~CmdFeature();
QAction* action();
QAction* action(QString customText);
QAction* actionWithCustomText(const QString& customText);
QAction* actionWithUserData(const QString& customText, const QVariant& userData);
void refreshEnabledState();
void refreshCheckedState();
@@ -93,6 +94,7 @@ protected:
virtual bool isCommandChecked();
void disableModelChangeContribution();
const QVariant userData() const;
private:
std::map<QString, QAction*> m_customTextToActionMap;

View File

@@ -108,7 +108,21 @@ QAction* CmdFeatureManager::action(const QString& commandId, const QString& cust
{
std::pair<CmdFeature*, size_t> featurePair = createFeature(commandId.toStdString());
QAction* act = featurePair.first->action(customActionText);
QAction* act = featurePair.first->actionWithCustomText(customActionText);
m_actionToFeatureIdxMap[act] = featurePair.second;
return act;
}
//--------------------------------------------------------------------------------------------------
/// Get action for the specified command, with custom action text
/// The action is owned by the PdmCommandItemManager
//--------------------------------------------------------------------------------------------------
QAction* CmdFeatureManager::actionWithUserData(const QString& commandId, const QString& customActionText, const QVariant& userData)
{
std::pair<CmdFeature*, size_t> featurePair = createFeature(commandId.toStdString());
QAction* act = featurePair.first->actionWithUserData(customActionText, userData);
m_actionToFeatureIdxMap[act] = featurePair.second;
return act;

View File

@@ -63,8 +63,8 @@ public:
virtual ~CmdFeatureManager();
QAction* action(const QString& commandId);
QAction* action(const QString& commandId, const QString& actionText);
QAction* action(const QString& commandId, const QString& customActionText);
QAction* actionWithUserData(const QString& commandId, const QString& customActionText, const QVariant& userData);
void refreshStates(const QStringList& commandIdList = QStringList());
void refreshEnabledState(const QStringList& commandIdList = QStringList());
void refreshCheckedState(const QStringList& commandIdList = QStringList());

View File

@@ -0,0 +1,209 @@
//##################################################################################################
//
// 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

View File

@@ -0,0 +1,110 @@
//##################################################################################################
//
// 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.
//
//##################################################################################################
#pragma once
#include <vector>
#include <map>
#include <set>
#include <QObject>
#include <QVariant>
class QAction;
class QMenu;
namespace caf
{
class CmdFeature;
//==================================================================================================
///
//==================================================================================================
class CmdFeatureMenuBuilder
{
public:
CmdFeatureMenuBuilder();
//CmdFeatureMenuBuilder(const CmdFeatureMenuBuilder& other);
virtual ~CmdFeatureMenuBuilder();
CmdFeatureMenuBuilder& operator<<(const QString& commandIdOrSeparator);
CmdFeatureMenuBuilder& addCmdFeature(const QString commandId, const QString& customUiText = "");
CmdFeatureMenuBuilder& addCmdFeatureWithUserData(const QString commandId, const QString& customUiText, const QVariant& userData);
CmdFeatureMenuBuilder& addSeparator();
CmdFeatureMenuBuilder& subMenuStart(const QString& menuName);
CmdFeatureMenuBuilder& subMenuEnd();
void appendToMenu(QMenu* menu);
// const std::vector<MenuItem> items() const;
private:
struct MenuItem
{
public:
enum ItemType { COMMAND, SEPARATOR, SUBMENU_START, SUBMENU_END };
ItemType itemType;
QString itemName;
QString uiText;
QVariant userData;
};
std::vector<MenuItem> m_items;
};
} // end namespace caf
//static MenuItem command(QString commandName)
//{
// MenuItem i;
// i.itemType = COMMAND;
// i.itemName = commandName;
// return i;
//}
//
//static MenuItem separator()
//{
// MenuItem i;
// i.itemType = SEPARATOR;
// return i;
//}