| 1 |
/*************************************************************************** |
| 2 |
* Copyright (c) 2016 Eivind Kvedalen <[email protected]> * |
| 3 |
* * |
| 4 |
* This file is part of the FreeCAD CAx development system. * |
| 5 |
* * |
| 6 |
* This library is free software; you can redistribute it and/or * |
| 7 |
* modify it under the terms of the GNU Library General Public * |
| 8 |
* License as published by the Free Software Foundation; either * |
| 9 |
* version 2 of the License, or (at your option) any later version. * |
| 10 |
* * |
| 11 |
* This library is distributed in the hope that it will be useful, * |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 14 |
* GNU Library General Public License for more details. * |
| 15 |
* * |
| 16 |
* You should have received a copy of the GNU Library General Public * |
| 17 |
* License along with this library; see the file COPYING.LIB. If not, * |
| 18 |
* write to the Free Software Foundation, Inc., 59 Temple Place, * |
| 19 |
* Suite 330, Boston, MA 02111-1307, USA * |
| 20 |
* * |
| 21 |
***************************************************************************/ |
| 22 |
|
| 23 |
#ifndef RENAMEOBJECTIDENTIFIEREXPRESSIONVISITOR_H |
| 24 |
#define RENAMEOBJECTIDENTIFIEREXPRESSIONVISITOR_H |
| 25 |
|
| 26 |
#include "Expression.h" |
| 27 |
|
| 28 |
#include "Document.h" |
| 29 |
|
| 30 |
namespace App |
| 31 |
{ |
| 32 |
|
| 33 |
/** |
| 34 |
* @brief The RenameObjectIdentifierExpressionVisitor class is a functor used to visit each node of |
| 35 |
* an expression, and possibly rename VariableExpression nodes. |
| 36 |
*/ |
| 37 |
|
| 38 |
template<class P> |
| 39 |
class RenameObjectIdentifierExpressionVisitor: public ExpressionModifier<P> |
| 40 |
{ |
| 41 |
public: |
| 42 |
RenameObjectIdentifierExpressionVisitor( |
| 43 |
P& _prop, |
| 44 |
const std::map<ObjectIdentifier, ObjectIdentifier>& _paths, |
| 45 |
const ObjectIdentifier& _owner) |
| 46 |
: ExpressionModifier<P>(_prop) |
| 47 |
, paths(_paths) |
| 48 |
, owner(_owner) |
| 49 |
{} |
| 50 |
|
| 51 |
void visit(Expression& node) override |
| 52 |
{ |
| 53 |
this->renameObjectIdentifier(node, paths, owner); |
| 54 |
} |
| 55 |
|
| 56 |
private: |
| 57 |
const std::map<ObjectIdentifier, ObjectIdentifier>& |
| 58 |
paths; /**< Map with current and new object identifiers */ |
| 59 |
const ObjectIdentifier owner; /**< Owner of expression */ |
| 60 |
}; |
| 61 |
|
| 62 |
template<class P> |
| 63 |
class UpdateElementReferenceExpressionVisitor: public ExpressionModifier<P> |
| 64 |
{ |
| 65 |
public: |
| 66 |
explicit UpdateElementReferenceExpressionVisitor(P& _prop, |
| 67 |
App::DocumentObject* feature = nullptr, |
| 68 |
bool reverse = false) |
| 69 |
: ExpressionModifier<P>(_prop) |
| 70 |
, feature(feature) |
| 71 |
, reverse(reverse) |
| 72 |
{} |
| 73 |
|
| 74 |
void visit(Expression& node) override |
| 75 |
{ |
| 76 |
this->updateElementReference(node, feature, reverse); |
| 77 |
} |
| 78 |
|
| 79 |
private: |
| 80 |
App::DocumentObject* feature; |
| 81 |
bool reverse; |
| 82 |
}; |
| 83 |
|
| 84 |
// Document relabel is not undoable, so we don't derive from |
| 85 |
// ExpressionModifier, hence not calling aboutToSetValue/hasSetValue(). |
| 86 |
// By right, modification of document label should not change evaluation result |
| 87 |
// of any expression. |
| 88 |
class RelabelDocumentExpressionVisitor: public ExpressionVisitor |
| 89 |
{ |
| 90 |
public: |
| 91 |
explicit RelabelDocumentExpressionVisitor(const App::Document& doc) |
| 92 |
: doc(doc) |
| 93 |
{} |
| 94 |
|
| 95 |
void visit(Expression& node) override |
| 96 |
{ |
| 97 |
this->relabeledDocument(node, doc.getOldLabel(), doc.Label.getStrValue()); |
| 98 |
} |
| 99 |
|
| 100 |
private: |
| 101 |
const App::Document& doc; |
| 102 |
}; |
| 103 |
|
| 104 |
template<class P> |
| 105 |
class MoveCellsExpressionVisitor: public ExpressionModifier<P> |
| 106 |
{ |
| 107 |
public: |
| 108 |
MoveCellsExpressionVisitor(P& prop, const CellAddress& address, int rowCount, int colCount) |
| 109 |
: ExpressionModifier<P>(prop) |
| 110 |
, address(address) |
| 111 |
, rowCount(rowCount) |
| 112 |
, colCount(colCount) |
| 113 |
{} |
| 114 |
|
| 115 |
void visit(Expression& node) override |
| 116 |
{ |
| 117 |
this->moveCells(node, address, rowCount, colCount); |
| 118 |
} |
| 119 |
|
| 120 |
private: |
| 121 |
CellAddress address; |
| 122 |
int rowCount; |
| 123 |
int colCount; |
| 124 |
}; |
| 125 |
|
| 126 |
template<class P> |
| 127 |
class OffsetCellsExpressionVisitor: public ExpressionModifier<P> |
| 128 |
{ |
| 129 |
public: |
| 130 |
OffsetCellsExpressionVisitor(P& prop, int rowOffset, int colOffset) |
| 131 |
: ExpressionModifier<P>(prop) |
| 132 |
, rowOffset(rowOffset) |
| 133 |
, colOffset(colOffset) |
| 134 |
{} |
| 135 |
|
| 136 |
void visit(Expression& node) override |
| 137 |
{ |
| 138 |
this->offsetCells(node, rowOffset, colOffset); |
| 139 |
} |
| 140 |
|
| 141 |
private: |
| 142 |
int rowOffset; |
| 143 |
int colOffset; |
| 144 |
}; |
| 145 |
|
| 146 |
} // namespace App |
| 147 |
|
| 148 |
#endif // RENAMEOBJECTIDENTIFIEREXPRESSIONVISITOR_H |