Salome HOME
bd47bd41953ed82eb0526e5f5c9018b505dbaa4e
[modules/visu.git] / src / VISUGUI / VisuGUI_EditContainerDlg.cxx
1 // Copyright (C) 2007-2011  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 //  File   : VisuGUI_EditContainerDlg.cxx
24 //  Author : VSV
25 //  Module : VISU
26 //
27 #include "VisuGUI_EditContainerDlg.h"
28
29 #include "VisuGUI.h"
30 #include "VisuGUI_Tools.h"
31
32 #include "VISU_Table_i.hh"
33
34 #include "SalomeApp_Application.h"
35 #include "SUIT_Desktop.h"
36 #include "SUIT_Session.h"
37 #include "SUIT_MessageBox.h"
38 #include "SUIT_ResourceMgr.h"
39
40 #include <QLayout>
41 #include <QCheckBox>
42 #include <QGroupBox>
43 #include <QPushButton>
44 #include <QLabel>
45 #include <QTreeWidget>
46 #include <QToolButton>
47 #include <QKeyEvent>
48
49
50 using namespace std;
51
52
53 /* XPM */
54 static const char * left_xpm[] = {
55 "24 24 61 1",
56 "       c None",
57 ".      c #323232",
58 "+      c #010101",
59 "@      c #4A4A4A",
60 "#      c #040404",
61 "$      c #979797",
62 "%      c #7B7B7B",
63 "&      c #939393",
64 "*      c #EEEEEE",
65 "=      c #686868",
66 "-      c #030303",
67 ";      c #9C9C9C",
68 ">      c #FFFFFF",
69 ",      c #B1B1B1",
70 "'      c #5E5E5E",
71 ")      c #060606",
72 "!      c #C1C1C1",
73 "~      c #626262",
74 "{      c #000000",
75 "]      c #989898",
76 "^      c #A1A1A1",
77 "/      c #A5A5A5",
78 "(      c #909090",
79 "_      c #868686",
80 ":      c #616161",
81 "<      c #959595",
82 "[      c #919191",
83 "}      c #848484",
84 "|      c #606060",
85 "1      c #0B0B0B",
86 "2      c #545454",
87 "3      c #727272",
88 "4      c #A2A2A2",
89 "5      c #969696",
90 "6      c #838383",
91 "7      c #5F5F5F",
92 "8      c #101010",
93 "9      c #434343",
94 "0      c #6B6B6B",
95 "a      c #858585",
96 "b      c #8E8E8E",
97 "c      c #373737",
98 "d      c #696969",
99 "e      c #8D8D8D",
100 "f      c #B5B5B5",
101 "g      c #111111",
102 "h      c #393939",
103 "i      c #898989",
104 "j      c #B0B0B0",
105 "k      c #191919",
106 "l      c #3A3A3A",
107 "m      c #646464",
108 "n      c #535353",
109 "o      c #050505",
110 "p      c #353535",
111 "q      c #585858",
112 "r      c #4C4C4C",
113 "s      c #0D0D0D",
114 "t      c #3E3E3E",
115 "u      c #020202",
116 "v      c #0A0A0A",
117 "                        ",
118 "                        ",
119 "         .+        .+   ",
120 "        .@#       .@#   ",
121 "       .$%#      .$%#   ",
122 "      .&*=-     .&*=-   ",
123 "     .;>,')    .;>,')   ",
124 "    .;>!;~{   .;>!;~{   ",
125 "   .]>!^&~{  .]>!^&~{   ",
126 "  ./>!/(_:{ ./>!/(_:{   ",
127 " .<*!^[}}|{.<*!^[}}|{   ",
128 " 123}45667{123}45667{   ",
129 "  890a45b7{ 890a45b7{   ",
130 "   8cdef5'{  8cdef5'{   ",
131 "    gh0ij7k   gh0ij7k   ",
132 "     8lm0no    8lm0no   ",
133 "      8pqr-     8pqr-   ",
134 "       sht-      sht-   ",
135 "        1.u       1.u   ",
136 "         v{        v{   ",
137 "                        ",
138 "                        ",
139 "                        ",
140 "                        "};
141
142 static QPixmap MYLeftPix(left_xpm);
143
144
145 static const char * right_xpm[] = {
146 "24 24 43 1",
147 "       g None",
148 ".      g #323232",
149 "+      g #5D5D5D",
150 "@      g #000000",
151 "#      g #C4C4C4",
152 "$      g #313131",
153 "%      g #C5C5C5",
154 "&      g #E4E4E4",
155 "*      g #2D2D2D",
156 "=      g #B7B7B7",
157 "-      g #EFEFEF",
158 ";      g #DCDCDC",
159 ">      g #282828",
160 ",      g #AFAFAF",
161 "'      g #E0E0E0",
162 ")      g #242424",
163 "!      g #C7C7C7",
164 "~      g #9A9A9A",
165 "{      g #8E8E8E",
166 "]      g #1F1F1F",
167 "^      g #A5A5A5",
168 "/      g #989898",
169 "(      g #888888",
170 "_      g #1B1B1B",
171 ":      g #ADADAD",
172 "<      g #858585",
173 "[      g #838383",
174 "}      g #868686",
175 "|      g #929292",
176 "1      g #C1C1C1",
177 "2      g #161616",
178 "3      g #909090",
179 "4      g #747474",
180 "5      g #3A3A3A",
181 "6      g #121212",
182 "7      g #0D0D0D",
183 "8      g #7A7A7A",
184 "9      g #8A8A8A",
185 "0      g #090909",
186 "a      g #040404",
187 "b      g #707070",
188 "c      g #6E6E6E",
189 "d      g #3F3F3F",
190 "                        ",
191 "                        ",
192 " ..        ..           ",
193 " .+@       .+@          ",
194 " .#+@      .#+@         ",
195 " $%&+@     $%&+@        ",
196 " *=-;+@    *=-;+@       ",
197 " >,'=;+@   >,'=;+@      ",
198 " ),!~{;+@  ),!~{;+@     ",
199 " ]^!/({;+@ ]^!/({;+@    ",
200 " _~:<[}|1+@_~:<[}|1+@   ",
201 " 23~[[{:45@23~[[{:45@   ",
202 " 6</[{:45@ 6</[{:45@    ",
203 " 789{:45@  789{:45@     ",
204 " 08~:45@   08~:45@      ",
205 " a4~45@    a4~45@       ",
206 " @b45@     @b45@        ",
207 " @c5@      @c5@         ",
208 " @d@       @d@          ",
209 " @@        @@           ",
210 "                        ",
211 "                        ",
212 "                        ",
213 "                        "};
214
215 static QPixmap MYRightPix(right_xpm);
216
217
218 VisuGUI_EditContainerDlg::VisuGUI_EditContainerDlg (VisuGUI* theModule, bool theIsModal)
219      : QDialog(VISU::GetDesktop(theModule), Qt::WindowTitleHint | Qt::WindowSystemMenuHint ),
220        myVisuGUI(theModule)
221 {
222   setModal( theIsModal );
223   if (!theIsModal) {
224     setAttribute( Qt::WA_DeleteOnClose, true );
225   }
226   setWindowTitle("Edit Plot 2D Presentation");
227   setSizeGripEnabled(true);
228
229   QVBoxLayout* TopLayout = new QVBoxLayout (this );
230   TopLayout->setMargin( 6 );
231   TopLayout->setSpacing( 11);
232
233   /***************************************************************/
234   QFrame* aControlFrame = new QFrame (this);
235   aControlFrame->setFrameStyle(QFrame::Box | QFrame::Sunken);
236
237   QGridLayout* aControlLay = new QGridLayout (aControlFrame);
238   aControlLay->setSpacing(6);
239   aControlLay->setMargin(11);
240   //aControlLay->addRowSpacing(1, 30);
241   //aControlLay->addRowSpacing(4, 30);
242   aControlLay->setRowStretch(1, 1);
243   aControlLay->setRowStretch(4, 1);
244   //aControlLay->addColSpacing(0, 180);
245   //aControlLay->addColSpacing(2, 180);
246
247   QLabel* aSelectLbl = new QLabel (tr("LBL_STUDY"), aControlFrame);
248   aControlLay->addWidget(aSelectLbl, 0, 0);
249
250   myStudyLst = new QTreeWidget (aControlFrame);
251   myStudyLst->setSelectionMode(QAbstractItemView::ExtendedSelection);
252   myStudyLst->setColumnCount( 3 );
253   QStringList aLabels;
254   aLabels.append( tr("TXT_TABLE") );
255   aLabels.append( tr("TXT_CURVE") );
256   aLabels.append( "" );
257   myStudyLst->setHeaderLabels( aLabels );
258   myStudyLst->setColumnWidth( 0, 80 );
259   myStudyLst->setColumnWidth( 1, 50 );
260   myStudyLst->setColumnWidth( 2, 0 );
261   
262   //myStudyLst->addColumn(tr("TXT_TABLE"), 80);
263   //myStudyLst->addColumn(tr("TXT_CURVE"), 50);
264   //myStudyLst->addColumn(tr(""), 0);
265   
266   myStudyLst->setAllColumnsShowFocus(true);
267   //myStudyLst->setMinimumHeight(130);
268   connect(myStudyLst, SIGNAL(itemSelectionChanged()), this, SLOT(onLeftSelected()));
269   aControlLay->addWidget(myStudyLst, 1, 0, 5, 1);
270
271   myRightBtn = new QToolButton (aControlFrame);
272   myRightBtn->setIcon(MYRightPix);
273   myRightBtn->setEnabled(false);
274   connect(myRightBtn, SIGNAL(clicked()), this, SLOT(onRightClicked()));
275   aControlLay->addWidget(myRightBtn, 2, 1);
276
277   myLeftBtn = new QToolButton(aControlFrame);
278   myLeftBtn->setIcon(MYLeftPix);
279   myLeftBtn->setEnabled(false);
280   connect(myLeftBtn, SIGNAL(clicked()), this, SLOT(onLeftClicked()));
281   aControlLay->addWidget(myLeftBtn, 3, 1);
282
283   QLabel* aForceLbl = new QLabel (tr("LBL_CONTAINER"), aControlFrame);
284   aControlLay->addWidget(aForceLbl, 0, 2);
285
286   myContainerLst = new QTreeWidget(aControlFrame);
287   myContainerLst->setSelectionMode(QAbstractItemView::ExtendedSelection);
288   //myContainerLst->addColumn(tr("TXT_TABLE"), 80);
289   //myContainerLst->addColumn(tr("TXT_CURVE"), 50);
290   //myContainerLst->addColumn(tr(""), 0);
291   myContainerLst->setColumnCount( 3 );
292   myContainerLst->setHeaderLabels( aLabels );
293   myContainerLst->setColumnWidth( 0, 80 );
294   myContainerLst->setColumnWidth( 1, 50 );
295   myContainerLst->setColumnWidth( 2, 0 );
296
297   
298   //myContainerLst->setMinimumWidth(130);
299   connect(myContainerLst, SIGNAL(itemSelectionChanged()), this, SLOT(onRightSelected()));
300   aControlLay->addWidget(myContainerLst, 1, 2, 5, 1);
301
302   TopLayout->addWidget(aControlFrame);
303
304   // Common buttons ===========================================================
305   QGroupBox* GroupButtons = new QGroupBox(this);
306   //GroupButtons->setColumnLayout(0, Qt::Vertical);
307   //GroupButtons->layout()->setSpacing(0);
308   //GroupButtons->layout()->setMargin(0);
309   QGridLayout* GroupButtonsLayout = new QGridLayout(GroupButtons);
310   GroupButtonsLayout->setAlignment(Qt::AlignTop);
311   GroupButtonsLayout->setSpacing(6);
312   GroupButtonsLayout->setMargin(11);
313
314   QPushButton* buttonOk = new QPushButton(tr("&OK"), GroupButtons);
315   buttonOk->setAutoDefault(TRUE);
316   buttonOk->setDefault(TRUE);
317   GroupButtonsLayout->addWidget(buttonOk, 0, 0);
318   GroupButtonsLayout->addItem(new QSpacerItem(5, 5, QSizePolicy::Expanding,
319                                               QSizePolicy::Minimum), 0, 1);
320
321   QPushButton* buttonCancel = new QPushButton(tr("&Cancel") , GroupButtons);
322   buttonCancel->setAutoDefault(TRUE);
323   GroupButtonsLayout->addWidget(buttonCancel, 0, 2);
324
325   QPushButton* buttonHelp = new QPushButton(tr("&Help") , GroupButtons);
326   buttonHelp->setAutoDefault(TRUE);
327   GroupButtonsLayout->addWidget(buttonHelp, 0, 3);
328
329   TopLayout->addWidget(GroupButtons);
330
331   connect(buttonOk,     SIGNAL(clicked()),      this, SLOT(accept()));
332   connect(buttonCancel, SIGNAL(clicked()),      this, SLOT(reject()));
333   connect(buttonHelp,   SIGNAL(clicked()),      this, SLOT(onHelp()));
334 }
335
336 void VisuGUI_EditContainerDlg::initFromPrsObject (VISU::Container_i* theContainer)
337 {
338   _PTR(Study) aStudy = VISU::GetCStudy(VISU::GetAppStudy(myVisuGUI));
339   _PTR(SComponent) aVisuSO = aStudy->FindComponent("VISU");
340   if (!aVisuSO) {
341     return;
342   }
343   QList<CurveStruct> aStudyCurves;
344   QList<CurveStruct> aContainerCurves;
345   // Find curves in container
346   for (int i = 1; i <= theContainer->GetNbCurves(); i++) {
347     VISU::Curve_i* aCurve = theContainer->GetCurve(i);
348     if (aCurve == NULL) continue;
349     CurveStruct aEntry;
350     aEntry.CurveName = aCurve->GetTitle();
351     aEntry.CurveEntry = aCurve->GetEntry().c_str();
352     _PTR(SObject) aTableSO = aStudy->FindObjectID(aCurve->GetTableID());
353     if (!aTableSO) continue;
354     aEntry.TableName = getSObjectName(aTableSO);
355     aContainerCurves.append(aEntry);
356     QStringList aList;
357     aList.append( aEntry.TableName );
358     aList.append( aEntry.CurveName );
359     aList.append( aEntry.CurveEntry );
360     new QTreeWidgetItem(myContainerLst, aList);
361   }
362   // Find curves in study
363   _PTR(ChildIterator) aIter = aStudy->NewChildIterator(aVisuSO);
364   for (aIter->InitEx(true); aIter->More(); aIter->Next()) {
365     _PTR(SObject) aSObject = aIter->Value();
366     CORBA::Object_var anObject = VISU::ClientSObjectToObject(aSObject);
367     VISU::Base_var aVisuObj = VISU::Base::_narrow(anObject);
368     if (!CORBA::is_nil(aVisuObj)) {
369       if (aVisuObj->GetType() == VISU::TCURVE) {
370         _PTR(SObject) aTableSO = aSObject->GetFather();
371         CurveStruct aEntry;
372         aEntry.TableName = getSObjectName(aTableSO);
373         aEntry.CurveName = getSObjectName(aSObject);
374         aEntry.CurveEntry = aSObject->GetID().c_str();
375         aStudyCurves.append(aEntry);
376       }
377     }
378   }
379   //Show Curves which are not in Curve
380   QList<CurveStruct>::iterator it;
381   QList<CurveStruct>::iterator it2;
382   bool isExist = false;
383   for (it = aStudyCurves.begin(); it != aStudyCurves.end(); ++it) {
384     for (it2 = aContainerCurves.begin(); it2 != aContainerCurves.end(); ++it2) {
385       if (isExist = ((*it).CurveEntry == (*it2).CurveEntry))
386         break;
387     }
388     if (!isExist)
389     {
390       QStringList aList;
391       aList.append( (*it).TableName );
392       aList.append( (*it).CurveName );
393       aList.append( (*it).CurveEntry );
394       new QTreeWidgetItem(myStudyLst, aList );
395     }
396   }
397 }
398
399 void VisuGUI_EditContainerDlg::storeToPrsObject (VISU::Container_i* theContainer)
400 {
401   theContainer->Clear();
402
403   _PTR(Study) aStudy = VISU::GetCStudy(VISU::GetAppStudy(myVisuGUI));
404   QTreeWidgetItem* anItem;
405   for ( int i= 0; i< myContainerLst->topLevelItemCount(); i++  )
406   {
407     anItem = myContainerLst->topLevelItem( i );
408     if ( !anItem )
409       continue;
410     _PTR(SObject) aCurveSO = aStudy->FindObjectID((const char*)anItem->text(2).toLatin1());
411     if (aCurveSO) {
412       CORBA::Object_var aObject = VISU::ClientSObjectToObject(aCurveSO);
413       if (!CORBA::is_nil(aObject)) {
414         VISU::Curve_i* aCurve = dynamic_cast<VISU::Curve_i*>(VISU::GetServant(aObject).in());
415         if (aCurve) theContainer->AddCurve(aCurve->_this());
416       }
417     }
418   }
419 }
420
421 QString VisuGUI_EditContainerDlg::getSObjectName (_PTR(SObject) theSObject)
422 {
423   if (!theSObject) return QString("");
424
425   _PTR(GenericAttribute) anAttr;
426   if (theSObject->FindAttribute(anAttr, "AttributeName")) {
427     _PTR(AttributeName) aName (anAttr);
428     return QString(aName->Value().c_str());
429   }
430   return QString("");
431 }
432
433 void VisuGUI_EditContainerDlg::onLeftClicked()
434 {
435   /*QListViewItem* anItem = myContainerLst->firstChild();
436   while (anItem) {
437     if (anItem->isSelected()) {
438       QListViewItem* anTmpItem = anItem;
439       anItem = anTmpItem->nextSibling();
440       myContainerLst->takeItem(anTmpItem);
441       myStudyLst->insertItem(anTmpItem);
442     } else {
443       anItem = anItem->nextSibling();
444     }
445     }*/
446   QList<QTreeWidgetItem*> selItem = myContainerLst->selectedItems();
447   QList<QTreeWidgetItem*>::Iterator anIt = selItem.begin();
448
449   QList<QTreeWidgetItem*> topSelectedItems;
450   for ( ; anIt != selItem.end(); anIt++  )
451   {
452     int index = myContainerLst->indexOfTopLevelItem( *anIt );
453     if ( index != -1 )
454       topSelectedItems.append( myContainerLst->takeTopLevelItem( index ) );
455   }
456
457   myStudyLst->addTopLevelItems(topSelectedItems);
458 }
459
460 void VisuGUI_EditContainerDlg::onRightClicked()
461 {
462   /*QListViewItem* anItem = myStudyLst->firstChild();
463   while (anItem) {
464     if (anItem->isSelected()) {
465       QListViewItem* anTmpItem = anItem;
466       anItem = anTmpItem->nextSibling();
467       myStudyLst->takeItem(anTmpItem);
468       myContainerLst->insertItem(anTmpItem);
469     } else {
470       anItem = anItem->nextSibling();
471     }
472     }*/
473   QList<QTreeWidgetItem*> selItem = myStudyLst->selectedItems();
474   QList<QTreeWidgetItem*>::Iterator anIt = selItem.begin();
475
476   QList<QTreeWidgetItem*> topSelectedItems;
477   for ( ; anIt != selItem.end(); anIt++  )
478   {
479     int index = myStudyLst->indexOfTopLevelItem( *anIt );
480     if ( index != -1 )
481       topSelectedItems.append( myStudyLst->takeTopLevelItem( index ) );
482   }
483
484   myContainerLst->addTopLevelItems(topSelectedItems);
485 }
486
487 void VisuGUI_EditContainerDlg::onLeftSelected()
488 {
489   myRightBtn->setEnabled( myStudyLst->selectedItems().count() > 0 );
490 }
491
492 void VisuGUI_EditContainerDlg::onRightSelected()
493 {
494   myLeftBtn->setEnabled( myContainerLst->selectedItems().count() > 0 );
495 }
496
497 void VisuGUI_EditContainerDlg::onHelp()
498 {
499   QString aHelpFileName = "creating_xy_plot_page.html";
500   SalomeApp_Application* app = (SalomeApp_Application*)(SUIT_Session::session()->activeApplication());
501   if (app)
502     app->onHelpContextModule(myVisuGUI ? app->moduleName(myVisuGUI->moduleName()) : QString(""), aHelpFileName);
503   else {
504     QString platform;
505 #ifdef WIN32
506     platform = "winapplication";
507 #else
508     platform = "application";
509 #endif
510     SUIT_MessageBox::warning(0, QObject::tr("WRN_WARNING"),
511                              QObject::tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
512                              arg(app->resourceMgr()->stringValue("ExternalBrowser", platform)).arg(aHelpFileName),
513                              QObject::tr("BUT_OK"));
514   }
515 }
516
517 void VisuGUI_EditContainerDlg::keyPressEvent( QKeyEvent* e )
518 {
519   QDialog::keyPressEvent( e );
520   if ( e->isAccepted() )
521     return;
522
523   if ( e->key() == Qt::Key_F1 )
524     {
525       e->accept();
526       onHelp();
527     }
528 }