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