summaryrefslogtreecommitdiffstats
path: root/kmymoney2/widgets/maketdewidgets.in
blob: 76924d573e2fce82365cd9064b7186d8242f5f1f (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
#!@PERL@

# copied from KDELIBS 3.1.4
# modified for usage with KMyMoney

use strict;
use vars qw($group $incpath $name $out @includes $init $destroy %widgets);

sub usage
{
	print STDERR "$_[0]\n" if @_;
	print STDERR <<EOT;
Usage: $0 [options] [<widget spec> ...]
Generates a TQt designer-plugin for the widget set defined in widget spec
or STDIN if no filename is given

Options:
	-g <group>       default widget group name to display in designer
	                 (default: KDE)
	-i <path>        path to prepend to include file (default: none)
	-n <plugin name> name of the plugin class to generate
	                 (default: <group>WidgetsPlugin)
	-o <filename>    write output to filename (default: STDOUT)
EOT
	exit 1;
}

sub warning($) { print STDERR "Warning: $_[0]\n" }

my ($class, %defs);
sub addwidget
{
	return if $class =~ /^(Includes|Init|Destroy)$/;
	if(! exists $defs{IncludeFile}) {
		$defs{IncludeFile} = join('/', $incpath, lc "$class.h") if ($incpath ne "");
		$defs{IncludeFile} = lc "$class.h" unless ($incpath ne "");
	}
	$defs{ImplClass} = $class unless exists $defs{ImplClass};
	$defs{Group} = $group unless exists $defs{Group};
	$defs{ToolTip} = $class unless exists $defs{ToolTip};
	$defs{WhatsThis} = $class unless exists $defs{Long};
	$defs{IconSet} = lc "$class.png" unless exists $defs{IconSet};
	$defs{ConstructorArgs} = '(parent, name)' unless exists $defs{ConstructorArgs};
	$defs{IsContainer} = $defs{IsContainer} ? 'true' : 'false';
	$widgets{$class} = { %defs };
}

$group = 'KDE';
$incpath = "";

while ($ARGV[0] =~ /^-/)
{
	my $opt = shift @ARGV;
	usage "missing parameter for $opt" unless @ARGV;
	if ($opt eq '-g') { $group = shift @ARGV }
	elsif ($opt eq '-n') { $name = shift @ARGV }
	elsif ($opt eq '-o') { $out = shift @ARGV }
	elsif ($opt eq '-i') { $incpath = shift @ARGV }
	else { usage "Unknown option $opt" }
}

$name = "${group}WidgetsPlugin"	unless $name;
warning "classname changed to \"$name\""
	if $name =~ s/(^[^A-Za-z_]+|[^A-Za-z0-9_])/_/g;

while (<>)
{
	chomp;
	next if /^(#|\s*$)/;
	
	if (/^\s*\[([A-Za-z_][A-Za-z0-9_:]*)\]\s*$/)
	{
		addwidget if $class;
		%defs = {};
		$class = $1;
		next;
	}
	elsif (/^\s*\[(.*)\]\s*$/) { die "Invalid class name \"$1\"" }
	die "Not in a widget definition" unless $class;
	if ($class eq 'Includes') { push @includes, $_ }
	elsif ($class eq 'Init') { $init .= "\n\t$_" }
	elsif ($class eq 'Destroy') { $destroy .= "\n\t$_" }
	elsif (/^\s*(IncludeFile|ImplClass|Group|ToolTip|WhatsThis|IconSet|ConstructorArgs|IsContainer)\s*=\s*(.*)\s*/)
	{
		$defs{$1} = $2;
	}
	else { die "Syntax error on line $." }
}
addwidget if $class;

warning "Nothing to do", exit 0 unless %widgets;

my @keys = sort keys %widgets;

if ($out) { open OUT, ">$out" or die "Can't open $out for writing" }
else { open OUT, ">&STDOUT" }

(my $scriptname = $0) =~ s|^.*/||;
print OUT <<EOT;
/*
 * This file was autogenerated by $scriptname. Any changes will be lost!
 */

#include <qwidgetplugin.h>
// for pixmap search
#include <kstandarddirs.h>

EOT

print OUT map { "#include \"$_\"\n" } @includes, map { $widgets{$_}->{IncludeFile} } @keys;

print OUT <<EOT;

class $name : public QWidgetPlugin
{
public:
	$name();
	virtual ~$name();

	virtual QStringList keys() const
	{
		QStringList result;
		for (WidgetInfos::ConstIterator it = m_widgets.begin(); it != m_widgets.end(); ++it)
			result << it.key();
		return result;
	}
	virtual TQWidget *create(const TQString &key, TQWidget *parent = 0, const char *name = 0);
	virtual QIconSet iconSet(const TQString &key) const
	{
		TQString path = locate("data", "kmymoney2/pics/" + m_widgets[key].iconSet);
		return QIconSet(path);
	}
	virtual bool isContainer(const TQString &key) const
	{
		return m_widgets[key].isContainer;
	}
EOT

print OUT map { <<EOT } qw(group includeFile toolTip whatsThis);
	virtual TQString $_(const TQString &key) const
	{
		return m_widgets[key].$_;
	}
EOT

print OUT <<EOT;

private:
	struct WidgetInfo
	{
		TQString group;
		TQString iconSet;
		TQString includeFile;
		TQString toolTip;
		TQString whatsThis;
		bool isContainer;
	};
	typedef QMap<TQString, WidgetInfo> WidgetInfos;
	WidgetInfos m_widgets;
};

${name}::$name()
{
	WidgetInfo widget;
EOT

print OUT map { my $w = $_; "\n", (map { my $attr = ucfirst $_; <<EOT } qw(group iconSet includeFile toolTip whatsThis)), <<EOT } @keys;
	widget.$_ = "$widgets{$w}->{$attr}";
EOT
	widget.isContainer = $widgets{$w}->{IsContainer};
	m_widgets.insert("$_", widget);
EOT

print OUT <<EOT;
$init
}

${name}::~$name()
{$destroy
}

TQWidget *${name}::create(const TQString &key, TQWidget *parent, const char *name)
{
EOT

print OUT map { <<EOT } @keys;
	if (key == "$_")
		return new $widgets{$_}->{ImplClass}$widgets{$_}->{ConstructorArgs};
EOT

print OUT <<EOT;
	return 0;
}

TQ_EXPORT_PLUGIN($name)
EOT