MeshLib Documentation
Loading...
Searching...
No Matches
MRRibbonSchema.h
Go to the documentation of this file.
1#pragma once
2#include "MRViewerFwd.h"
3#include "MRMesh/MRMeshFwd.h"
4#include "MRPch/MRJson.h"
5#include "MRMesh/MRphmap.h"
6#include <filesystem>
7#include <vector>
8#include <string>
9#include <memory>
10
11namespace MR
12{
13
14// needed for big buttons text aligning
15using SplitCaptionInfo = std::vector<std::pair<std::string_view, float>>;
16
22
24{
25 std::shared_ptr<RibbonMenuItem> item;
26 std::string caption;
27 std::string tooltip;
28 std::string icon;
30 std::string helpLink; // link to help page
31};
32
36using MenuItemsList = std::vector<std::string>;
38{
39 std::string name;
40 int priority{ 0 };
41 bool experimental{ false };
42};
43
44// This structure describes UI schema of ribbon menu
55
56// This class holds static ribbon schema,
57// note that schema becomes valid after loading (RibbonSchemaLoader)
58class MRVIEWER_CLASS RibbonSchemaHolder
59{
60public:
61 MRVIEWER_API static RibbonSchema& schema();
62
65 MRVIEWER_API static bool addItem( const std::shared_ptr<RibbonMenuItem>& item );
66
69 MRVIEWER_API static bool delItem( const std::shared_ptr<RibbonMenuItem>& item );
70
73 {
74 int tabIndex{ -1 }; // -1 is default value if item has no tab
75 const MenuItemInfo* item{ nullptr }; // item info to show correct caption
76 };
77
80 {
81 float captionWeight{ 1.f };
82 float captionOrderWeight{ 1.f };
83 float tooltipWeight{ 1.f };
84 float tooltipOrderWeight{ 1.f };
85 };
86
87 MRVIEWER_API static std::vector<SearchResult> search( const std::string& searchStr, int* captionCount = nullptr,
88 std::vector<SearchResultWeight>* weights = nullptr );
89
90private:
91 RibbonSchemaHolder() = default;
92};
93
94// Class for loading ribbon schema from structure files (basically called from RibbonMenu, but can be called separately)
95class MRVIEWER_CLASS RibbonSchemaLoader
96{
97public:
98 RibbonSchemaLoader() = default;
99 virtual ~RibbonSchemaLoader() = default;
100
101 // Loads schema from files
102 MRVIEWER_API virtual void loadSchema() const;
103
104 // Reads schema items list from `root` to `list`
105 MRVIEWER_API static void readMenuItemsList( const Json::Value& root, MenuItemsList& list );
106
107 // Recalc items sizes
108 MRVIEWER_API static void recalcItemSizes();
109protected:
110 // finds structure json files in exe directory
111 MRVIEWER_API virtual std::vector<std::filesystem::path> getStructureFiles_( const std::string& fileExtension ) const;
112 // sort structure json files by order
113 MRVIEWER_API void sortFilesByOrder_( std::vector<std::filesystem::path>& files ) const;
114 // appends one menu items json info
115 MRVIEWER_API void readItemsJson_( const std::filesystem::path& path ) const;
116 MRVIEWER_API void readItemsJson_( const Json::Value& root ) const;
117 // appends one ui json info
118 MRVIEWER_API void readUIJson_( const std::filesystem::path& path ) const;
119 MRVIEWER_API void readUIJson_( const Json::Value& root ) const;
120};
121
122
123template<typename T>
125{
126public:
127 static_assert( std::is_base_of_v<RibbonMenuItem, T> );
128
129 template<typename... Args>
130 RibbonMenuItemAdder( Args&&... args ) : item_( std::make_shared<T>( std::forward<Args>( args )... ) )
131 {
133 }
134
139
140private:
141 std::shared_ptr<T> item_;
142};
143
144template<typename T>
146{
147 static_assert( std::is_base_of_v<RibbonMenuItem, T> );
148public:
149 template<typename F>
150 RibbonMenuItemCall( F f, std::function<void(std::shared_ptr<T>)> g ) : g_( std::move( g ) )
151 {
152 const auto& items = RibbonSchemaHolder::schema().items;
153 for ( const auto& item : items )
154 {
155 auto plugin = std::dynamic_pointer_cast< T >( item.second.item );
156 if ( !plugin )
157 continue;
158 f( plugin_ = std::move( plugin ) );
159 break;
160 }
161 }
163 {
164 if ( g_ && plugin_ )
165 g_( plugin_ );
166 }
167private:
168 std::shared_ptr<T> plugin_;
169 std::function<void(std::shared_ptr<T>)> g_;
170};
171
173#define MR_REGISTER_RIBBON_ITEM(pluginType) \
174 static MR::RibbonMenuItemAdder<pluginType> ribbonMenuItemAdder##pluginType##_;
175
177#define MR_RIBBON_ITEM_CALL(pluginType,f,g) \
178 static MR::RibbonMenuItemCall<pluginType> ribbonMenuItemCall##func##pluginType##_( f, g );
179
180}
Definition MRRibbonSchema.h:125
RibbonMenuItemAdder(Args &&... args)
Definition MRRibbonSchema.h:130
~RibbonMenuItemAdder()
Definition MRRibbonSchema.h:135
Definition MRRibbonSchema.h:146
RibbonMenuItemCall(F f, std::function< void(std::shared_ptr< T >)> g)
Definition MRRibbonSchema.h:150
~RibbonMenuItemCall()
Definition MRRibbonSchema.h:162
Definition MRRibbonSchema.h:59
static MRVIEWER_API RibbonSchema & schema()
static MRVIEWER_API bool delItem(const std::shared_ptr< RibbonMenuItem > &item)
static MRVIEWER_API bool addItem(const std::shared_ptr< RibbonMenuItem > &item)
static MRVIEWER_API std::vector< SearchResult > search(const std::string &searchStr, int *captionCount=nullptr, std::vector< SearchResultWeight > *weights=nullptr)
Definition MRRibbonSchema.h:96
virtual ~RibbonSchemaLoader()=default
MRVIEWER_API void sortFilesByOrder_(std::vector< std::filesystem::path > &files) const
static MRVIEWER_API void recalcItemSizes()
virtual MRVIEWER_API std::vector< std::filesystem::path > getStructureFiles_(const std::string &fileExtension) const
MRVIEWER_API void readUIJson_(const std::filesystem::path &path) const
MRVIEWER_API void readItemsJson_(const Json::Value &root) const
MRVIEWER_API void readItemsJson_(const std::filesystem::path &path) const
virtual MRVIEWER_API void loadSchema() const
MRVIEWER_API void readUIJson_(const Json::Value &root) const
static MRVIEWER_API void readMenuItemsList(const Json::Value &root, MenuItemsList &list)
RibbonSchemaLoader()=default
Definition MRCameraOrientationPlugin.h:8
std::vector< std::pair< std::string_view, float > > SplitCaptionInfo
Definition MRRibbonSchema.h:15
HashMap< std::string, MenuItemInfo > ItemMap
Definition MRRibbonSchema.h:33
std::vector< std::string > MenuItemsList
Definition MRRibbonMenuItem.h:9
HashMap< std::string, std::vector< std::string > > TabsGroupsMap
Definition MRRibbonSchema.h:34
TabsGroupsMap GroupsItemsMap
Definition MRRibbonSchema.h:35
phmap::flat_hash_map< K, V, Hash, Eq > HashMap
Definition MRMesh/MRMeshFwd.h:460
Definition MRRibbonSchema.h:18
SplitCaptionInfo splitInfo
Definition MRRibbonSchema.h:20
float baseSize
Definition MRRibbonSchema.h:19
Definition MRRibbonSchema.h:24
std::string caption
Definition MRRibbonSchema.h:26
std::string helpLink
Definition MRRibbonSchema.h:30
std::string icon
Definition MRRibbonSchema.h:28
std::shared_ptr< RibbonMenuItem > item
Definition MRRibbonSchema.h:25
std::string tooltip
Definition MRRibbonSchema.h:27
MenuItemCaptionSize captionSize
Definition MRRibbonSchema.h:29
ancillary struct to hold information for search result order
Definition MRRibbonSchema.h:80
struct to hold information for search result presentation
Definition MRRibbonSchema.h:73
Definition MRRibbonSchema.h:46
MenuItemsList sceneButtonsList
Definition MRRibbonSchema.h:53
MenuItemsList defaultQuickAccessList
Definition MRRibbonSchema.h:51
TabsGroupsMap tabsMap
Definition MRRibbonSchema.h:48
GroupsItemsMap groupsMap
Definition MRRibbonSchema.h:49
std::vector< RibbonTab > tabsOrder
Definition MRRibbonSchema.h:47
ItemMap items
Definition MRRibbonSchema.h:50
MenuItemsList headerQuickAccessList
Definition MRRibbonSchema.h:52
Definition MRRibbonSchema.h:38
bool experimental
Definition MRRibbonSchema.h:41
int priority
Definition MRRibbonSchema.h:40
std::string name
Definition MRRibbonSchema.h:39