/*
* 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.
*
* This program 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 this program. If not, see .
*/
#include "Rectangle.h"
#include "Utils.h"
#include "Object.h"
#include "Parser.h"
#include
#include "core/memcheck.h"
using namespace merge_lib;
Rectangle::Rectangle(const char * rectangleName):
x1(0),
y1(0),
x2(0),
y2(0),
_rectangleName(rectangleName),
_tm()
{}
Rectangle::Rectangle(const char * rectangleName, const std::string content):
x1(0),
y1(0),
x2(0),
y2(0),
_rectangleName(rectangleName)
{
unsigned int rectanglePosition = Parser::findToken(content,rectangleName);
if((int) rectanglePosition == -1 )
{
std::cerr<<"Unable to find rectangle name "<>x1>>y1>>x2>>y2;
}
}
void Rectangle::appendRectangleToString(std::string & content, const char * delimeter)
{
content.append(_getRectangleAsString(delimeter));
}
const std::string Rectangle::_getRectangleAsString(const char * delimeter)
{
std::string result(_rectangleName);
result.append(" [");
result.append(Utils::doubleToStr(x1));
result.append(delimeter);
result.append(Utils::doubleToStr(y1));
result.append(delimeter);
result.append(Utils::doubleToStr(x2));
result.append(delimeter);
result.append(Utils::doubleToStr(y2));
result.append(" ]\n");
return result;
}
void Rectangle::setNewRectangleName(const char * newName)
{
_rectangleName = newName;
}
void Rectangle::recalculateInternalRectangleCoordinates(const PageTransformations & transformations)
{
TransformationMatrix tempTm;
for(size_t i = 0; i < transformations.size(); ++i)
{
tempTm = transformations[i]->getMatrix();
tempTm.add(_tm);
_tm = tempTm;
}
_tm.recalculateCoordinates(x1, y1);
_tm.recalculateCoordinates(x2, y2);
}
void Rectangle::updateRectangle(Object * objectWithRectangle, const char * delimeter)
{
Object * foundObjectWithRectangle;
unsigned int fake;
objectWithRectangle->findObject(std::string(_rectangleName), foundObjectWithRectangle, fake);
std::string objectContent = foundObjectWithRectangle->getObjectContent();
unsigned int rectanglePosition = objectContent.find(_rectangleName);
unsigned int endOfRectangle = objectContent.find("]", rectanglePosition) + 1;
foundObjectWithRectangle->eraseContent(rectanglePosition, endOfRectangle - rectanglePosition);
foundObjectWithRectangle->insertToContent(rectanglePosition, _getRectangleAsString(delimeter));
// reread the objectContent, since it was changed just above;
objectContent = foundObjectWithRectangle->getObjectContent();
//update matrix
unsigned int startOfAP = Parser::findToken(objectContent,"/AP");
unsigned int endOfAP = objectContent.find(">>", startOfAP);
std::vector