mirror of
https://github.com/OPM/ResInsight.git
synced 2025-02-25 18:55:39 -06:00
#2020 Move LAS files. Support for submenus and command features with custom name and user data
This commit is contained in:
@@ -61,6 +61,8 @@ set( PROJECT_FILES
|
||||
cafCmdFeature.h
|
||||
cafCmdFeatureManager.cpp
|
||||
cafCmdFeatureManager.h
|
||||
cafCmdFeatureMenuBuilder.cpp
|
||||
cafCmdFeatureMenuBuilder.h
|
||||
)
|
||||
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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());
|
||||
|
||||
209
Fwk/AppFwk/cafCommand/cafCmdFeatureMenuBuilder.cpp
Normal file
209
Fwk/AppFwk/cafCommand/cafCmdFeatureMenuBuilder.cpp
Normal 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
|
||||
110
Fwk/AppFwk/cafCommand/cafCmdFeatureMenuBuilder.h
Normal file
110
Fwk/AppFwk/cafCommand/cafCmdFeatureMenuBuilder.h
Normal 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;
|
||||
//}
|
||||
Reference in New Issue
Block a user