File indexing completed on 2024-12-08 05:07:04

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 QGridLayout constructors:
0007 #     QGridLayout ( QWidget * parent, int nRows, int nCols = 1, int margin = 0, int space = -1, const char * name = 0 ) 
0008 #     QGridLayout ( int nRows, int nCols = 1, int spacing = -1, const char * name = 0 ) 
0009 #     QGridLayout ( QLayout * parentLayout, int nRows = 1, int nCols = 1, int spacing = -1, const char * name = 0 ) 
0010 # with the Qt4 API, which is
0011 #     QGridLayout( QWidget* parent);
0012 #     QGridLayout::set(Margin|Spacing|ObjectName)(...);
0013 # The row and col count is no longer needed at all.
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 grid layout object (for the set* calls that we need to add)
0039       # $call is the actual constructor call (e.g. " = new QGridLayout ")
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 QGridLayout(...);
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 QGridLayout[^(]*)\((\s*)([^0-9 ][^,]*|0),\s*(.*[^\s])\s*\);$! ) ||
0047         ( ($spaces, $trailer, $object, $call, $ws,          $params) = m!^(\s*)(.*[\s\*]|)([a-zA-Z0-9]+)(\s*=\s*new QGridLayout[^(]*)\((\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 ( $parent ) {
0054           $_ = "$spaces$trailer$object$call($ws$parent$ws);\n";
0055         } else {
0056           $_ = "$spaces$trailer$object$call();\n";
0057         }
0058         my ($rows, $cols, $margin, $space, $name );
0059         my @parms = split( /,\s*/, $params );
0060 
0061         if ( scalar( @parms ) >= 5 ) {
0062           # All params are given: rows, cols, margin, spacing, name
0063           ($rows, $cols, $margin, $space, $name) = @parms;
0064         } else {
0065           # rows and cols are always the first two.
0066           $rows = $parms[0] if ( scalar( @parms ) >= 1 );
0067           $cols = $parms[1] if ( scalar( @parms ) >= 2 );
0068           if ( !defined($parent) ) { # constructor without parent layout/widget => rows, cols, spacing, name
0069             $space = $parms[2] if ( scalar( @parms ) >= 3 );
0070             $name = $parms[3] if ( scalar( @parms ) >= 4 );
0071           } elsif ( scalar( @parms ) >= 4 && @parms[3] =~ m/^"/ ) {
0072             # fourth parameter is the name -> third must be the spacing
0073             $space = $parms[2];
0074             $name = $parms[3];
0075           } elsif ( scalar( @parms ) >= 4 ) { # fourth param is not name
0076             $margin = $parms[2];
0077             $space = $parms[3];
0078           } else {
0079             $space = $parms[2] if ( scalar( @parms ) >= 3 );
0080           }
0081         }
0082 # print "Row: $rows\nCols: $cols\n";
0083 # print "Margin: $margin\nSpacing: $space\nName: $name\n";
0084 
0085         # We don't need the row/col count any more in the new Qt4 API!!!
0086         $_ .= "$spaces$object->setObjectName($ws$name$ws);\n" if ( defined($name) );
0087         $_ .= "$spaces$object->setSpacing($ws$space$ws);\n" if ( defined($space) );
0088         $_ .= "$spaces$object->setMargin($ws$margin$ws);\n" if ( defined($margin) );
0089 # print "Old line:      \n$orig";
0090 # print "New line:      \n$_====================\n";
0091       }
0092       $modified ||= $orig ne $_;
0093       $_;
0094     } <$FILE>;
0095 
0096     if ($modified) {
0097       print "Modified: $file\n";
0098       open (my $OUT, ">", $file);
0099       print $OUT @l;
0100       close $OUT;
0101     }
0102 
0103 }
0104 functionUtilkde::diffFile( <$F> );
0105