You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
243 lines
12 KiB
243 lines
12 KiB
15 years ago
|
/* Copyright (C) 2004-2005 ian reinhart geiser <geiseri@sourcextreme.com> */
|
||
12 years ago
|
#include <tdeaboutdata.h>
|
||
15 years ago
|
#include <kinstance.h>
|
||
12 years ago
|
#include <tdecmdlineargs.h>
|
||
12 years ago
|
#include <tdeconfig.h>
|
||
15 years ago
|
#include <kmacroexpander.h>
|
||
|
#include <kdebug.h>
|
||
|
|
||
13 years ago
|
#include <tqtextstream.h>
|
||
15 years ago
|
#include <tqfileinfo.h>
|
||
|
#include <tqfile.h>
|
||
|
#include <tqbuffer.h>
|
||
|
#include <tqimage.h>
|
||
15 years ago
|
|
||
|
static const char description[] = I18N_NOOP( "Builds Qt widget plugins from an ini style description file." );
|
||
|
static const char version[] = "0.2";
|
||
|
static const char classHeader[] = "/**\n"
|
||
12 years ago
|
"* This file was autogenerated by maketdewidgets. Any changes will be lost!\n"
|
||
15 years ago
|
"* The generated code in this file is licensed under the same license that the\n"
|
||
|
"* input file.\n"
|
||
|
"*/\n"
|
||
15 years ago
|
"#include <tqwidgetplugin.h>\n";
|
||
15 years ago
|
static const char classDef[] = "#ifndef EMBED_IMAGES\n"
|
||
|
"#include <kstandarddirs.h>\n"
|
||
|
"#endif\n"
|
||
|
"\n"
|
||
14 years ago
|
"class %PluginName : public TQWidgetPlugin\n"
|
||
15 years ago
|
"{\n"
|
||
|
"public:\n"
|
||
|
" %PluginName();\n"
|
||
|
" \n"
|
||
|
" virtual ~%PluginName();\n"
|
||
|
" \n"
|
||
15 years ago
|
" virtual TQStringList keys() const\n"
|
||
15 years ago
|
" {\n"
|
||
15 years ago
|
" TQStringList result;\n"
|
||
15 years ago
|
" for (WidgetInfos::ConstIterator it = m_widgets.begin(); it != m_widgets.end(); ++it)\n"
|
||
|
" result << it.key();\n"
|
||
|
" return result;\n"
|
||
|
" }\n"
|
||
|
" \n"
|
||
15 years ago
|
" virtual TQWidget *create(const TQString &key, TQWidget *parent = 0, const char *name = 0);\n"
|
||
15 years ago
|
" \n"
|
||
15 years ago
|
" virtual TQIconSet iconSet(const TQString &key) const\n"
|
||
15 years ago
|
" {\n"
|
||
|
"#ifdef EMBED_IMAGES\n"
|
||
15 years ago
|
" TQPixmap pix(m_widgets[key].iconSet);\n"
|
||
15 years ago
|
"#else\n"
|
||
15 years ago
|
" TQPixmap pix(locate( \"data\", \n"
|
||
13 years ago
|
" TQString::fromLatin1(\"%PluginNameLower/pics/\") + m_widgets[key].iconSet));\n"
|
||
15 years ago
|
"#endif\n"
|
||
15 years ago
|
" return TQIconSet(pix);\n"
|
||
15 years ago
|
" }\n"
|
||
|
" \n"
|
||
15 years ago
|
" virtual bool isContainer(const TQString &key) const { return m_widgets[key].isContainer; }\n"
|
||
15 years ago
|
" \n"
|
||
15 years ago
|
" virtual TQString group(const TQString &key) const { return m_widgets[key].group; }\n"
|
||
15 years ago
|
" \n"
|
||
15 years ago
|
" virtual TQString includeFile(const TQString &key) const { return m_widgets[key].includeFile; }\n"
|
||
15 years ago
|
" \n"
|
||
15 years ago
|
" virtual TQString toolTip(const TQString &key) const { return m_widgets[key].toolTip; }\n"
|
||
15 years ago
|
" \n"
|
||
15 years ago
|
" virtual TQString whatsThis(const TQString &key) const { return m_widgets[key].whatsThis; }\n"
|
||
15 years ago
|
"private:\n"
|
||
|
" struct WidgetInfo\n"
|
||
|
" {\n"
|
||
15 years ago
|
" TQString group;\n"
|
||
15 years ago
|
"#ifdef EMBED_IMAGES\n"
|
||
15 years ago
|
" TQPixmap iconSet;\n"
|
||
15 years ago
|
"#else\n"
|
||
15 years ago
|
" TQString iconSet;\n"
|
||
15 years ago
|
"#endif\n"
|
||
15 years ago
|
" TQString includeFile;\n"
|
||
|
" TQString toolTip;\n"
|
||
|
" TQString whatsThis;\n"
|
||
15 years ago
|
" bool isContainer;\n"
|
||
|
" };\n"
|
||
15 years ago
|
" typedef TQMap<TQString, WidgetInfo> WidgetInfos;\n"
|
||
15 years ago
|
" WidgetInfos m_widgets;\n"
|
||
|
"};\n"
|
||
|
"%PluginName::%PluginName()\n"
|
||
|
"{\n"
|
||
|
" WidgetInfo widget;\n";
|
||
13 years ago
|
static const char widgetDef[] = " widget.group = TQString::fromLatin1(\"%Group\");\n"
|
||
15 years ago
|
"#ifdef EMBED_IMAGES\n"
|
||
15 years ago
|
" widget.iconSet = TQPixmap(%Pixmap);\n"
|
||
15 years ago
|
"#else\n"
|
||
13 years ago
|
" widget.iconSet = TQString::fromLatin1(\"%IconSet\");\n"
|
||
15 years ago
|
"#endif\n"
|
||
13 years ago
|
" widget.includeFile = TQString::fromLatin1(\"%IncludeFile\");\n"
|
||
|
" widget.toolTip = TQString::fromLatin1(\"%ToolTip\");\n"
|
||
|
" widget.whatsThis = TQString::fromLatin1(\"%WhatsThis\");\n"
|
||
15 years ago
|
" widget.isContainer = %IsContainer;\n"
|
||
13 years ago
|
" m_widgets.insert(TQString::fromLatin1(\"%Class\"), widget);\n";
|
||
15 years ago
|
static const char endCtor[] = " %Init\n"
|
||
|
"}\n"
|
||
|
"%PluginName::~%PluginName()\n"
|
||
|
"{\n"
|
||
|
" %Destroy\n"
|
||
|
"}\n"
|
||
15 years ago
|
"TQWidget *%PluginName::create(const TQString &key, TQWidget *parent, const char *name)\n"
|
||
15 years ago
|
"{\n";
|
||
13 years ago
|
static const char widgetCreate[] = " if (key == TQString::fromLatin1(\"%Class\"))\n"
|
||
15 years ago
|
" return new %ImplClass%ConstructorArgs;\n";
|
||
|
static const char endCreate[] = " return 0;\n"
|
||
|
"}\n"
|
||
|
"KDE_Q_EXPORT_PLUGIN(%PluginName)\n";
|
||
|
|
||
|
|
||
12 years ago
|
static TDECmdLineOptions options[] =
|
||
15 years ago
|
{
|
||
|
{ "+file", I18N_NOOP( "Input file" ), 0 },
|
||
|
{ "o <file>", I18N_NOOP( "Output file" ), 0 },
|
||
|
{ "n <plugin name>", I18N_NOOP( "Name of the plugin class to generate" ), "WidgetsPlugin" },
|
||
|
{ "g <group>", I18N_NOOP( "Default widget group name to display in designer" ), "Custom" },
|
||
|
{ "p <pixmap dir>", I18N_NOOP( "Embed pixmaps from a source directory" ), 0 },
|
||
12 years ago
|
TDECmdLineLastOption
|
||
15 years ago
|
};
|
||
|
|
||
12 years ago
|
static TQString buildWidgetDef( const TQString &name, TDEConfig &input, const TQString &group );
|
||
|
static TQString buildWidgetCreate( const TQString &name, TDEConfig &input );
|
||
|
static TQString buildWidgetInclude( const TQString &name, TDEConfig &input );
|
||
15 years ago
|
static void buildFile( TQTextStream &stream, const TQString& group, const TQString& fileName, const TQString& pluginName, const TQString& iconPath );
|
||
12 years ago
|
static TQString buildPixmap( const TQString &name, TDEConfig &input, const TQString &iconPath );
|
||
15 years ago
|
|
||
|
int main( int argc, char **argv ) {
|
||
12 years ago
|
new TDEInstance( "maketdewidgets" );
|
||
15 years ago
|
|
||
12 years ago
|
TDEAboutData about( "maketdewidgets", I18N_NOOP( "maketdewidgets" ), version, description, TDEAboutData::License_GPL, "(C) 2004-2005 ian reinhart geiser", 0, 0, "geiseri@kde.org" );
|
||
15 years ago
|
about.addAuthor( "ian reinhart geiser", 0, "geiseri@kde.org" );
|
||
12 years ago
|
TDECmdLineArgs::init( argc, argv, &about );
|
||
|
TDECmdLineArgs::addCmdLineOptions( options );
|
||
|
TDECmdLineArgs *args = TDECmdLineArgs::parsedArgs();
|
||
15 years ago
|
if ( args->count() < 1 ) {
|
||
|
args->usage();
|
||
|
return ( 1 );
|
||
|
}
|
||
|
|
||
15 years ago
|
TQFileInfo fi( args->arg( args->count() - 1 ) );
|
||
15 years ago
|
|
||
15 years ago
|
TQString outputFile = args->getOption( "o" );
|
||
|
TQString pluginName = args->getOption( "n" );
|
||
|
TQString group = args->getOption( "g" );
|
||
|
TQString iconPath = "";
|
||
15 years ago
|
if ( args->isSet( "p" ) )
|
||
|
iconPath = args->getOption( "p" );
|
||
15 years ago
|
TQString fileName = fi.absFilePath();
|
||
15 years ago
|
|
||
|
if ( args->isSet( "o" ) ) {
|
||
15 years ago
|
TQFile output( outputFile );
|
||
15 years ago
|
if ( output.open( IO_WriteOnly ) ) {
|
||
15 years ago
|
TQTextStream ts( &output );
|
||
15 years ago
|
buildFile( ts, group, fileName , pluginName, iconPath );
|
||
|
}
|
||
|
output.close();
|
||
|
} else {
|
||
15 years ago
|
TQTextStream ts( stdout, IO_WriteOnly );
|
||
15 years ago
|
buildFile( ts, group, fileName , pluginName, iconPath );
|
||
|
}
|
||
|
}
|
||
|
|
||
15 years ago
|
void buildFile( TQTextStream &ts, const TQString& group, const TQString& fileName, const TQString& pluginName, const TQString& iconPath ) {
|
||
12 years ago
|
TDEConfig input( fileName, true, false );
|
||
15 years ago
|
input.setGroup( "Global" );
|
||
15 years ago
|
TQMap<TQString, TQString> MainMap;
|
||
15 years ago
|
MainMap.insert( "PluginName", input.readEntry( "PluginName", pluginName ) );
|
||
|
MainMap.insert( "PluginNameLower", input.readEntry( "PluginName", pluginName ).lower() );
|
||
|
MainMap.insert( "Init", input.readEntry( "Init", "" ) );
|
||
|
MainMap.insert( "Destroy", input.readEntry( "Destroy", "" ) );
|
||
|
ts << classHeader << endl;
|
||
15 years ago
|
TQStringList includes = input.readListEntry( "Includes", ',' );
|
||
15 years ago
|
for ( uint idx = 0; idx < includes.count(); ++idx )
|
||
|
ts << "#include <" << includes[ idx ] << ">" << endl;
|
||
15 years ago
|
TQStringList classes = input.groupList();
|
||
14 years ago
|
classes.remove( classes.find( "Global" ) );
|
||
15 years ago
|
// Autogenerate widget includes here
|
||
|
for ( uint idx = 0; idx < classes.count(); ++idx )
|
||
|
ts << buildWidgetInclude( classes[ idx ], input ) << endl;
|
||
|
// Generate embedded icons
|
||
|
if ( !iconPath.isEmpty() ) {
|
||
|
for ( uint idx = 0; idx < classes.count(); ++idx )
|
||
|
ts << buildPixmap( classes[ idx ], input, iconPath ) << endl;
|
||
|
ts << "#define EMBED_IMAGES" << endl;
|
||
|
}
|
||
|
// Generate the main class code.
|
||
|
ts << KMacroExpander::expandMacros( classDef, MainMap ) << endl;
|
||
|
// Autogenerate widget defs here
|
||
|
for ( uint idx = 0; idx < classes.count(); ++idx )
|
||
|
ts << buildWidgetDef( classes[ idx ], input, group ) << endl;
|
||
|
ts << KMacroExpander::expandMacros( endCtor, MainMap ) << endl;
|
||
|
// Autogenerate create code here...
|
||
|
for ( uint idx = 0; idx < classes.count(); ++idx )
|
||
|
ts << buildWidgetCreate( classes[ idx ], input ) << endl;
|
||
|
ts << KMacroExpander::expandMacros( endCreate, MainMap ) << endl;
|
||
|
|
||
|
}
|
||
|
|
||
12 years ago
|
TQString buildWidgetDef( const TQString &name, TDEConfig &input, const TQString &group ) {
|
||
15 years ago
|
input.setGroup( name );
|
||
15 years ago
|
TQMap<TQString, TQString> defMap;
|
||
14 years ago
|
defMap.insert( "Group", input.readEntry( "Group", group ).replace( "\"", "\\\"" ) );
|
||
|
defMap.insert( "IconSet", input.readEntry( "IconSet", name.lower() + ".png" ).replace( ":", "_" ) );
|
||
|
defMap.insert( "Pixmap", name.lower().replace( ":", "_" ) + "_xpm" );
|
||
15 years ago
|
defMap.insert( "IncludeFile", input.readEntry( "IncludeFile", name.lower() + ".h" ).remove( ":" ) );
|
||
14 years ago
|
defMap.insert( "ToolTip", input.readEntry( "ToolTip", name + " Widget" ).replace( "\"", "\\\"" ) );
|
||
|
defMap.insert( "WhatsThis", input.readEntry( "WhatsThis", name + " Widget" ).replace( "\"", "\\\"" ) );
|
||
15 years ago
|
defMap.insert( "IsContainer", input.readEntry( "IsContainer", "false" ) );
|
||
|
defMap.insert( "Class", name );
|
||
|
return KMacroExpander::expandMacros( widgetDef, defMap );
|
||
|
}
|
||
|
|
||
12 years ago
|
TQString buildWidgetCreate( const TQString &name, TDEConfig &input ) {
|
||
15 years ago
|
input.setGroup( name );
|
||
15 years ago
|
TQMap<TQString, TQString> createMap;
|
||
15 years ago
|
createMap.insert( "ImplClass", input.readEntry( "ImplClass", name ) );
|
||
|
createMap.insert( "ConstructorArgs", input.readEntry( "ConstructorArgs", "(parent, name)" ) );
|
||
|
createMap.insert( "Class", name );
|
||
|
return KMacroExpander::expandMacros( widgetCreate, createMap );
|
||
|
}
|
||
|
|
||
12 years ago
|
TQString buildWidgetInclude( const TQString &name, TDEConfig &input ) {
|
||
15 years ago
|
input.setGroup( name );
|
||
|
return "#include <" + input.readEntry( "IncludeFile", name.lower() + ".h" ) + ">";
|
||
|
}
|
||
|
|
||
12 years ago
|
TQString buildPixmap( const TQString &name, TDEConfig &input, const TQString &iconPath ) {
|
||
15 years ago
|
input.setGroup( name );
|
||
14 years ago
|
TQString cleanName = name.lower().replace( ":", "_" );
|
||
15 years ago
|
TQString iconName = input.readEntry( "IconSet", cleanName + ".png" );
|
||
15 years ago
|
|
||
15 years ago
|
TQFileInfo fi( iconPath + "/" + iconName );
|
||
|
TQImage pix( fi.absFilePath() );
|
||
|
TQCString xpm;
|
||
|
TQBuffer buff( xpm );
|
||
15 years ago
|
buff.open( IO_WriteOnly );
|
||
15 years ago
|
TQImageIO io( &buff, "XPM" );
|
||
15 years ago
|
io.setFileName( cleanName + "_xpm" );
|
||
|
io.setImage( pix );
|
||
|
io.write();
|
||
|
buff.close();
|
||
|
return xpm;
|
||
|
}
|