2013-11-15 13:41:26 +01:00
|
|
|
/************************************************************************
|
2013-09-18 21:16:19 +02:00
|
|
|
**
|
2013-11-15 13:50:05 +01:00
|
|
|
** @file vdrawtool.cpp
|
2014-04-30 07:38:52 +02:00
|
|
|
** @author Roman Telezhynskyi <dismine(at)gmail.com>
|
2013-11-15 13:50:05 +01:00
|
|
|
** @date November 15, 2013
|
2013-09-18 21:16:19 +02:00
|
|
|
**
|
2013-11-15 13:41:26 +01:00
|
|
|
** @brief
|
|
|
|
** @copyright
|
|
|
|
** This source code is part of the Valentine project, a pattern making
|
|
|
|
** program, whose allow create and modeling patterns of clothing.
|
|
|
|
** Copyright (C) 2013 Valentina project
|
|
|
|
** <https://bitbucket.org/dismine/valentina> All Rights Reserved.
|
2013-09-18 21:16:19 +02:00
|
|
|
**
|
2013-11-15 13:41:26 +01:00
|
|
|
** Valentina is free software: you can redistribute it and/or modify
|
2013-09-18 21:16:19 +02:00
|
|
|
** it under the terms of the GNU General Public License as published by
|
|
|
|
** the Free Software Foundation, either version 3 of the License, or
|
|
|
|
** (at your option) any later version.
|
|
|
|
**
|
2013-10-27 13:36:29 +01:00
|
|
|
** Valentina is distributed in the hope that it will be useful,
|
2013-09-18 21:16:19 +02:00
|
|
|
** 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 Valentina. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
**
|
2013-11-15 13:41:26 +01:00
|
|
|
*************************************************************************/
|
2013-09-18 21:16:19 +02:00
|
|
|
|
2013-09-10 14:29:06 +02:00
|
|
|
#include "vdrawtool.h"
|
|
|
|
|
2014-05-30 11:33:30 +02:00
|
|
|
#include <qmuparsererror.h>
|
2014-06-08 20:10:57 +02:00
|
|
|
#include "dialogs/tools/dialogeditwrongformula.h"
|
|
|
|
#include "container/calculator.h"
|
2014-06-09 19:39:18 +02:00
|
|
|
#include "../../undocommands/addtocalc.h"
|
2014-06-11 18:32:32 +02:00
|
|
|
#include "../../undocommands/savetooloptions.h"
|
2014-05-30 11:33:30 +02:00
|
|
|
|
2013-10-16 11:17:34 +02:00
|
|
|
qreal VDrawTool::factor = 1;
|
|
|
|
|
2014-05-02 13:11:30 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2014-05-30 11:12:20 +02:00
|
|
|
/**
|
|
|
|
* @brief VDrawTool constructor.
|
|
|
|
* @param doc dom document container.
|
|
|
|
* @param data container with variables.
|
|
|
|
* @param id object id in container.
|
|
|
|
*/
|
2014-02-25 15:40:24 +01:00
|
|
|
VDrawTool::VDrawTool(VPattern *doc, VContainer *data, quint32 id)
|
2014-01-02 16:50:01 +01:00
|
|
|
:VAbstractTool(doc, data, id), ignoreContextMenuEvent(false), ignoreFullUpdate(false),
|
2014-02-26 10:22:05 +01:00
|
|
|
nameActivDraw(doc->GetNameActivDraw()), dialog(nullptr)
|
2013-11-04 21:35:15 +01:00
|
|
|
{
|
2014-06-06 17:06:41 +02:00
|
|
|
connect(this->doc, &VPattern::ChangedActivPP, this, &VDrawTool::ChangedActivDraw);
|
2014-02-25 15:02:09 +01:00
|
|
|
connect(this->doc, &VPattern::ChangedNameDraw, this, &VDrawTool::ChangedNameDraw);
|
|
|
|
connect(this->doc, &VPattern::ShowTool, this, &VDrawTool::ShowTool);
|
2013-09-10 14:29:06 +02:00
|
|
|
}
|
|
|
|
|
2014-05-02 13:11:30 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2014-02-06 14:57:23 +01:00
|
|
|
VDrawTool::~VDrawTool()
|
|
|
|
{
|
|
|
|
delete dialog;
|
|
|
|
}
|
|
|
|
|
2014-05-02 13:11:30 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2014-05-30 11:12:20 +02:00
|
|
|
/**
|
|
|
|
* @brief ShowTool highlight tool.
|
|
|
|
* @param id object id in container.
|
|
|
|
* @param color highlight color.
|
|
|
|
* @param enable enable or disable highlight.
|
|
|
|
*/
|
2014-02-25 15:40:24 +01:00
|
|
|
void VDrawTool::ShowTool(quint32 id, Qt::GlobalColor color, bool enable)
|
2013-11-04 21:35:15 +01:00
|
|
|
{
|
2013-09-10 14:29:06 +02:00
|
|
|
Q_UNUSED(id);
|
|
|
|
Q_UNUSED(color);
|
|
|
|
Q_UNUSED(enable);
|
|
|
|
}
|
|
|
|
|
2014-05-02 13:11:30 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2014-05-30 11:12:20 +02:00
|
|
|
/**
|
|
|
|
* @brief ChangedActivDraw disable or enable context menu after change active pattern peace.
|
|
|
|
* @param newName new name active pattern peace. name new active pattern peace.
|
|
|
|
*/
|
2013-11-06 22:11:12 +01:00
|
|
|
void VDrawTool::ChangedActivDraw(const QString &newName)
|
2013-11-04 21:35:15 +01:00
|
|
|
{
|
|
|
|
if (nameActivDraw == newName)
|
|
|
|
{
|
2013-09-10 14:29:06 +02:00
|
|
|
ignoreContextMenuEvent = false;
|
2013-11-04 21:35:15 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-09-10 14:29:06 +02:00
|
|
|
ignoreContextMenuEvent = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-02 13:11:30 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2014-05-30 11:12:20 +02:00
|
|
|
/**
|
|
|
|
* @brief ChangedNameDraw save new name active pattern peace.
|
|
|
|
* @param oldName old name.
|
|
|
|
* @param newName new name active pattern peace. new name.
|
|
|
|
*/
|
2013-11-06 22:11:12 +01:00
|
|
|
void VDrawTool::ChangedNameDraw(const QString &oldName, const QString &newName)
|
2013-11-04 21:35:15 +01:00
|
|
|
{
|
|
|
|
if (nameActivDraw == oldName)
|
|
|
|
{
|
2013-09-10 14:29:06 +02:00
|
|
|
nameActivDraw = newName;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-02 13:11:30 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2014-05-30 11:12:20 +02:00
|
|
|
/**
|
2014-06-11 18:38:09 +02:00
|
|
|
* @brief FullUpdateFromGuiOk refresh tool data after change in options.
|
2014-05-30 11:12:20 +02:00
|
|
|
* @param result keep result working dialog.
|
|
|
|
*/
|
2014-06-11 18:38:09 +02:00
|
|
|
void VDrawTool::FullUpdateFromGuiOk(int result)
|
2014-02-06 14:57:23 +01:00
|
|
|
{
|
|
|
|
if (result == QDialog::Accepted)
|
|
|
|
{
|
2014-06-11 18:45:39 +02:00
|
|
|
SaveDialogChange();
|
2014-02-06 14:57:23 +01:00
|
|
|
}
|
2014-06-12 16:29:08 +02:00
|
|
|
delete dialog;
|
|
|
|
dialog=nullptr;
|
2014-02-06 14:57:23 +01:00
|
|
|
}
|
|
|
|
|
2014-05-25 21:54:17 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2014-06-11 18:45:39 +02:00
|
|
|
/**
|
|
|
|
* @brief FullUpdateFromGuiApply refresh tool data after change in options but do not delete dialog
|
|
|
|
*/
|
2014-05-25 21:54:17 +02:00
|
|
|
void VDrawTool::FullUpdateFromGuiApply()
|
2014-06-11 18:45:39 +02:00
|
|
|
{
|
|
|
|
SaveDialogChange();
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VDrawTool::SaveDialogChange()
|
2014-05-25 21:54:17 +02:00
|
|
|
{
|
2014-06-11 18:32:32 +02:00
|
|
|
QDomElement oldDomElement = doc->elementById(QString().setNum(id));
|
|
|
|
if (oldDomElement.isElement())
|
2014-05-25 21:54:17 +02:00
|
|
|
{
|
2014-06-11 18:32:32 +02:00
|
|
|
QDomElement newDomElement = oldDomElement.cloneNode().toElement();
|
|
|
|
SaveDialog(newDomElement);
|
2014-05-25 21:54:17 +02:00
|
|
|
|
2014-06-11 18:32:32 +02:00
|
|
|
SaveToolOptions *saveOptions = new SaveToolOptions(oldDomElement, newDomElement, doc, id);
|
|
|
|
connect(saveOptions, &SaveToolOptions::NeedLiteParsing, doc, &VPattern::LiteParseTree);
|
|
|
|
qApp->getUndoStack()->push(saveOptions);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
qDebug()<<"Can't find tool with id ="<< id << Q_FUNC_INFO;
|
2014-05-25 21:54:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-26 13:48:42 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2014-06-13 19:02:41 +02:00
|
|
|
/**
|
|
|
|
* @brief DialogLinkDestroy removes dialog pointer
|
|
|
|
*/
|
2014-05-26 13:48:42 +02:00
|
|
|
void VDrawTool::DialogLinkDestroy()
|
|
|
|
{
|
|
|
|
this->dialog=nullptr;
|
|
|
|
}
|
|
|
|
|
2014-05-02 13:11:30 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2014-05-30 11:12:20 +02:00
|
|
|
/**
|
|
|
|
* @brief SetFactor set current scale factor of scene.
|
|
|
|
* @param factor scene scale factor.
|
|
|
|
*/
|
2013-11-04 21:35:15 +01:00
|
|
|
void VDrawTool::SetFactor(qreal factor)
|
|
|
|
{
|
|
|
|
if (factor <= 2 && factor >= 0.5)
|
|
|
|
{
|
2013-10-16 11:17:34 +02:00
|
|
|
this->factor = factor;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-02 13:11:30 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2014-05-30 11:33:30 +02:00
|
|
|
/**
|
|
|
|
* @brief CheckFormula check formula.
|
|
|
|
*
|
|
|
|
* Try calculate formula. If find error show dialog that allow user try fix formula. If user can't throw exception. In
|
|
|
|
* successes case return result calculation and fixed formula string. If formula ok don't touch formula.
|
|
|
|
* @param formula [in|out] string with formula.
|
|
|
|
* @param data [in] container with variables. Need for math parser.
|
|
|
|
* @throw QmuParserError.
|
|
|
|
* @return result of calculation formula.
|
|
|
|
*/
|
|
|
|
qreal VDrawTool::CheckFormula(QString &formula, VContainer *data)
|
|
|
|
{
|
|
|
|
qreal result = 0;
|
|
|
|
Calculator *cal = nullptr;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
cal = new Calculator(data);
|
|
|
|
result = cal->EvalFormula(formula);
|
|
|
|
delete cal;
|
|
|
|
}
|
|
|
|
catch(qmu::QmuParserError &e)
|
|
|
|
{
|
2014-06-02 09:43:27 +02:00
|
|
|
Q_UNUSED(e)
|
2014-05-30 11:33:30 +02:00
|
|
|
delete cal;
|
|
|
|
DialogEditWrongFormula *dialog = new DialogEditWrongFormula(data);
|
|
|
|
dialog->setFormula(formula);
|
|
|
|
if (dialog->exec() == QDialog::Accepted)
|
|
|
|
{
|
|
|
|
formula = dialog->getFormula();
|
|
|
|
//Need delete dialog here because parser in dialog don't allow use correct separator for parsing here.
|
|
|
|
//Don't know why.
|
|
|
|
delete dialog;
|
2014-05-30 21:03:17 +02:00
|
|
|
Calculator *cal1 = new Calculator(data);
|
|
|
|
result = cal1->EvalFormula(formula);
|
|
|
|
delete cal1;//Here can be memory leak, but dialog already check this formula and probability very low.
|
2014-05-30 11:33:30 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
delete dialog;
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2014-05-30 11:12:20 +02:00
|
|
|
/**
|
|
|
|
* @brief AddToCalculation add tool to calculation tag in pattern file.
|
|
|
|
* @param domElement tag in xml tree.
|
|
|
|
*/
|
2013-11-04 21:35:15 +01:00
|
|
|
void VDrawTool::AddToCalculation(const QDomElement &domElement)
|
|
|
|
{
|
2014-06-09 19:39:18 +02:00
|
|
|
AddToCalc *addToCal = new AddToCalc(domElement, doc);
|
|
|
|
connect(addToCal, &AddToCalc::NeedFullParsing, doc, &VPattern::NeedFullParsing);
|
2014-06-06 12:36:34 +02:00
|
|
|
qApp->getUndoStack()->push(addToCal);
|
2013-09-10 14:29:06 +02:00
|
|
|
}
|