Salome HOME
5b6752f59444232897b335f6a149c9c50fb85f4a
[modules/yacs.git] / src / genericgui / SchemaContainerItem.cxx
1 //  Copyright (C) 2006-2008  CEA/DEN, EDF R&D
2 //
3 //  This library is free software; you can redistribute it and/or
4 //  modify it under the terms of the GNU Lesser General Public
5 //  License as published by the Free Software Foundation; either
6 //  version 2.1 of the License.
7 //
8 //  This library 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 GNU
11 //  Lesser General Public License for more details.
12 //
13 //  You should have received a copy of the GNU Lesser General Public
14 //  License along with this library; if not, write to the Free Software
15 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 #include "SchemaContainerItem.hxx"
20 #include "QtGuiContext.hxx"
21 #include "SchemaModel.hxx"
22 #include "SchemaComponentItem.hxx"
23
24 #include <QIcon>
25 #include <cassert>
26
27 //#define _DEVDEBUG_
28 #include "YacsTrace.hxx"
29
30 using namespace std;
31 using namespace YACS::HMI;
32
33 SchemaContainerItem::SchemaContainerItem(SchemaItem *parent, QString label, Subject* subject)
34   : SchemaItem::SchemaItem(parent, label, subject)
35 {
36   DEBTRACE("SchemaContainerItem::SchemaContainerItem " << subject->getName());
37   SubjectContainer *scont = dynamic_cast<SubjectContainer*>(subject);
38   DEBTRACE(scont);
39   _itemDeco.replace(YLabel, QIcon("icons:container.png"));
40 }
41
42 void SchemaContainerItem::update(GuiEvent event, int type, Subject* son)
43 {
44   DEBTRACE("SchemaContainerItem::update " << eventName(event) << " " << son->getName());
45   //SchemaItem::update(event, type, son);
46   switch (event)
47     {
48     case YACS::HMI::ADDCHILDREF:
49       {
50         DEBTRACE("ADDCHILDREF ");
51         SchemaModel *model = QtGuiContext::getQtCurrent()->getSchemaModel();
52         SubjectReference *ref = dynamic_cast<SubjectReference*>(son);
53         assert(ref);
54         DEBTRACE("ADDCHILDREF " << ref->getReference()->getName());
55         addComponentInstance(ref->getReference());
56       }
57       break;
58
59     case YACS::HMI::REMOVECHILDREF:
60       {
61         DEBTRACE("REMOVECHILDREF ");
62       }
63       break;
64
65     case YACS::HMI::CUT:
66       {
67         DEBTRACE("CUT on " << getSubject()->getName());
68         SchemaModel *model = QtGuiContext::getQtCurrent()->getSchemaModel();
69         SubjectReference *ref = dynamic_cast<SubjectReference*>(son);
70         assert(ref);
71         DEBTRACE("CUT " << ref->getReference()->getName());
72         SchemaItem *toMove = QtGuiContext::getQtCurrent()->_mapOfSchemaItem[ref->getReference()];
73
74         int position = toMove->row();
75         model->beginRemoveRows(modelIndex(), position, position);
76         removeChild(toMove);
77         model->endRemoveRows();
78       }
79       break;
80
81     case YACS::HMI::PASTE:
82       {
83         DEBTRACE("PASTE on " << getSubject()->getName());
84         SchemaModel *model = QtGuiContext::getQtCurrent()->getSchemaModel();
85         SubjectReference *ref = dynamic_cast<SubjectReference*>(son);
86         assert(ref);
87         DEBTRACE("PASTE " << ref->getReference()->getName());
88         SchemaItem *toPaste = QtGuiContext::getQtCurrent()->_mapOfSchemaItem[ref->getReference()];
89
90         int nbsons = childCount();
91         model->beginInsertRows(modelIndex(), nbsons, nbsons);
92         toPaste->reparent(this);
93         model->endInsertRows();
94       }
95       break;
96
97     default:
98       ;
99     }
100 }
101
102 void SchemaContainerItem::addComponentInstance(Subject* subject)
103 {
104   DEBTRACE("SchemaContainersItem::addComponentInstance");
105   SchemaModel *model = QtGuiContext::getQtCurrent()->getSchemaModel();
106   int nbsons = childCount();
107   model->beginInsertRows(modelIndex(), nbsons, nbsons);
108   SchemaComponentItem *item = new SchemaComponentItem(this,
109                                                       subject->getName().c_str(),
110                                                       subject);
111   model->endInsertRows();
112 }