OSDN Git Service

[denncoCreator] refactoring
[dennco/denncoCreator.git] / Source / utils / dccomponentutil.cpp
1 //  Copyright (c) 2012 Dennco Project
2 //
3 // This program is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, either version 3 of the License, or
6 // (at your option) any later version.
7 //
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
16 //
17 //  Created by tkawata on Sep-30, 2012.
18 //
19 #include "dccomponentutil.h"
20
21 #include "dccell.h"
22 #include "dcvccell.h"
23 #include "dccellcode.h"
24 #include "dcvccellcode.h"
25 #include "dcaxon.h"
26 #include "dcvcaxon.h"
27 #include "dcaxonterminal.h"
28 #include "dcvcaxonterminal.h"
29 #include "dcreceptor.h"
30 #include "dcvcreceptor.h"
31 #include "dcvcpage.h"
32 #include "TKReceptor.h"
33 #include "dcvceditmodeterminal.h"
34
35 #include <map>
36
37 //static
38 DCCell* DCComponentUtil::createCell(DCContainer *container, DCVCPage *page, std::string location, std::string name, std::string type, bool canInterface)
39 {
40     DCCell *cell = new DCCell(container, location, name, type, canInterface);
41     DCVPageComponent *vcompo = new DCVCCell(cell, page, 0.5, 0.5);
42     cell->bindComponent(vcompo);
43     page->registerCell(vcompo);
44
45     return cell;
46 }
47
48 //static
49 DCCellCode* DCComponentUtil::createCellCode(DCContainer *container, DCVCPage *page, std::string theName, std::string theCellAPIName)
50 {
51     DCCellCode *cellcode = new DCCellCode(container, theName, theCellAPIName);
52     DCVPageComponent *vcompo = new DCVCCellCode(cellcode, page);
53     cellcode->bindComponent(vcompo);
54     if (page)
55         page->registerCellCodeClass(vcompo);
56
57     return cellcode;
58 }
59
60 //static
61 DCAxon* DCComponentUtil::createAxon(DCCell *theOwner)
62 {
63     DCAxon *axon = new DCAxon(theOwner);
64     axon->bindComponent(new DCVCAxon(axon));
65
66     return axon;
67 }
68
69 //static
70 DCAxonTerminal* DCComponentUtil::createAxonTerminal(DCAxon *theOwner)
71 {
72     DCAxonTerminal *axonTerminal = new DCAxonTerminal(theOwner);
73     axonTerminal->bindComponent(new DCVCAxonTerminal(axonTerminal));
74
75     return axonTerminal;
76 }
77
78 //static
79 DCReceptor* DCComponentUtil::createReceptor(DCCell *theOwner)
80 {
81     DCReceptor *receptor = new DCReceptor(theOwner);
82     receptor->bindComponent(new DCVCReceptor(receptor));
83
84     return receptor;
85 }
86
87 //static
88 bool DCComponentUtil::isConnectedToSelectedCell(DCVComponent *vcell)
89 {
90     DCCell *cell = dynamic_cast<DCVCCell*>(vcell)->getOwnerCell();
91     bool r = false;
92     const TKReceptorMap *receptors = cell->getReceptors();
93     for ( TKReceptorMap::const_iterator it = receptors->begin(); it != receptors->end(); ++it )
94     {
95         DCReceptor *receptor = dynamic_cast<DCReceptor*>(it->second);
96         if ( receptor && receptor->getTarget() && ((DCCell*)receptor->getTarget()->getOwner()->getOwner())->getVComponent()->getIsSelected())
97         {
98             r = true;
99             break;
100         }
101     }
102
103     if (!r)
104     {
105         DCAxon *axon = cell->getAxon();
106         if (axon)
107         {
108             for (int i = axon->getNumberOfTerminals()-1; i >= 0; i--)
109             {
110                 DCAxonTerminal *terminal = axon->getTerminalAt(i);
111                 if (terminal && terminal->getTarget() && ((DCCell*)terminal->getTarget()->getOwnerCell())->getVComponent()->getIsSelected())
112                 {
113                     r = true;
114                     break;
115                 }
116             }
117         }
118     }
119     return r;
120 }