Salome HOME
MPV: Merge V1_2d
[modules/visu.git] / src / VISUGUI / VisuGUI_EditContainerDlg.cxx
1 using namespace std;
2
3 //  VISU VISUGUI : GUI of VISU component
4 //
5 //  Copyright (C) 2003  CEA/DEN, EDF R&D
6 //
7 //
8 //
9 //  File   : VisuGUI_EditContainerDlg.cxx
10 //  Author : VSV
11 //  Module : VISU
12
13
14 #include "VisuGUI_EditContainerDlg.h"
15 #include "VisuGUI.h"
16 #include "VISU_Table_i.hh"
17
18 #include "QAD_Application.h"
19 #include "QAD_Desktop.h"
20
21 #include <qlayout.h>
22 #include <qcheckbox.h>
23 #include <qgroupbox.h>
24
25
26 #include CORBA_SERVER_HEADER(VISU_Gen)
27 #include CORBA_SERVER_HEADER(SALOMEDS_Attributes)
28
29 extern VisuGUI *visuGUI;
30
31
32
33 /* XPM */
34 static const char * left_xpm[] = {
35 "24 24 61 1",
36 "       c None",
37 ".      c #323232",
38 "+      c #010101",
39 "@      c #4A4A4A",
40 "#      c #040404",
41 "$      c #979797",
42 "%      c #7B7B7B",
43 "&      c #939393",
44 "*      c #EEEEEE",
45 "=      c #686868",
46 "-      c #030303",
47 ";      c #9C9C9C",
48 ">      c #FFFFFF",
49 ",      c #B1B1B1",
50 "'      c #5E5E5E",
51 ")      c #060606",
52 "!      c #C1C1C1",
53 "~      c #626262",
54 "{      c #000000",
55 "]      c #989898",
56 "^      c #A1A1A1",
57 "/      c #A5A5A5",
58 "(      c #909090",
59 "_      c #868686",
60 ":      c #616161",
61 "<      c #959595",
62 "[      c #919191",
63 "}      c #848484",
64 "|      c #606060",
65 "1      c #0B0B0B",
66 "2      c #545454",
67 "3      c #727272",
68 "4      c #A2A2A2",
69 "5      c #969696",
70 "6      c #838383",
71 "7      c #5F5F5F",
72 "8      c #101010",
73 "9      c #434343",
74 "0      c #6B6B6B",
75 "a      c #858585",
76 "b      c #8E8E8E",
77 "c      c #373737",
78 "d      c #696969",
79 "e      c #8D8D8D",
80 "f      c #B5B5B5",
81 "g      c #111111",
82 "h      c #393939",
83 "i      c #898989",
84 "j      c #B0B0B0",
85 "k      c #191919",
86 "l      c #3A3A3A",
87 "m      c #646464",
88 "n      c #535353",
89 "o      c #050505",
90 "p      c #353535",
91 "q      c #585858",
92 "r      c #4C4C4C",
93 "s      c #0D0D0D",
94 "t      c #3E3E3E",
95 "u      c #020202",
96 "v      c #0A0A0A",
97 "                        ",
98 "                        ",
99 "         .+        .+   ",
100 "        .@#       .@#   ",
101 "       .$%#      .$%#   ",
102 "      .&*=-     .&*=-   ",
103 "     .;>,')    .;>,')   ",
104 "    .;>!;~{   .;>!;~{   ",
105 "   .]>!^&~{  .]>!^&~{   ",
106 "  ./>!/(_:{ ./>!/(_:{   ",
107 " .<*!^[}}|{.<*!^[}}|{   ",
108 " 123}45667{123}45667{   ",
109 "  890a45b7{ 890a45b7{   ",
110 "   8cdef5'{  8cdef5'{   ",
111 "    gh0ij7k   gh0ij7k   ",
112 "     8lm0no    8lm0no   ",
113 "      8pqr-     8pqr-   ",
114 "       sht-      sht-   ",
115 "        1.u       1.u   ",
116 "         v{        v{   ",
117 "                        ",
118 "                        ",
119 "                        ",
120 "                        "};
121
122 static QPixmap MYLeftPix(left_xpm);
123
124
125 static const char * right_xpm[] = {
126 "24 24 43 1",
127 "       g None",
128 ".      g #323232",
129 "+      g #5D5D5D",
130 "@      g #000000",
131 "#      g #C4C4C4",
132 "$      g #313131",
133 "%      g #C5C5C5",
134 "&      g #E4E4E4",
135 "*      g #2D2D2D",
136 "=      g #B7B7B7",
137 "-      g #EFEFEF",
138 ";      g #DCDCDC",
139 ">      g #282828",
140 ",      g #AFAFAF",
141 "'      g #E0E0E0",
142 ")      g #242424",
143 "!      g #C7C7C7",
144 "~      g #9A9A9A",
145 "{      g #8E8E8E",
146 "]      g #1F1F1F",
147 "^      g #A5A5A5",
148 "/      g #989898",
149 "(      g #888888",
150 "_      g #1B1B1B",
151 ":      g #ADADAD",
152 "<      g #858585",
153 "[      g #838383",
154 "}      g #868686",
155 "|      g #929292",
156 "1      g #C1C1C1",
157 "2      g #161616",
158 "3      g #909090",
159 "4      g #747474",
160 "5      g #3A3A3A",
161 "6      g #121212",
162 "7      g #0D0D0D",
163 "8      g #7A7A7A",
164 "9      g #8A8A8A",
165 "0      g #090909",
166 "a      g #040404",
167 "b      g #707070",
168 "c      g #6E6E6E",
169 "d      g #3F3F3F",
170 "                        ",
171 "                        ",
172 " ..        ..           ",
173 " .+@       .+@          ",
174 " .#+@      .#+@         ",
175 " $%&+@     $%&+@        ",
176 " *=-;+@    *=-;+@       ",
177 " >,'=;+@   >,'=;+@      ",
178 " ),!~{;+@  ),!~{;+@     ",
179 " ]^!/({;+@ ]^!/({;+@    ",
180 " _~:<[}|1+@_~:<[}|1+@   ",
181 " 23~[[{:45@23~[[{:45@   ",
182 " 6</[{:45@ 6</[{:45@    ",
183 " 789{:45@  789{:45@     ",
184 " 08~:45@   08~:45@      ",
185 " a4~45@    a4~45@       ",
186 " @b45@     @b45@        ",
187 " @c5@      @c5@         ",
188 " @d@       @d@          ",
189 " @@        @@           ",
190 "                        ",
191 "                        ",
192 "                        ",
193 "                        "};
194
195 static QPixmap MYRightPix(right_xpm);
196
197
198 VisuGUI_EditContainerDlg::VisuGUI_EditContainerDlg()    
199   : QDialog( QAD_Application::getDesktop(), "VisuGUI_EditContainerDlg", true, 
200              WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu )
201 {
202   setCaption( "Edit Plot 2D Presentation" );
203   setSizeGripEnabled( true );
204   QVBoxLayout* TopLayout = new QVBoxLayout(this, 6, 11);
205
206   /***************************************************************/
207   QFrame* aControlFrame = new QFrame(this);
208   aControlFrame->setFrameStyle(QFrame::Box | QFrame::Sunken);
209
210   QGridLayout* aControlLay = new QGridLayout(aControlFrame);
211   aControlLay->setSpacing( 6 );
212   aControlLay->setMargin( 11 );
213   aControlLay->addRowSpacing( 1, 30 );
214   aControlLay->addRowSpacing( 4, 30 );
215   aControlLay->setRowStretch( 1, 1 );
216   aControlLay->setRowStretch( 4, 1 );
217   aControlLay->addColSpacing( 0, 180 );
218   aControlLay->addColSpacing( 2, 180 );
219
220   QLabel* aSelectLbl = new QLabel(tr("Study"), aControlFrame);
221   aControlLay->addWidget(aSelectLbl, 0, 0);
222
223   myStudyLst = new QListView(aControlFrame);
224   myStudyLst->setSelectionMode(QListView::Extended);
225   myStudyLst->addColumn(tr("Table"), 80);
226   myStudyLst->addColumn(tr("Curve"), 50);
227   myStudyLst->addColumn(tr(""), 0);
228   myStudyLst->setAllColumnsShowFocus(true);
229   myStudyLst->setMinimumHeight(130);
230   connect(myStudyLst, SIGNAL( selectionChanged()), this, SLOT( onLeftSelected() ));
231   aControlLay->addMultiCellWidget(myStudyLst, 1, 4, 0, 0);
232   
233   myRightBtn = new QToolButton(aControlFrame);
234   myRightBtn->setIconSet(MYRightPix);
235   myRightBtn->setEnabled(false);
236   connect(myRightBtn, SIGNAL( clicked()), this, SLOT( onRightClicked() ));
237   aControlLay->addWidget(myRightBtn, 2, 1);
238   
239   myLeftBtn = new QToolButton(aControlFrame);
240   myLeftBtn->setIconSet(MYLeftPix);
241   myLeftBtn->setEnabled(false);
242   connect(myLeftBtn, SIGNAL( clicked()), this, SLOT( onLeftClicked() ));
243   aControlLay->addWidget(myLeftBtn, 3, 1);
244   
245   QLabel* aForceLbl = new QLabel(tr("Container"), aControlFrame);
246   aControlLay->addWidget(aForceLbl, 0, 2);
247
248   myContainerLst = new QListView(aControlFrame);
249   myContainerLst->setSelectionMode(QListView::Extended);
250   myContainerLst->addColumn(tr("Table"), 80);
251   myContainerLst->addColumn(tr("Curve"), 50);
252   myContainerLst->addColumn(tr(""), 0);
253   myContainerLst->setMinimumWidth(130);
254   connect(myContainerLst, SIGNAL( selectionChanged()), this, SLOT( onRightSelected() ));
255   aControlLay->addMultiCellWidget(myContainerLst, 1, 4, 2, 2);
256   
257   TopLayout->addWidget( aControlFrame );
258
259   // Common buttons ===========================================================
260   QGroupBox* GroupButtons = new QGroupBox( this, "GroupButtons" );
261   GroupButtons->setColumnLayout(0, Qt::Vertical );
262   GroupButtons->layout()->setSpacing( 0 );
263   GroupButtons->layout()->setMargin( 0 );
264   QGridLayout* GroupButtonsLayout = new QGridLayout( GroupButtons->layout() );
265   GroupButtonsLayout->setAlignment( Qt::AlignTop );
266   GroupButtonsLayout->setSpacing( 6 );
267   GroupButtonsLayout->setMargin( 11 );
268
269   QPushButton* buttonOk = new QPushButton( tr( "&OK" ), GroupButtons, "buttonOk" );
270   buttonOk->setAutoDefault( TRUE );
271   buttonOk->setDefault( TRUE );
272   GroupButtonsLayout->addWidget( buttonOk, 0, 0 );
273   GroupButtonsLayout->addItem( new QSpacerItem( 5, 5, QSizePolicy::Expanding, QSizePolicy::Minimum ), 0, 1 );
274
275   QPushButton* buttonCancel = new QPushButton( tr( "&Cancel" ) , GroupButtons, "buttonCancel" );
276   buttonCancel->setAutoDefault( TRUE );
277   GroupButtonsLayout->addWidget( buttonCancel, 0, 2 );
278
279   TopLayout->addWidget( GroupButtons );
280
281   connect( buttonOk,     SIGNAL( clicked() ),      this, SLOT( accept() ) );
282   connect( buttonCancel, SIGNAL( clicked() ),      this, SLOT( reject() ) );
283 }
284
285
286 void VisuGUI_EditContainerDlg::initFromPrsObject(VISU::Container_i* theContainer) {
287   SALOMEDS::Study_var aStudy = visuGUI->GetStudyDocument();
288   SALOMEDS::SComponent_var aVisuSO = aStudy->FindComponent("VISU");
289   if (aVisuSO->_is_nil()) {
290     return;
291   }
292   QValueList<CurveStruct> aStudyCurves;
293   QValueList<CurveStruct> aContainerCurves;
294   // Find curves in container
295   for (int i = 1; i <= theContainer->GetNbCurves(); i++) {
296     VISU::Curve_i* aCurve = theContainer->GetCurve(i);
297     if (aCurve == NULL) continue;
298     CurveStruct aEntry;
299     aEntry.CurveName = aCurve->GetTitle();
300     aEntry.CurveEntry = aCurve->GetEntry();
301     SALOMEDS::SObject_var aTableSO = aStudy->FindObjectID(aCurve->GetTableID());
302     if (CORBA::is_nil(aTableSO)) continue;
303     aEntry.TableName = getSObjectName(aTableSO);
304     aContainerCurves.append(aEntry);
305     new QListViewItem(myContainerLst, aEntry.TableName, aEntry.CurveName, aEntry.CurveEntry);
306   }
307   // Find curves in study
308   SALOMEDS::ChildIterator_var aIter = aStudy->NewChildIterator( aVisuSO );
309   for (aIter->InitEx(true); aIter->More(); aIter->Next()) {
310     SALOMEDS::SObject_var aSObject = aIter->Value();
311     CORBA::Object_var anObject = VISU::SObjectToObject(aSObject);
312     VISU::Base_var aVisuObj = VISU::Base::_narrow(anObject);
313     if (!CORBA::is_nil(aVisuObj)) {
314       if (aVisuObj->GetType() == VISU::TCURVE) {
315         SALOMEDS::SObject_var aTableSO = aSObject->GetFather();
316         CurveStruct aEntry;
317         aEntry.TableName = getSObjectName(aTableSO);
318         aEntry.CurveName = getSObjectName(aSObject);
319         aEntry.CurveEntry = aSObject->GetID();
320         aStudyCurves.append(aEntry);
321       }
322     }
323   }
324   //Show Curves which are not in Curve
325   QValueList<CurveStruct>::iterator it;
326   QValueList<CurveStruct>::iterator it2;
327   bool isExist = false;
328   for (it = aStudyCurves.begin(); it != aStudyCurves.end(); ++it ) {
329     for (it2 = aContainerCurves.begin(); it2 != aContainerCurves.end(); ++it2 ) {
330       if (isExist = ((*it).CurveEntry == (*it2).CurveEntry))
331         break;
332     }
333     if (!isExist)
334       new QListViewItem(myStudyLst, (*it).TableName, (*it).CurveName, (*it).CurveEntry);
335   }
336 }
337
338
339 void VisuGUI_EditContainerDlg::storeToPrsObject(VISU::Container_i* theContainer) {
340   theContainer->Clear();
341
342   SALOMEDS::Study_var aStudy = visuGUI->GetStudyDocument();
343   QListViewItem* anItem = myContainerLst->firstChild();
344   while (anItem) {
345     SALOMEDS::SObject_var aCurveSO = aStudy->FindObjectID(anItem->text(2));
346     if (!aCurveSO->_is_nil()) {
347       CORBA::Object_var aObject = VISU::SObjectToObject(aCurveSO);
348       if (!CORBA::is_nil(aObject)) {
349         VISU::Curve_i* aCurve = dynamic_cast<VISU::Curve_i*>( VISU::GetServant(aObject).in() );
350         if (aCurve) theContainer->AddCurve(aCurve->_this());
351       }
352     }
353     anItem = anItem->nextSibling();
354   }
355 }
356
357
358 QString VisuGUI_EditContainerDlg::getSObjectName(SALOMEDS::SObject_var theSObject) {
359   if (theSObject->_is_nil()) return QString("");
360
361   SALOMEDS::GenericAttribute_var anAttr;  
362   SALOMEDS::AttributeName_var    aName;
363   if ( theSObject->FindAttribute(anAttr, "AttributeName") ) {
364     aName = SALOMEDS::AttributeName::_narrow( anAttr );
365     return QString(strdup( aName->Value() ));
366   } 
367   return QString("");
368 }
369
370
371 void VisuGUI_EditContainerDlg::onLeftClicked() {
372   QListViewItem* anItem = myContainerLst->firstChild();
373   while (anItem) {
374     if (anItem->isSelected()) {
375       QListViewItem* anTmpItem = anItem;
376       anItem = anTmpItem->nextSibling();
377       myContainerLst->takeItem(anTmpItem);
378       myStudyLst->insertItem(anTmpItem);
379     } else {
380       anItem = anItem->nextSibling();
381     }
382   }
383 }
384
385
386 void VisuGUI_EditContainerDlg::onRightClicked() {
387   QListViewItem* anItem = myStudyLst->firstChild();
388   while (anItem) {
389     if (anItem->isSelected()) {
390       QListViewItem* anTmpItem = anItem;
391       anItem = anTmpItem->nextSibling();
392       myStudyLst->takeItem(anTmpItem);
393       myContainerLst->insertItem(anTmpItem);
394     } else {
395       anItem = anItem->nextSibling();
396     }
397   }
398 }
399
400
401 void VisuGUI_EditContainerDlg::onLeftSelected() {
402   QListViewItem* anItem = myStudyLst->firstChild();
403   bool isSelected = false;
404   while (anItem) {
405     if (anItem->isSelected()) {
406       isSelected = true;
407       break;
408     }
409     anItem = anItem->nextSibling();
410   }
411   myRightBtn->setEnabled(isSelected);
412 }
413
414
415 void VisuGUI_EditContainerDlg::onRightSelected() {
416   QListViewItem* anItem = myContainerLst->firstChild();
417   bool isSelected = false;
418   while (anItem) {
419     if (anItem->isSelected()) {
420       isSelected = true;
421       break;
422     }
423     anItem = anItem->nextSibling();
424   }
425   myLeftBtn->setEnabled(isSelected);
426 }