KiCad PCB EDA Suite
action_plugin.cpp
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2017-2021 KiCad Developers, see AUTHORS.txt for contributors.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, you may find one here:
18  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19  * or you may search the http://www.gnu.org website for the version 2 license,
20  * or you may write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22  */
23 
24 
30 #include <wx/log.h>
31 
32 #include "action_plugin.h"
33 #include "bitmaps.h"
34 #include "bitmap_store.h"
35 #include <pgm_base.h>
36 
37 
39 {
40 }
41 
42 
44 {
46 }
47 
48 
49 std::vector<ACTION_PLUGIN*> ACTION_PLUGINS::m_actionsList;
50 
51 
53 
54 
56 {
57  return m_actionsList[aIndex];
58 }
59 
60 
62 {
63  int max = GetActionsCount();
64 
65  for( int i = 0; i < max; i++ )
66  {
67  if( m_actionsList[i]->m_actionMenuId == aMenu )
68  return m_actionsList[i];
69  }
70 
71  return nullptr;
72 }
73 
74 
75 void ACTION_PLUGINS::SetActionMenu( int aIndex, int idMenu )
76 {
77  m_actionsList[aIndex]->m_actionMenuId = idMenu;
78 }
79 
80 
82 {
83  int max = GetActionsCount();
84 
85  for( int i = 0; i < max; i++ )
86  {
87  if( m_actionsList[i]->m_actionButtonId == aButton )
88  return m_actionsList[i];
89  }
90 
91  return nullptr;
92 }
93 
94 
95 void ACTION_PLUGINS::SetActionButton( ACTION_PLUGIN* aAction, int idButton )
96 {
97  aAction->m_actionButtonId = idButton;
98 }
99 
100 
102 {
103  for( int i = 0; i < GetActionsCount() ; i++ )
104  {
105  if( m_actionsList[i]->GetPluginPath() == aPath)
106  {
107  return m_actionsList[i];
108  }
109  }
110 
111  return nullptr;
112 }
113 
114 
115 ACTION_PLUGIN* ACTION_PLUGINS::GetAction( const wxString& aName )
116 {
117  int max = GetActionsCount();
118 
119  for( int i = 0; i<max; i++ )
120  {
121  ACTION_PLUGIN* action = GetAction( i );
122 
123  wxString name = action->GetName();
124 
125  if( name.Cmp( aName )==0 )
126  return action;
127  }
128 
129  return nullptr;
130 }
131 
132 
134 {
135  return m_actionsList.size();
136 }
137 
138 
140 {
141  // Search for this entry do not register twice this action:
142  for( int ii = 0; ii < GetActionsCount(); ii++ )
143  {
144  if( aAction == GetAction( ii ) ) // Already registered
145  return;
146  }
147 
148  // Search for a action with the same name, and remove it if found
149  for( int ii = 0; ii < GetActionsCount(); ii++ )
150  {
151  ACTION_PLUGIN* action = GetAction( ii );
152 
153  if( action->GetName() == aAction->GetName() )
154  {
155  m_actionsList.erase( m_actionsList.begin() + ii );
156 
157  delete action;
158 
159  break;
160  }
161  }
162 
163  wxASSERT( PgmOrNull() ); // PgmOrNull() returning nullptr should never happen,
164  // but it sometimes happens on msys2 build
165 
166  if( PgmOrNull() ) // Hack for msys2. Must be removed when the root cause is fixed
167  {
168  // Load icon if supplied
169  wxString icon_file_name = aAction->GetIconFileName( GetBitmapStore()->IsDarkTheme() );
170 
171  if( !icon_file_name.IsEmpty() )
172  {
173  {
174  wxLogNull eat_errors;
175  aAction->iconBitmap.LoadFile( icon_file_name, wxBITMAP_TYPE_PNG );
176  }
177 
178  if ( !aAction->iconBitmap.IsOk() )
179  {
180  wxLogVerbose( "Failed to load icon " + icon_file_name + " for action plugin " );
181  }
182  }
183  }
184 
185  m_actionsList.push_back( aAction );
186 }
187 
188 
190 {
191  int max = GetActionsCount();
192 
193  for( int i = 0; i<max; i++ )
194  {
195  ACTION_PLUGIN* action = GetAction( i );
196 
197  if( action->GetObject() == aObject )
198  {
199  m_actionsList.erase( m_actionsList.begin() + i );
200 
201  //m_actionsListMenu.erase( m_actionsListMenu.begin() + i );
202  delete action;
203  return true;
204  }
205  }
206 
207  return false;
208 }
209 
210 
212 {
214 }
215 
216 
217 void ACTION_PLUGINS::SetActionRunning( bool aRunning )
218 {
220 }
221 
222 
224 {
225  for( ACTION_PLUGIN* plugin : m_actionsList )
226  delete plugin;
227 
228  m_actionsList.clear();
229 }
virtual wxString GetName()=0
This is the parent class from where any action plugin class must derive.
Definition: action_plugin.h:38
static bool m_actionRunning
static bool IsActionRunning()
static ACTION_PLUGIN * GetActionByPath(const wxString &aPath)
Find action plugin by module path.
wxBitmap iconBitmap
bool IsDarkTheme()
Determine if the desktop interface is currently using a dark theme or a light theme.
Definition: gtk/ui.cpp:31
void register_action()
It's the standard method of a "ACTION_PLUGIN" to register itself into the ACTION_PLUGINS singleton ma...
virtual ~ACTION_PLUGIN()
static void UnloadAll()
Unload (deregister) all action plugins.
virtual wxString GetIconFileName(bool aDark)=0
PGM_BASE * PgmOrNull()
similar to PGM_BASE& Pgm(), but return a reference that can be nullptr when running a shared lib from...
Definition: cvpcb.cpp:125
static ACTION_PLUGIN * GetActionByMenu(int aMenu)
Find action plugin associated to a menu ID.
static int GetActionsCount()
static void register_action(ACTION_PLUGIN *aAction)
An action calls this static method when it wants to register itself into the system actions.
static std::vector< ACTION_PLUGIN * > m_actionsList
ACTION_PLUGIN system wide static list.
static void SetActionButton(ACTION_PLUGIN *aAction, int idButton)
Associate a button id to an action plugin.
static ACTION_PLUGIN * GetAction(const wxString &aName)
Class PCBNEW_ACTION_PLUGINS.
see class PGM_BASE
const char * name
Definition: DXF_plotter.cpp:56
static void SetActionMenu(int aIndex, int idMenu)
Associate a menu id to an action plugin.
static bool deregister_object(void *aObject)
Deregister an object which builds a action.
static ACTION_PLUGIN * GetActionByButton(int aButton)
Find action plugin associated to a button ID.
static void SetActionRunning(bool aRunning)
virtual void * GetObject()=0
This method gets the pointer to the object from where this action constructs.
BITMAP_STORE * GetBitmapStore()
Definition: bitmap.cpp:93