Skip to content
OE_controller.cpp 28.1 KiB
Newer Older
/*
 * Copyright (c) 2015 Tricoire Sebastien 3dsman@free.fr
 *
 * This software is provided 'as-is', without any express or implied
 * warranty.  In no event will the authors be held liable for any damages
 * arising from the use of this software.
 *
 * Permission is granted to anyone to use this software for any purpose,
 * including commercial applications, and to alter it and redistribute it
 * freely, subject to the following restrictions:
 *
 * 1. The origin of this software must not be misrepresented; you must not
 * claim that you wrote the original software. If you use this software
 * in a product, an acknowledgment in the product documentation would be
 * appreciated but is not required.
 * 2. Altered source versions must be plainly marked as such, and must not be
 * misrepresented as being the original software.
 * 3. This notice may not be removed or altered from any source distribution.
 *
 */

#include "OE_controller.h"
#include <iostream>
#include <typeinfo>

#include "actions/OE_actionsCurves.h"
#include "actions/OE_actionsStitchs.h"
#include "actions/OE_actionsLineStitchs.h"
#include "actions/OE_actionsMetaLineStitchs.h"
#include "actions/OE_actionsBirailStitchs.h"
#include "actions/OE_actionsSelection.h"
#include "actions/OE_actionsThreads.h"

//OE_controller::OE_controller(OE_display * display, OE_document* document ) : comm("/dev/ttyUSB0", 80, 10)
OE_controller::OE_controller(OE_display * display, OE_document* document ) : comm("20:15:05:11:28:40", 80, 30)
{
	this->display = display;
	this->document = document;
    OE_linestitch::initMotifs();
    OE_metaLinestitch::initMotifs();

    if (document) document->addThread(new OE_thread(100,100,100,2));
}

OE_controller::~OE_controller()
{
}
        
bool OE_controller::testLogo2()
{
	// test motifs
	if (document)
	{
        OE_actions::curDocument = document;


        addAction(new OE_actionNewThread(new OE_thread(220,120,20,2)));

		document->setZeroPoint(vector_2d(0,1050));
        addAction(new OE_actionNewMetaLineStitch(getCurve(0),1.2,0.5,1,0));
        addAction(new OE_actionAddMetaLineStitchSubCurve(getMetaLineStitch(document->stitchs.size()-1),getCurve(1),0,1));

        addAction(new OE_actionAddMetaLineStitchSubCurve(getMetaLineStitch(document->stitchs.size()-1),getCurve(1),2,3.5));


#if 0
		// interrieur 1er rectangle
        addAction(new OE_actionNewSubCurve(getCurve(2),2, 3, false));
        addAction(new OE_actionNewSubCurve(getCurve(2),0, 1, true));

        addAction(new OE_actionNewBirailStitch(getCurve(document->curves.size()-1), getCurve(document->curves.size()-2), false, false, -0.2, -0.2, 0.5));
        curStitch = document->stitchs.size()-1;
        addAction(new OE_actionSetStitchThread(getStitch(curStitch),getThread(1)));
		
		// interrieur 2eme rectangle
        addAction(new OE_actionNewSubCurve(getCurve(4),2, 4, true));
        addAction(new OE_actionNewSubCurve(getCurve(4),6, 1, false));

        addAction(new OE_actionNewBirailStitch(getCurve(document->curves.size()-1), getCurve(document->curves.size()-2), false, false, 0.2, 0.2, 0.5));
        curStitch = document->stitchs.size()-1;
        addAction(new OE_actionSetStitchThread(getStitch(curStitch),getThread(1)));
		
		// interrieur 3eme rectangle
        addAction(new OE_actionNewSubCurve(getCurve(3),0, 1, false));
        addAction(new OE_actionNewSubCurve(getCurve(3),2, 3, true));
        addAction(new OE_actionNewBirailStitch(getCurve(document->curves.size()-1),getCurve(document->curves.size()-2), false, false, -0.2, -0.2, 0.5));
        curStitch = document->stitchs.size()-1;
        addAction(new OE_actionSetStitchThread(getStitch(curStitch),getThread(1)));

        addAction(new OE_actionNewThread(new OE_thread(120,220,20,2)));
        //addThread(120,220,20,2);
        addAction(new OE_actionNewSubCurve(getCurve(1),0, 2, true));
        addAction(new OE_actionNewSubCurve(getCurve(1),2, 4, true));
        addAction(new OE_actionNewJoinCurve(getCurve(document->curves.size()-2),getCurve(document->curves.size()-1)));
        addAction(new OE_actionNewBirailStitch(getCurve(0), getCurve(document->curves.size()-1),false,false, 0.2, 0.2, 0.5));
        curStitch = document->stitchs.size()-1;
        addAction(new OE_actionSetStitchMaxLen(getStitch(curStitch),5));
        addAction(new OE_actionSetStitchThread(getStitch(curStitch),getThread(1)));
		//selectCurve(getCurve(document->curves.size()-1), true);
        //E
        addAction(new OE_actionNewSubCurve(getCurve(5),9, 10, false));
        addAction(new OE_actionNewSubCurve(getCurve(5),11, 12, true));
        addAction(new OE_actionNewBirailStitch(getCurve(document->curves.size()-1), getCurve(document->curves.size()-2),false,false, 0.2, 0.2, 0.5));
        addAction(new OE_actionNewSubCurve(getCurve(5),0, 2, false));
        addAction(new OE_actionNewSubCurve(getCurve(5),7, 9, false));
        addAction(new OE_actionNewJoinCurve(getCurve(document->curves.size()-1),getCurve(document->curves.size()-2)));
        addAction(new OE_actionNewSubCurve(getCurve(5),3, 6, true));
        addAction(new OE_actionNewBirailStitch(getCurve(document->curves.size()-1), getCurve(document->curves.size()-2),false,false, 0.2, 0.2, 0.5));
        addAction(new OE_actionNewSubCurve(getCurve(6),3, 0, true));
        addAction(new OE_actionNewSubCurve(getCurve(6),1, 2, false));
        addAction(new OE_actionNewBirailStitch(getCurve(document->curves.size()-1), getCurve(document->curves.size()-2),false,false, -0.2, -0.2, 0.5));

        //e
        addAction(new OE_actionNewSubCurve(getCurve(8),0, 2, false));
        addAction(new OE_actionNewJoinCurve(getCurve(7),getCurve(document->curves.size()-1)));
        addAction(new OE_actionNewSubCurve(getCurve(8),3, 9, true));
        addAction(new OE_actionNewBirailStitch(getCurve(document->curves.size()-1), getCurve(document->curves.size()-2),false,false, 0.2, 0.2, 0.5));

        //c
        addAction(new OE_actionNewSubCurve(getCurve(9),10, 4, false));
        addAction(new OE_actionNewSubCurve(getCurve(9),5, 9, true));
        addAction(new OE_actionNewBirailStitch(getCurve(document->curves.size()-1), getCurve(document->curves.size()-2),false,false, 0.2, 0.2, 0.5));
        addAction(new OE_actionNewSubCurve(getCurve(10),1, 2, true));
        addAction(new OE_actionNewSubCurve(getCurve(10),5, 6, true));
        addAction(new OE_actionNewJoinCurve(getCurve(document->curves.size()-1),getCurve(document->curves.size()-2)));
        addAction(new OE_actionNewSubCurve(getCurve(10),7,8, true));
        addAction(new OE_actionNewSubCurve(getCurve(10),15,16, true));
        addAction(new OE_actionNewJoinCurve(getCurve(document->curves.size()-1),getCurve(document->curves.size()-2)));
        addAction(new OE_actionNewBirailStitch(getCurve(document->curves.size()-1), getCurve(document->curves.size()-4),false,true, 0.2, 0.2, 0.5));

        addAction(new OE_actionNewSubCurve(getCurve(10),2, 3, false));
        addAction(new OE_actionNewSubCurve(getCurve(10),12,15, false));
        addAction(new OE_actionNewJoinCurve(getCurve(document->curves.size()-1),getCurve(document->curves.size()-2)));
        addAction(new OE_actionNewSubCurve(getCurve(10),4, 5, true));
        addAction(new OE_actionNewSubCurve(getCurve(10),8,11, true));
        addAction(new OE_actionNewJoinCurve(getCurve(document->curves.size()-1),getCurve(document->curves.size()-2)));
        addAction(new OE_actionNewBirailStitch(getCurve(document->curves.size()-1), getCurve(document->curves.size()-4),true,true, -0.2, -0.2, 0.5));
        addAction(new OE_actionNewSubCurve(getCurve(11),6, 7, true));
        addAction(new OE_actionNewSubCurve(getCurve(11),2, 3, true));
        addAction(new OE_actionNewJoinCurve(getCurve(document->curves.size()-1),getCurve(document->curves.size()-2)));
        addAction(new OE_actionNewSubCurve(getCurve(11),4, 5, false));
        addAction(new OE_actionNewBirailStitch(getCurve(document->curves.size()-2),getCurve(document->curves.size()-1),false,false, 0.2, 0.2, 0.5));
        addAction(new OE_actionNewSubCurve(getCurve(11),0, 2, true));
        addAction(new OE_actionNewSubCurve(getCurve(11),7, 9, false));
        addAction(new OE_actionNewBirailStitch(getCurve(document->curves.size()-1), getCurve(document->curves.size()-2),false,false, -0.2, -0.2, 0.5));
        addAction(new OE_actionNewSubCurve(getCurve(13),0, 4, false));
        addAction(new OE_actionNewSubCurve(getCurve(12),0, 4, true));
        addAction(new OE_actionNewBirailStitch(getCurve(document->curves.size()-1), getCurve(document->curves.size()-2),false,false, 0.2, 0.2, 0.5));
        addAction(new OE_actionNewSubCurve(getCurve(14),3, 0, true));
        addAction(new OE_actionNewSubCurve(getCurve(14),1, 2, false));
        addAction(new OE_actionNewBirailStitch(getCurve(document->curves.size()-1), getCurve(document->curves.size()-2),false,false, -0.2, -0.2, 0.5));

        addAction(new OE_actionNewSubCurve(getCurve(16),11, 12, true));
        addAction(new OE_actionNewSubCurve(getCurve(16),0, 2, true));
        addAction(new OE_actionNewJoinCurve(getCurve(document->curves.size()-1),getCurve(document->curves.size()-2)));

        addAction(new OE_actionNewSubCurve(getCurve(16),8, 10, false));
        addAction(new OE_actionNewSubCurve(getCurve(15),4, 0, false));
        addAction(new OE_actionNewJoinCurve(getCurve(document->curves.size()-1),getCurve(document->curves.size()-2)));
        addAction(new OE_actionNewSubCurve(getCurve(16),3, 4, false));
        addAction(new OE_actionNewJoinCurve(getCurve(document->curves.size()-1),getCurve(document->curves.size()-2)));
        addAction(new OE_actionNewBirailStitch(getCurve(document->curves.size()-6), getCurve(document->curves.size()-1),true,true, -0.2, -0.2, 0.5));

        addAction(new OE_actionNewSubCurve(getCurve(15),0, 4, true));
        addAction(new OE_actionNewSubCurve(getCurve(16),4, 8, false));
        addAction(new OE_actionNewBirailStitch(getCurve(document->curves.size()-1), getCurve(document->curves.size()-2),false,false, -0.2, -0.2, 0.5));
        addAction(new OE_actionNewSubCurve(getCurve(18),6, 7, true));
        addAction(new OE_actionNewSubCurve(getCurve(17),2, 3, true));
        addAction(new OE_actionNewJoinCurve(getCurve(document->curves.size()-1),getCurve(document->curves.size()-2)));
        addAction(new OE_actionNewSubCurve(getCurve(18),2, 3, true));
        addAction(new OE_actionNewJoinCurve(getCurve(document->curves.size()-1),getCurve(document->curves.size()-2)));
        addAction(new OE_actionNewSubCurve(getCurve(18),4, 5, false));
        addAction(new OE_actionNewBirailStitch(getCurve(document->curves.size()-2),getCurve(document->curves.size()-1),true,true, -0.2, -0.2, 0.5));
		

        addAction(new OE_actionNewSubCurve(getCurve(17),3, 5, true));
        addAction(new OE_actionNewSubCurve(getCurve(17),0, 2, true));
        addAction(new OE_actionNewJoinCurve(getCurve(document->curves.size()-1),getCurve(document->curves.size()-2)));
        addAction(new OE_actionNewSubCurve(getCurve(18),0, 2, false));
        addAction(new OE_actionNewSubCurve(getCurve(18),7, 9, false));
        addAction(new OE_actionNewJoinCurve(getCurve(document->curves.size()-1),getCurve(document->curves.size()-2)));
        addAction(new OE_actionNewBirailStitch(getCurve(document->curves.size()-1), getCurve(document->curves.size()-4),true,true, 0.2, 0.2, 0.5));

        addAction(new OE_actionNewLineStitch(getCurve(19),1.2,0.5,1,0));
        addAction(new OE_actionNewLineStitch(getCurve(20),1.2,0.5,1,0));
        addAction(new OE_actionNewLineStitch(getCurve(21),1.2,0.5,1,0));
        addAction(new OE_actionNewLineStitch(getCurve(22),1.2,0.5,1,0));
3dsman's avatar
3dsman committed
		
		//e
        addAction(new OE_actionNewLineStitch(getCurve(41),1.2,0.5,1,0));
        addAction(new OE_actionNewLineStitch(getCurve(40),1.2,0.5,1,0));
3dsman's avatar
3dsman committed
		//c
        addAction(new OE_actionNewLineStitch(getCurve(38),1.2,0.5,1,0));
3dsman's avatar
3dsman committed
		//a
        addAction(new OE_actionNewLineStitch(getCurve(37),1.2,0.5,1,0));
        addAction(new OE_actionNewLineStitch(getCurve(36),1.2,0.5,1,0));
3dsman's avatar
3dsman committed
		//p
        addAction(new OE_actionNewLineStitch(getCurve(35),1.2,0.5,1,0));
3dsman's avatar
3dsman committed
		//s
        addAction(new OE_actionNewLineStitch(getCurve(34),1.2,0.5,1,0));
3dsman's avatar
3dsman committed
		//r
        addAction(new OE_actionNewLineStitch(getCurve(33),1.2,0.5,1,0));
        addAction(new OE_actionNewLineStitch(getCurve(32),1.2,0.5,1,0));
3dsman's avatar
3dsman committed
		//e
        addAction(new OE_actionNewLineStitch(getCurve(31),1.2,0.5,1,0));
        addAction(new OE_actionNewLineStitch(getCurve(30),1.2,0.5,1,0));
3dsman's avatar
3dsman committed
		//k
        addAction(new OE_actionNewLineStitch(getCurve(29),1.2,0.5,1,0));
        addAction(new OE_actionNewLineStitch(getCurve(28),1.2,0.5,1,0));
3dsman's avatar
3dsman committed
		//c
        addAction(new OE_actionNewLineStitch(getCurve(27),1.2,0.5,1,0));
3dsman's avatar
3dsman committed
		//a
        addAction(new OE_actionNewLineStitch(getCurve(26),1.2,0.5,1,0));
        addAction(new OE_actionNewLineStitch(getCurve(25),1.2,0.5,1,0));
3dsman's avatar
3dsman committed
		//h
        addAction(new OE_actionNewLineStitch(getCurve(39),1.2,0.5,1,0));
        addAction(new OE_actionNewLineStitch(getCurve(24),1.2,0.5,1,0));
        addAction(new OE_actionNewLineStitch(getCurve(23),1.2,0.5,1,0));
3dsman's avatar
3dsman committed
		return true;
	}
	// fin motifs
	return false;
}

3dsman's avatar
3dsman committed
bool OE_controller::testOpenEmbroider()
{
	if (document)
	{
        OE_actions::curDocument = document;

        addAction(new OE_actionNewThread(new OE_thread(220,120,20,2)));

        //addThread(220,120,20,2);
3dsman's avatar
3dsman committed
		document->setZeroPoint(vector_2d(255,267));
		unsigned curStitch = 0;	
		
		//fil
        addAction(new OE_actionNewLineStitch(getCurve(3),3,0.4,1,0));
        addAction(new OE_actionNewLineStitch(getCurve(4),3,0.4,1,0));
        //addLineStitch(getCurve(3),3,0.4,1,0);
        //addLineStitch(getCurve(4),3,0.4,1,0);
3dsman's avatar
3dsman committed
		//dents
        addAction(new OE_actionNewBirailStitch(getCurve(17),getCurve(18),true,false, 0, 0, 0.5));
        addAction(new OE_actionNewBirailStitch(getCurve(15),getCurve(16),true,false, 0, 0, 0.5));
3dsman's avatar
3dsman committed
		
        addAction(new OE_actionNewBirailStitch(getCurve(19),getCurve(20),true,false, 0, 0, 0.5));
3dsman's avatar
3dsman committed
		
        addAction(new OE_actionNewBirailStitch(getCurve(7),getCurve(8),false,true, 0, 0, 0.5));
        addAction(new OE_actionNewBirailStitch(getCurve(9),getCurve(10),false,true, 0, 0, 0.5));
        addAction(new OE_actionNewBirailStitch(getCurve(11),getCurve(12),false,true, 0, 0, 0.5));
        addAction(new OE_actionNewBirailStitch(getCurve(13),getCurve(14),false,true, 0, 0, 0.5));
3dsman's avatar
3dsman committed
		
		//cercle
3dsman's avatar
3dsman committed
		//addBirailStitch(getCurve(5),getCurve(6),true,true, 0, 0, 0.5);
        addAction(new OE_actionNewBirailStitch(getCurve(5),getCurve(21),true,true, -0.4, -0.6, 0.5));
        addAction(new OE_actionNewBirailStitch(getCurve(21),getCurve(6),true,true, -0.6, -0.4, 0.5));
3dsman's avatar
3dsman committed
		//setStitchMaxLen(document->stitchs.size()-1,4);
		
		
		//pointe
        addAction(new OE_actionNewSubCurve(getCurve(2),0, 1, true));
        addAction(new OE_actionNewSubCurve(getCurve(2),1, 2, false));
        addAction(new OE_actionNewBirailStitch(getCurve(document->curves.size()-2),getCurve(document->curves.size()-1),false,false, 0, 0, 0.5));
        addAction(new OE_actionSetStitchThread(getStitch(document->stitchs.size()-1), getThread(1)));
3dsman's avatar
3dsman committed
		
		
		
		//aiguille
        addAction(new OE_actionNewSubCurve(getCurve(1),3, 3, false));
        addAction(new OE_actionNewSubCurve(getCurve(0),3.5, 5, true));
        addAction(new OE_actionNewJoinCurve(getCurve(document->curves.size()-1),getCurve(document->curves.size()-2)));
3dsman's avatar
3dsman committed
		
        addAction(new OE_actionNewSubCurve(getCurve(document->curves.size()-1),0, 5.2, false));
        addAction(new OE_actionNewSubCurve(getCurve(0),0, 2.4, false));
        addAction(new OE_actionNewBirailStitch(getCurve(document->curves.size()-2),getCurve(document->curves.size()-1),false,false, 0, 0, 0.5));
        addAction(new OE_actionSetStitchThread(getStitch(document->stitchs.size()-1), getThread(1)));
        addAction(new OE_actionNewSubCurve(getCurve(document->curves.size()-3),5.2, 7, false));
        addAction(new OE_actionNewSubCurve(getCurve(0),2.4, 3.5, false));
        addAction(new OE_actionNewBirailStitch(getCurve(document->curves.size()-2),getCurve(document->curves.size()-1),false,false, 0, 0, 0.5));
        addAction(new OE_actionSetStitchThread(getStitch(document->stitchs.size()-1), getThread(1)));

3dsman's avatar
3dsman committed
		return true;
	}
	return false;
}

bool OE_controller::testStarTrek()
{
	if (document)
	{

        OE_actions::curDocument = document;

        addAction(new OE_actionNewThread(new OE_thread(220,120,20,2)));

3dsman's avatar
3dsman committed
		document->setZeroPoint(vector_2d(45,60));
3dsman's avatar
3dsman committed
		
        addAction(new OE_actionNewLineStitch(getCurve(0),4,1,0,0.5));
3dsman's avatar
3dsman committed
		
        addAction(new OE_actionNewLineStitch(getCurve(0),4,1,0,0.5));
3dsman's avatar
3dsman committed
		
        addAction(new OE_actionNewLineStitch(getCurve(0),3,2,4,-0.4));
3dsman's avatar
3dsman committed
		
        addAction(new OE_actionNewSubCurve(getCurve(1),1, 3, false));
        addAction(new OE_actionNewSubCurve(getCurve(2),0, 4, false));

        addAction(new OE_actionNewBirailStitch(getCurve(document->curves.size()-2),getCurve(document->curves.size()-1),false,false, 0.2, 0.2, 0.5));
3dsman's avatar
3dsman committed
		
        addAction(new OE_actionNewSubCurve(getCurve(1),3, 4, false));
        addAction(new OE_actionNewSubCurve(getCurve(1),4, 5, true));
        addAction(new OE_actionNewBirailStitch(getCurve(document->curves.size()-2),getCurve(document->curves.size()-1),false,false, 0.2, 0.2, 0.5));
3dsman's avatar
3dsman committed
		
        addAction(new OE_actionNewSubCurve(getCurve(1),5, 7, false));
        addAction(new OE_actionNewSubCurve(getCurve(3),0, 3, false));
3dsman's avatar
3dsman committed
		
        addAction(new OE_actionNewBirailStitch(getCurve(document->curves.size()-2),getCurve(document->curves.size()-1),false,false, 0.2, 0.2, 0.5));
3dsman's avatar
3dsman committed
		
        addAction(new OE_actionNewSubCurve(getCurve(1),7, 8, false));
        addAction(new OE_actionNewSubCurve(getCurve(1),0, 1, true));
        addAction(new OE_actionNewBirailStitch(getCurve(document->curves.size()-2),getCurve(document->curves.size()-1),false,false, 0.2, 0.2, 0.5));
3dsman's avatar
3dsman committed

        addAction(new OE_actionNewBirailStitch(getCurve(4),getCurve(5),true,false, 0.2, 0.2, 0.5));
        addAction(new OE_actionNewBirailStitch(getCurve(6),getCurve(7),true,false, 0.2, 0.2, 0.5));
        addAction(new OE_actionNewBirailStitch(getCurve(8),getCurve(9),true,false, 0.2, 0.2, 0.5));
        addAction(new OE_actionNewBirailStitch(getCurve(10),getCurve(11),false,true, 0.2, 0.2, 0.5));


        addAction(new OE_actionNewLineStitch(getCurve(12),2,0.6,1,0));
3dsman's avatar
3dsman committed

		return true;
	}
	return false;
}

bool OE_controller::setDefault()
{
	OE_birailstitch::defaultLen = 1;
	OE_birailstitch::defaultMaxlen = 0;
OE_curve* OE_controller::getCurve (unsigned index)
{
	if ((document)&&(index<document->curves.size()))
	{
		std::list<OE_curve*>::iterator curve = document->curves.begin();
		for(unsigned i=0; i<index;i++)
			curve++;
		return (*curve);
	}

	return nullptr;
}

OE_pointcurve* OE_controller::getPointcurve (unsigned index)
{
        return (dynamic_cast<OE_pointcurve*> (getCurve (index)));
}

OE_subcurve* OE_controller::getSubcurve (unsigned index)
{
        return (dynamic_cast<OE_subcurve*> (getCurve (index)));
}

OE_joincurve* OE_controller::getJoincurve (unsigned index)
{
        return (dynamic_cast<OE_joincurve*> (getCurve (index)));
OE_stitchs* OE_controller::getStitch (unsigned index)
{
	if ((document)&&(index<document->stitchs.size()))
	{
		std::list<OE_stitchs*>::iterator stitch = document->stitchs.begin();
		for(unsigned i=0; i<index;i++)
			stitch++;
		return (*stitch);
OE_linestitch* OE_controller::getLineStitch (unsigned index)
		return (dynamic_cast<OE_linestitch*> (getStitch(index)));
OE_metaLinestitch* OE_controller::getMetaLineStitch (unsigned index)
{
        return (dynamic_cast<OE_metaLinestitch*> (getStitch(index)));
}

OE_birailstitch* OE_controller::getBirailStitch (unsigned index)
{
        return (dynamic_cast<OE_birailstitch*> (getStitch(index)));
OE_linkstitch* OE_controller::getLinkStitch (unsigned index)
{
        return (dynamic_cast<OE_linkstitch*> (getStitch(index)));
OE_thread* OE_controller::getThread (unsigned index)
{
	if ((document)&&(index<document->threads.size()))
	{
		std::list<OE_thread*>::iterator thread = document->threads.begin();
		for(unsigned i=0; i<index;i++)
			thread++;
		return (*thread);
bool OE_controller::addAction(OE_actions * action)
{
    if (document)
    {
        //flush the undoActionStack
        while(!(document->undoActionsStack.empty()))
            delete document->undoActionsStack.back();
            document->undoActionsStack.pop_back();
        }

        //push the new action to the active stack
        document->activeActionsStack.push_front(action);
    }
}

bool OE_controller::undoAction()
{
    if (document && document->activeActionsStack.size())
    {
        // get and undo the last action from active action stack
        OE_actions * action = *document->activeActionsStack.begin();
        if (action)
        {
            action->undo();

            // move the action from active to undo action stack
            document->activeActionsStack.pop_front();
            document->undoActionsStack.push_front(action);
        }
    }
}

bool OE_controller::redoAction()
{
    if (document && document->undoActionsStack.size())
    {
        // get and redo the last action from undo action stack
        OE_actions * action = *document->undoActionsStack.begin();

        action->redo();

        // move the action from undo to active action stack
        document->undoActionsStack.pop_front();
        document->activeActionsStack.push_front(action);
    }
}

bool OE_controller::selectCurve( OE_curve* curve, bool replace)
    std::list<OE_curve*> tmp;
    tmp.push_back(curve);

    return selectCurves(tmp, replace);
bool OE_controller::selectCurves( std::list<OE_curve*> curves, bool replace)
    if (document)
    {
        addAction(new OE_actionSelectionAddCurves(curves, replace));
        return true;
    }
    return false;
}

3dsman's avatar
3dsman committed
bool OE_controller::unselectCurves( std::list<OE_curve*> curves)
{
    if (document)
    {
        addAction(new OE_actionSelectionRemoveCurves(curves));
        return true;
    }
    return false;
}

bool OE_controller::selectStitch( OE_stitchs* stitch, bool replace)
{
    std::list<OE_stitchs*> tmp;
    tmp.push_back(stitch);

    return selectStitches(tmp, replace);
}

bool OE_controller::selectStitches( std::list<OE_stitchs*> stitches, bool replace)
{
    if (document)
    {
3dsman's avatar
3dsman committed
        addAction(new OE_actionSelectionAddStitches(stitches, replace));
        return true;
    }
    return false;
}

bool OE_controller::unselectStitches( std::list<OE_stitchs*> stitches)
{
    if (document)
    {
        addAction(new OE_actionSelectionRemoveStitches(stitches));
bool OE_controller::editLastAction(vector_2d offset)
{
    if (document && document->activeActionsStack.size())
    {
        // get and undo the last action from active action stack
        OE_actions * action = *document->activeActionsStack.begin();
        if (action)
        {
            OE_actionMovePointCurve* actionMovePointCurve = dynamic_cast<OE_actionMovePointCurve*>(action);
            if (actionMovePointCurve)
                actionMovePointCurve->setMove( offset);
        }
    }
}

bool OE_controller::editLastAction(float val)
{
    if (document && document->activeActionsStack.size())
    {
        // get and undo the last action from active action stack
        OE_actions * action = *document->activeActionsStack.begin();
        if (action)
        {
            OE_actionSetSubcurvePos* actionSetSubcurvePos = dynamic_cast<OE_actionSetSubcurvePos*>(action);
            if (actionSetSubcurvePos)
                actionSetSubcurvePos->setPos(val);
            /*OE_actionSetMetaLineStitchSub* actionSetMetaLineStitchSub = dynamic_cast<OE_actionSetMetaLineStitchSub*>(action);
            if (actionSetMetaLineStitchSub)
                actionSetMetaLineStitchSub->setPos( val);*/
        }
    }
}
/*
bool OE_controller::refreshStitchs()
{
	if (document)
	{
		std::list<OE_stitchs*>::iterator stitch = document->stitchs.begin();
		
		while (stitch != document->stitchs.end())
bool OE_controller::setThreadColor(unsigned index, unsigned char r, unsigned char g, unsigned char b )
{
	OE_thread * tmpthread = getThread(index);
	if (!tmpthread)return false;
	
	tmpthread->setColor(r, g, b);

	return true;
}

bool OE_controller::setThreadWidth(unsigned index, float width)
{
	OE_thread * tmpthread = getThread(index);
	if (!tmpthread)return false;
	
	tmpthread->setWidth(width);

	return true;
}
bool OE_controller::setsubcurvePos( unsigned index, float start, float end)
{
	OE_subcurve * tmpcurve = getSubcurve(index);
	if (!tmpcurve)return false;
	
	tmpcurve->setStart(start);
	tmpcurve->setEnd(end);
	return true;
}

bool OE_controller::generateInstructions()
{
		
	if (document)
	{
		document->instPoints.clear();
		for (unsigned i = 0; i < document->instCommand.size(); i++) {
			delete (document->instCommand.at(i));
		}
		document->instCommand.clear();
		
		std::list<OE_stitchs*>::iterator stitch = document->stitchs.begin();
		
		OE_thread * instThread = (*stitch)->getThread();
		if(!instThread)
			instThread = document->threads.front();
		
		document->instCommand.push_back(new OE_start(document->instPoints.size(), instThread->getColor()));
		
		while (stitch != document->stitchs.end())
		{
            //(*stitch)->refresh();
			if((*stitch)->check()&&(*stitch)->getNpts()>0)
			{
				OE_thread * curThread = (*stitch)->getThread();
				if (!curThread)
					curThread = document->threads.front();
				if ((curThread!=instThread))
					instThread = curThread;
					document->instCommand.push_back(new OE_waitcolor(document->instPoints.size(), instThread->getColor()));
				}								
				std::vector<vector_2d> points = (*stitch)->getPoints();
			
				document->instPoints.insert( document->instPoints.end(), points.begin(), points.end() );

			}
			stitch++;
		}
		
		vector_2d zero = document->getZeroPoint();
		float size = document->getPulseByMm();
		for ( unsigned i = 0; i < document->instPoints.size(); i++)
		{
			document->instPoints.at(i) = (document->instPoints.at(i) - zero)*size;
		}
	}
	return true;
}

3dsman's avatar
3dsman committed
uint8_t int7(int i) // transform int32 to int7
{
	if (i>=0)
		return i;
	int8_t i8 = 2*i;
	return (*(uint8_t*)&i8)>>1;
}

void OE_controller::sendInstPoint()
{
	static bool running = false;
	static int oldX, oldY;
	static unsigned sndPoint;
	static uint8_t dxys[32];
	static bool started = false;

	if (!running)
	{
3dsman's avatar
3dsman committed
		std::cout << "sendInstPoint x " << document->instPoints.size() <<std::endl;
		comm.stopMove();
		if (!comm.ack()) std::cerr << "Can't set pos" << std::endl;
		comm.setPos(0, 0);
		if (!comm.ack()) std::cerr << "Can't set pos" << std::endl;
		comm.setNPoint(0);
		if (!comm.ack()) std::cerr << "Can't set point" << std::endl;
		comm.deletePoints();
		if (!comm.ack()) std::cerr << "Can't delete points" << std::endl;
		oldX = oldY = 0;
		sndPoint = 0;
		running = true;
	} else {
		document->curPoint = comm.info.nPoint;
		int x, y, dx, dy, bigdx, bigdy;
		unsigned cmdPoints = 0;
3dsman's avatar
3dsman committed
		if (!started && comm.info.spaceSize && (comm.info.freeSpace<512) ) {
			comm.startMove();
			if (!comm.ack()) std::cerr << "Can't start" << std::endl;
			started = true;
		}
		if ((sndPoint<document->instPoints.size()) && comm.info.spaceSize && (comm.info.freeSpace>64))
3dsman's avatar
3dsman committed
		{
			while (sndPoint<document->instPoints.size())
3dsman's avatar
3dsman committed
			{
				x = roundf(document->instPoints.at(sndPoint).x);
				y = roundf(-document->instPoints.at(sndPoint).y);
				dx = x-oldX;
				dy = y-oldY;
				if ((dx<-64)||(63<dx)||(dy<-64)||(63<dy))
3dsman's avatar
3dsman committed
				{
					bigdx = (dx+(dx>=0?0:1))/64;
					bigdy = (dy+(dy>=0?0:1))/64;
					dx -= 64*bigdx;
					dy -= 64*bigdy;
					if (cmdPoints==15)
					{
						comm.addPoints(dxys, 15);
						if (!comm.ack()) std::cerr << "Can't add points" << std::endl;
						return;
					}
					dxys[2*cmdPoints] = 0x80|int7(bigdx);
					dxys[2*cmdPoints+1] = int7(bigdy);
					cmdPoints++;
3dsman's avatar
3dsman committed
				}
				dxys[2*cmdPoints] = int7(dx);
				dxys[2*cmdPoints+1] = int7(dy);
3dsman's avatar
3dsman committed
				cmdPoints++;
				sndPoint++;
				oldX = x;
				oldY = y;
				if (cmdPoints==16)
				{
					comm.addPoints(dxys, 16);
					if (!comm.ack()) std::cerr << "Can't add points" << std::endl;
					return;
				}
3dsman's avatar
3dsman committed
			}
3dsman's avatar
3dsman committed
			{
				comm.addPoints(dxys, cmdPoints);
3dsman's avatar
3dsman committed
				if (!comm.ack()) std::cerr << "Can't add points" << std::endl;
3dsman's avatar
3dsman committed
			}
		}