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.
|
2015-02-27 11:27:48 +01:00
|
|
|
** Copyright (C) 2013-2015 Valentina project
|
2013-11-15 13:41:26 +01:00
|
|
|
** <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"
|
|
|
|
|
2015-06-18 19:23:24 +02:00
|
|
|
#include "../../../qmuparser/qmuparsererror.h"
|
|
|
|
#include "../../dialogs/support/dialogeditwrongformula.h"
|
|
|
|
#include "../../dialogs/support/dialogundo.h"
|
|
|
|
#include "../../../vpatterndb/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"
|
2015-06-18 19:23:24 +02:00
|
|
|
#include "../../../ifc/exception/vexceptionundo.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.
|
|
|
|
*/
|
2015-06-18 19:23:24 +02:00
|
|
|
VDrawTool::VDrawTool(VAbstractPattern *doc, VContainer *data, quint32 id, QObject *parent)
|
2015-04-29 16:57:02 +02:00
|
|
|
:VAbstractTool(doc, data, id, parent), nameActivDraw(doc->GetNameActivPP()),
|
2016-05-19 12:11:50 +02:00
|
|
|
dialog(nullptr), typeLine(TypeLineLine), enabled(true)
|
2013-11-04 21:35:15 +01:00
|
|
|
{
|
2015-06-18 19:23:24 +02:00
|
|
|
connect(this->doc, &VAbstractPattern::ChangedActivPP, this, &VDrawTool::ChangedActivDraw);
|
|
|
|
connect(this->doc, &VAbstractPattern::ChangedNameDraw, this, &VDrawTool::ChangedNameDraw);
|
|
|
|
connect(this->doc, &VAbstractPattern::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 enable enable or disable highlight.
|
|
|
|
*/
|
2015-02-07 16:18:18 +01:00
|
|
|
void VDrawTool::ShowTool(quint32 id, bool enable)
|
2013-11-04 21:35:15 +01:00
|
|
|
{
|
2013-09-10 14:29:06 +02:00
|
|
|
Q_UNUSED(id);
|
|
|
|
Q_UNUSED(enable);
|
|
|
|
}
|
|
|
|
|
2015-05-23 16:42:53 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
|
|
* @brief ChangedActivDraw disable or enable context menu after change active pattern peace.
|
|
|
|
* @param newName new name active pattern peace.
|
|
|
|
*/
|
|
|
|
void VDrawTool::ChangedActivDraw(const QString &newName)
|
|
|
|
{
|
2015-05-25 08:24:16 +02:00
|
|
|
Disable(!(nameActivDraw == newName), newName);
|
2015-05-23 16:42:53 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2015-04-01 19:08:35 +02:00
|
|
|
qCDebug(vTool, "Saving tool options after using dialog");
|
2015-02-11 11:43:50 +01:00
|
|
|
QDomElement oldDomElement = doc->elementById(id);
|
2014-06-11 18:32:32 +02:00
|
|
|
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);
|
2015-06-16 17:01:17 +02:00
|
|
|
connect(saveOptions, &SaveToolOptions::NeedLiteParsing, doc, &VAbstractPattern::LiteParseTree);
|
2014-06-11 18:32:32 +02:00
|
|
|
qApp->getUndoStack()->push(saveOptions);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-04-01 19:08:35 +02:00
|
|
|
qCDebug(vTool, "Can't find tool with id = %u", id);
|
2014-05-25 21:54:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-30 21:58:31 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
|
|
* @brief AddToFile add tag with informations about tool into file.
|
|
|
|
*/
|
|
|
|
void VDrawTool::AddToFile()
|
|
|
|
{
|
|
|
|
QDomElement domElement = doc->createElement(getTagName());
|
|
|
|
QSharedPointer<VGObject> obj = VAbstractTool::data.GetGObject(id);
|
|
|
|
SaveOptions(domElement, obj);
|
|
|
|
AddToCalculation(domElement);
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
|
|
* @brief RefreshDataInFile refresh attributes in file. If attributes don't exist create them.
|
|
|
|
*/
|
|
|
|
void VDrawTool::RefreshDataInFile()
|
|
|
|
{
|
2015-02-11 11:43:50 +01:00
|
|
|
QDomElement domElement = doc->elementById(id);
|
2014-08-30 21:58:31 +02:00
|
|
|
if (domElement.isElement())
|
|
|
|
{
|
|
|
|
QSharedPointer<VGObject> obj = VAbstractTool::data.GetGObject(id);
|
|
|
|
SaveOptions(domElement, obj);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-04-01 19:08:35 +02:00
|
|
|
qCDebug(vTool, "Can't find tool with id = %u", id);
|
2014-08-30 21:58:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-06 10:53:36 +01:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2015-05-28 14:13:37 +02:00
|
|
|
void VDrawTool::SaveOption(QSharedPointer<VGObject> &obj)
|
|
|
|
{
|
|
|
|
qCDebug(vTool, "Saving tool options");
|
|
|
|
QDomElement oldDomElement = doc->elementById(id);
|
|
|
|
if (oldDomElement.isElement())
|
|
|
|
{
|
|
|
|
QDomElement newDomElement = oldDomElement.cloneNode().toElement();
|
|
|
|
|
|
|
|
SaveOptions(newDomElement, obj);
|
|
|
|
|
|
|
|
SaveToolOptions *saveOptions = new SaveToolOptions(oldDomElement, newDomElement, doc, id);
|
2015-06-16 17:01:17 +02:00
|
|
|
connect(saveOptions, &SaveToolOptions::NeedLiteParsing, doc, &VAbstractPattern::LiteParseTree);
|
2015-05-28 14:13:37 +02:00
|
|
|
qApp->getUndoStack()->push(saveOptions);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
qCDebug(vTool, "Can't find tool with id = %u", id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VDrawTool::SaveOptions(QDomElement &tag, QSharedPointer<VGObject> &obj)
|
|
|
|
{
|
|
|
|
Q_UNUSED(obj)
|
|
|
|
|
|
|
|
doc->SetAttribute(tag, VDomDocument::AttrId, id);
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2015-02-06 10:53:36 +01:00
|
|
|
QColor VDrawTool::CorrectColor(const QColor &color) const
|
|
|
|
{
|
|
|
|
if (enabled)
|
|
|
|
{
|
|
|
|
return color;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return Qt::gray;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-25 08:24:16 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
bool VDrawTool::CorrectDisable(bool disable, const QString &namePP) const
|
|
|
|
{
|
|
|
|
if (disable)
|
|
|
|
{
|
|
|
|
return disable;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return !(nameActivDraw == namePP);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-06 18:43:31 +01:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VDrawTool::ReadAttributes()
|
|
|
|
{
|
2015-02-11 11:43:50 +01:00
|
|
|
const QDomElement domElement = doc->elementById(id);
|
2015-02-06 18:43:31 +01:00
|
|
|
if (domElement.isElement())
|
|
|
|
{
|
|
|
|
ReadToolAttributes(domElement);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-04-01 19:08:35 +02:00
|
|
|
qCDebug(vTool, "Can't find tool with id = %u", id);
|
2015-02-06 18:43:31 +01: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)
|
|
|
|
{
|
2015-06-18 19:23:24 +02:00
|
|
|
CheckFactor(this->factor, factor);
|
2013-10-16 11:17:34 +02:00
|
|
|
}
|
|
|
|
|
2015-02-09 10:54:19 +01:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VDrawTool::EnableToolMove(bool move)
|
|
|
|
{
|
|
|
|
Q_UNUSED(move)
|
|
|
|
// Do nothing.
|
|
|
|
}
|
|
|
|
|
2016-03-31 16:01:41 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VDrawTool::DetailsMode(bool mode)
|
|
|
|
{
|
|
|
|
Q_UNUSED(mode)
|
|
|
|
// Do nothing.
|
|
|
|
}
|
|
|
|
|
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.
|
2014-07-29 14:19:11 +02:00
|
|
|
*
|
2014-09-11 18:52:02 +02:00
|
|
|
* @param toolId [in] tool's id.
|
2014-05-30 11:33:30 +02:00
|
|
|
* @param formula [in|out] string with formula.
|
|
|
|
* @param data [in] container with variables. Need for math parser.
|
|
|
|
* @throw QmuParserError.
|
|
|
|
* @return result of calculation formula.
|
|
|
|
*/
|
2014-07-29 14:19:11 +02:00
|
|
|
qreal VDrawTool::CheckFormula(const quint32 &toolId, QString &formula, VContainer *data)
|
2014-05-30 11:33:30 +02:00
|
|
|
{
|
2014-07-08 16:35:46 +02:00
|
|
|
SCASSERT(data != nullptr)
|
2014-05-30 11:33:30 +02:00
|
|
|
qreal result = 0;
|
|
|
|
Calculator *cal = nullptr;
|
|
|
|
try
|
|
|
|
{
|
2015-10-15 12:07:43 +02:00
|
|
|
cal = new Calculator();
|
|
|
|
result = cal->EvalFormula(data->PlainVariables(), formula);
|
2014-05-30 11:33:30 +02:00
|
|
|
delete cal;
|
|
|
|
}
|
2014-06-16 19:18:36 +02:00
|
|
|
catch (qmu::QmuParserError &e)
|
2014-05-30 11:33:30 +02:00
|
|
|
{
|
2014-07-08 16:35:46 +02:00
|
|
|
//Q_UNUSED(e)
|
|
|
|
qDebug() << "\nMath parser error:\n"
|
|
|
|
<< "--------------------------------------\n"
|
|
|
|
<< "Message: " << e.GetMsg() << "\n"
|
|
|
|
<< "Expression: " << e.GetExpr() << "\n"
|
|
|
|
<< "--------------------------------------";
|
2014-05-30 11:33:30 +02:00
|
|
|
delete cal;
|
2014-06-24 10:23:39 +02:00
|
|
|
|
2015-12-22 19:12:09 +01:00
|
|
|
if (qApp->IsAppInGUIMode())
|
2014-05-30 11:33:30 +02:00
|
|
|
{
|
2015-12-22 19:12:09 +01:00
|
|
|
DialogUndo *dialogUndo = new DialogUndo(qApp->getMainWindow());
|
|
|
|
forever
|
2014-06-24 10:23:39 +02:00
|
|
|
{
|
2015-12-22 19:12:09 +01:00
|
|
|
if (dialogUndo->exec() == QDialog::Accepted)
|
2014-06-24 10:23:39 +02:00
|
|
|
{
|
2015-12-22 19:12:09 +01:00
|
|
|
const UndoButton resultUndo = dialogUndo->Result();
|
|
|
|
if (resultUndo == UndoButton::Fix)
|
2015-03-03 16:47:31 +01:00
|
|
|
{
|
2015-12-22 19:12:09 +01:00
|
|
|
DialogEditWrongFormula *dialog = new DialogEditWrongFormula(data, toolId,
|
|
|
|
qApp->getMainWindow());
|
|
|
|
dialog->setWindowTitle(tr("Edit wrong formula"));
|
|
|
|
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. */
|
|
|
|
delete dialog;
|
|
|
|
Calculator *cal1 = new Calculator();
|
|
|
|
result = cal1->EvalFormula(data->PlainVariables(), formula);
|
|
|
|
delete cal1; /* Here can be memory leak, but dialog already check this formula and
|
|
|
|
probability very low. */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
delete dialog;
|
|
|
|
}
|
2015-03-03 16:47:31 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-12-22 19:12:09 +01:00
|
|
|
throw VExceptionUndo(QString("Undo wrong formula %1").arg(formula));
|
2015-03-03 16:47:31 +01:00
|
|
|
}
|
2014-06-24 10:23:39 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-12-22 19:12:09 +01:00
|
|
|
delete dialogUndo;
|
|
|
|
throw;
|
2014-06-24 10:23:39 +02:00
|
|
|
}
|
|
|
|
}
|
2015-12-22 19:12:09 +01:00
|
|
|
delete dialogUndo;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw;
|
2014-05-30 11:33:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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);
|
2015-06-18 19:23:24 +02:00
|
|
|
connect(addToCal, &AddToCalc::NeedFullParsing, doc, &VAbstractPattern::NeedFullParsing);
|
2014-06-06 12:36:34 +02:00
|
|
|
qApp->getUndoStack()->push(addToCal);
|
2013-09-10 14:29:06 +02:00
|
|
|
}
|
2015-02-03 15:46:14 +01:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
QString VDrawTool::getLineType() const
|
|
|
|
{
|
|
|
|
return typeLine;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VDrawTool::SetTypeLine(const QString &value)
|
|
|
|
{
|
|
|
|
typeLine = value;
|
|
|
|
|
|
|
|
QSharedPointer<VGObject> obj = VAbstractTool::data.GetGObject(id);
|
|
|
|
SaveOption(obj);
|
|
|
|
}
|