Files @ 21afbd2a2c01
Branch filter:

Location: EI/VirtualLeaf/src/perl/make_pardialog_source.pl

Michael Guravage
Renamed tutorial examples and added makefile, etc.

--
user: Michael Guravage <michael.guravage@cwi.nl>
branch 'default'
added doc/gpl3.txt
added doc/installation.pdf
added doc/installation.rst
added src/TutorialCode/Makefile
added src/TutorialCode/Tutorial0/Makefile
added src/TutorialCode/Tutorial0/tutorial0.cpp
added src/TutorialCode/Tutorial0/tutorial0.h
added src/TutorialCode/Tutorial0/tutorial0.pro
added src/TutorialCode/Tutorial1A/Makefile
added src/TutorialCode/Tutorial1A/tutorial1A.cpp
added src/TutorialCode/Tutorial1A/tutorial1A.h
added src/TutorialCode/Tutorial1A/tutorial1A.pro
added src/TutorialCode/Tutorial1B/Makefile
added src/TutorialCode/Tutorial1B/tutorial1B.cpp
added src/TutorialCode/Tutorial1B/tutorial1B.h
added src/TutorialCode/Tutorial1B/tutorial1B.pro
added src/TutorialCode/Tutorial1C/Makefile
added src/TutorialCode/Tutorial1C/tutorial1C.cpp
added src/TutorialCode/Tutorial1C/tutorial1C.h
added src/TutorialCode/Tutorial1C/tutorial1C.pro
added src/TutorialCode/Tutorial1D/Makefile
added src/TutorialCode/Tutorial1D/tutorial1D.cpp
added src/TutorialCode/Tutorial1D/tutorial1D.h
added src/TutorialCode/Tutorial1D/tutorial1D.pro
added src/TutorialCode/Tutorial2/Makefile
added src/TutorialCode/Tutorial2/tutorial2.cpp
added src/TutorialCode/Tutorial2/tutorial2.h
added src/TutorialCode/Tutorial2/tutorial2.pro
added src/TutorialCode/Tutorial3/Makefile
added src/TutorialCode/Tutorial3/tutorial3.cpp
added src/TutorialCode/Tutorial3/tutorial3.h
added src/TutorialCode/Tutorial3/tutorial3.pro
added src/TutorialCode/Tutorial4/Makefile
added src/TutorialCode/Tutorial4/tutorial4.cpp
added src/TutorialCode/Tutorial4/tutorial4.h
added src/TutorialCode/Tutorial4/tutorial4.pro
added src/TutorialCode/Tutorial5/Makefile
added src/TutorialCode/Tutorial5/tutorial5.cpp
added src/TutorialCode/Tutorial5/tutorial5.h
added src/TutorialCode/Tutorial5/tutorial5.pro
added src/perl/ChangeLog
changed src/ChangeLog
changed src/Makefile
changed src/build_models/ChangeLog
changed src/build_models/Makefile
changed src/canvas.cpp
changed src/parameter.cpp
changed src/parameter.h
changed src/pardialog.cpp
changed src/pardialog.h
changed src/perl/make_parameter_source.pl
changed src/perl/make_pardialog_source.pl
changed src/perl/make_xmlwritecode.pl
changed src/vleafmodel.h
removed doc/GPL
removed src/TutorialCode/Tutorial0/mymodel.cpp
removed src/TutorialCode/Tutorial0/mymodel.h
removed src/TutorialCode/Tutorial0/mymodel.pro
removed src/TutorialCode/Tutorial1/mymodel.cpp
removed src/TutorialCode/Tutorial1/mymodel.h
removed src/TutorialCode/Tutorial1/mymodel.pro
removed src/TutorialCode/Tutorial1A/mymodel.cpp
removed src/TutorialCode/Tutorial1A/mymodel.h
removed src/TutorialCode/Tutorial1A/mymodel.pro
removed src/TutorialCode/Tutorial1B/mymodel.cpp
removed src/TutorialCode/Tutorial1B/mymodel.h
removed src/TutorialCode/Tutorial1B/mymodel.pro
removed src/TutorialCode/Tutorial1C/mymodel.cpp
removed src/TutorialCode/Tutorial1C/mymodel.h
removed src/TutorialCode/Tutorial1C/mymodel.pro
removed src/TutorialCode/Tutorial1D/mymodel.cpp
removed src/TutorialCode/Tutorial1D/mymodel.h
removed src/TutorialCode/Tutorial1D/mymodel.pro
removed src/TutorialCode/Tutorial2/mymodel.cpp
removed src/TutorialCode/Tutorial2/mymodel.h
removed src/TutorialCode/Tutorial2/mymodel.pro
removed src/TutorialCode/Tutorial3/mymodel.cpp
removed src/TutorialCode/Tutorial3/mymodel.h
removed src/TutorialCode/Tutorial3/mymodel.pro
removed src/TutorialCode/Tutorial4/mymodel
removed src/TutorialCode/Tutorial4/mymodel.cpp
removed src/TutorialCode/Tutorial4/mymodel.h
removed src/TutorialCode/Tutorial4/mymodel.pro
removed src/TutorialCode/Tutorial5/mymodel
removed src/TutorialCode/Tutorial5/mymodel.cpp
removed src/TutorialCode/Tutorial5/mymodel.h
removed src/TutorialCode/Tutorial5/mymodel.pro
#!/usr/bin/perl 

#
# $Id$
#
#  This file is part of the Virtual Leaf.
#
#  The Virtual Leaf is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
#
#  The Virtual Leaf is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with the Virtual Leaf.  If not, see <http://www.gnu.org/licenses/>.
#
#  Copyright 2010 Roeland Merks.
#

# input: parameter file + types
# format: par_name = default_value/type

# output: C++ source code of class Parameter
# and sample parameter file

%funname = (
    "double" => "toDouble",
    "int" => "toInt",
    );

%typetrans = (
    "double" => "double",
    "float" => "double",
    "int" => "int",
    "bool" => "bool",
    "char *" => "char *",
    "string" => "char *",
    "directory" => "char *",
    "doublelist" => "double *",
    "label" => "label",
    "title" => "title",
    );

$basename = "pardialog";

open parfile,"<$ARGV[0]";
open cppfile,">$basename.cpp";

$i=0;
while (<parfile>) {
    if (/^#/) {
	next;
    }
    @line=split(/=/);
#ignore empty lines
    if ($#line<1) {
	next;
    }
    $param[$i]=$line[0];
    $value_type=$line[1];
    
    @typel=split(/ \/ /,$value_type);
    $value[$i] = $typel[0];
    $type[$i] = $typel[1];

    #get rid of spaces
    $type[$i] =~ s/ //g;
    $type[$i] =~s/\n//g;
    $convtype[$i]=$typetrans{$type[$i]};
    $param[$i] =~ s/ //g;
    if ($convtype[$i] ne "label" && $convtype[$i] ne "title") {
	#get rid of spaces
	$value[$i] =~ s/ //g;
    }
    if ($convtype[$i] eq "char *") {
	$value[$i] = "\"$value[$i]\"";
    }
    #print cppfile "param = $param, value = $value, type = $type\n";

    $i++;
}

$lines=$i;


print cppfile <<END_HEADER;
#include "$basename.h"
#include "parameter.h"
#include <cstring>
#include <qdialog.h>
#include <qlabel.h>
#include <qlineedit.h>
#include <qmessagebox.h>

ParameterDialog::ParameterDialog(QWidget *parent, const char *name, Qt::WindowFlags f) : QDialog(parent,name,false,f) {
    extern Parameter par;
END_HEADER

for ($i=0;$i<$lines;$i++) {
    if ($convtype[$i] eq "label" || $convtype[$i] eq "title") {
	next;
    }
    if ($convtype[$i] eq "double *") {
	print cppfile  "  QString $param[$i]_string(\"";
	@paramlist = split(/,/,$value[$i]);
	for ($j=1;$j<=$#paramlist;$j++) {
	    print cppfile "%$j,";
	}
	print cppfile "%$j\");\n";
	print cppfile "  $param[$i]_string = $param[$i]_string";
	for ($j=0;$j<=$#paramlist;$j++) {
	    print cppfile ".arg(par.$param[$i]\[$j\])";
	}
	print cppfile ";\n";
	print cppfile "  $param[$i]_edit = new QLineEdit( $param[$i]_string, this, \"$param[$i]_edit\" );\n";
    } else {
	if ($convtype[$i] eq "bool") {
	    print cppfile "  $param[$i]_edit = new QLineEdit( QString(\"%1\").arg(sbool(par.$param[$i])), this, \"$param[$i]_edit\" );\n";
	} else {
	    print cppfile "  $param[$i]_edit = new QLineEdit( QString(\"%1\").arg(par.$param[$i]), this, \"$param[$i]_edit\" );\n";
	}
    }
}


print cppfile <<END_HEADER3;
// make a 1x1 grid; it will auto-expand
QGridLayout *grid = new QGridLayout( this, 1, 1 );
    
// add the first four widgets with (row, column) addressing
END_HEADER3

$numrows = 30;
$c = 0;
for ($i=0;$i<$lines;$i++) {
    $col = 2*int($c/($numrows-3));
    $row = $c % ($numrows-3) + $ntitles * 3;
    if ($convtype[$i] eq "label") {
	print cppfile "  grid->addWidget( new QLabel( \"$value[$i]\", this), $row, $col, 1, 2 );\n";
	$c++;
    } else {
	if ($convtype[$i] eq "title") {
	    if ($ntitles) {
		print stderr "Sorry, only one title allowed. Aborting source construction...\n";
		exit(1);
	    }
	    print cppfile "  setWindowTitle( QString( \"$value[$i]\") );\n";
	    print cppfile "  grid->addWidget( new QLabel( \"<h3>$value[$i]</h3>\",this), $row, $col, 1, -1, Qt::AlignCenter);\n";
	    print cppfile "  grid->addWidget( new QLabel( \"\", this), $row+1, $col, 1, -1);\n";
	    $ntitles++;
	} else {
	    print cppfile "  grid->addWidget( new QLabel( \"$param[$i]\", this ),$row, $col );\n";
	    print cppfile "  grid->addWidget( $param[$i]_edit, $row, $col+1  );\n";
	    $c++;
	}
    }
}

$row = $numrows+1;
$col = 2*int($i/$numrows);

print cppfile <<ANOTHER_LABEL;
QPushButton *pb = new QPushButton( \"&Write\", this );
grid->addWidget(pb, $row, $col );
connect( pb, SIGNAL( clicked() ), this, SLOT( write() ) );
QPushButton *pb2 = new QPushButton( \"&Close\", this );
grid->addWidget(pb2,$row, $col+1 );
connect( pb2, SIGNAL( clicked() ), this, SLOT( close() ) );
QPushButton *pb3 = new QPushButton( \"&Reset\", this );
grid->addWidget(pb3, $row, $col+2 );
connect( pb3, SIGNAL( clicked() ), this, SLOT( Reset() ) );
show();
};

ParameterDialog::~ParameterDialog(void) {
ANOTHER_LABEL

for ($i=0;$i<$lines;$i++) {
    if ($convtype[$i] ne "label" && $convtype[$i] ne "title") {
	print cppfile "delete $param[$i]_edit;\n";
    }
}
print cppfile <<END_HEADER4;
}

void ParameterDialog::write(void) {
    
extern Parameter par;
QString tmpval;
END_HEADER4

    for ($i=0;$i<$lines;$i++) {
	if ($convtype[$i] eq "label" || $convtype[$i] eq "title") {
	    next;
	}
	if ($convtype[$i] eq "double *") {
	    @paramlist = split(/,/,$value[$i]);
	    for ($j=0;$j<=$#paramlist;$j++) {
		print cppfile "  tmpval = $param[$i]_edit->text().section(',', $j, $j);\n";
		print cppfile "  par.$param[$i]\[$j\] = tmpval.toDouble();\n";
	    }
	} else {
	    if ($convtype[$i] eq "bool") {
		print cppfile "  tmpval = $param[$i]_edit->text().stripWhiteSpace();\n";
		print cppfile "  if (tmpval == \"true\" || tmpval == \"yes\" ) par.$param[$i] = true;\n";
		print cppfile "  else if (tmpval == \"false\" || tmpval == \"no\") par.$param[$i] = false;\n";
		print cppfile "  else {\n";
		print cppfile "    if (QMessageBox::question(this, \"Syntax error\", tr(\"Value %1 of parameter %2 is not recognized as Boolean.\\nDo you mean TRUE or FALSE?\").arg(tmpval).arg(\"$param[$i]\"),\"True\",\"False\", QString::null, 0, 1)==0) par.$param[$i]=true;\n";
		print cppfile "      else par.$param[$i]=false;\n";
		print cppfile "  }\n";
	    } else {
		if ($convtype[$i] eq "char *") {
		    print cppfile "  par.$param[$i] = strdup((const char *)$param[$i]_edit->text());\n";
		} else {
		    print cppfile "  par.$param[$i] = $param[$i]_edit->text().$funname{$convtype[$i]}();\n";
		}
	    }
	}
}



print cppfile <<END_MIDPART;
Reset();

}
END_MIDPART

print cppfile "void ParameterDialog::Reset(void) {\n";
print cppfile "  extern Parameter par;\n";

for ($i=0;$i<$lines;$i++) {
    if ($convtype[$i] eq "label" || $convtype[$i] eq "title") {
	next;
    }
    if ($convtype[$i] eq "double *") {
	print cppfile  "  QString $param[$i]_string(\"";
	@paramlist = split(/,/,$value[$i]);
	for ($j=1;$j<=$#paramlist;$j++) {
	    print cppfile "%$j,";
	}
	print cppfile "%$j\");\n";
	print cppfile "  $param[$i]_string = $param[$i]_string";
	for ($j=0;$j<=$#paramlist;$j++) {
	    print cppfile ".arg(par.$param[$i]\[$j\])";
	}
	print cppfile ";\n";
	print cppfile "  $param[$i]_edit->setText( $param[$i]_string );\n";
    } else {
	if ($convtype[$i] eq "bool") {
	    print cppfile "  $param[$i]_edit->setText( QString(\"%1\").arg(sbool(par.$param[$i])));\n";
	} else {
	    print cppfile "  $param[$i]_edit->setText( QString(\"%1\").arg(par.$param[$i]) );\n";
	}
    }
}

print cppfile "}\n\n";

# qparameter.h

open hfile, ">$basename.h";
print hfile <<END_HEADER2;
#ifndef PARAMETER_DIALOG_H
#define PARAMETER_DIALOG_H
#include <qdialog.h>
#include <qspinbox.h>
#include <qlineedit.h>
#include <qlayout.h>
#include <qpushbutton.h>
#include <iostream>

class ParameterDialog : public QDialog {
    Q_OBJECT
	
      public:
	ParameterDialog(QWidget *parent=0, const char *name = 0, Qt::WindowFlags f = 0);
    virtual ~ParameterDialog(void);
    public slots:
    void Reset(void);

    private slots:
    void write(void);

  private:
END_HEADER2

for ($i=0;$i<$lines;$i++) {
    if ($convtype[$i] ne "label" && $convtype[$i] ne "title") {
	print hfile "  QLineEdit *$param[$i]_edit;\n";
    }
}

print hfile <<END_TRAILER2;
};
#endif
END_TRAILER2

# finis