File indexing completed on 2024-04-28 13:39:10

0001 /***************************************************************************
0002  *   Copyright (C) 2003-2004 by David Saxton                               *
0003  *   david@bluehaze.org                                                    *
0004  *                                                                         *
0005  *   This program is free software; you can redistribute it and/or modify  *
0006  *   it under the terms of the GNU General Public License as published by  *
0007  *   the Free Software Foundation; either version 2 of the License, or     *
0008  *   (at your option) any later version.                                   *
0009  ***************************************************************************/
0010 
0011 #include "forloop.h"
0012 
0013 #include "flowcode.h"
0014 #include "libraryitem.h"
0015 #include "icndocument.h"
0016 
0017 #include <KLocalizedString>
0018 
0019 Item *ForLoop::construct(ItemDocument *itemDocument, bool newItem, const char *id)
0020 {
0021     return new ForLoop(static_cast<ICNDocument *>(itemDocument), newItem, id);
0022 }
0023 
0024 LibraryItem *ForLoop::libraryItem()
0025 {
0026     return new LibraryItem(QStringList(QString("flow/forloop")), i18n("For"), i18n("Loops"), "for.png", LibraryItem::lit_flowpart, ForLoop::construct);
0027 }
0028 
0029 ForLoop::ForLoop(ICNDocument *icnDocument, bool newItem, const char *id)
0030     : FlowContainer(icnDocument, newItem, id ? id : "forloop")
0031 {
0032     m_name = i18n("For Loop");
0033 
0034     createTopContainerNode();
0035     createBotContainerNode();
0036 
0037     createProperty("0-var", Variant::Type::Combo);
0038     property("0-var")->setToolbarCaption("for");
0039     property("0-var")->setEditorCaption(i18n("Variable"));
0040     property("0-var")->setValue("x");
0041 
0042     createProperty("1-initial", Variant::Type::Combo);
0043     property("1-initial")->setToolbarCaption("=");
0044     property("1-initial")->setEditorCaption(i18n("Initial Value"));
0045     property("1-initial")->setValue("1");
0046 
0047     createProperty("2-end", Variant::Type::Combo);
0048     property("2-end")->setToolbarCaption(i18nc("for x = 1 to", "to"));
0049     property("2-end")->setEditorCaption(i18n("End Value"));
0050     property("2-end")->setValue("10");
0051 
0052     createProperty("3-step", Variant::Type::Combo);
0053     property("3-step")->setToolbarCaption("step");
0054     property("3-step")->setEditorCaption(i18n("Step"));
0055     property("3-step")->setValue("1");
0056     property("3-step")->setAdvanced(true);
0057 }
0058 
0059 ForLoop::~ForLoop()
0060 {
0061 }
0062 
0063 void ForLoop::dataChanged()
0064 {
0065     if (dataString("3-step").toInt() == 1)
0066         setCaption("for " + dataString("0-var") + " = " + dataString("1-initial") + " to " + dataString("2-end"));
0067     else
0068         setCaption("for " + dataString("0-var") + " = " + dataString("1-initial") + " to " + dataString("2-end") + " step " + dataString("3-step"));
0069 }
0070 
0071 void ForLoop::generateMicrobe(FlowCode *code)
0072 {
0073     if (dataString("3-step").toInt() == 1)
0074         code->addCode("for " + dataString("0-var") + " = " + dataString("1-initial") + " to " + dataString("2-end") + "\n{");
0075     else
0076         code->addCode("for " + dataString("0-var") + " = " + dataString("1-initial") + " to " + dataString("2-end") + " step " + dataString("3-step") + "\n{");
0077     code->addCodeBranch(outputPart("int_in"));
0078     code->addCode("}");
0079     code->addCodeBranch(outputPart("ext_out"));
0080 }