File indexing completed on 2024-02-25 05:53:23

0001 #!/usr/bin/perl -w
0002 
0003 # Laurent Montel <montel@kde.org> (2014)
0004 # convert KDialog to QDialog
0005 # find -iname "*.cpp"|xargs kde-dev-scripts/kf5/convert-kdialog.pl
0006 
0007 use strict;
0008 use File::Basename;
0009 use lib dirname($0);
0010 use functionUtilkde;
0011 
0012 my %dialogButtonType = (
0013    "Ok" => "QDialogButtonBox::Ok",
0014    "KDialog::Ok" => "QDialogButtonBox::Ok",
0015    "Cancel" => "QDialogButtonBox::Cancel",
0016    "KDialog::Cancel" => "QDialogButtonBox::Cancel",
0017    "Help" => "QDialogButtonBox::Help",
0018    "KDialog::Help" => "QDialogButtonBox::Help",
0019    "Default" => "QDialogButtonBox::RestoreDefaults",
0020    "KDialog::Default" => "QDialogButtonBox::RestoreDefaults",
0021    "Try" => "QDialogButtonBox::Retry",
0022    "KDialog::Try" => "QDialogButtonBox::Retry",
0023    "Close" => "QDialogButtonBox::Close",
0024    "KDialog::Close" => "QDialogButtonBox::Close",
0025    "No" => "QDialogButtonBox::No",
0026    "KDialog::No" => "QDialogButtonBox::No",
0027    "Yes" => "QDialogButtonBox::Yes",
0028    "KDialog::Yes" => "QDialogButtonBox::Yes",
0029    "Reset" => "QDialogButtonBox::Reset",
0030    "KDialog::Reset" => "QDialogButtonBox::Reset",
0031    "None" => "QDialogButtonBox::NoButton",
0032    "KDialog::None" => "QDialogButtonBox::NoButton",
0033    "Apply" => "QDialogButtonBox::Apply",
0034    "KDialog::Apply" => "QDialogButtonBox::Apply"
0035 
0036 );
0037 
0038 sub removePrivateVariable($)
0039 {
0040    my ($localLeft) =@_;
0041    if ($localLeft =~ /\-\>/) {
0042       $localLeft =~ s/(\w+)\-\>//;
0043    }
0044    return $localLeft;
0045 }
0046 
0047 foreach my $file (@ARGV) {
0048 
0049     my $modified;
0050     open(my $FILE, "<", $file) or warn "We can't open file $file:$!\n";
0051     my %varname = ();
0052     my $hasUser1Button;
0053     my $hasUser2Button;
0054     my $hasUser3Button;
0055     my $hasOkButton;
0056     my $needQDialogButtonBox;
0057     my $needQBoxLayout;
0058     my $hasMainWidget;
0059     my $needKGuiItem;
0060     my @l = map {
0061         my $orig = $_;
0062         my $regexp = qr/
0063           ^(\s*(?:[\-\>:\w]+)?)
0064           setMainWidget\s*\(
0065           (\w+)            # (2) variable name
0066           \);/x; # /x Enables extended whitespace mode
0067         if (my ($left, $var) = $_ =~ $regexp) {
0068            warn "setMainWidget found :$left $var\n";
0069            my $localLeft = removePrivateVariable($left);
0070 
0071            if (defined $hasMainWidget) {
0072              $_ = $localLeft . "mainLayout->addWidget($var);\n";
0073            } else {
0074              $_ = $localLeft . "QVBoxLayout *mainLayout = new QVBoxLayout;\n";
0075              $_ .= $left . "setLayout(mainLayout);\n";
0076              $_ .= $localLeft . "mainLayout->addWidget($var);\n";
0077            }
0078            $varname{$var} = $var;
0079         }
0080         my $widget_regexp = qr/
0081            ^(\s*)            # (1) Indentation
0082            (.*?)             # (2) Possibly "Classname *" (the ? means non-greedy)
0083            (\w+)             # (3) variable name
0084            \s*=\s*           #     assignment
0085            new\s+            #     new
0086            (\w+)\s*          # (4) classname
0087            ${functionUtilkde::paren_begin}5${functionUtilkde::paren_end}  # (5) (args)
0088            /x; # /x Enables extended whitespace mode
0089         if (my ($indent, $left, $var, $classname, $args) = $_ =~ $widget_regexp) {
0090            # Extract last argument
0091            #print STDERR "left=$left var=$var classname=$classname args=$args\n";
0092            my $extract_parent_regexp = qr/
0093             ^\(
0094             (?:.*?)                # args before the parent (not captured, not greedy)
0095             \s*(\w+)\s*            # (1) parent
0096             (?:,\s*\"[^\"]*\"\s*)? # optional: object name
0097              \)$
0098             /x; # /x Enables extended whitespace mode
0099            if (my ($lastArg) = $args =~ $extract_parent_regexp) {
0100               print STDERR "extracted parent=" . $lastArg . "\n";
0101               my $mylayoutname = $varname{$lastArg};
0102               if (defined $mylayoutname) {
0103                   $_ .= $indent . "mainLayout->addWidget(" . $var . ");" . "\n";
0104                }
0105            } else {
0106               #warn $functionUtilkde::current_file . ":" . $functionUtilkde::current_line . ": couldn't extract last argument from " . $args . "\n";
0107            }
0108         }
0109         my $regexpMainWidget =qr/
0110          ^(\s*(?:[\-\>:\w]+)?)           # (1) Indentation
0111          (.*?)            # (2) (the ? means non-greedy)
0112          \(\s*mainWidget\(\)
0113          \s*\);/x; # /x Enables extended whitespace mode
0114         if ( my ($left, $widget) = $_ =~ $regexpMainWidget) {
0115            my $localLeft = removePrivateVariable($left);
0116 
0117 
0118            if (not defined $hasMainWidget) {
0119 
0120              $_ = $localLeft . "QWidget *mainWidget = new QWidget(this);\n";
0121              $_ .= $localLeft . "QVBoxLayout *mainLayout = new QVBoxLayout;\n";
0122              $_ .= $left . "setLayout(mainLayout);\n";
0123              $_ .= $localLeft . "mainLayout->addWidget(mainWidget);\n";
0124              $_ .= $localLeft . "$widget(mainWidget);\n";
0125            } else {
0126              $_ = $localLeft . "$widget(mainWidget);\n";
0127            }
0128            warn "found mainWidget \n";
0129            $needQBoxLayout = 1;
0130            $hasMainWidget = 1;
0131         }
0132 
0133         my $regexpSetButtons = qr/
0134           ^(\s*(?:[\-\>:\w]+)?)           # (1) Indentation
0135           setButtons\s*\(
0136           (.*?)            # (2) variable name
0137           \);/x; # /x Enables extended whitespace mode
0138         if (my ($left, $var) = $_ =~ $regexpSetButtons) {
0139            #Remove space in enum
0140            $var =~ s, ,,g;
0141            $needQDialogButtonBox = 1;
0142            warn "setButtons found : $var\n";
0143            my @listButton = split(/\|/, $var);
0144            my @myNewDialogButton;
0145            warn " list button found @listButton\n";
0146            if ( "Ok" ~~ @listButton || "KDialog::Ok" ~~ @listButton )  {
0147               push @myNewDialogButton , "QDialogButtonBox::Ok";
0148               $hasOkButton = 1;
0149            }
0150            if ( "Cancel" ~~ @listButton || "KDialog::Cancel" ~~ @listButton)  {
0151               push @myNewDialogButton , "QDialogButtonBox::Cancel";
0152            }
0153            if ( "Help" ~~ @listButton || "KDialog::Help" ~~ @listButton)  {
0154               push @myNewDialogButton , "QDialogButtonBox::Help";
0155            }
0156            if ( "Default" ~~ @listButton || "KDialog::Default" ~~ @listButton)  {
0157               push @myNewDialogButton , "QDialogButtonBox::RestoreDefaults";
0158            }
0159            if ( "Try" ~~ @listButton || "KDialog::Try" ~~ @listButton)  {
0160               push @myNewDialogButton , "QDialogButtonBox::Retry";
0161            }
0162            if ( "Close" ~~ @listButton || "KDialog::Close" ~~ @listButton)  {
0163               push @myNewDialogButton , "QDialogButtonBox::Close";
0164            }
0165            if ( "No" ~~ @listButton || "KDialog::No" ~~ @listButton)  {
0166               push @myNewDialogButton , "QDialogButtonBox::No";
0167            }
0168            if ( "Yes" ~~ @listButton || "KDialog::Yes" ~~ @listButton)  {
0169               push @myNewDialogButton , "QDialogButtonBox::Yes";
0170            }
0171            if ( "Apply" ~~ @listButton || "KDialog::Apply" ~~ @listButton)  {
0172               push @myNewDialogButton , "QDialogButtonBox::Apply";
0173            }
0174            if ( "Reset" ~~ @listButton || "KDialog::Reset" ~~ @listButton)  {
0175               push @myNewDialogButton , "QDialogButtonBox::Reset";
0176            }
0177            if ( "Details" ~~ @listButton || "KDialog::Details" ~~ @listButton)  {
0178               warn "DETAILS is not implemented. Need to reimplement it\n";
0179            }
0180            if ( "None" ~~ @listButton || "KDialog::None" ~~ @listButton)  {
0181               push @myNewDialogButton , "QDialogButtonBox::NoButton";
0182            }
0183            if ( "User1" ~~ @listButton || "KDialog::User1" ~~ @listButton)  {
0184               $hasUser1Button = 1;
0185            }
0186            if ( "User2" ~~ @listButton || "KDialog::User2" ~~ @listButton)  {
0187               $hasUser2Button = 1;
0188            }
0189            if ( "User3" ~~ @listButton || "KDialog::User3" ~~ @listButton)  {
0190               $hasUser3Button = 1;
0191            }
0192            my $resultList = join('|', @myNewDialogButton);
0193            my $localLeft = removePrivateVariable($left);
0194 
0195            $_ = $localLeft . "QDialogButtonBox *buttonBox = new QDialogButtonBox($resultList);\n";
0196            if (not defined $hasMainWidget) {
0197              $_ .= $localLeft . "QWidget *mainWidget = new QWidget(this);\n";
0198              $_ .= $localLeft . "QVBoxLayout *mainLayout = new QVBoxLayout;\n";
0199              $_ .= $left . "setLayout(mainLayout);\n";
0200              $_ .= $localLeft . "mainLayout->addWidget(mainWidget);\n";
0201              #$_ .= $left . "$widget(mainWidget);\n";
0202              $hasMainWidget = 1;
0203              $needQBoxLayout = 1;
0204              $needQDialogButtonBox = 1;
0205            }
0206 
0207            if (defined $hasOkButton) {
0208               $_ .= $localLeft . "QPushButton *okButton = buttonBox->button(QDialogButtonBox::Ok);\n";
0209               $_ .= $localLeft . "okButton->setDefault(true);\n";
0210               $_ .= $localLeft . "okButton->setShortcut(Qt::CTRL | Qt::Key_Return);\n";
0211            }
0212 
0213            if (defined $hasUser1Button) {
0214               $_ .= $localLeft . "QPushButton *user1Button = new QPushButton;\n";
0215               $_ .= $localLeft . "buttonBox->addButton(user1Button, QDialogButtonBox::ActionRole);\n";
0216            }
0217            if (defined $hasUser2Button) {
0218               $_ .= $localLeft . "QPushButton *user2Button = new QPushButton;\n";
0219               $_ .= $localLeft . "buttonBox->addButton(user2Button, QDialogButtonBox::ActionRole);\n";
0220            }
0221            if (defined $hasUser3Button) {
0222               $_ .= $localLeft . "QPushButton *user3Button = new QPushButton;\n";
0223               $_ .= $localLeft . "buttonBox->addButton(user3Button, QDialogButtonBox::ActionRole);\n";
0224            }
0225 
0226            $_ .= $left . "connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));\n";
0227            $_ .= $left . "connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));\n";
0228            $_ .= $localLeft . "//PORTING SCRIPT: WARNING mainLayout->addWidget(buttonBox) must be last item in layout. Please move it.\n";
0229            $_ .= $localLeft . "mainLayout->addWidget(buttonBox);\n";
0230 
0231            warn "WARNING we can't move this code at the end of constructor. Need to move it !!!!\n";
0232         }
0233         if (/defaultClicked\(\)/) {
0234              s/connect\s*\(\s*this,/connect(buttonBox->button(QDialogButtonBox::RestoreDefaults),/;
0235              s/defaultClicked\(\)/clicked()/;
0236         }
0237         if (/okClicked\(\)/) {
0238              s/connect\s*\(\s*this,/connect(okButton,/;
0239              s/SIGNAL\s*\(\s*okClicked\(\)/SIGNAL\(clicked()/;
0240         }
0241         if (/applyClicked\(\)/) {
0242              s/connect\s*\(\s*this,/connect(buttonBox->button(QDialogButtonBox::Apply),/;
0243              s/SIGNAL\s*\(\s*applyClicked\(\)/SIGNAL\(clicked()/;
0244         }
0245 
0246         if (/closeClicked\(\)/) {
0247              s/connect\s*\(\s*this,/connect(buttonBox->button(QDialogButtonBox::Close),/;
0248              s/SIGNAL\s*\(\s*closeClicked\(\)/SIGNAL\(clicked()/;
0249         }
0250 
0251         if (/cancelClicked\(\)/) {
0252              s/connect\s*\(\s*this,/connect(buttonBox->button(QDialogButtonBox::Cancel),/;
0253              s/SIGNAL\s*\(\s*cancelClicked\(\)/SIGNAL\(clicked()/;
0254         }
0255         if (/user1Clicked\(\)/) {
0256              s/connect\s*\(\s*this,/connect(user1Button,/;
0257              s/user1Clicked\(\)/clicked()/;
0258         }
0259         if (/user2Clicked\(\)/) {
0260              s/connect\s*\(\s*this,/connect(user2Button,/;
0261              s/SIGNAL\s*\(\s*user2Clicked\(\)/SIGNAL\(clicked()/;
0262         }
0263         if (/user3Clicked\(\)/) {
0264              s/connect\s*\(\s*this,/connect(user3Button,/;
0265              s/SIGNAL\s*\(\s*user3Clicked\(\)/SIGNAL\(clicked()/;
0266         }
0267         if (/resetClicked\(\)/) {
0268              s/connect\s*\(\s*this,/connect(buttonBox->button(QDialogButtonBox::Reset),/;
0269              s/SIGNAL\s*\(\s*resetClicked\(\)/SIGNAL\(clicked()/;
0270         }
0271 
0272         my $regexEnableButton = qr/
0273           ^(\s*(?:[\-\>:\w]+)?)           # (1) Indentation
0274           enableButton
0275           ${functionUtilkde::paren_begin}2${functionUtilkde::paren_end}  # (2) (args)
0276           /x; # /x Enables extended whitespace mode
0277         if (my ($left, $args) = $_ =~ $regexEnableButton) {
0278            warn "found enableButton $args\n";
0279            my $localLeft = removePrivateVariable($left);
0280 
0281            my $extract_args_regexp = qr/
0282                                  ^\(([^,]*)         # button
0283                                  ,\s*([^,]*)        # state
0284                                  (.*)$              # after
0285                                  /x;
0286 
0287            if ( my ($defaultButtonType, $state) = $args =~  $extract_args_regexp ) {
0288               $defaultButtonType =~ s, ,,g;
0289               $defaultButtonType =~ s,^KDialog::,,;
0290               $state =~ s, ,,g;
0291               $state =~ s,\),,g;
0292              if (defined $dialogButtonType{$defaultButtonType}) {
0293                 if ( $defaultButtonType eq "Ok") {
0294                    $_ = $localLeft . "okButton->setEnabled($state);\n";
0295                 } else {
0296                    $_ = $localLeft . "buttonBox->button($dialogButtonType{$defaultButtonType})->setEnabled($state);\n";
0297                 }
0298              } else {
0299                 if ($defaultButtonType eq "User1") {
0300                    $_ = $localLeft . "user1Button\->setEnabled($state);\n";
0301                 } elsif ($defaultButtonType eq "User2") {
0302                    $_ = $localLeft . "user2Button\->setEnabled($state);\n";
0303                 } elsif ($defaultButtonType eq "User3") {
0304                    $_ = $localLeft . "user3Button\->setEnabled($state);\n";
0305                 } else {
0306                    warn "Enable button: unknown or not supported \'$defaultButtonType\'\n";
0307                 }
0308              }
0309              warn "Found enabled button \'$defaultButtonType\', state \'$state\'\n";
0310            }
0311         }
0312 
0313         my $regexDefaultButton = qr/
0314                                ^(\s*(?:[\-\>:\w]+)?)           # (1) Indentation
0315                                setDefaultButton\s*\(
0316                                (.*)             # (2) button type
0317                                \);/x; # /x Enables extended whitespace mode
0318         if ( my ($left, $defaultButtonType) = $_ =~ $regexDefaultButton ) {
0319            warn "Found default button type : $defaultButtonType\n";
0320            $defaultButtonType =~ s, ,,g;
0321            my $localLeft = removePrivateVariable($left);
0322 
0323            if (defined $dialogButtonType{$defaultButtonType}) {
0324               if ( $defaultButtonType eq "Ok") {
0325                  $_ = $localLeft . "okButton->setDefault(true);\n";
0326               } else {
0327                  $_ = $localLeft . "buttonBox->button($dialogButtonType{$defaultButtonType})->setDefault(true);\n";
0328               }
0329            } else {
0330               if ($defaultButtonType eq "User1") {
0331                  $_ = $localLeft . "user1Button\->setDefault(true);\n";
0332               } elsif ($defaultButtonType eq "User2") {
0333                  $_ = $localLeft . "user2Button\->setDefault(true);\n";
0334               } elsif ($defaultButtonType eq "User3") {
0335                  $_ = $localLeft . "user3Button\->setDefault(true);\n";
0336               } elsif ($defaultButtonType eq "NoButton") {
0337                  $_ = "";
0338               } else {
0339                 warn "Default button type unknown or not supported \'$defaultButtonType\'\n";
0340               }
0341            }
0342         }
0343 
0344         my $regexButtonFocus = qr/
0345                                ^(\s*(?:[\-\>:\w]+)?)           # (1) Indentation
0346                                setButtonFocus\s*\(
0347                                (.*)
0348                                \s*\);/x; # /x Enables extended whitespace mode
0349         if ( my ($left, $defaultButtonType) = $_ =~ $regexButtonFocus ) {
0350            $defaultButtonType =~ s, ,,g;
0351            my $localLeft = removePrivateVariable($left);
0352 
0353            warn "Found default button focus : $defaultButtonType\n";
0354            if (defined $dialogButtonType{$defaultButtonType}) {
0355               if ( $defaultButtonType eq "Ok") {
0356                  $_ = $localLeft . "okButton->setFocus();\n";
0357               } else {
0358                  $_ = $localLeft . "buttonBox->button($dialogButtonType{$defaultButtonType})->setFocus();\n";
0359               }
0360            } else {
0361               if ($defaultButtonType eq "User1") {
0362                  $_ = $localLeft . "user1Button\->setFocus();\n";
0363               } elsif ($defaultButtonType eq "User2") {
0364                  $_ = $localLeft . "user2Button\->setFocus();\n";
0365               } elsif ($defaultButtonType eq "User3") {
0366                  $_ = $localLeft . "user3Button\->setFocus();\n";
0367               } else {
0368                 warn "Default button focus: unknown or not supported \'$defaultButtonType\'\n";
0369               }
0370            }
0371         }
0372 
0373 
0374         my $regexSetButtonText = qr/
0375           ^(\s*(?:[\-\>:\w]+)?)           # (1) Indentation
0376           setButtonText
0377           ${functionUtilkde::paren_begin}2${functionUtilkde::paren_end}  # (2) (args)
0378           /x; # /x Enables extended whitespace mode
0379         if (my ($left, $args) = $_ =~ $regexSetButtonText) {
0380            warn "found setButtonText $args\n";
0381            my $localLeft = removePrivateVariable($left);
0382 
0383            my $extract_args_regexp = qr/
0384                                  ^\(([^,]*)           # button
0385                                  ,\s*([^,]*)        # state
0386                                  (.*)$              # after
0387                                  /x;
0388            if ( my ($defaultButtonType, $text) = $args =~  $extract_args_regexp ) {
0389               $defaultButtonType =~ s, ,,g;
0390               $defaultButtonType =~ s,^KDialog::,,;
0391               #$text =~ s, ,,g;
0392               $text =~ s,\),,g;
0393               if (defined $dialogButtonType{$defaultButtonType}) {
0394                  if ( $defaultButtonType eq "Ok") {
0395                     $_ = $localLeft . "okButton->setText($text));\n";
0396                  } else {
0397                     $_ = $localLeft . "buttonBox->button($dialogButtonType{$defaultButtonType})->setText($text));\n";
0398                 }
0399               } else {
0400                  if ($defaultButtonType eq "User1") {
0401                     $_ = $localLeft . "user1Button\->setText($text));\n";
0402                  } elsif ($defaultButtonType eq "User2") {
0403                     $_ = $localLeft . "user2Button\->setText($text));\n";
0404                  } elsif ($defaultButtonType eq "User3") {
0405                     $_ = $localLeft . "user3Button\->setText($text));\n";
0406                  } else {
0407                      warn "Set button Text: unknown or not supported \'$defaultButtonType\'\n";
0408                  }
0409               }
0410            }
0411         }
0412 
0413         my $regexSetButtonMenu = qr/
0414           ^(\s*(?:[\-\>:\w]+)?)           # (1) Indentation
0415           setButtonMenu
0416           ${functionUtilkde::paren_begin}2${functionUtilkde::paren_end}  # (2) (args)
0417           /x; # /x Enables extended whitespace mode
0418         if (my ($left, $args) = $_ =~ $regexSetButtonMenu) {
0419            warn "found setButtonMenu $args\n";
0420            my $localLeft = removePrivateVariable($left);
0421 
0422            my $extract_args_regexp = qr/
0423                                  ^\(([^,]*)           # button
0424                                  ,\s*([^,]*)        # state
0425                                  (.*)$              # after
0426                                  /x;
0427            if ( my ($button, $menuName) = $args =~  $extract_args_regexp ) {
0428               $button =~ s, ,,g;
0429               $menuName =~ s, ,,g;
0430               $menuName =~ s,\),,g;
0431               warn "Found setButtonMenu: \'$button\', menu variable \'$menuName\'\n";
0432               if (defined $dialogButtonType{$button}) {
0433                  if ( $button eq "Ok" || $button eq "KDialog::Ok") {
0434                     $_ = $localLeft . "okButton->setMenu($menuName);\n";
0435                  } else {
0436                     $_ = $localLeft . "buttonBox->button($dialogButtonType{$button})->setMenu($menuName);\n";
0437                 }
0438               } else {
0439                  if ($button eq "User1") {
0440                     $_ = $localLeft . "user1Button\->setMenu($menuName);\n";
0441                  } elsif ($button eq "User2") {
0442                     $_ = $localLeft . "user2Button\->setMenu($menuName);\n";
0443                  } elsif ($button eq "User3") {
0444                     $_ = $localLeft . "user3Button\->setMenu($menuName);\n";
0445                  } else {
0446                      warn "Set Button Menu: unknown or not supported \'$button\'\n";
0447                  }
0448               }
0449            }
0450         }
0451         my $regexSetButtonIcon = qr/
0452           ^(\s*(?:[\-\>:\w]+)?)           # (1) Indentation
0453           setButtonIcon
0454           ${functionUtilkde::paren_begin}2${functionUtilkde::paren_end}  # (2) (args)
0455           /x; # /x Enables extended whitespace mode
0456         if (my ($left, $args) = $_ =~ $regexSetButtonIcon) {
0457            warn "found setButtonIcon $args\n";
0458            my $localLeft = removePrivateVariable($left);
0459 
0460            my $extract_args_regexp = qr/
0461                                  ^\(([^,]*)           # button
0462                                  ,\s*([^,]*)        # state
0463                                  (.*)$              # after
0464                                  /x;
0465            if ( my ($button, $menuName) = $args =~  $extract_args_regexp ) {
0466               $button =~ s, ,,g;
0467               $menuName =~ s, ,,g;
0468               $menuName =~ s,\),,g;
0469               warn "Found setButtonIcon: \'$button\', menu variable \'$menuName\'\n";
0470               if (defined $dialogButtonType{$button}) {
0471                  if ( $button eq "Ok" || $button eq "KDialog::Ok") {
0472                     $_ = $localLeft . "okButton->setIcon($menuName);\n";
0473                  } else {
0474                     $_ = $localLeft . "buttonBox->button($dialogButtonType{$button})->setIcon($menuName);\n";
0475                 }
0476               } else {
0477                  if ($button eq "User1") {
0478                     $_ = $localLeft . "user1Button\->setIcon($menuName);\n";
0479                  } elsif ($button eq "User2") {
0480                     $_ = $localLeft . "user2Button\->setIcon($menuName);\n";
0481                  } elsif ($button eq "User3") {
0482                     $_ = $localLeft . "user3Button\->setIcon($menuName);\n";
0483                  } else {
0484                      warn "Set Button Icon: unknown or not supported \'$button\'\n";
0485                  }
0486               }
0487            }
0488         }
0489 
0490 
0491         my $regexSetButtonWhatsThis = qr/
0492           ^(\s*(?:[\-\>:\w]+)?)           # (1) Indentation
0493           setButtonWhatsThis
0494           ${functionUtilkde::paren_begin}2${functionUtilkde::paren_end}  # (2) (args)
0495           /x; # /x Enables extended whitespace mode
0496         if (my ($left, $args) = $_ =~ $regexSetButtonWhatsThis) {
0497            warn "found setButtonWhatsThis $args\n";
0498            my $extract_args_regexp = qr/
0499                                  ^\(([^,]*)           # button
0500                                  ,\s*([^,]*)        # i18n
0501                                  (.*)$              # after
0502                                  /x;
0503            my $localLeft = removePrivateVariable($left);
0504 
0505            if ( my ($button, $i18n) = $args =~  $extract_args_regexp ) {
0506               $button =~ s, ,,g;
0507               $i18n =~ s,\),,g;
0508               $button =~ s,^KDialog::,,;
0509 
0510               warn "Found setButtonWhatsThis: \'$button\', i18n \'$i18n\'\n";
0511               if (defined $dialogButtonType{$button}) {
0512                  if ( $button eq "Ok" || $button eq "KDialog::Ok") {
0513                     $_ = $localLeft . "okButton->setWhatsThis($i18n));\n";
0514                  } else {
0515                     $_ = $localLeft . "buttonBox->button($dialogButtonType{$button})->setWhatsThis($i18n));\n";
0516                 }
0517               } else {
0518                  if ($button eq "User1") {
0519                     $_ = $localLeft . "user1Button->setWhatsThis($i18n));\n";
0520                  } elsif ($button eq "User2") {
0521                     $_ = $localLeft . "user2Button->setWhatsThis($i18n));\n";
0522                  } elsif ($button eq "User3") {
0523                     $_ = $localLeft . "user3Button->setWhatsThis($i18n));\n";
0524                  } else {
0525                      warn "setButtonWhatsThis: unknown or not supported \'$button\'\n";
0526                  }
0527               }
0528            }
0529         }
0530 
0531 
0532 
0533         my $regexSetButtonToolTip = qr/
0534           ^(\s*(?:[\-\>:\w]+)?)           # (1) Indentation
0535           setButtonToolTip
0536           ${functionUtilkde::paren_begin}2${functionUtilkde::paren_end}  # (2) (args)
0537           /x; # /x Enables extended whitespace mode
0538         if (my ($left, $args) = $_ =~ $regexSetButtonToolTip) {
0539            warn "found setButtonToolTip $args\n";
0540            my $extract_args_regexp = qr/
0541                                  ^\(([^,]*)           # button
0542                                  ,\s*([^,]*)        # i18n
0543                                  (.*)$              # after
0544                                  /x;
0545            my $localLeft = removePrivateVariable($left);
0546            if ( my ($button, $i18n) = $args =~  $extract_args_regexp ) {
0547               $button =~ s, ,,g;
0548               $i18n =~ s,\),,g;
0549               $button =~ s,^KDialog::,,;
0550 
0551               warn "Found setButtonToolTip: \'$button\', i18n \'$i18n\'\n";
0552               if (defined $dialogButtonType{$button}) {
0553                  if ( $button eq "Ok" || $button eq "KDialog::Ok") {
0554                     $_ = $localLeft . "okButton->setToolTip($i18n));\n";
0555                  } else {
0556                     $_ = $localLeft . "buttonBox->button($dialogButtonType{$button})->setToolTip($i18n));\n";
0557                 }
0558               } else {
0559                  if ($button eq "User1") {
0560                     $_ = $localLeft . "user1Button->setToolTip($i18n));\n";
0561                  } elsif ($button eq "User2") {
0562                     $_ = $localLeft . "user2Button->setToolTip($i18n));\n";
0563                  } elsif ($button eq "User3") {
0564                     $_ = $localLeft . "user3Button->setToolTip($i18n));\n";
0565                  } else {
0566                      warn "setButtonToolTip: unknown or not supported \'$button\'\n";
0567                  }
0568               }
0569            }
0570         }
0571 
0572 
0573         my $regexSetButtonGuiItem = qr/
0574           ^(\s*(?:[\-\>:\w]+)?)           # (1) Indentation
0575           setButtonGuiItem
0576           ${functionUtilkde::paren_begin}2${functionUtilkde::paren_end}  # (2) (args)
0577           /x; # /x Enables extended whitespace mode
0578         if (my ($left, $args) = $_ =~ $regexSetButtonGuiItem) {
0579            warn "found setButtonGuiItem $args\n";
0580            my $extract_args_regexp = qr/
0581                                  ^\(([^,]*)           # button
0582                                  ,\s*(.*)             # KGuiItem argument
0583                                  \)$
0584                                  /x;
0585            my $localLeft = removePrivateVariable($left);
0586 
0587            if ( my ($button, $menuName) = $args =~  $extract_args_regexp ) {
0588               $button =~ s, ,,g;
0589               $button =~ s,^KDialog::,,;
0590 
0591               $needKGuiItem = 1;
0592               warn "Found setButtonGuiItem: \'$button\', menu variable \'$menuName\'\n";
0593               if (defined $dialogButtonType{$button}) {
0594                  if ( $button eq "Ok" || $button eq "KDialog::Ok") {
0595                     $_ = $localLeft . "KGuiItem::assign(okButton, $menuName));\n";
0596                  } else {
0597                     $_ = $localLeft . "KGuiItem::assign(buttonBox->button($dialogButtonType{$button}), $menuName));\n";
0598                 }
0599               } else {
0600                  if ($button eq "User1") {
0601                     $_ = $localLeft . "KGuiItem::assign(user1Button, $menuName));\n";
0602                  } elsif ($button eq "User2") {
0603                     $_ = $localLeft . "KGuiItem::assign(user2Button, $menuName));\n";
0604                  } elsif ($button eq "User3") {
0605                     $_ = $localLeft . "KGuiItem::assign(user3Button, $menuName));\n";
0606                  } else {
0607                      warn "Set Button Gui Item: unknown or not supported \'$button\'\n";
0608                  }
0609               }
0610            }
0611         }
0612 
0613 
0614         my $regexShowButton = qr/
0615           ^(\s*(?:[\-\>:\w]+)?)           # (1) Indentation
0616           showButton
0617           ${functionUtilkde::paren_begin}2${functionUtilkde::paren_end}  # (2) (args)
0618           /x; # /x Enables extended whitespace mode
0619         if (my ($left, $args) = $_ =~ $regexShowButton) {
0620            warn "found showButton $args\n";
0621            my $extract_args_regexp = qr/
0622                                  ^\(([^,]*)           # button
0623                                  ,\s*([^,]*)        # state
0624                                  (.*)$              # after
0625                                  /x;
0626            my $localLeft = removePrivateVariable($left);
0627 
0628            if ( my ($defaultButtonType, $state) = $args =~  $extract_args_regexp ) {
0629               $defaultButtonType =~ s, ,,g;
0630               $state =~ s, ,,g;
0631               $state =~ s,\),,g;
0632              if (defined $dialogButtonType{$defaultButtonType}) {
0633                 if ( $defaultButtonType eq "Ok") {
0634                    $_ = $localLeft . "okButton->setVisible($state);\n";
0635                 } else {
0636                    $_ = $localLeft . "buttonBox->button($dialogButtonType{$defaultButtonType})->setVisible($state);\n";
0637                 }
0638              } else {
0639                 if ($defaultButtonType eq "User1") {
0640                    $_ = $localLeft . "user1Button\->setVisible($state);\n";
0641                 } elsif ($defaultButtonType eq "User2") {
0642                    $_ = $localLeft . "user2Button\->setVisible($state);\n";
0643                 } elsif ($defaultButtonType eq "User3") {
0644                    $_ = $localLeft . "user3Button\->setVisible($state);\n";
0645                 } else {
0646                    warn "Show button: unknown or not supported \'$defaultButtonType\'\n";
0647                 }
0648              }
0649              warn "Found show button \'$defaultButtonType\', state \'$state\'\n";
0650            }
0651         }
0652 
0653 
0654         if (/KDialog::spacingHint/) {
0655            $_ = "//TODO PORT QT5 " .  $_;
0656         }
0657         if (/KDialog::marginHint/) {
0658            $_ = "//TODO PORT QT5 " .  $_;
0659         }
0660 
0661         my $regexSetEscapeButton = qr/
0662           (.*)           # (1) Indentation
0663           setEscapeButton\s*\(\s*([:\w]+)\s*\)   # (2) (args)
0664           (.*)                            # (3) after
0665           /x; # /x Enables extended whitespace mode
0666         if (my ($left, $button, $after) = $_ =~ $regexSetEscapeButton) {
0667            $button =~ s, ,,g;
0668            $button =~ s,^KDialog::,,;
0669            if (defined $dialogButtonType{$button}) {
0670               if ( $button eq "Ok") {
0671                  $_ = $left . "okButton->setShortcut(Qt::Key_Escape);\n";
0672               } else {
0673                  $_ = $left . "buttonBox->button($dialogButtonType{$button})->setShortcut(Qt::Key_Escape)" . $after . "\n";
0674               }
0675              } else {
0676                 if ($button eq "User1") {
0677                    $_ = $left . "user1Button\->setShortcut(Qt::Key_Escape)" . $after . "\n";
0678                 } elsif ($button eq "User2") {
0679                    $_ = $left . "user2Button\->setShortcut(Qt::Key_Escape)" . $after . "\n";
0680                 } elsif ($button eq "User3") {
0681                    $_ = $left . "user3Button\->setShortcut(Qt::Key_Escape)" . $after . "\n";
0682                 } else {
0683                    warn "Show button: unknown or not supported \'$button\'\n";
0684                 }
0685              }
0686 
0687         }
0688 
0689 
0690         # if ( isButtonEnabled( KDialog::Apply ) ) {
0691 
0692         my $regexIsButtonEnabled = qr/
0693           (.*)           # (1) Indentation
0694           isButtonEnabled\s*\(\s*([:\w]+)\s*\)   # (2) (args)
0695           (.*)                            # (3) after
0696           /x; # /x Enables extended whitespace mode
0697         if (my ($left, $button, $after) = $_ =~ $regexIsButtonEnabled) {
0698            $button =~ s, ,,g;
0699            $button =~ s,^KDialog::,,;
0700            if (defined $dialogButtonType{$button}) {
0701               if ( $button eq "Ok") {
0702                  $_ = $left . "okButton->isEnabled()" . $after . "\n";
0703               } else {
0704                  $_ = $left . "buttonBox->button($dialogButtonType{$button})->isEnabled()" . $after . "\n";
0705               }
0706              } else {
0707                 if ($button eq "User1") {
0708                    $_ = $left . "user1Button\->isEnabled()" . $after . "\n";
0709                 } elsif ($button eq "User2") {
0710                    $_ = $left . "user2Button\->isEnabled()" . $after . "\n";
0711                 } elsif ($button eq "User3") {
0712                    $_ = $left . "user3Button\->isEnabled()" . $after . "\n";
0713                 } else {
0714                    warn "Show button: unknown or not supported \'$button\'\n";
0715                 }
0716              }
0717 
0718         }
0719 
0720         if (/\bsetMainWidget\s*\(\s*(\w+)\s*\)/) {
0721            # remove setMainWidget doesn't exist now.
0722            my $var = $1;
0723            $_ = "//PORTING: Verify that widget was added to mainLayout: " . $_;
0724            $_ .= "// Add mainLayout->addWidget($var); if necessary\n";
0725         }
0726         if (/\bshowButtonSeparator\b/) {
0727            # remove showButtonSeparator doesn't exist now.
0728            $_ = "";
0729         }
0730 
0731         my $regexButton = qr/
0732           ^(\s*(?:[\-\>:\w]+)?)           # (1) Indentation
0733           button
0734           ${functionUtilkde::paren_begin}2${functionUtilkde::paren_end}  # (2) (args)
0735           /x; # /x Enables extended whitespace mode
0736         if (my ($left, $button) = $_ =~ $regexButton) {
0737            $button =~ s/\(//;
0738            $button =~ s/\)//;
0739            $button =~ s, ,,g;
0740            $button =~ s,^KDialog::,,;
0741            my $localLeft = removePrivateVariable($left);
0742 
0743            if (defined $dialogButtonType{$button}) {
0744               if ( $button eq "Ok" || $button eq "KDialog::Ok") {
0745                  s/button\s*\(\s*KDialog::Ok\s*\)/okButton/;
0746                  s/button\s*\(\s*Ok\s*\)/okButton/;
0747               } else {
0748                  s/button\s*\(\s*$button\s*\)/buttonBox->button($dialogButtonType{$button})/;
0749               }
0750            } else {
0751               if ($button eq "User1") {
0752                  s/button\s*\(\s*KDialog::User1\s*\)/user1Button/;
0753                  s/button\s*\(\s*User1\s*\)/user1Button/;
0754 
0755               } elsif ($button eq "User2") {
0756                  s/button\s*\(\s*KDialog::User2\s*\)/user2Button/;
0757                  s/button\s*\(\s*User2\s*\)/user2Button/;
0758               } elsif ($button eq "User3") {
0759                  s/button\s*\(\s*KDialog::User3\s*\)/user3Button/;
0760                  s/button\s*\(\s*User3\s*\)/user3Button/;
0761               } else {
0762                  warn "button(...): unknown or not supported \'$button\'\n";
0763               }
0764            }
0765 
0766            warn "Found button(...) with argument $button\n";
0767         }
0768         if (/::slotButtonClicked\b/) {
0769            $_ = "//Adapt code and connect okbutton or other to new slot. It doesn't exist in qdialog\n" . $_;
0770            warn "$file connect okbutton and other to new slot here\n";
0771         }
0772 
0773         s/\bsetCaption\b/setWindowTitle/;
0774         s/\benableButtonOk\b/okButton->setEnabled/;
0775         s/\benableButtonApply\b/buttonBox->button(QDialogButtonBox::Apply)->setEnabled/;
0776         s/\bKDialog\b/QDialog/g;
0777         s/\<KDialog\b\>/\<QDialog>/ if (/#include/);
0778         s/\<kdialog.h\>/\<QDialog>/ if (/#include/);
0779         s/\bsetInitialSize\b/resize/;
0780         s/button\s*\(\s*User2\s*\)/user2Button/;
0781         s/button\s*\(\s*User1\s*\)/user1Button/;
0782         s/button\s*\(\s*User3\s*\)/user3Button/;
0783 
0784         $modified ||= $orig ne $_;
0785         $_;
0786     } <$FILE>;
0787 
0788     if ($modified) {
0789         open (my $OUT, ">", $file);
0790         print $OUT @l;
0791         close ($OUT);
0792         # Need to add KConfigGroup because it was added by kdialog before
0793         if ($file =~ /\.cpp$/) {
0794             functionUtilkde::addIncludeInFile($file, "KConfigGroup");
0795         }
0796 
0797         if (defined $needQDialogButtonBox) {
0798            functionUtilkde::addIncludeInFile($file, "QDialogButtonBox");
0799            functionUtilkde::addIncludeInFile($file, "QPushButton");
0800         } else {
0801            if ( $file =~ /\.cpp$/) {
0802              warn "WARNING: check if you are using the default buttons Ok|Cancel without calling setButtons\n";
0803              warn "In this case, add this code:\n";
0804              warn "#include <QDialogButtonBox>\n";
0805              warn "#include <QPushButton>\n";
0806              warn "QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok|QDialogButtonBox::Cancel);\n";
0807              warn "QPushButton *okButton = buttonBox->button(QDialogButtonBox::Ok);\n";
0808              warn "okButton->setDefault(true);\n";
0809              warn "okButton->setShortcut(Qt::CTRL | Qt::Key_Return);\n";
0810              warn "connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));\n";
0811              warn "connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));\n";
0812              warn "mainLayout->addWidget(buttonBox);\n";
0813            }
0814         }
0815         if (defined $needKGuiItem) {
0816            functionUtilkde::addIncludeInFile($file, "KGuiItem");
0817         }
0818         if (defined $needQBoxLayout) {
0819            functionUtilkde::addIncludeInFile($file, "QVBoxLayout");
0820         }
0821     }
0822 }
0823 
0824 functionUtilkde::diffFile( "@ARGV" );