1 /***************************************************************************
2  *   Copyright (C) 2003-2004 by David Saxton                               *
3  *   david@bluehaze.org                                                    *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) any later version.                                   *
9  ***************************************************************************/
10 
11 #include "forloop.h"
12 
13 #include "libraryitem.h"
14 #include "flowcode.h"
15 
16 #include <KLocalizedString>
17 
construct(ItemDocument * itemDocument,bool newItem,const char * id)18 Item* ForLoop::construct( ItemDocument *itemDocument, bool newItem, const char *id )
19 {
20 	return new ForLoop( (ICNDocument*)itemDocument, newItem, id );
21 }
22 
libraryItem()23 LibraryItem* ForLoop::libraryItem()
24 {
25 	return new LibraryItem(
26 		QStringList(QString("flow/forloop")),
27 		i18n("For"),
28 		i18n("Loops"),
29 		"for.png",
30 		LibraryItem::lit_flowpart,
31 		ForLoop::construct );
32 }
33 
ForLoop(ICNDocument * icnDocument,bool newItem,const char * id)34 ForLoop::ForLoop( ICNDocument *icnDocument, bool newItem, const char *id )
35 	: FlowContainer( icnDocument, newItem, id ? id : "forloop" )
36 {
37 	m_name = i18n("For Loop");
38 
39 	createTopContainerNode();
40 	createBotContainerNode();
41 
42 	createProperty( "0-var", Variant::Type::Combo );
43 	property("0-var")->setToolbarCaption("for");
44 	property("0-var")->setEditorCaption( i18n("Variable") );
45 	property("0-var")->setValue("x");
46 
47 	createProperty( "1-initial", Variant::Type::Combo );
48 	property("1-initial")->setToolbarCaption("=");
49 	property("1-initial")->setEditorCaption( i18n("Initial Value") );
50 	property("1-initial")->setValue("1");
51 
52 	createProperty( "2-end", Variant::Type::Combo );
53 	property("2-end")->setToolbarCaption( i18nc( "for x = 1 to", "to" ) );
54 	property("2-end")->setEditorCaption( i18n("End Value") );
55 	property("2-end")->setValue("10");
56 
57 	createProperty( "3-step", Variant::Type::Combo );
58 	property("3-step")->setToolbarCaption("step");
59 	property("3-step")->setEditorCaption( i18n("Step") );
60 	property("3-step")->setValue("1");
61 	property("3-step")->setAdvanced(true);
62 }
63 
~ForLoop()64 ForLoop::~ForLoop()
65 {
66 }
67 
dataChanged()68 void ForLoop::dataChanged()
69 {
70 	if( dataString("3-step").toInt() == 1 )
71 	setCaption( "for " + dataString("0-var") + " = " + dataString("1-initial") + " to " + dataString("2-end") );
72 	else setCaption( "for " + dataString("0-var") + " = " + dataString("1-initial") + " to " + dataString("2-end") + " step " + dataString("3-step"));
73 }
74 
generateMicrobe(FlowCode * code)75 void ForLoop::generateMicrobe( FlowCode *code )
76 {
77 	if( dataString("3-step").toInt() == 1 ) code->addCode( "for " + dataString("0-var") + " = " + dataString("1-initial") + " to " + dataString("2-end") + "\n{" );
78 	else code->addCode( "for " + dataString("0-var") + " = " + dataString("1-initial") + " to " + dataString("2-end") + " step " + dataString("3-step") +"\n{" );
79 	code->addCodeBranch( outputPart("int_in") );
80 	code->addCode("}");
81 	code->addCodeBranch( outputPart("ext_out") );
82 }
83 
84