KiCad PCB EDA Suite
PYTHON_ACTION_PLUGIN Class Reference

#include <pcbnew_action_plugins.h>

Inheritance diagram for PYTHON_ACTION_PLUGIN:
ACTION_PLUGIN

Public Member Functions

 PYTHON_ACTION_PLUGIN (PyObject *action)
 
 ~PYTHON_ACTION_PLUGIN ()
 
wxString GetCategoryName () override
 
wxString GetName () override
 
wxString GetDescription () override
 
bool GetShowToolbarButton () override
 
wxString GetIconFileName (bool aDark) override
 
wxString GetPluginPath () override
 
void Run () override
 This method the the action. More...
 
void * GetObject () override
 This method gets the pointer to the object from where this action constructs. More...
 
void register_action ()
 It's the standard method of a "ACTION_PLUGIN" to register itself into the ACTION_PLUGINS singleton manager. More...
 

Public Attributes

int m_actionMenuId
 
int m_actionButtonId
 
wxBitmap iconBitmap
 
bool show_on_toolbar
 

Private Member Functions

PyObject * CallMethod (const char *aMethod, PyObject *aArglist=nullptr)
 
wxString CallRetStrMethod (const char *aMethod, PyObject *aArglist=nullptr)
 

Private Attributes

PyObject * m_PyAction
 

Detailed Description

Definition at line 40 of file pcbnew_action_plugins.h.

Constructor & Destructor Documentation

◆ PYTHON_ACTION_PLUGIN()

PYTHON_ACTION_PLUGIN::PYTHON_ACTION_PLUGIN ( PyObject *  action)

Definition at line 38 of file pcbnew_action_plugins.cpp.

39 {
40  PyLOCK lock;
41 
42  m_PyAction = aAction;
43  Py_XINCREF( aAction );
44 }

References m_PyAction.

◆ ~PYTHON_ACTION_PLUGIN()

PYTHON_ACTION_PLUGIN::~PYTHON_ACTION_PLUGIN ( )

Definition at line 47 of file pcbnew_action_plugins.cpp.

48 {
49  PyLOCK lock;
50 
51  Py_XDECREF( m_PyAction );
52 }

References m_PyAction.

Member Function Documentation

◆ CallMethod()

PyObject * PYTHON_ACTION_PLUGIN::CallMethod ( const char *  aMethod,
PyObject *  aArglist = nullptr 
)
private

Definition at line 55 of file pcbnew_action_plugins.cpp.

56 {
57  PyLOCK lock;
58 
59  PyErr_Clear();
60 
61  // pFunc is a new reference to the desired method
62  PyObject* pFunc = PyObject_GetAttrString( m_PyAction, aMethod );
63 
64  if( pFunc && PyCallable_Check( pFunc ) )
65  {
66  PyObject* result = PyObject_CallObject( pFunc, aArglist );
67 
68  if( PyErr_Occurred() )
69  {
70  wxMessageBox( PyErrStringWithTraceback(),
71  _( "Exception on python action plugin code" ),
72  wxICON_ERROR | wxOK );
73  }
74 
75  if( result )
76  {
77  Py_XDECREF( pFunc );
78  return result;
79  }
80  }
81  else
82  {
83  wxString msg = wxString::Format( _( "Method \"%s\" not found, or not callable" ), aMethod );
84  wxMessageBox( msg, _( "Unknown Method" ), wxICON_ERROR | wxOK );
85  }
86 
87  if( pFunc )
88  {
89  Py_XDECREF( pFunc );
90  }
91 
92  return nullptr;
93 }
#define _(s)
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200

References _, Format(), and m_PyAction.

Referenced by CallRetStrMethod(), GetShowToolbarButton(), and Run().

◆ CallRetStrMethod()

wxString PYTHON_ACTION_PLUGIN::CallRetStrMethod ( const char *  aMethod,
PyObject *  aArglist = nullptr 
)
private

Definition at line 96 of file pcbnew_action_plugins.cpp.

97 {
98  wxString ret;
99  PyLOCK lock;
100 
101  PyObject* result = CallMethod( aMethod, aArglist );
102 
103  ret = PyStringToWx( result );
104  Py_XDECREF( result );
105 
106  return ret;
107 }
PyObject * CallMethod(const char *aMethod, PyObject *aArglist=nullptr)

References CallMethod().

Referenced by GetCategoryName(), GetDescription(), GetIconFileName(), GetName(), and GetPluginPath().

◆ GetCategoryName()

wxString PYTHON_ACTION_PLUGIN::GetCategoryName ( )
overridevirtual
Returns
the category name of the action (to be able to group action under the same submenu).

Implements ACTION_PLUGIN.

Definition at line 110 of file pcbnew_action_plugins.cpp.

111 {
112  PyLOCK lock;
113 
114  return CallRetStrMethod( "GetCategoryName" );
115 }
wxString CallRetStrMethod(const char *aMethod, PyObject *aArglist=nullptr)

References CallRetStrMethod().

◆ GetDescription()

wxString PYTHON_ACTION_PLUGIN::GetDescription ( )
overridevirtual
Returns
a description of the action plugin.

Implements ACTION_PLUGIN.

Definition at line 126 of file pcbnew_action_plugins.cpp.

127 {
128  PyLOCK lock;
129 
130  return CallRetStrMethod( "GetDescription" );
131 }
wxString CallRetStrMethod(const char *aMethod, PyObject *aArglist=nullptr)

References CallRetStrMethod().

◆ GetIconFileName()

wxString PYTHON_ACTION_PLUGIN::GetIconFileName ( bool  aDark)
overridevirtual
Parameters
aDarkset to true if requesting dark theme icon.
Returns
a path to icon for the action plugin button.

Implements ACTION_PLUGIN.

Definition at line 144 of file pcbnew_action_plugins.cpp.

145 {
146  PyLOCK lock;
147 
148  PyObject* arglist = Py_BuildValue( "(i)", static_cast<int>( aDark ) );
149 
150  wxString result = CallRetStrMethod( "GetIconFileName", arglist );
151 
152  Py_DECREF( arglist );
153 
154  return result;
155 }
wxString CallRetStrMethod(const char *aMethod, PyObject *aArglist=nullptr)

References CallRetStrMethod().

◆ GetName()

wxString PYTHON_ACTION_PLUGIN::GetName ( )
overridevirtual
Returns
the name of the action.

Implements ACTION_PLUGIN.

Definition at line 118 of file pcbnew_action_plugins.cpp.

119 {
120  PyLOCK lock;
121 
122  return CallRetStrMethod( "GetName" );
123 }
wxString CallRetStrMethod(const char *aMethod, PyObject *aArglist=nullptr)

References CallRetStrMethod().

◆ GetObject()

void * PYTHON_ACTION_PLUGIN::GetObject ( )
overridevirtual

This method gets the pointer to the object from where this action constructs.

Returns
it's a void pointer, as it could be a PyObject or any other

Implements ACTION_PLUGIN.

Definition at line 174 of file pcbnew_action_plugins.cpp.

175 {
176  return (void*) m_PyAction;
177 }

References m_PyAction.

◆ GetPluginPath()

wxString PYTHON_ACTION_PLUGIN::GetPluginPath ( )
overridevirtual
Returns
a path this plugin was loaded from.

Implements ACTION_PLUGIN.

Definition at line 158 of file pcbnew_action_plugins.cpp.

159 {
160  PyLOCK lock;
161 
162  return CallRetStrMethod( "GetPluginPath" );
163 }
wxString CallRetStrMethod(const char *aMethod, PyObject *aArglist=nullptr)

References CallRetStrMethod().

◆ GetShowToolbarButton()

bool PYTHON_ACTION_PLUGIN::GetShowToolbarButton ( )
overridevirtual
Returns
true if button should be shown on top toolbar.

Implements ACTION_PLUGIN.

Definition at line 134 of file pcbnew_action_plugins.cpp.

135 {
136  PyLOCK lock;
137 
138  PyObject* result = CallMethod( "GetShowToolbarButton");
139 
140  return PyObject_IsTrue(result);
141 }
PyObject * CallMethod(const char *aMethod, PyObject *aArglist=nullptr)

References CallMethod().

◆ register_action()

void ACTION_PLUGIN::register_action ( )
inherited

It's the standard method of a "ACTION_PLUGIN" to register itself into the ACTION_PLUGINS singleton manager.

Definition at line 43 of file action_plugin.cpp.

44 {
46 }
static void register_action(ACTION_PLUGIN *aAction)
An action calls this static method when it wants to register itself into the system actions.

References ACTION_PLUGINS::register_action().

Referenced by PYTHON_ACTION_PLUGINS::register_action().

◆ Run()

void PYTHON_ACTION_PLUGIN::Run ( )
overridevirtual

This method the the action.

Implements ACTION_PLUGIN.

Definition at line 166 of file pcbnew_action_plugins.cpp.

167 {
168  PyLOCK lock;
169 
170  CallMethod( "Run" );
171 }
PyObject * CallMethod(const char *aMethod, PyObject *aArglist=nullptr)

References CallMethod().

Member Data Documentation

◆ iconBitmap

wxBitmap ACTION_PLUGIN::iconBitmap
inherited

◆ m_actionButtonId

int ACTION_PLUGIN::m_actionButtonId
inherited

Definition at line 101 of file action_plugin.h.

Referenced by ACTION_PLUGINS::SetActionButton().

◆ m_actionMenuId

int ACTION_PLUGIN::m_actionMenuId
inherited

Definition at line 98 of file action_plugin.h.

◆ m_PyAction

PyObject* PYTHON_ACTION_PLUGIN::m_PyAction
private

◆ show_on_toolbar

bool ACTION_PLUGIN::show_on_toolbar
inherited

Definition at line 107 of file action_plugin.h.


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