summaryrefslogtreecommitdiffstats
path: root/tdewidgets/maketdewidgets.cpp
blob: d890433c3247142a7089277108c45ae8e2d91bb8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/* Copyright (C) 2004-2005 ian reinhart geiser <geiseri@sourcextreme.com> */
#include <tdeaboutdata.h>
#include <kinstance.h>
#include <tdecmdlineargs.h>
#include <tdeconfig.h>
#include <kmacroexpander.h>
#include <kdebug.h>

#include <tqtextstream.h>
#include <tqfileinfo.h>
#include <tqfile.h>
#include <tqbuffer.h>
#include <tqimage.h>

static const char description[] = I18N_NOOP( "Builds TQt widget plugins from an ini style description file." );
static const char version[] = "0.2";
static const char classHeader[] = 	"/**\n"
					"* This file was autogenerated by maketdewidgets. Any changes will be lost!\n"
					"* The generated code in this file is licensed under the same license that the\n"
					"* input file.\n"
                                   	"*/\n"
					"#include <tqwidgetplugin.h>\n";
static const char classDef[] = "#ifndef EMBED_IMAGES\n"
                                "#include <kstandarddirs.h>\n"
                                "#endif\n"
                                "\n"
                                "class %PluginName : public TQWidgetPlugin\n"
                                "{\n"
                                "public:\n"
                                "	%PluginName();\n"
                                "	\n"
                                "	virtual ~%PluginName();\n"
                                "	\n"
                                "	virtual TQStringList keys() const\n"
                                "	{\n"
                                "		TQStringList result;\n"
                                "		for (WidgetInfos::ConstIterator it = m_widgets.begin(); it != m_widgets.end(); ++it)\n"
                                "			result << it.key();\n"
                                "		return result;\n"
                                "	}\n"
                                "	\n"
                                "	virtual TQWidget *create(const TQString &key, TQWidget *parent = 0, const char *name = 0);\n"
                                "	\n"
                                "	virtual TQIconSet iconSet(const TQString &key) const\n"
                                "	{\n"
                                "#ifdef EMBED_IMAGES\n"
                                "		TQPixmap pix(m_widgets[key].iconSet);\n"
                                "#else\n"
                                "		TQPixmap pix(locate( \"data\", \n"
				"			TQString::fromLatin1(\"%PluginNameLower/pics/\") + m_widgets[key].iconSet));\n"
                                "#endif\n"
                                "		return TQIconSet(pix);\n"
                                "	}\n"
                                "	\n"
                                "	virtual bool isContainer(const TQString &key) const { return m_widgets[key].isContainer; }\n"
                                "	\n"
                                "	virtual TQString group(const TQString &key) const { return m_widgets[key].group; }\n"
                                "	\n"
                                "	virtual TQString includeFile(const TQString &key) const { return m_widgets[key].includeFile; }\n"
                                "	\n"
                                "	virtual TQString toolTip(const TQString &key) const { return m_widgets[key].toolTip; }\n"
                                "	\n"
                                "	virtual TQString whatsThis(const TQString &key) const { return m_widgets[key].whatsThis; }\n"
                                "private:\n"
                                "	struct WidgetInfo\n"
                                "	{\n"
                                "		TQString group;\n"
                                "#ifdef EMBED_IMAGES\n"
                                "		TQPixmap iconSet;\n"
                                "#else\n"
                                "		TQString iconSet;\n"
                                "#endif\n"
                                "		TQString includeFile;\n"
                                "		TQString toolTip;\n"
                                "		TQString whatsThis;\n"
                                "		bool isContainer;\n"
                                "	};\n"
                                "	typedef TQMap<TQString, WidgetInfo> WidgetInfos;\n"
                                "	WidgetInfos m_widgets;\n"
                                "};\n"
                                "%PluginName::%PluginName()\n"
                                "{\n"
                                "        WidgetInfo widget;\n";
static const char widgetDef[] = "	widget.group = TQString::fromLatin1(\"%Group\");\n"
                                 "#ifdef EMBED_IMAGES\n"
                                 "	widget.iconSet = TQPixmap(%Pixmap);\n"
                                 "#else\n"
                                 "	widget.iconSet = TQString::fromLatin1(\"%IconSet\");\n"
                                 "#endif\n"
                                 "	widget.includeFile = TQString::fromLatin1(\"%IncludeFile\");\n"
                                 "	widget.toolTip = TQString::fromLatin1(\"%ToolTip\");\n"
                                 "	widget.whatsThis = TQString::fromLatin1(\"%WhatsThis\");\n"
                                 "	widget.isContainer = %IsContainer;\n"
                                 "	m_widgets.insert(TQString::fromLatin1(\"%Class\"), widget);\n";
static const char endCtor[] = "	%Init\n"
                               "}\n"
                               "%PluginName::~%PluginName()\n"
                               "{\n"
                               "	%Destroy\n"
                               "}\n"
                               "TQWidget *%PluginName::create(const TQString &key, TQWidget *parent, const char *name)\n"
                               "{\n";
static const char widgetCreate[] = "         if (key == TQString::fromLatin1(\"%Class\"))\n"
                                    "                return new %ImplClass%ConstructorArgs;\n";
static const char endCreate[] = "	return 0;\n"
                                 "}\n"
                                 "KDE_Q_EXPORT_PLUGIN(%PluginName)\n";


static TDECmdLineOptions options[] =
    {
        { "+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 },
        TDECmdLineLastOption
    };

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 );
static void buildFile( TQTextStream &stream, const TQString& group, const TQString& fileName, const TQString& pluginName, const TQString& iconPath );
static TQString buildPixmap( const TQString &name, TDEConfig &input, const TQString &iconPath );

int main( int argc, char **argv ) {
    new TDEInstance( "maketdewidgets" );

    TDEAboutData about( "maketdewidgets", I18N_NOOP( "maketdewidgets" ), version, description, TDEAboutData::License_GPL, "(C) 2004-2005 ian reinhart geiser", 0, 0, "geiseri@kde.org" );
    about.addAuthor( "ian reinhart geiser", 0, "geiseri@kde.org" );
    TDECmdLineArgs::init( argc, argv, &about );
    TDECmdLineArgs::addCmdLineOptions( options );
    TDECmdLineArgs *args = TDECmdLineArgs::parsedArgs();
    if ( args->count() < 1 ) {
        args->usage();
        return ( 1 );
    }

    TQFileInfo fi( args->arg( args->count() - 1 ) );

    TQString outputFile = args->getOption( "o" );
    TQString pluginName = args->getOption( "n" );
    TQString group = args->getOption( "g" );
    TQString iconPath = "";
    if ( args->isSet( "p" ) )
        iconPath = args->getOption( "p" );
    TQString fileName = fi.absFilePath();

    if ( args->isSet( "o" ) ) {
        TQFile output( outputFile );
        if ( output.open( IO_WriteOnly ) ) {
            TQTextStream ts( &output );
            buildFile( ts, group, fileName , pluginName, iconPath );
        }
        output.close();
    } else {
        TQTextStream ts( stdout, IO_WriteOnly );
        buildFile( ts, group, fileName , pluginName, iconPath );
    }
}

void buildFile( TQTextStream &ts, const TQString& group, const TQString& fileName, const TQString& pluginName, const TQString& iconPath ) {
    TDEConfig input( fileName, true, false );
    input.setGroup( "Global" );
    TQMap<TQString, TQString> MainMap;
    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;
    TQStringList includes = input.readListEntry( "Includes", ',' );
    for ( uint idx = 0; idx < includes.count(); ++idx )
        ts << "#include <" << includes[ idx ] << ">" << endl;
    TQStringList classes = input.groupList();
    classes.remove( classes.find( "Global" ) );
    // 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;

}

TQString buildWidgetDef( const TQString &name, TDEConfig &input, const TQString &group ) {
    input.setGroup( name );
    TQMap<TQString, TQString> defMap;
    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" );
    defMap.insert( "IncludeFile", input.readEntry( "IncludeFile", name.lower() + ".h" ).remove( ":" ) );
    defMap.insert( "ToolTip", input.readEntry( "ToolTip", name + " Widget" ).replace( "\"", "\\\"" ) );
    defMap.insert( "WhatsThis", input.readEntry( "WhatsThis", name + " Widget" ).replace( "\"", "\\\"" ) );
    defMap.insert( "IsContainer", input.readEntry( "IsContainer", "false" ) );
    defMap.insert( "Class", name );
    return KMacroExpander::expandMacros( widgetDef, defMap );
}

TQString buildWidgetCreate( const TQString &name, TDEConfig &input ) {
    input.setGroup( name );
    TQMap<TQString, TQString> createMap;
    createMap.insert( "ImplClass", input.readEntry( "ImplClass", name ) );
    createMap.insert( "ConstructorArgs", input.readEntry( "ConstructorArgs", "(parent, name)" ) );
    createMap.insert( "Class", name );
    return KMacroExpander::expandMacros( widgetCreate, createMap );
}

TQString buildWidgetInclude( const TQString &name, TDEConfig &input ) {
    input.setGroup( name );
    return "#include <" + input.readEntry( "IncludeFile", name.lower() + ".h" ) + ">";
}

TQString buildPixmap( const TQString &name, TDEConfig &input, const TQString &iconPath ) {
    input.setGroup( name );
    TQString cleanName = name.lower().replace( ":", "_" );
    TQString iconName = input.readEntry( "IconSet", cleanName + ".png" );

    TQFileInfo fi( iconPath + "/" + iconName );
    TQImage pix( fi.absFilePath() );
    TQCString xpm;
    TQBuffer buff( xpm );
    buff.open( IO_WriteOnly );
    TQImageIO io( &buff, "XPM" );
    io.setFileName( cleanName + "_xpm" );
    io.setImage( pix );
    io.write();
    buff.close();
    return xpm;
}