Skip to content
Permalink
Browse files

Python: Add wrapper function to bitmap paths for easier runtime-path … (

#496)

Python: Add wrapper function to bitmap paths for easier runtime-path configuration. Addresses #204 #104 #75 #78
  • Loading branch information...
ANoDE85 authored and jhasse committed Feb 15, 2019
1 parent e22e0da commit b61d08788aedcea37867ef06cf2e62680ca32246
Showing with 68 additions and 1 deletion.
  1. +1 −0 output/xml/default.xml
  2. +42 −1 src/codegen/pythoncg.cpp
  3. +16 −0 src/codegen/pythoncg.h
  4. +9 −0 src/rad/pythonpanel/pythonpanel.cpp
@@ -61,6 +61,7 @@
<option name="handler_name" help="Pass event handler name to Unbind() function."/>source_name
</property>
<property name="indent_with_spaces" type="bool" help="For Python only.&#x0A;Use 4-spaces for indentation instead of tabs" />
<property name="image_path_wrapper_function_name" type="text" help="For Python only.&#x0A;Wrap image paths into a function with this name.&#x0A;Useful for freezing the application." />
</category>
<category name="PHP Properties">
<property name="skip_php_events" type="bool" help="For PHP Only.&#x0A;Call Skip() function in all generated event handlers.">1</property>
@@ -292,7 +292,18 @@ wxString PythonTemplateParser::ValueToCode( PropertyType type, wxString value )

wxString file = ( m_useRelativePath ? TypeConv::MakeRelativePath( absPath, m_basePath ) : absPath );

result << wxT("wx.Bitmap( u\"") << PythonCodeGenerator::ConvertPythonString( file ) << wxT("\", wx.BITMAP_TYPE_ANY )");
result << wxT("wx.Bitmap( ");
if ( !m_imagePathWrapperFunctionName.empty() )
{
result << wxT( "self." ) << m_imagePathWrapperFunctionName << wxT( "( ");
}
result << wxT( "u\"") << PythonCodeGenerator::ConvertPythonString( file ) << wxT( "\"" );
if ( !m_imagePathWrapperFunctionName.empty() )
{
result << wxT( " )");
}
result << wxT( ", wx.BITMAP_TYPE_ANY )");

}
else if ( source == _("Load From Resource") )
{
@@ -354,6 +365,12 @@ wxString PythonTemplateParser::ValueToCode( PropertyType type, wxString value )
return result;
}

// Parameterized setters
void PythonTemplateParser::SetImagePathWrapperFunctionName( wxString imagePathWrapperFunctionName )
{
m_imagePathWrapperFunctionName = imagePathWrapperFunctionName;
}

///////////////////////////////////////////////////////////////////////////////

PythonCodeGenerator::PythonCodeGenerator()
@@ -756,6 +773,20 @@ void PythonCodeGenerator::GenDefinedEventHandlers( PObjectInfo info, PObjectBase
}
}

void PythonCodeGenerator::GenImagePathWrapperFunction()
{
if ( !m_imagePathWrapperFunctionName.empty() )
{
m_source->WriteLn( wxT( "# Virtual image path resolution method. Override this in your derived class." ) );
wxString decl = wxT( "def " ) + m_imagePathWrapperFunctionName + wxT("( self, bitmap_path ):");
m_source->WriteLn( decl );
m_source->Indent();
m_source->WriteLn( wxT("return bitmap_path") );
m_source->WriteLn( wxT("") );
m_source->Unindent();
}
}


wxString PythonCodeGenerator::GetCode(PObjectBase obj, wxString name, bool silent)
{
@@ -783,6 +814,7 @@ wxString PythonCodeGenerator::GetCode(PObjectBase obj, wxString name, bool silen
}

PythonTemplateParser parser( obj, _template, m_i18n, m_useRelativePath, m_basePath );
parser.SetImagePathWrapperFunctionName( m_imagePathWrapperFunctionName );
wxString code = parser.ParseTemplate();

return code;
@@ -902,6 +934,9 @@ void PythonCodeGenerator::GenClassDeclaration(PObjectBase class_obj, bool /*use_
GenVirtualEventHandlers(events, eventHandlerPostfix);
GetGenEventHandlers( class_obj );

// Bitmap wrapper code
GenImagePathWrapperFunction();

m_source->Unindent();
m_source->WriteLn(wxEmptyString);
}
@@ -1545,6 +1580,7 @@ void PythonCodeGenerator::UseRelativePath(bool relative, wxString basePath)
}
}
}

/*
wxString CppCodeGenerator::ConvertToRelativePath(wxString path, wxString basePath)
{
@@ -1558,6 +1594,11 @@ auxPath = _STDSTR(filename.GetFullPath());
return auxPath;
}*/

void PythonCodeGenerator::SetImagePathWrapperFunctionName( wxString imagePathWrapperFunctionName )
{
m_imagePathWrapperFunctionName = imagePathWrapperFunctionName;
}

#define ADD_PREDEFINED_MACRO(x) m_predMacros.insert( wxT(#x) )
#define ADD_PREDEFINED_PREFIX(k, v) m_predModulePrefix[ wxT(#k) ] = wxT(#v)

@@ -56,6 +56,7 @@ class PythonTemplateParser : public TemplateParser
bool m_i18n;
bool m_useRelativePath;
wxString m_basePath;
wxString m_imagePathWrapperFunctionName;

std::map<wxString, wxString> m_predModulePrefix;

@@ -69,6 +70,9 @@ class PythonTemplateParser : public TemplateParser
PTemplateParser CreateParser(const TemplateParser* oldparser, wxString _template) override;
wxString RootWxParentToCode() override;
wxString ValueToCode(PropertyType type, wxString value) override;

// Parameterized setters
void SetImagePathWrapperFunctionName( wxString imagePathWrapperFunctionName );
};

/**
@@ -82,6 +86,7 @@ class PythonCodeGenerator : public CodeGenerator
bool m_useRelativePath;
bool m_i18n;
wxString m_basePath;
wxString m_imagePathWrapperFunctionName;
unsigned int m_firstID;
bool m_disconnectEvents;
wxString m_disconnecMode;
@@ -199,6 +204,7 @@ class PythonCodeGenerator : public CodeGenerator
void GetAddToolbarCode( PObjectInfo info, PObjectBase obj, wxArrayString& codelines );

void GenVirtualEventHandlers( const EventVector &events, const wxString& eventHandlerPostfix );
void GenImagePathWrapperFunction();

public:
/**
@@ -231,6 +237,16 @@ class PythonCodeGenerator : public CodeGenerator
*/
void SetFirstID( const unsigned int id ){ m_firstID = id; }

/**
* Configures the function name, image paths should be wrapped
* into when generating python code.
*
* When set to a non-empty string, this will generate a default
* implementation of the method in each class, that just returns
* the unmodified path string.
*/
void SetImagePathWrapperFunctionName( wxString imagePathWrapperFunctionName );

/**
* Generate the project's code
*/
@@ -276,11 +276,19 @@ void PythonPanel::OnCodeGeneration( wxFBEvent& event )
}
m_pythonCW->SetIndentWithSpaces(useSpaces);

wxString imagePathWrapperFunctionName;
PProperty pImagePathWrapperFunctionName = project->GetProperty( wxT( "image_path_wrapper_function_name" ) );
if (pImagePathWrapperFunctionName)
{
imagePathWrapperFunctionName = pImagePathWrapperFunctionName->GetValueAsString();
}

// Generate code in the panel
if ( doPanel )
{
PythonCodeGenerator codegen;
codegen.UseRelativePath( useRelativePath, path );
codegen.SetImagePathWrapperFunctionName( imagePathWrapperFunctionName );

if ( pFirstID )
{
@@ -314,6 +322,7 @@ void PythonPanel::OnCodeGeneration( wxFBEvent& event )
{
PythonCodeGenerator codegen;
codegen.UseRelativePath( useRelativePath, path );
codegen.SetImagePathWrapperFunctionName( imagePathWrapperFunctionName );

if ( pFirstID )
{

0 comments on commit b61d087

Please sign in to comment.
You can’t perform that action at this time.