KiCad PCB EDA Suite
CONDITIONAL_MENU Class Reference

#include <conditional_menu.h>

Classes

class  ENTRY
 

Helper class to organize menu entries.

More...
 

Public Member Functions

 CONDITIONAL_MENU (TOOL_INTERACTIVE *aTool)
 
void AddItem (const TOOL_ACTION &aAction, const SELECTION_CONDITION &aCondition=SELECTION_CONDITIONS::ShowAlways, int aOrder=ANY_ORDER)
 Function AddItem() More...
 
void AddMenu (CONTEXT_MENU *aMenu, bool aExpand=false, const SELECTION_CONDITION &aCondition=SELECTION_CONDITIONS::ShowAlways, int aOrder=ANY_ORDER)
 Function AddMenu() More...
 
void AddSeparator (const SELECTION_CONDITION &aCondition=SELECTION_CONDITIONS::ShowAlways, int aOrder=ANY_ORDER)
 Function AddSeparator() More...
 
CONTEXT_MENUGenerate (SELECTION &aSelection)
 Function Generate() More...
 

Static Public Attributes

static const int ANY_ORDER = -1
 

Constant to indicate that we do not care about an ENTRY location in the menu.

More...
 

Private Member Functions

void addEntry (ENTRY aEntry)
 

Inserts the entry, preserving the requested order.

More...
 

Private Attributes

std::list< ENTRYm_entries
 

List of all menu entries.

More...
 
TOOL_INTERACTIVEm_tool
 

tool owning the menu

More...
 

Detailed Description

Definition at line 38 of file conditional_menu.h.

Constructor & Destructor Documentation

CONDITIONAL_MENU::CONDITIONAL_MENU ( TOOL_INTERACTIVE aTool)
inline

Definition at line 44 of file conditional_menu.h.

44  :
45  m_tool( aTool )
46  {}
TOOL_INTERACTIVE * m_tool
tool owning the menu

Member Function Documentation

void CONDITIONAL_MENU::addEntry ( ENTRY  aEntry)
private

Inserts the entry, preserving the requested order.

Definition at line 96 of file conditional_menu.cpp.

References m_entries, CONDITIONAL_MENU::ENTRY::Order(), and CONDITIONAL_MENU::ENTRY::SetOrder().

Referenced by AddItem(), AddMenu(), and AddSeparator().

97 {
98  if( aEntry.Order() < 0 ) // Any order, so give it any order number
99  aEntry.SetOrder( m_entries.size() );
100 
101  std::list<ENTRY>::iterator it = m_entries.begin();
102 
103  // Find the right spot for the entry
104  while( it != m_entries.end() && it->Order() <= aEntry.Order() )
105  ++it;
106 
107  m_entries.insert( it, aEntry );
108 }
std::list< ENTRY > m_entries
List of all menu entries.
void CONDITIONAL_MENU::AddItem ( const TOOL_ACTION aAction,
const SELECTION_CONDITION aCondition = SELECTION_CONDITIONS::ShowAlways,
int  aOrder = ANY_ORDER 
)

Function AddItem()

Adds a menu entry to run a TOOL_ACTION on selected items.

Parameters
aActionis a menu entry to be added.
aConditionis a condition that has to be fulfilled to enable the menu entry.
aOrderdetermines location of the added item, higher numbers are put on the bottom. You may use ANY_ORDER here if you think it does not matter.

Definition at line 28 of file conditional_menu.cpp.

References addEntry(), and TOOL_ACTION::GetId().

Referenced by TOOL_MENU::AddStandardSubMenus(), ROUTER_TOOL::Init(), MICROWAVE_TOOL::Init(), POINT_EDITOR::Init(), EDIT_TOOL::Init(), and DRAWING_TOOL::Init().

30 {
31  assert( aAction.GetId() > 0 ); // Check if action was previously registered in ACTION_MANAGER
32  addEntry( ENTRY( &aAction, aCondition, aOrder ) );
33 }
void addEntry(ENTRY aEntry)
Inserts the entry, preserving the requested order.
int GetId() const
Function GetId() Returns the unique id of the TOOL_ACTION object.
Definition: tool_action.h:85
void CONDITIONAL_MENU::AddMenu ( CONTEXT_MENU aMenu,
bool  aExpand = false,
const SELECTION_CONDITION aCondition = SELECTION_CONDITIONS::ShowAlways,
int  aOrder = ANY_ORDER 
)

Function AddMenu()

Adds a submenu to the menu. CONDITIONAL_MENU takes ownership of the added menu, so it will be freed when the CONDITIONAL_MENU object is destroyed.

Parameters
aMenuis the submenu to be added.
aExpanddetermines if the added submenu items should be added as individual items or as a submenu.
aConditionis a condition that has to be fulfilled to enable the submenu entry.
aOrderdetermines location of the added menu, higher numbers are put on the bottom. You may use ANY_ORDER here if you think it does not matter.

Definition at line 36 of file conditional_menu.cpp.

References addEntry().

Referenced by TOOL_MENU::AddStandardSubMenus(), ALIGN_DISTRIBUTE_TOOL::Init(), PCB_EDITOR_CONTROL::Init(), and SELECTION_TOOL::Init().

38 {
39  addEntry( ENTRY( aMenu, aExpand, aCondition, aOrder ) );
40 }
void addEntry(ENTRY aEntry)
Inserts the entry, preserving the requested order.
void CONDITIONAL_MENU::AddSeparator ( const SELECTION_CONDITION aCondition = SELECTION_CONDITIONS::ShowAlways,
int  aOrder = ANY_ORDER 
)

Function AddSeparator()

Adds a separator to the menu.

Parameters
aConditionis a condition that has to be fulfilled to enable the submenu entry.
aOrderdetermines location of the added menu, higher numbers are put on the bottom. You may use ANY_ORDER here if you think it does not matter.

Definition at line 43 of file conditional_menu.cpp.

References addEntry().

Referenced by TOOL_MENU::AddStandardSubMenus().

44 {
45  addEntry( ENTRY( aCondition, aOrder ) );
46 }
void addEntry(ENTRY aEntry)
Inserts the entry, preserving the requested order.
CONTEXT_MENU * CONDITIONAL_MENU::Generate ( SELECTION aSelection)

Function Generate()

Generates a context menu that contains only entries that are satisfying assigned conditions.

Parameters
aSelectionis selection for which the conditions are checked against.
Returns
Menu filtered by the entry conditions.

Definition at line 49 of file conditional_menu.cpp.

References CONDITIONAL_MENU::ENTRY::ACTION, CONTEXT_MENU::Add(), m_entries, m_tool, CONDITIONAL_MENU::ENTRY::MENU, CONDITIONAL_MENU::ENTRY::SEPARATOR, CONTEXT_MENU::SetTool(), and CONDITIONAL_MENU::ENTRY::WXITEM.

Referenced by TOOL_MENU::ShowContextMenu().

50 {
51  CONTEXT_MENU* m_menu = new CONTEXT_MENU;
52  m_menu->SetTool( m_tool );
53 
54  for( std::list<ENTRY>::iterator it = m_entries.begin(); it != m_entries.end(); ++it )
55  {
56  const SELECTION_CONDITION& cond = it->Condition();
57 
58  try
59  {
60  if( !cond( aSelection ) )
61  continue;
62  }
63  catch( std::exception& )
64  {
65  continue;
66  }
67 
68  switch( it->Type() )
69  {
70  case ENTRY::ACTION:
71  m_menu->Add( *it->Action() );
72  break;
73 
74  case ENTRY::MENU:
75  m_menu->Add( it->Menu(), it->Expand() );
76  break;
77 
78  case ENTRY::WXITEM:
79  m_menu->Append( it->wxItem() );
80  break;
81 
82  case ENTRY::SEPARATOR:
83  m_menu->AppendSeparator();
84  break;
85 
86  default:
87  assert( false );
88  break;
89  }
90  }
91 
92  return m_menu;
93 }
TOOL_INTERACTIVE * m_tool
tool owning the menu
Class CONTEXT_MENU.
Definition: context_menu.h:44
std::list< ENTRY > m_entries
List of all menu entries.
wxMenuItem * Add(const wxString &aLabel, int aId, const BITMAP_OPAQUE *aIcon=NULL)
Function Add() Adds an entry to the menu.
void SetTool(TOOL_INTERACTIVE *aTool)
Function SetTool() Sets a tool that is the creator of the menu.
std::function< bool(const SELECTION &)> SELECTION_CONDITION
Functor type that checks a specific condition for selected items.

Member Data Documentation

const int CONDITIONAL_MENU::ANY_ORDER = -1
static

Constant to indicate that we do not care about an ENTRY location in the menu.

Definition at line 42 of file conditional_menu.h.

std::list<ENTRY> CONDITIONAL_MENU::m_entries
private

List of all menu entries.

Definition at line 208 of file conditional_menu.h.

Referenced by addEntry(), and Generate().

TOOL_INTERACTIVE* CONDITIONAL_MENU::m_tool
private

tool owning the menu

Definition at line 211 of file conditional_menu.h.

Referenced by Generate().


The documentation for this class was generated from the following files: