summaryrefslogtreecommitdiffstats
path: root/src/flowparts/forloop.cpp
blob: 23f4da5dc4877c4ebb87c6e87196216097844938 (plain)
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
/***************************************************************************
 *   Copyright (C) 2003-2004 by David Saxton                               *
 *   david@bluehaze.org                                                    *
 *                                                                         *
 *   This program 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 2 of the License, or     *
 *   (at your option) any later version.                                   *
 ***************************************************************************/

#include "forloop.h"

#include "libraryitem.h"
#include "flowcode.h"

#include <klocale.h>

Item* ForLoop::construct( ItemDocument *itemDocument, bool newItem, const char *id )
{
	return new ForLoop( (ICNDocument*)itemDocument, newItem, id );
}

LibraryItem* ForLoop::libraryItem()
{
	return new LibraryItem(
		TQString("flow/forloop"),
		i18n("For"),
		i18n("Loops"),
		"for.png",
		LibraryItem::lit_flowpart,
		ForLoop::construct );
}

ForLoop::ForLoop( ICNDocument *icnDocument, bool newItem, const char *id )
	: FlowContainer( icnDocument, newItem, (id) ? id : "forloop" )
{
	m_name = i18n("For Loop");
	m_desc = i18n("The code contained in the foor loop is repeatedly executed. By default, the variable used will be incremented every time. This can be changed by entering a value other than 1 into Step.<br><br>The for loop will exit when the value contained in the variable is equal to the end value.");
	
	createTopContainerNode();
	createBotContainerNode();
	
	createProperty( "0-var", Variant::Type::Combo );
	property("0-var")->setToolbarCaption("for");
	property("0-var")->setEditorCaption( i18n("Variable") );
	property("0-var")->setValue("x");
	
	createProperty( "1-initial", Variant::Type::Combo );
	property("1-initial")->setToolbarCaption("=");
	property("1-initial")->setEditorCaption( i18n("Initial Value") );
	property("1-initial")->setValue("1");
	
	createProperty( "2-end", Variant::Type::Combo );
	property("2-end")->setToolbarCaption("to");
	property("2-end")->setEditorCaption( i18n("End Value") );
	property("2-end")->setValue("10");
	
	createProperty( "3-step", Variant::Type::Combo );
	property("3-step")->setToolbarCaption("step");
	property("3-step")->setEditorCaption( i18n("Step") );
	property("3-step")->setValue("1");
	property("3-step")->setAdvanced(true);
}

ForLoop::~ForLoop()
{
}

void ForLoop::dataChanged()
{
	if( dataString("3-step").toInt() == 1 )
	setCaption( "for " + dataString("0-var") + " = " + dataString("1-initial") + " to " + dataString("2-end") );
	else setCaption( "for " + dataString("0-var") + " = " + dataString("1-initial") + " to " + dataString("2-end") + " step " + dataString("3-step"));
}

void ForLoop::generateMicrobe( FlowCode *code )
{
	if( dataString("3-step").toInt() == 1 ) code->addCode( "for " + dataString("0-var") + " = " + dataString("1-initial") + " to " + dataString("2-end") + "\n{" );
	else code->addCode( "for " + dataString("0-var") + " = " + dataString("1-initial") + " to " + dataString("2-end") + " step " + dataString("3-step") +"\n{" );
	code->addCodeBranch( outputPart("int_in") );
	code->addCode("}");
	code->addCodeBranch( outputPart("ext_out") );
}