File indexing completed on 2024-04-14 05:35:08

0001 #!/usr/bin/perl
0002 
0003 # laurent Montel <montel@kde.org> 2006 GPL
0004 # Reinhold Kainhofer <reinhold@kainhofer.com> 2006 GPL
0005 #
0006 # simple script to replace the QT3_SUPPORT Q(H|V|)BoxLayout constructors:
0007 #     QVBoxLayout ( QWidget * parent, int margin, int spacing = -1, const char * name = 0 ) 
0008 #     QVBoxLayout ( QLayout * parentLayout, int spacing = -1, const char * name = 0 ) 
0009 #     QVBoxLayout ( int spacing, const char * name = 0 )
0010 # with the Qt4 API, which is
0011 #     Q(H|V|)BoxLayout( QWidget* parent);
0012 #     Q(H|V|)BoxLayout();
0013 #     Q(H|V|)BoxLayout::set(Margin|Spacing|ObjectName)(...);
0014 #
0015 # This script does NOT detect the QLayout*parentLayout case, which needs to be converted manually
0016 
0017 use File::Basename;
0018 use lib dirname( $0 );
0019 use functionUtilkde;
0020 use strict;
0021 
0022 open(my $F, "-|", qw(find . -type f));
0023 my $file;
0024 while ($file = <$F>) {
0025     next if $file =~ /\.svn/;
0026     chomp $file;
0027     next if functionUtilkde::excludeFile( $file);
0028 
0029     my $modified;
0030     my @necessaryIncludes = ();
0031     open(my $FILE, "<", $file) or warn "We can't open file $file:$!\n";
0032     my @l = map {
0033       my $orig = $_;
0034       my ($spaces, $trailer, $object, $call, $ws, $parent, $params);
0035       # $ws is the whitespace between brackets and contents
0036       # $spaces are the spaces at the beginning of the line
0037       # $trailer is the text between the spaces at the beginning and the object name
0038       # $object is the layout object (for the set* calls that we need to add)
0039       # $call is the actual constructor call (e.g. " = new QVBoxLayout ")
0040       # $parent is the first argument to the constructor, unless it starts with a number
0041       # $params is everything after the parent layout/widget until the end of the bracket
0042       #
0043       # this code assumes the constructor is a call on its own line, i.e. ends with new QVBoxLayout(...);
0044       $parent = "";
0045       if (  # Either parent widget/layout or first param is rows (=starts with number)
0046         ( ($spaces, $trailer, $object, $call, $ws, $parent, $params) = m!^(\s*)(.*[\s\*]|)([a-zA-Z0-9]+)(\s*=\s*new Q[HV]BoxLayout[^(]*)\((\s*)([^0-9 ][^,]*|0),\s*(.*[^\s])\s*\);$! ) ||
0047         ( ($spaces, $trailer, $object, $call, $ws,          $params) = m!^(\s*)(.*[\s\*]|)([a-zA-Z0-9]+)(\s*=\s*new Q[HV]BoxLayout[^(]*)\((\s*)([0-9][^,]*,\s*.*[^\s])\s*\);$! ) ) {
0048 # print "Spaces: '$spaces', Trailer: '$trailer', Object: '$object', Call: '$call'";
0049 # print "WS: '$ws', Parent: '$parent'\n";
0050 
0051 # print "Params '$params'\n";
0052         if ( $parent eq "0" ) { $parent = ""; }
0053 #         if ( length($parent)>0 && $parent != 0) {
0054 # print "Parent: '$parent', \$parent:";
0055 # if ( length($parent) ) {
0056 #   print "In IF";
0057 # } else {
0058 #   print "NOT in IF";
0059 # }
0060         if ( $parent ) {
0061           $_ = "$spaces$trailer$object$call($ws$parent$ws);\n";
0062         } else {
0063           $_ = "$spaces$trailer$object$call();\n";
0064         }
0065         my ( $margin, $space, $name );
0066         my @parms = split( /,\s*/, $params );
0067 # print "Params: ";
0068 # print join(" - ", @parms );
0069 # print "\n";
0070         if ( scalar( @parms ) >= 3 ) {
0071           # All params are given: margin, spacing, name
0072           ( $margin, $space, $name ) = @parms;
0073         } else {
0074           if ( !defined($parent) ) { # constructor without parent layout/widget => spacing, name
0075             $space = $parms[0] if ( scalar( @parms ) >= 1 );
0076             $name = $parms[1] if ( scalar( @parms ) >= 2 );
0077           } elsif ( scalar( @parms ) >= 2 && @parms[1] =~ m/^"/ ) {
0078             # Second parameter is the name -> First must be the spacing
0079             $space = $parms[0];
0080             $name = $parms[1];
0081           } elsif ( scalar( @parms ) >= 2 ) { # fourth param is not name
0082             $margin = $parms[0];
0083             $space = $parms[1];
0084           } else {
0085             $space = $parms[0] if ( scalar( @parms ) >= 1 );
0086           }
0087         }
0088 # print "Margin: $margin\nSpacing: $space\nName: $name\n";
0089 
0090         # We don't need the row/col count any more in the new Qt4 API!!!
0091         $_ .= "$spaces$object->setObjectName($ws$name$ws);\n" if ( defined($name) );
0092         $_ .= "$spaces$object->setSpacing($ws$space$ws);\n" if ( defined($space) );
0093         $_ .= "$spaces$object->setMargin($ws$margin$ws);\n" if ( defined($margin) );
0094 # print "Old line:      \n$orig";
0095 # print "New line:      \n$_====================\n";
0096       }
0097       $modified ||= $orig ne $_;
0098       $_;
0099     } <$FILE>;
0100 
0101     if ($modified) {
0102       print "Modified: $file\n";
0103       open (my $OUT, ">", $file);
0104       print $OUT @l;
0105       close $OUT;
0106     }
0107 
0108 }
0109 functionUtilkde::diffFile( <$F> );
0110