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

0001 #!/usr/bin/perl -w
0002 
0003 # Laurent Montel <montel@kde.org> (2014)
0004 # KIntNumInput -> QSpinBox
0005 # find -iname "*.cpp" -o -iname "*.h"|xargs kde-dev-scripts/kf5/convert-kintnuminput.pl
0006 
0007 use strict;
0008 use File::Basename;
0009 use lib dirname($0);
0010 use functionUtilkde;
0011 
0012 foreach my $file (@ARGV) {
0013     my %varname = ();
0014     my $modified;
0015     open(my $FILE, "<", $file) or warn "We can't open file $file:$!\n";
0016     my @l = map {
0017         my $orig = $_;
0018         my $regexp = qr/
0019            ^(\s*)                        # (1) Indentation, possibly "Classname *" (the ? means non-greedy)
0020            (.*?)                         # (2) Possibly "Classname *" (the ? means non-greedy)
0021            (\w+)                         # (3) variable name
0022            \s*=\s*                       #   assignment
0023            new\s+KIntNumInput\s*\((.*)\)   # (4)  new KIntSpinBox(...,...,...,...);
0024            (.*)$                         # (5) afterreg
0025            /x; # /x Enables extended whitespace mode
0026         if (my ($indent, $left, $var, $argument, $afterreg) = $_ =~ $regexp) {
0027           warn "kintnuminput found $var $argument\n";
0028           #KIntSpinBox(int lower, int upper, int singleStep, int value, QWidget *parent, int base = 10);
0029 
0030           $varname{$var} = 1;
0031           my ($lower, $upper, $singleStep, $value, $parent, $base, $after);
0032           my $constructor_regexp = qr/
0033                                  ^([^,]*)\s*        # lower
0034                                  ,\s*([^,]*)\s*     # upper
0035                                  ,\s([^,]*)         # singleStep
0036                                  ,\s([^,]*)         # value
0037                                  ,\s([^,]*)         # widget
0038                                  (?:,\s([^,]*))?    # base
0039                                  (.*)$              # after
0040                                  /x;
0041           if ( ($lower, $upper, $singleStep, $value, $parent, $base, $after) = $argument =~ $constructor_regexp ) {
0042              $_ = $indent . $left . $var . " = new QSpinBox($parent);" . $after . "\n";
0043            
0044              $_ .= $indent . $var . "->setMaximum($upper);" . $after . "\n";
0045              $_ .= $indent . $var . "->setMinimum($lower);" . $after . "\n";
0046              $_ .= $indent . $var . "->setSingleStep($singleStep);" . $after . "\n";
0047              $_ .= $indent . $var . "->setValue($value);" . $after . "\n";
0048              if ($base) {
0049                 $_ .= $indent . $var . "->setDisplayIntegerBase($base);" . $after . "\n";
0050              }
0051           }
0052         }
0053        my $regexpRange = qr/
0054            ^(\s*)                        # (1) Indentation, possibly "Classname *" (the ? means non-greedy)
0055            (\w+)                         # (2) variable name
0056            \->setRange\s*\((.*)\)        # (3) argument
0057            (.*)$                         # (4) afterreg
0058            /x; # /x Enables extended whitespace mode
0059         if (my ($indent, $var, $argument, $afterreg) = $_ =~ $regexpRange) {
0060             warn "found setRange \'$var\'\n";
0061             if ( defined $varname{$var} ) {
0062                  my $argument_regexp = qr/
0063                                  ^([^,]*)\s*        # mini
0064                                  ,\s*([^,]*)\s*     # maxi
0065                                  ,\s([^,]*)         # singleStep
0066                                  (.*)$              # after
0067                                  /x;
0068                  if (my ($minimum, $maximum, $singleStep) = $argument =~ $argument_regexp) {
0069                     $_ = $indent . "$var->setRange($minimum, $maximum);\n";
0070                     $_ .=  $indent . "$var->setSingleStep($singleStep);\n";
0071                  }
0072             }
0073         }
0074         if (/(\w+)\-\>setSliderEnabled\s*\((.*)\)/) {
0075            my $var = $1;
0076            warn "found setSliderEnabled $2\n";
0077            if ( defined $varname{$var} ) {
0078               my $sliderargs = $2;
0079               $sliderargs =~ s, ,,g;
0080               if ($sliderargs eq "false") {
0081                  $_ = "";
0082               } else {
0083                  $_ = "//PORT SCRIPT: Warning QSpinBox doesn't support slider: $_\n";
0084               }
0085            }
0086         }
0087 
0088         s/\bKIntNumInput\b/QSpinBox/g;
0089         s/\<KIntNumInput\b\>/\<QSpinBox>/ if (/#include/);
0090         s/\<kintnuminput.h\>/\<QSpinBox>/ if (/#include/);
0091 
0092         $modified ||= $orig ne $_;
0093         $_;
0094     } <$FILE>;
0095 
0096     if ($modified) {
0097         open (my $OUT, ">", $file);
0098         print $OUT @l;
0099         close ($OUT);
0100         warn "$file: Verify maximum value. KIntNumInput defined maximum as 32767, but QSpinBox define it as 99.\n";
0101     }
0102 }
0103 
0104 functionUtilkde::diffFile( "@ARGV" );