Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#5122: Add ortho context menu entries to select/deselect by filter
- Loading branch information
1 parent
5ecb33e
commit 78dd0d8
Showing
12 changed files
with
281 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
#include "FilterContextMenu.h" | ||
|
||
#include "iuimanager.h" | ||
#include "imap.h" | ||
#include "wxutil/menu/IconTextMenuItem.h" | ||
|
||
namespace ui | ||
{ | ||
|
||
namespace | ||
{ | ||
const std::string FILTER_ICON("iconFilter16.png"); | ||
} | ||
|
||
FilterContextMenu::FilterContextMenu(OnSelectionFunc& onSelection) : | ||
wxMenu(), | ||
_onSelection(onSelection) | ||
{} | ||
|
||
void FilterContextMenu::populate() | ||
{ | ||
// Clear all existing items | ||
for (const MenuItemIdToLayerMapping::value_type& i : _menuItemMapping) | ||
{ | ||
wxMenu* parentMenu = GetParent(); | ||
|
||
#ifdef __WXGTK__ | ||
// wxMSW and wxGTK are doing it differently (see comments in createMenuItems() below) | ||
parentMenu = this; | ||
#endif | ||
|
||
if (parentMenu != nullptr) | ||
{ | ||
parentMenu->Unbind(wxEVT_MENU, &FilterContextMenu::onActivate, this, i.first); | ||
} | ||
|
||
Delete(i.first); | ||
} | ||
|
||
_menuItemMapping.clear(); | ||
|
||
// Populate the map with all layer names and IDs | ||
GlobalFilterSystem().forEachFilter(*this); | ||
} | ||
|
||
void FilterContextMenu::visit(const std::string& filterName) | ||
{ | ||
// Create a new menuitem | ||
wxMenuItem* menuItem = new wxutil::IconTextMenuItem(filterName, FILTER_ICON); | ||
|
||
// Add it to the parent menu | ||
Append(menuItem); | ||
|
||
// remember the filter name for this item | ||
_menuItemMapping[menuItem->GetId()] = filterName; | ||
|
||
wxMenu* parentMenu = GetParent(); | ||
|
||
#ifdef __WXGTK__ | ||
// wxMSW and wxGTK are doing it differently (which is always great): | ||
// in MSW the parent menu (of this class) is firing the events, whereas | ||
// in GTK+ the submenu (this class) itself is doing that. So let's do an IFDEF | ||
parentMenu = this; | ||
#endif | ||
|
||
// If we're packed to a parent menu (ourselves acting as submenu), connect the event to the parent | ||
if (parentMenu != nullptr) | ||
{ | ||
parentMenu->Bind(wxEVT_MENU, &FilterContextMenu::onActivate, this, menuItem->GetId()); | ||
} | ||
} | ||
|
||
void FilterContextMenu::onActivate(wxCommandEvent& ev) | ||
{ | ||
assert(_menuItemMapping.find(ev.GetId()) != _menuItemMapping.end()); | ||
|
||
// Pass the call to the function | ||
_onSelection(_menuItemMapping[ev.GetId()]); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#pragma once | ||
|
||
#include <functional> | ||
#include <map> | ||
#include <memory> | ||
|
||
#include "ifilter.h" | ||
|
||
#include <wx/menu.h> | ||
|
||
namespace ui | ||
{ | ||
|
||
class FilterContextMenu : | ||
public wxMenu, | ||
public IFilterVisitor | ||
{ | ||
public: | ||
// The function to be called on menu selection, the name of the | ||
// selected filter is passed along. | ||
typedef std::function<void(const std::string& filterName)> OnSelectionFunc; | ||
|
||
private: | ||
OnSelectionFunc _onSelection; | ||
|
||
typedef std::map<int, std::string> MenuItemIdToLayerMapping; | ||
MenuItemIdToLayerMapping _menuItemMapping; | ||
|
||
public: | ||
FilterContextMenu(OnSelectionFunc& onSelection); | ||
|
||
// IFilterVisitor implementation | ||
void visit(const std::string& filterName) override; | ||
|
||
// Loads filer names into the menu, clears existing items first | ||
void populate(); | ||
|
||
private: | ||
// wx Callback for menu selections | ||
void onActivate(wxCommandEvent& ev); | ||
}; | ||
|
||
} // namespace ui |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
#include "FilterOrthoContextMenuItem.h" | ||
|
||
#include "ifilter.h" | ||
#include "icommandsystem.h" | ||
#include "filters/BasicFilterSystem.h" | ||
|
||
namespace ui | ||
{ | ||
|
||
namespace | ||
{ | ||
const char* const FILTER_ICON = "iconFilter16.png"; | ||
} | ||
|
||
FilterOrthoContextMenuItem::FilterOrthoContextMenuItem(const std::string& caption, | ||
const FilterContextMenu::OnSelectionFunc& callback) : | ||
wxutil::IconTextMenuItem(caption, FILTER_ICON), | ||
_func(callback) | ||
{ | ||
// Re-populate the submenu | ||
_submenu = new FilterContextMenu(_func); | ||
|
||
// wxWidgets will take care of deleting the submenu | ||
SetSubMenu(_submenu); | ||
} | ||
|
||
FilterOrthoContextMenuItem::~FilterOrthoContextMenuItem() | ||
{ | ||
if (GetMenu() != nullptr) | ||
{ | ||
// Destroying a menu item doesn't de-register it from the parent menu | ||
// To prevent double-deletions, we de-register the item on our own | ||
GetMenu()->Remove(GetId()); | ||
} | ||
} | ||
|
||
wxMenuItem* FilterOrthoContextMenuItem::getMenuItem() | ||
{ | ||
return this; | ||
} | ||
|
||
void FilterOrthoContextMenuItem::execute() | ||
{} | ||
|
||
bool FilterOrthoContextMenuItem::isSensitive() | ||
{ | ||
return true; | ||
} | ||
|
||
void FilterOrthoContextMenuItem::preShow() | ||
{ | ||
// Re-populate the submenu | ||
_submenu->populate(); | ||
} | ||
|
||
void FilterOrthoContextMenuItem::SelectByFilter(const std::string& filterName) | ||
{ | ||
GlobalCommandSystem().executeCommand(filters::SELECT_OBJECTS_BY_FILTER_CMD, cmd::Argument(filterName)); | ||
} | ||
|
||
void FilterOrthoContextMenuItem::DeselectByFilter(const std::string& filterName) | ||
{ | ||
GlobalCommandSystem().executeCommand(filters::DESELECT_OBJECTS_BY_FILTER_CMD, cmd::Argument(filterName)); | ||
} | ||
|
||
} // namespace |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
#pragma once | ||
|
||
#include <memory> | ||
|
||
#include "imenu.h" | ||
#include "ifilter.h" | ||
|
||
#include "FilterContextMenu.h" | ||
#include "wxutil/menu/IconTextMenuItem.h" | ||
|
||
namespace ui | ||
{ | ||
|
||
// A menu item which can be packed into the OrthoContextMenu | ||
class FilterOrthoContextMenuItem : | ||
public IMenuItem, | ||
public wxutil::IconTextMenuItem | ||
{ | ||
private: | ||
// Function object for the submenus | ||
FilterContextMenu::OnSelectionFunc _func; | ||
|
||
// The submenu (carrying the layer names) | ||
// will be deallocated by wxWidgets on shutdown | ||
FilterContextMenu* _submenu; | ||
|
||
public: | ||
FilterOrthoContextMenuItem(const std::string& caption, | ||
const FilterContextMenu::OnSelectionFunc& callback); | ||
|
||
~FilterOrthoContextMenuItem(); | ||
|
||
// IMenuItem implementation | ||
wxMenuItem* getMenuItem() override; | ||
void execute() override; | ||
bool isSensitive() override; | ||
void preShow() override; | ||
|
||
// Gets called by the items in the submenus | ||
static void SelectByFilter(const std::string& filterName); | ||
static void DeselectByFilter(const std::string& filterName); | ||
}; | ||
|
||
} // namespace |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters