File: convert-kvbox.pl

package info (click to toggle)
kde-dev-scripts 4%3A18.08.0-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 3,496 kB
  • sloc: perl: 15,466; lisp: 5,627; sh: 4,157; python: 3,892; ruby: 2,158; makefile: 16; sed: 9
file content (133 lines) | stat: -rwxr-xr-x 4,906 bytes parent folder | download | duplicates (5)
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
#!/usr/bin/perl -w

# Laurent Montel <montel@kde.org> (2014)
# K[V|H]Box -> QWidget + QH|VBoxLayout
# find -iname "*.cpp" -o -iname "*.h"|xargs kde-dev-scripts/kf5/convert-kvbox.pl

use strict;
use File::Basename;
use lib dirname($0);
use functionUtilkde;

foreach my $file (@ARGV) {

    # Key = variable name of the parent QWidget
    # Value = variable name of the associated layout
    my %varname = ();

    my $modified;
    my $needQHBoxLayout;
    my $needQVBoxLayout;
    open(my $FILE, "<", $file) or warn "We can't open file $file:$!\n";
    my @l = map {
        my $orig = $_;
        my $regexp = qr/
           ^(\s*)            # (1) Indentation
           (.*?)             # (2) Possibly "Classname *" (the ? means non-greedy)
           (\w+)             # (3) variable name
           \s*=\s*           #     assignment
           new\s+            #     new
           (K[HV]Box)\s*    # (4) KHBox or KVBox
           /x; # /x Enables extended whitespace mode
         if (my ($indent, $left, $var, $classname) = $_ =~ $regexp) {
           s/K[HV]Box/QWidget/g;
           my $mylayoutname = $classname eq "KHBox" ? "${var}HBoxLayout" : "${var}VBoxLayout";
           $mylayoutname =~ s/^m?_//; # coding style: this is a local var
           my $mylayoutclass = $classname eq "KHBox" ? "QHBoxLayout" : "QVBoxLayout";
           if ( defined $varname{$var}) {
              $_ .= $indent . "${mylayoutname} = new $mylayoutclass($var);" . "\n";
           } else {
              $_ .= $indent . $mylayoutclass . " *${mylayoutname} = new $mylayoutclass($var);" . "\n";
           } 
           $_ .= $indent . "${mylayoutname}->setMargin(0);\n";
           $varname{$var} = ${mylayoutname};
           if ($mylayoutclass eq "QHBoxLayout") {
              $needQHBoxLayout = 1;
           } else {
              $needQVBoxLayout = 1;
           }
        }
        my $widget_regexp = qr/
           ^(\s*)            # (1) Indentation
           (.*?)             # (2) Possibly "Classname *" (the ? means non-greedy)
           (\w+)             # (3) variable name
           \s*=\s*           #     assignment
           new\s+            #     new
           (\w+)\s*          # (4) classname
           ${functionUtilkde::paren_begin}5${functionUtilkde::paren_end}  # (5) (args)
           /x; # /x Enables extended whitespace mode
       if (my ($indent, $left, $var, $classname, $args) = $_ =~ $widget_regexp) {
           # Extract last argument
           #print STDERR "left=$left var=$var classname=$classname args=$args\n";
           my $extract_parent_regexp = qr/
            ^\(
            (?:.*?)                # args before the parent (not captured, not greedy)
            \s*(\w+)\s*            # (1) parent 
            (?:,\s*\"[^\"]*\"\s*)? # optional: object name
             \)$
            /x; # /x Enables extended whitespace mode      
           if (my ($lastArg) = $args =~ $extract_parent_regexp) {
              print STDERR "extracted parent=" . $lastArg . "\n";
              my $mylayoutname = $varname{$lastArg};
              if (defined $mylayoutname) {
                  $_ .= $indent . $mylayoutname . "->addWidget(" . $var . ");" . "\n";
               }
           } else {
              #warn $functionUtilkde::current_file . ":" . $functionUtilkde::current_line . ": couldn't extract last argument from " . $args . "\n";
          }
        }
 
        if (/(\w+)->setSpacing\s*\(.*/) {
          my $var = $1;
          my $mylayoutname = $varname{$var};
          if ( defined $mylayoutname ) {
              s/$var->setSpacing/$mylayoutname->setSpacing/;
          }
        }
        if (/(\w+)->setMargin\s*\(.*/) {
          my $var = $1;
          my $mylayoutname = $varname{$var};
          if ( defined $mylayoutname ) {
               s/$var->setMargin/$mylayoutname->setMargin/;
          }
        }
        if (/(\w+)->setStretchFactor\s*\(.*/) {
          my $var = $1;
          my $mylayoutname = $varname{$var};
          if ( defined $mylayoutname ) {
               s/$var->setStretchFactor/$mylayoutname->setStretchFactor/;
          }
        }



        s/#include <khbox\.h>/#include <QHBoxLayout>/;
        s/#include <kvbox\.h>/#include <QVBoxLayout>/;
        s/#include <KHBox>/#include <QHBoxLayout>/;
        s/#include <KVBox>/#include <QVBoxLayout>/;
        s/class KVBox;//;
        s/class KHBox;//;
        s/KVBox\s*\*/QWidget */;
        s/KHBox\s*\*/QWidget */;




        $modified ||= $orig ne $_;
        $_;
    } <$FILE>;

    if ($modified) {
        open (my $OUT, ">", $file);
        print $OUT @l;
        close ($OUT);
        if ( $needQVBoxLayout) {
           functionUtilkde::addIncludeInFile($file, "QVBoxLayout");
        }
        if ( $needQHBoxLayout) {
           functionUtilkde::addIncludeInFile($file, "QHBoxLayout");
        }
    }
}

functionUtilkde::diffFile( "@ARGV" );