1 // Project MULTIPR, IOLS WP1.2.1 - EDF/CS
2 // Partitioning/decimation module for the SALOME v3.2 platform
5 * \file MULTIPR_GUI.cxx
7 * \brief see MULTIPR_GUI.h
9 * \author Olivier LE ROUX - CS, Virtual Reality Dpt
14 //*****************************************************************************
16 //*****************************************************************************
19 #include "MULTIPR_GUI.h"
20 #include "MULTIPR_GUI_Dlg.h"
21 #include "MULTIPR_Utils.hxx"
24 #include <SUIT_MessageBox.h>
25 #include <SUIT_ResourceMgr.h>
26 #include <SUIT_Session.h>
27 #include <SalomeApp_Application.h>
28 #include <SalomeApp_DataModel.h>
29 #include <SalomeApp_Study.h>
30 #include <SalomeApp_CheckFileDlg.h>
31 #include <LightApp_Study.h>
32 #include <LightApp_DataModel.h>
33 #include <LightApp_DataOwner.h>
34 #include <LightApp_SelectionMgr.h>
35 #include <CAM_DataModel.h>
36 #include <CAM_Module.h>
38 #include <SALOME_LifeCycleCORBA.hxx>
40 #include <QtxPopupMgr.h>
43 #include <qapplication.h>
44 #include <qinputdialog.h>
46 #include <qpushbutton.h>
47 #include <qgroupbox.h>
49 #include <qbuttongroup.h>
51 #include <qcombobox.h>
53 #include <qlineedit.h>
56 #include <qwhatsthis.h>
59 #include <qmessagebox.h>
68 //*****************************************************************************
70 //*****************************************************************************
74 // progress callback used by the MULTIPR library
75 extern MULTIPR_ProgressCallback* gProgressCallback;
79 //*****************************************************************************
81 //*****************************************************************************
84 MULTIPR_ORB::MULTIPR_Gen_ptr GetMultiprGen(const CAM_Module* theModule)
86 static MULTIPR_ORB::MULTIPR_Gen_ptr aGen = NULL;
90 SALOME_LifeCycleCORBA aLCC(SalomeApp_Application::namingService());
91 Engines::Component_var aComponent = aLCC.FindOrLoad_Component("FactoryServer", "MULTIPR");
92 aGen = MULTIPR_ORB::MULTIPR_Gen::_narrow(aComponent);
93 if (!CORBA::is_nil(aGen))
95 //aGen->SetCurrentStudy(GetDSStudy(GetCStudy(GetAppStudy(theModule))));
99 if (CORBA::is_nil(aGen))
100 throw std::runtime_error("Can't find MULTIPR component");
106 //*****************************************************************************
107 // Class MULTIPR_GUI implementation
108 //*****************************************************************************
110 MULTIPR_GUI::MULTIPR_GUI() : SalomeApp_Module("MULTIPR")
117 MULTIPR_GUI::~MULTIPR_GUI()
119 if (mMULTIPRObj != NULL)
121 CORBA::release(mMULTIPRObj);
126 MULTIPR_ORB::MULTIPR_Obj_ptr MULTIPR_GUI::getMULTIPRObj()
132 SalomeApp_Application* MULTIPR_GUI::getAppli() const
138 void MULTIPR_GUI::initialize(CAM_Application* app)
140 SalomeApp_Module::initialize(app);
144 QWidget* aParent = app->desktop();
145 SUIT_ResourceMgr* aResourceMgr = app->resourceMgr();
147 //-------------------------------------------------------------------------
149 //-------------------------------------------------------------------------
150 QPixmap aPixmapImportFromMEDFile = aResourceMgr->loadPixmap("MULTIPR", tr("MULTIPR_ICON_IMPORT_MED"));
154 tr("MULTIPR_TLT_IMPORT_FROM_MED_FILE"),
155 QIconSet(aPixmapImportFromMEDFile),
156 tr("MULTIPR_MEN_IMPORT_FROM_MED_FILE"),
157 tr("MULTIPR_STS_IMPORT_FROM_MED_FILE"),
162 SLOT(OnImportFromMEDFile()));
166 tr("MULTIPR_TLT_SPLIT"),
168 tr("MULTIPR_MEN_SPLIT"),
169 tr("MULTIPR_STS_SPLIT"),
174 SLOT(OnPartition2()));
178 tr("MULTIPR_TLT_DECIMATE"),
180 tr("MULTIPR_MEN_DECIMATE"),
181 tr("MULTIPR_STS_DECIMATE"),
190 tr("MULTIPR_TLT_REMOVE"),
192 tr("MULTIPR_MEN_REMOVE"),
193 tr("MULTIPR_STS_REMOVE"),
200 QPixmap aPixmapSaveMEDFile = aResourceMgr->loadPixmap("MULTIPR", tr("MULTIPR_ICON_SAVE_MED"));
204 tr("MULTIPR_TLT_SAVE"),
205 QIconSet(aPixmapSaveMEDFile),
206 tr("MULTIPR_MEN_SAVE"),
207 tr("MULTIPR_STS_SAVE"),
214 //-------------------------------------------------------------------------
216 //-------------------------------------------------------------------------
218 aMenuId = createMenu(tr("MULTIPR_MEN_FILE"), -1, -1);
219 createMenu(separator(), aMenuId, -1, 10);
220 aMenuId = createMenu(tr("MULTIPR_MEN_FILE_MULTIPR"), aMenuId, -1, 10);
221 createMenu(ACTION_IMPORT_MED, aMenuId);
223 aMenuId = createMenu(tr("MULTIPR_MEN_MULTIPR"), -1, -1, 30);
224 createMenu(ACTION_IMPORT_MED, aMenuId, 10);
225 createMenu(ACTION_SAVE, aMenuId, 10);
226 createMenu(ACTION_SPLIT, aMenuId, 10);
227 createMenu(ACTION_DECIMATE, aMenuId, 10);
228 createMenu(ACTION_REMOVE, aMenuId, 10);
230 //-------------------------------------------------------------------------
232 //-------------------------------------------------------------------------
233 int aToolId = createTool(tr("MULTIPR_TOOL_MULTIPR"));
234 createTool(ACTION_IMPORT_MED, aToolId);
235 createTool(ACTION_SAVE, aToolId);
237 //-------------------------------------------------------------------------
238 // create popup menus
239 //-------------------------------------------------------------------------
240 QtxPopupMgr* mgr = popupMgr();
241 mgr->insert( action(ACTION_SPLIT), -1, -1, -1 );
242 mgr->insert( action(ACTION_DECIMATE), -1, -1, -1 );
243 mgr->insert( action(ACTION_REMOVE), -1, -1, -1 );
244 mgr->insert( action(ACTION_SAVE), -1, -1, -1 );
246 QString aRule = "client='ObjectBrowser' and selcount>=1"; // $type in {'VISU::TMESH'}";
247 mgr->setRule(action(ACTION_SPLIT), aRule, true);
248 mgr->setRule(action(ACTION_DECIMATE), aRule, true);
249 mgr->setRule(action(ACTION_REMOVE), aRule, true);
250 mgr->setRule(action(ACTION_SAVE), aRule, true);
252 //-------------------------------------------------------------------------
253 // set progress dialog
254 //-------------------------------------------------------------------------
255 MULTIPR_GUI_ProgressCallbackDlg* progressDlg = new MULTIPR_GUI_ProgressCallbackDlg(application()->desktop());
256 multipr::gProgressCallback = progressDlg;
260 CAM_DataModel* MULTIPR_GUI::createDataModel()
262 return new MULTIPR_GUI_DataModel(this);
266 QString MULTIPR_GUI::engineIOR() const
268 CORBA::String_var anIOR = getApp()->orb()->object_to_string(GetMultiprGen(this));
269 return QString(anIOR.in());
273 bool MULTIPR_GUI::activateModule(SUIT_Study* theStudy)
275 bool bOk = SalomeApp_Module::activateModule(theStudy);
280 action(ACTION_IMPORT_MED)->setAccel(QKeySequence(CTRL + Key_I));
286 bool MULTIPR_GUI::deactivateModule(SUIT_Study* theStudy)
291 // Unset actions accelerator keys
292 action(ACTION_IMPORT_MED)->setAccel(QKeySequence());
294 return SalomeApp_Module::deactivateModule(theStudy);
298 void MULTIPR_GUI::windows(QMap<int, int>& theMap) const
301 theMap.insert(SalomeApp_Application::WT_ObjectBrowser, Qt::DockLeft);
302 theMap.insert(SalomeApp_Application::WT_PyConsole, Qt::DockBottom);
306 void MULTIPR_GUI::selected(QStringList& entries, const bool multiple)
308 LightApp_SelectionMgr* mgr = getApp()->selectionMgr();
312 SUIT_DataOwnerPtrList anOwnersList;
313 mgr->selected(anOwnersList);
315 for (int i = 0 ; i < anOwnersList.size() ; i++)
317 const LightApp_DataOwner* owner = dynamic_cast<const LightApp_DataOwner*>(anOwnersList[i].get());
319 if (!entries.contains(owner->entry()))
321 entries.append(owner->entry());
330 void MULTIPR_GUI::OnImportFromMEDFile()
334 aFilter.append(tr("MULTIPR_FLT_MED_FILES"));
335 aFilter.append(tr("MULTIPR_FLT_ALL_FILES"));
337 SalomeApp_CheckFileDlg* fd = new SalomeApp_CheckFileDlg(
338 this->application()->desktop(),
342 fd->setCaption(tr("MULTIPR_MEN_IMPORT_FROM_MED_FILE"));
343 fd->setFilters(aFilter);
344 if (fd->exec() == QDialog::Rejected)
350 QFileInfo aFileInfo(fd->selectedFile());
353 // Check the file name
354 if (!aFileInfo.exists())
357 mMEDFileName = aFileInfo.filePath();
359 QApplication::setOverrideCursor(Qt::waitCursor);
363 MULTIPR_ORB::MULTIPR_Gen_ptr multiprgen = GetMultiprGen(this);
364 mMULTIPRObj = multiprgen->getObject(mMEDFileName.latin1());
368 SUIT_MessageBox::error1(
370 "Import MED file error",
371 "Invalid MED file (not recognized by MULTIPR)",
372 tr("MULTIPR_BUT_OK") );
374 QApplication::restoreOverrideCursor();
376 if (mMULTIPRObj != NULL)
380 if (mMULTIPRObj->isValidSequentialMEDFile())
389 getApp()->updateObjectBrowser();
394 void MULTIPR_GUI::OnPartition1()
396 // check if MULTIPRObj exists
397 if (mMULTIPRObj == NULL)
402 MULTIPR_GUI_Partition1Dlg* dialog = new MULTIPR_GUI_Partition1Dlg(this);
408 void MULTIPR_GUI::OnPartition2()
410 // check if MULTIPRObj exists
411 if (mMULTIPRObj == NULL)
416 retrieveSelectedParts();
418 if (mSelectedParts.count() == 0)
420 SUIT_MessageBox::warn1(
424 tr("MULTIPR_BUT_OK") );
428 if (!removeLowerResolution())
433 MULTIPR_GUI_Partition2Dlg* dialog = new MULTIPR_GUI_Partition2Dlg(this);
436 getApp()->updateObjectBrowser();
440 void MULTIPR_GUI::OnDecimate()
442 // check if MULTIPRObj exists
443 if (mMULTIPRObj == NULL)
448 retrieveSelectedParts();
450 if (mSelectedParts.count() == 0)
452 SUIT_MessageBox::warn1(
454 "Decimation warning",
456 tr("MULTIPR_BUT_OK") );
460 if (!removeLowerResolution())
465 MULTIPR_GUI_DecimateDlg* dialog = new MULTIPR_GUI_DecimateDlg(this);
468 getApp()->updateObjectBrowser();
472 void MULTIPR_GUI::OnRemove()
474 // check if MULTIPRObj exists
475 if (mMULTIPRObj == NULL)
480 retrieveSelectedParts();
482 if (mSelectedParts.count() == 0)
484 SUIT_MessageBox::warn1(
488 tr("MULTIPR_BUT_OK") );
492 if (QMessageBox::question(
494 tr("Remove selected part(s)"),
495 tr("Do you want to remove selected part(s)?"),
496 tr("&Yes"), tr("&No"),
497 QString::null, 0, 1 ) )
502 QApplication::setOverrideCursor(Qt::waitCursor);
506 for (QStringList::const_iterator it = mSelectedParts.begin(), last = mSelectedParts.end(); it != last; it++)
508 const QString& partName = (*it);
509 cout << "Remove " << partName.latin1() << endl;
510 mMULTIPRObj->removeParts(partName.latin1());
516 SUIT_MessageBox::error1(
519 "Error while removing selected part(s)",
520 tr("MULTIPR_BUT_OK") );
523 QApplication::restoreOverrideCursor();
525 getApp()->updateObjectBrowser();
529 void MULTIPR_GUI::OnSave()
531 // check if MULTIPRObj exists
532 if (mMULTIPRObj == NULL)
537 SalomeApp_CheckFileDlg* fd = new SalomeApp_CheckFileDlg(
538 this->application()->desktop(),
542 fd->setCaption(tr("Save distributed MED file - Destination directory"));
543 fd->setMode(QFileDialog::DirectoryOnly);
545 if (fd->exec() == QDialog::Rejected)
551 QFileInfo aFileInfo(fd->selectedFile());
554 QString path = aFileInfo.filePath();
556 QApplication::setOverrideCursor(Qt::waitCursor);
560 mMULTIPRObj->save(path);
561 getApp()->updateObjectBrowser();
565 SUIT_MessageBox::error1(
567 "Save distributed MED file error",
568 "Error while writing distributed MED file",
569 tr("MULTIPR_BUT_OK") );
572 QApplication::restoreOverrideCursor();
577 void MULTIPR_GUI::retrieveSelectedParts()
579 mSelectedParts.clear();
581 QStringList userSelection;
582 selected(userSelection, true);
583 for (QStringList::const_iterator it = userSelection.begin(), last = userSelection.end(); it != last; it++)
585 const QString& str = (*it);
586 QStringList words = QStringList::split(":", str);
587 if (words.count() == 2)
589 if (words[0] == "MULTIPR_PART")
591 mSelectedParts.push_back(words[1]);
598 bool MULTIPR_GUI::isPartExist(const char* partName)
600 if (mMULTIPRObj == NULL) return false;
602 MULTIPR_ORB::string_array* listParts = mMULTIPRObj->getParts();
603 for (int i=0 ; i<listParts->length() ; i++)
605 const char* strItem = (*listParts)[i];
606 if (strcmp(strItem, partName) == 0)
615 bool MULTIPR_GUI::removeLowerResolution()
617 // for each selected part, check if there are lower resolution
618 // and then propose to remove them before performing new process
619 QStringList partNameLowerResolution;
620 for (QStringList::const_iterator it = mSelectedParts.begin(), last = mSelectedParts.end(); it != last; it++)
622 const QString& partName = (*it);
623 QString partNameLow = partName + "_LOW";
624 QString partNameMed = partName + "_MED";
625 const char* strPartNameLow = partNameLow.latin1();
626 const char* strPartNameMed = partNameMed.latin1();
628 if (isPartExist(strPartNameLow))
630 partNameLowerResolution.push_back(partNameLow);
631 cout << "Part to be removed: " << partNameLow << endl;
634 if (isPartExist(strPartNameMed))
636 partNameLowerResolution.push_back(partNameMed);
637 cout << "Part to be removed: " << partNameMed << endl;
641 if (partNameLowerResolution.count() > 0)
643 if (QMessageBox::question(
645 tr("Remove previous results"),
646 tr("Do you want to remove previous results?"),
647 tr("&Yes"), tr("&No"),
648 QString::null, 0, 1 ) )
653 QApplication::setOverrideCursor(Qt::waitCursor);
657 for (QStringList::const_iterator it = partNameLowerResolution.begin(), last = partNameLowerResolution.end(); it != last; it++)
659 const QString& partName = (*it);
660 cout << "Remove " << partName.latin1() << endl;
661 mMULTIPRObj->removeParts(partName.latin1());
667 SUIT_MessageBox::error1(
670 "Error while removing previous results",
671 tr("MULTIPR_BUT_OK") );
674 QApplication::restoreOverrideCursor();
676 getApp()->updateObjectBrowser();
683 //*****************************************************************************
684 // Super class Data Object implementation
685 //*****************************************************************************
687 MULTIPR_GUI_DataObject::MULTIPR_GUI_DataObject(SUIT_DataObject* parent, const char* name) :
688 LightApp_DataObject(parent),
689 CAM_DataObject(parent)
695 MULTIPR_GUI_DataObject::~MULTIPR_GUI_DataObject()
701 QString MULTIPR_GUI_DataObject::entry() const
703 return QString("MULTIPR_OBJECT");
707 QString MULTIPR_GUI_DataObject::name() const
713 QPixmap MULTIPR_GUI_DataObject::icon() const
715 //static QPixmap icon = SUIT_Session::session()->resourceMgr()->loadPixmap("MULTIPR", QObject::tr("ICON_IMPORT_MED"), false);
721 QString MULTIPR_GUI_DataObject::toolTip() const
723 // default behaviour: return an empty string
728 //*****************************************************************************
729 // Class Data Object Module implementation
730 //*****************************************************************************
732 MULTIPR_GUI_DataObject_Module::MULTIPR_GUI_DataObject_Module(CAM_DataModel* dm, SUIT_DataObject* parent, const char* name) :
733 MULTIPR_GUI_DataObject(parent, name),
734 LightApp_ModuleObject(dm, parent),
735 CAM_DataObject(parent)
741 MULTIPR_GUI_DataObject_Module::~MULTIPR_GUI_DataObject_Module()
747 QString MULTIPR_GUI_DataObject_Module::entry() const
749 return QString("MULTIPR_MODULE:" + mName);
753 QString MULTIPR_GUI_DataObject_Module::name() const
755 return CAM_RootObject::name();
759 QPixmap MULTIPR_GUI_DataObject_Module::icon() const
765 QString MULTIPR_GUI_DataObject_Module::toolTip() const
767 return QString("Module MULTIPR");
771 //*****************************************************************************
772 // Class Data Object Mesh implementation
773 //*****************************************************************************
775 MULTIPR_GUI_DataObject_Mesh::MULTIPR_GUI_DataObject_Mesh(SUIT_DataObject* parent, const char* name) :
776 MULTIPR_GUI_DataObject(parent, name),
777 CAM_DataObject(parent)
783 MULTIPR_GUI_DataObject_Mesh::~MULTIPR_GUI_DataObject_Mesh()
789 QString MULTIPR_GUI_DataObject_Mesh::entry() const
791 return QString("MULTIPR_MESH:") + mName;
795 QPixmap MULTIPR_GUI_DataObject_Mesh::icon() const
801 QString MULTIPR_GUI_DataObject_Mesh::toolTip() const
803 return QString("Original mesh");
807 //*****************************************************************************
808 // Class Data Object Part implementation
809 //*****************************************************************************
811 MULTIPR_GUI_DataObject_Part::MULTIPR_GUI_DataObject_Part(SUIT_DataObject* parent, const char* name, const char* info) :
812 MULTIPR_GUI_DataObject(parent, name),
813 CAM_DataObject(parent)
822 // parse info to retrieve all the fields
827 char lMEDFileName[256];
829 int ret = sscanf(info, "%s %d %s %s %s",
836 // number of read parameters should be 5
837 if (ret != 5) return;
839 mMeshName = lMeshName;
842 mMEDFileName = lMEDFileName;
846 MULTIPR_GUI_DataObject_Part::~MULTIPR_GUI_DataObject_Part()
852 QString MULTIPR_GUI_DataObject_Part::entry() const
854 return QString("MULTIPR_PART:") + mName;
858 QPixmap MULTIPR_GUI_DataObject_Part::icon() const
864 QString MULTIPR_GUI_DataObject_Part::toolTip() const
870 //*****************************************************************************
871 // Class Data Object Resolution implementation
872 //*****************************************************************************
874 MULTIPR_GUI_DataObject_Resolution::MULTIPR_GUI_DataObject_Resolution(SUIT_DataObject* parent, const char* name, const char* info) :
875 MULTIPR_GUI_DataObject_Part(parent, name, info),
876 CAM_DataObject(parent)
882 MULTIPR_GUI_DataObject_Resolution::~MULTIPR_GUI_DataObject_Resolution()
888 QString MULTIPR_GUI_DataObject_Resolution::entry() const
890 return QString("MULTIPR_RESOLUTION:") + mName;
894 QPixmap MULTIPR_GUI_DataObject_Resolution::icon() const
900 QString MULTIPR_GUI_DataObject_Resolution::toolTip() const
906 //*****************************************************************************
908 //*****************************************************************************
910 MULTIPR_GUI_DataModel::MULTIPR_GUI_DataModel(CAM_Module* module) :
911 LightApp_DataModel(module)
913 mMULTIPR_GUI = dynamic_cast<MULTIPR_GUI*>(module);
917 MULTIPR_GUI_DataModel::~MULTIPR_GUI_DataModel()
923 void MULTIPR_GUI_DataModel::build()
927 MULTIPR_GUI_DataObject_Module* modelRoot = dynamic_cast<MULTIPR_GUI_DataObject_Module*>(root());
931 // root is not set yet
932 modelRoot = new MULTIPR_GUI_DataObject_Module(this, NULL, "MULTIPR");
936 MULTIPR_ORB::MULTIPR_Obj_ptr obj = mMULTIPR_GUI->getMULTIPRObj();
940 MULTIPR_ORB::string_array* listParts = obj->getParts();
942 if (listParts->length() >= 1)
944 const char* strPartName0 = (*listParts)[0];
945 char* strPartInfo0 = obj->getPartInfo(strPartName0);
951 char lMEDFileName[256];
954 int ret = sscanf(strPartInfo0, "%s %d %s %s %s",
963 cout << "MULTIPR: build() tree; error while parsing part info" << endl;
964 std::runtime_error("MULTIPR: build() tree; error while parsing part info");
968 MULTIPR_GUI_DataObject_Mesh* dataObjectMesh = new MULTIPR_GUI_DataObject_Mesh(modelRoot, lMeshName);
970 MULTIPR_GUI_DataObject_Part* dataObjectPart_prev = NULL;
972 for (int i = 0 ; i < listParts->length() ; i++)
974 const char* strItem = (*listParts)[i];
975 char* strPartInfo = obj->getPartInfo(strItem);
978 int ret = sscanf(strPartInfo, "%s %d %s %s %s",
985 if (ret != 5) return;
987 if ((strstr(lPartName,"_MED") != NULL) || (strstr(lPartName,"_LOW") != NULL))
989 new MULTIPR_GUI_DataObject_Resolution(dataObjectPart_prev, strItem, strPartInfo);
993 dataObjectPart_prev = new MULTIPR_GUI_DataObject_Part(dataObjectMesh, strItem, strPartInfo);
1007 CAM_Module* createModule()
1009 return new MULTIPR_GUI();