1 // Copyright (C) 2012-2019 The VPaint Developers.
2 // See the COPYRIGHT file at the top-level directory of this distribution
3 // and at https://github.com/dalboris/vpaint/blob/master/COPYRIGHT
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 
17 #include "Operator.h"
18 
19 #include "VAC.h"
20 #include "Cell.h"
21 
22 #include <QtDebug>
23 
24 namespace VectorAnimationComplex
25 {
26 
Operator()27 Operator::Operator() :
28     alreadyPerformed_(false), trusted_(false)
29 {
30 }
31 
~Operator()32 Operator::~Operator()
33 {
34 }
35 
now()36 void Operator::now()
37 {
38     // pre-check
39     if(alreadyPerformed_)
40     {
41         qDebug() << "Trying to perform an operation already performed: abort.";
42         return;
43     }
44 
45     // customizable behaviour (virtual function)
46     operate();
47 
48     // post-check
49     if(!trusted_)
50         check();
51 }
52 
53 // Set trust mode
trustMe()54 void Operator::trustMe() { trusted_ = true; }
dontTrustMe()55 void Operator::dontTrustMe() { trusted_ = false; }
56 /*
57 // Operating on VAC
58 void Operator::insertCell(VAC * vac, Cell * c) { modify(c); modify(g); g->insertCell(c); }
59 void Operator::removeCell(VAC * vac, Cell * c) { modify(c); modify(g); g->removeCell(c); }
60 */
61 // Operating on Cell
setVAC(Cell * c,VAC * vac)62 void Operator::setVAC(Cell * c, VAC * vac) { modify(c); modify(vac); c->vac_ = vac; }
setID(Cell * c,int id)63 void Operator::setID(Cell * c, int id) { modify(c); c->id_ = id; }
64 /*
65 // Operating on Vertex
66 void Operator::setX(Vertex * v, double x) { modify(v); v->x_ = x; }
67 void Operator::setY(Vertex * v, double y) { modify(v); v->y_ = y; }
68 
69 // Operating on Edge
70 void Operator::setLeft(Edge * e, Vertex * left) { modify(e); modify(left); modify(e->left_); e->left_ = left; }
71 void Operator::setRight(Edge * e, Vertex * right) { modify(e); modify(right); modify(e->right_); e->right_ = right; }
72 */
73 // Modify Entities
modify(Cell * c)74 void Operator::modify(Cell * c)
75 {
76     if(!trusted_)
77         /*root_->*/modifiedCells_ << c;
78 }
modify(VAC * vac)79 void Operator::modify(VAC * vac)
80 {
81     if(!trusted_)
82         /*root_->*/modifiedVAC_ << vac;
83 }
84 
85 // check the validity
check()86 bool Operator::check()
87 {
88     foreach(VAC * vac, /*root_->*/modifiedVAC_)
89         if(!vac->check())
90         {
91             qDebug() << "A VAC modified by the operator is not valid anymore.";
92             return false;
93         }
94     /*root_->*/modifiedVAC_.clear();
95 
96     foreach(Cell * c, /*root_->*/modifiedCells_)
97         if(!c->check())
98         {
99             qDebug() << "Cell(" << c->id() << ") modified by the operator is not valid anymore.";
100             return false;
101         }
102     /*root_->*/modifiedCells_.clear();
103 
104     return true;
105 }
106 
107 }
108