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;
76 extern MULTIPR_EmptyMeshCallback* gEmptyMeshCallback;
80 //*****************************************************************************
82 //*****************************************************************************
85 MULTIPR_ORB::MULTIPR_Gen_ptr GetMultiprGen(const CAM_Module* theModule)
87 static MULTIPR_ORB::MULTIPR_Gen_ptr aGen = NULL;
91 SALOME_LifeCycleCORBA aLCC(SalomeApp_Application::namingService());
92 Engines::Component_var aComponent = aLCC.FindOrLoad_Component("FactoryServer", "MULTIPR");
93 aGen = MULTIPR_ORB::MULTIPR_Gen::_narrow(aComponent);
94 if (!CORBA::is_nil(aGen))
96 //aGen->SetCurrentStudy(GetDSStudy(GetCStudy(GetAppStudy(theModule))));
100 if (CORBA::is_nil(aGen))
101 throw std::runtime_error("Can't find MULTIPR component");
107 //*****************************************************************************
108 // Class MULTIPR_GUI implementation
109 //*****************************************************************************
111 MULTIPR_GUI::MULTIPR_GUI() : SalomeApp_Module("MULTIPR")
118 MULTIPR_GUI::~MULTIPR_GUI()
120 if (mMULTIPRObj != NULL)
122 CORBA::release(mMULTIPRObj);
127 MULTIPR_ORB::MULTIPR_Obj_ptr MULTIPR_GUI::getMULTIPRObj()
133 SalomeApp_Application* MULTIPR_GUI::getAppli() const
139 void MULTIPR_GUI::initialize(CAM_Application* app)
141 SalomeApp_Module::initialize(app);
145 QWidget* aParent = app->desktop();
146 SUIT_ResourceMgr* aResourceMgr = app->resourceMgr();
148 //-------------------------------------------------------------------------
150 //-------------------------------------------------------------------------
151 QPixmap aPixmapImportFromMEDFile = aResourceMgr->loadPixmap("MULTIPR", tr("MULTIPR_ICON_IMPORT_MED"));
155 tr("MULTIPR_TLT_IMPORT_FROM_MED_FILE"),
156 QIconSet(aPixmapImportFromMEDFile),
157 tr("MULTIPR_MEN_IMPORT_FROM_MED_FILE"),
158 tr("MULTIPR_STS_IMPORT_FROM_MED_FILE"),
163 SLOT(OnImportFromMEDFile()));
167 tr("MULTIPR_TLT_SPLIT"),
169 tr("MULTIPR_MEN_SPLIT"),
170 tr("MULTIPR_STS_SPLIT"),
175 SLOT(OnPartition2()));
179 tr("MULTIPR_TLT_DECIMATE"),
181 tr("MULTIPR_MEN_DECIMATE"),
182 tr("MULTIPR_STS_DECIMATE"),
191 tr("MULTIPR_TLT_REMOVE"),
193 tr("MULTIPR_MEN_REMOVE"),
194 tr("MULTIPR_STS_REMOVE"),
201 QPixmap aPixmapSaveMEDFile = aResourceMgr->loadPixmap("MULTIPR", tr("MULTIPR_ICON_SAVE_MED"));
205 tr("MULTIPR_TLT_SAVE"),
206 QIconSet(aPixmapSaveMEDFile),
207 tr("MULTIPR_MEN_SAVE"),
208 tr("MULTIPR_STS_SAVE"),
215 //-------------------------------------------------------------------------
217 //-------------------------------------------------------------------------
219 aMenuId = createMenu(tr("MULTIPR_MEN_FILE"), -1, -1);
220 createMenu(separator(), aMenuId, -1, 10);
221 aMenuId = createMenu(tr("MULTIPR_MEN_FILE_MULTIPR"), aMenuId, -1, 10);
222 createMenu(ACTION_IMPORT_MED, aMenuId);
224 aMenuId = createMenu(tr("MULTIPR_MEN_MULTIPR"), -1, -1, 30);
225 createMenu(ACTION_IMPORT_MED, aMenuId, 10);
226 createMenu(ACTION_SAVE, aMenuId, 10);
227 createMenu(ACTION_SPLIT, aMenuId, 10);
228 createMenu(ACTION_DECIMATE, aMenuId, 10);
229 createMenu(ACTION_REMOVE, aMenuId, 10);
231 //-------------------------------------------------------------------------
233 //-------------------------------------------------------------------------
234 int aToolId = createTool(tr("MULTIPR_TOOL_MULTIPR"));
235 createTool(ACTION_IMPORT_MED, aToolId);
236 createTool(ACTION_SAVE, aToolId);
238 //-------------------------------------------------------------------------
239 // create popup menus
240 //-------------------------------------------------------------------------
241 QtxPopupMgr* mgr = popupMgr();
242 mgr->insert( action(ACTION_SPLIT), -1, -1, -1 );
243 mgr->insert( action(ACTION_DECIMATE), -1, -1, -1 );
244 mgr->insert( action(ACTION_REMOVE), -1, -1, -1 );
245 mgr->insert( action(ACTION_SAVE), -1, -1, -1 );
247 QString aRule = "client='ObjectBrowser' and selcount>=1"; // $type in {'VISU::TMESH'}";
248 mgr->setRule(action(ACTION_SPLIT), aRule, true);
249 mgr->setRule(action(ACTION_DECIMATE), aRule, true);
250 mgr->setRule(action(ACTION_REMOVE), aRule, true);
251 mgr->setRule(action(ACTION_SAVE), aRule, true);
253 //-------------------------------------------------------------------------
254 // set progress dialog
255 //-------------------------------------------------------------------------
256 MULTIPR_GUI_ProgressCallbackDlg* progressDlg = new MULTIPR_GUI_ProgressCallbackDlg(application()->desktop());
257 multipr::gProgressCallback = progressDlg;
259 MULTIPR_GUI_EmptyMeshCallbackDlg* emptyMeshDlg = new MULTIPR_GUI_EmptyMeshCallbackDlg(application()->desktop());
260 multipr::gEmptyMeshCallback = emptyMeshDlg;
264 CAM_DataModel* MULTIPR_GUI::createDataModel()
266 return new MULTIPR_GUI_DataModel(this);
270 QString MULTIPR_GUI::engineIOR() const
272 CORBA::String_var anIOR = getApp()->orb()->object_to_string(GetMultiprGen(this));
273 return QString(anIOR.in());
277 bool MULTIPR_GUI::activateModule(SUIT_Study* theStudy)
279 bool bOk = SalomeApp_Module::activateModule(theStudy);
284 action(ACTION_IMPORT_MED)->setAccel(QKeySequence(CTRL + Key_I));
290 bool MULTIPR_GUI::deactivateModule(SUIT_Study* theStudy)
295 // Unset actions accelerator keys
296 action(ACTION_IMPORT_MED)->setAccel(QKeySequence());
298 return SalomeApp_Module::deactivateModule(theStudy);
302 void MULTIPR_GUI::windows(QMap<int, int>& theMap) const
305 theMap.insert(SalomeApp_Application::WT_ObjectBrowser, Qt::DockLeft);
306 theMap.insert(SalomeApp_Application::WT_PyConsole, Qt::DockBottom);
310 void MULTIPR_GUI::selected(QStringList& entries, const bool multiple)
312 LightApp_SelectionMgr* mgr = getApp()->selectionMgr();
316 SUIT_DataOwnerPtrList anOwnersList;
317 mgr->selected(anOwnersList);
319 for (int i = 0 ; i < anOwnersList.size() ; i++)
321 const LightApp_DataOwner* owner = dynamic_cast<const LightApp_DataOwner*>(anOwnersList[i].get());
323 if (!entries.contains(owner->entry()))
325 entries.append(owner->entry());
334 void MULTIPR_GUI::OnImportFromMEDFile()
338 aFilter.append(tr("MULTIPR_FLT_MED_FILES"));
339 aFilter.append(tr("MULTIPR_FLT_ALL_FILES"));
341 SalomeApp_CheckFileDlg* fd = new SalomeApp_CheckFileDlg(
342 this->application()->desktop(),
346 fd->setCaption(tr("MULTIPR_MEN_IMPORT_FROM_MED_FILE"));
347 fd->setFilters(aFilter);
348 if (fd->exec() == QDialog::Rejected)
354 QFileInfo aFileInfo(fd->selectedFile());
357 // Check the file name
358 if (!aFileInfo.exists())
361 mMEDFileName = aFileInfo.filePath();
363 QApplication::setOverrideCursor(Qt::waitCursor);
367 MULTIPR_ORB::MULTIPR_Gen_ptr multiprgen = GetMultiprGen(this);
368 mMULTIPRObj = multiprgen->getObject(mMEDFileName.latin1());
372 SUIT_MessageBox::error1(
374 "Import MED file error",
375 "Invalid MED file (not recognized by MULTIPR)",
376 tr("MULTIPR_BUT_OK") );
378 QApplication::restoreOverrideCursor();
380 if (mMULTIPRObj != NULL)
384 if (mMULTIPRObj->isValidSequentialMEDFile())
393 getApp()->updateObjectBrowser();
398 void MULTIPR_GUI::OnPartition1()
400 // check if MULTIPRObj exists
401 if (mMULTIPRObj == NULL)
406 MULTIPR_GUI_Partition1Dlg* dialog = new MULTIPR_GUI_Partition1Dlg(this);
412 void MULTIPR_GUI::OnPartition2()
414 // check if MULTIPRObj exists
415 if (mMULTIPRObj == NULL)
420 retrieveSelectedParts();
422 if (mSelectedParts.count() == 0)
424 SUIT_MessageBox::warn1(
428 tr("MULTIPR_BUT_OK") );
432 if (!removeLowerResolution())
437 MULTIPR_GUI_Partition2Dlg* dialog = new MULTIPR_GUI_Partition2Dlg(this);
440 getApp()->updateObjectBrowser();
444 void MULTIPR_GUI::OnDecimate()
446 // check if MULTIPRObj exists
447 if (mMULTIPRObj == NULL)
452 retrieveSelectedParts();
454 if (mSelectedParts.count() == 0)
456 SUIT_MessageBox::warn1(
458 "Decimation warning",
460 tr("MULTIPR_BUT_OK") );
464 if (!removeLowerResolution())
469 MULTIPR_GUI_DecimateDlg* dialog = new MULTIPR_GUI_DecimateDlg(this);
472 getApp()->updateObjectBrowser();
476 void MULTIPR_GUI::OnRemove()
478 // check if MULTIPRObj exists
479 if (mMULTIPRObj == NULL)
484 retrieveSelectedParts();
486 if (mSelectedParts.count() == 0)
488 SUIT_MessageBox::warn1(
492 tr("MULTIPR_BUT_OK") );
496 if (QMessageBox::question(
498 tr("Remove selected part(s)"),
499 tr("Do you want to remove selected part(s)?"),
500 tr("&Yes"), tr("&No"),
501 QString::null, 0, 1 ) )
506 QApplication::setOverrideCursor(Qt::waitCursor);
510 for (QStringList::const_iterator it = mSelectedParts.begin(), last = mSelectedParts.end(); it != last; it++)
512 const QString& partName = (*it);
513 cout << "Remove " << partName.latin1() << endl;
514 mMULTIPRObj->removeParts(partName.latin1());
520 SUIT_MessageBox::error1(
523 "Error while removing selected part(s)",
524 tr("MULTIPR_BUT_OK") );
527 QApplication::restoreOverrideCursor();
529 getApp()->updateObjectBrowser();
533 void MULTIPR_GUI::OnSave()
535 // check if MULTIPRObj exists
536 if (mMULTIPRObj == NULL)
541 SalomeApp_CheckFileDlg* fd = new SalomeApp_CheckFileDlg(
542 this->application()->desktop(),
546 fd->setCaption(tr("Save distributed MED file - Destination directory"));
547 fd->setMode(QFileDialog::DirectoryOnly);
549 if (fd->exec() == QDialog::Rejected)
555 QFileInfo aFileInfo(fd->selectedFile());
558 QString path = aFileInfo.filePath();
560 QApplication::setOverrideCursor(Qt::waitCursor);
564 mMULTIPRObj->save(path);
565 getApp()->updateObjectBrowser();
569 SUIT_MessageBox::error1(
571 "Save distributed MED file error",
572 "Error while writing distributed MED file",
573 tr("MULTIPR_BUT_OK") );
576 QApplication::restoreOverrideCursor();
581 void MULTIPR_GUI::retrieveSelectedParts()
583 mSelectedParts.clear();
585 QStringList userSelection;
586 selected(userSelection, true);
587 for (QStringList::const_iterator it = userSelection.begin(), last = userSelection.end(); it != last; it++)
589 const QString& str = (*it);
590 QStringList words = QStringList::split(":", str);
591 if (words.count() == 2)
593 if (words[0] == "MULTIPR_PART")
595 mSelectedParts.push_back(words[1]);
602 bool MULTIPR_GUI::isPartExist(const char* partName)
604 if (mMULTIPRObj == NULL) return false;
606 MULTIPR_ORB::string_array* listParts = mMULTIPRObj->getParts();
607 for (int i=0 ; i<listParts->length() ; i++)
609 const char* strItem = (*listParts)[i];
610 if (strcmp(strItem, partName) == 0)
619 bool MULTIPR_GUI::removeLowerResolution()
621 // for each selected part, check if there are lower resolution
622 // and then propose to remove them before performing new process
623 QStringList partNameLowerResolution;
624 for (QStringList::const_iterator it = mSelectedParts.begin(), last = mSelectedParts.end(); it != last; it++)
626 const QString& partName = (*it);
627 QString partNameLow = partName + "_LOW";
628 QString partNameMed = partName + "_MED";
629 const char* strPartNameLow = partNameLow.latin1();
630 const char* strPartNameMed = partNameMed.latin1();
632 if (isPartExist(strPartNameLow))
634 partNameLowerResolution.push_back(partNameLow);
635 cout << "Part to be removed: " << partNameLow << endl;
638 if (isPartExist(strPartNameMed))
640 partNameLowerResolution.push_back(partNameMed);
641 cout << "Part to be removed: " << partNameMed << endl;
645 if (partNameLowerResolution.count() > 0)
647 if (QMessageBox::question(
649 tr("Remove previous results"),
650 tr("Do you want to remove previous results?"),
651 tr("&Yes"), tr("&No"),
652 QString::null, 0, 1 ) )
657 QApplication::setOverrideCursor(Qt::waitCursor);
661 for (QStringList::const_iterator it = partNameLowerResolution.begin(), last = partNameLowerResolution.end(); it != last; it++)
663 const QString& partName = (*it);
664 cout << "Remove " << partName.latin1() << endl;
665 mMULTIPRObj->removeParts(partName.latin1());
671 SUIT_MessageBox::error1(
674 "Error while removing previous results",
675 tr("MULTIPR_BUT_OK") );
678 QApplication::restoreOverrideCursor();
680 getApp()->updateObjectBrowser();
687 //*****************************************************************************
688 // Super class Data Object implementation
689 //*****************************************************************************
691 MULTIPR_GUI_DataObject::MULTIPR_GUI_DataObject(SUIT_DataObject* parent, const char* name) :
692 LightApp_DataObject(parent),
693 CAM_DataObject(parent)
699 MULTIPR_GUI_DataObject::~MULTIPR_GUI_DataObject()
705 QString MULTIPR_GUI_DataObject::entry() const
707 return QString("MULTIPR_OBJECT");
711 QString MULTIPR_GUI_DataObject::name() const
717 QPixmap MULTIPR_GUI_DataObject::icon() const
719 //static QPixmap icon = SUIT_Session::session()->resourceMgr()->loadPixmap("MULTIPR", QObject::tr("ICON_IMPORT_MED"), false);
725 QString MULTIPR_GUI_DataObject::toolTip() const
727 // default behaviour: return an empty string
732 //*****************************************************************************
733 // Class Data Object Module implementation
734 //*****************************************************************************
736 MULTIPR_GUI_DataObject_Module::MULTIPR_GUI_DataObject_Module(CAM_DataModel* dm, SUIT_DataObject* parent, const char* name) :
737 MULTIPR_GUI_DataObject(parent, name),
738 LightApp_ModuleObject(dm, parent),
739 CAM_DataObject(parent)
745 MULTIPR_GUI_DataObject_Module::~MULTIPR_GUI_DataObject_Module()
751 QString MULTIPR_GUI_DataObject_Module::entry() const
753 return QString("MULTIPR_MODULE:" + mName);
757 QString MULTIPR_GUI_DataObject_Module::name() const
759 return CAM_RootObject::name();
763 QPixmap MULTIPR_GUI_DataObject_Module::icon() const
769 QString MULTIPR_GUI_DataObject_Module::toolTip() const
771 return QString("Module MULTIPR");
775 //*****************************************************************************
776 // Class Data Object Mesh implementation
777 //*****************************************************************************
779 MULTIPR_GUI_DataObject_Mesh::MULTIPR_GUI_DataObject_Mesh(SUIT_DataObject* parent, const char* name) :
780 MULTIPR_GUI_DataObject(parent, name),
781 CAM_DataObject(parent)
787 MULTIPR_GUI_DataObject_Mesh::~MULTIPR_GUI_DataObject_Mesh()
793 QString MULTIPR_GUI_DataObject_Mesh::entry() const
795 return QString("MULTIPR_MESH:") + mName;
799 QPixmap MULTIPR_GUI_DataObject_Mesh::icon() const
805 QString MULTIPR_GUI_DataObject_Mesh::toolTip() const
807 return QString("Original mesh");
811 //*****************************************************************************
812 // Class Data Object Part implementation
813 //*****************************************************************************
815 MULTIPR_GUI_DataObject_Part::MULTIPR_GUI_DataObject_Part(SUIT_DataObject* parent, const char* name, const char* info) :
816 MULTIPR_GUI_DataObject(parent, name),
817 CAM_DataObject(parent)
826 // parse info to retrieve all the fields
831 char lMEDFileName[256];
833 int ret = sscanf(info, "%s %d %s %s %s",
840 // number of read parameters should be 5
841 if (ret != 5) return;
843 mMeshName = lMeshName;
846 mMEDFileName = lMEDFileName;
850 MULTIPR_GUI_DataObject_Part::~MULTIPR_GUI_DataObject_Part()
856 QString MULTIPR_GUI_DataObject_Part::entry() const
858 return QString("MULTIPR_PART:") + mName;
862 QPixmap MULTIPR_GUI_DataObject_Part::icon() const
868 QString MULTIPR_GUI_DataObject_Part::toolTip() const
874 //*****************************************************************************
875 // Class Data Object Resolution implementation
876 //*****************************************************************************
878 MULTIPR_GUI_DataObject_Resolution::MULTIPR_GUI_DataObject_Resolution(SUIT_DataObject* parent, const char* name, const char* info) :
879 MULTIPR_GUI_DataObject_Part(parent, name, info),
880 CAM_DataObject(parent)
886 MULTIPR_GUI_DataObject_Resolution::~MULTIPR_GUI_DataObject_Resolution()
892 QString MULTIPR_GUI_DataObject_Resolution::entry() const
894 return QString("MULTIPR_RESOLUTION:") + mName;
898 QPixmap MULTIPR_GUI_DataObject_Resolution::icon() const
904 QString MULTIPR_GUI_DataObject_Resolution::toolTip() const
910 //*****************************************************************************
912 //*****************************************************************************
914 MULTIPR_GUI_DataModel::MULTIPR_GUI_DataModel(CAM_Module* module) :
915 LightApp_DataModel(module)
917 mMULTIPR_GUI = dynamic_cast<MULTIPR_GUI*>(module);
921 MULTIPR_GUI_DataModel::~MULTIPR_GUI_DataModel()
927 void MULTIPR_GUI_DataModel::build()
931 MULTIPR_GUI_DataObject_Module* modelRoot = dynamic_cast<MULTIPR_GUI_DataObject_Module*>(root());
935 // root is not set yet
936 modelRoot = new MULTIPR_GUI_DataObject_Module(this, NULL, "MULTIPR");
940 MULTIPR_ORB::MULTIPR_Obj_ptr obj = mMULTIPR_GUI->getMULTIPRObj();
944 MULTIPR_ORB::string_array* listParts = obj->getParts();
946 if (listParts->length() >= 1)
948 const char* strPartName0 = (*listParts)[0];
949 char* strPartInfo0 = obj->getPartInfo(strPartName0);
955 char lMEDFileName[256];
958 int ret = sscanf(strPartInfo0, "%s %d %s %s %s",
967 cout << "MULTIPR: build() tree; error while parsing part info" << endl;
968 std::runtime_error("MULTIPR: build() tree; error while parsing part info");
972 MULTIPR_GUI_DataObject_Mesh* dataObjectMesh = new MULTIPR_GUI_DataObject_Mesh(modelRoot, lMeshName);
974 MULTIPR_GUI_DataObject_Part* dataObjectPart_prev = NULL;
976 for (int i = 0 ; i < listParts->length() ; i++)
978 const char* strItem = (*listParts)[i];
979 char* strPartInfo = obj->getPartInfo(strItem);
982 int ret = sscanf(strPartInfo, "%s %d %s %s %s",
989 if (ret != 5) return;
991 if ((strstr(lPartName,"_MED") != NULL) || (strstr(lPartName,"_LOW") != NULL))
993 new MULTIPR_GUI_DataObject_Resolution(dataObjectPart_prev, strItem, strPartInfo);
997 dataObjectPart_prev = new MULTIPR_GUI_DataObject_Part(dataObjectMesh, strItem, strPartInfo);
1011 CAM_Module* createModule()
1013 return new MULTIPR_GUI();