+
+/*!
+ \brief Called when application is closed.
+
+ Process finalize application functionality from ParaView in order to save server settings
+ and nullify application pointer if the application is being closed.
+
+ \param theApp application
+*/
+void PVGUI_Module::onApplicationClosed( SUIT_Application* theApp )
+{
+ PVViewer_ViewManager::ParaviewCleanup();
+
+ int aAppsNb = SUIT_Session::session()->applications().size();
+ if (aAppsNb == 1) {
+ deleteTemporaryFiles();
+ }
+ CAM_Module::onApplicationClosed(theApp);
+}
+
+
+/*!
+ \brief Deletes temporary files created during import operation from VISU
+*/
+void PVGUI_Module::deleteTemporaryFiles()
+{
+ foreach(QString aFile, myTemporaryFiles) {
+ if (QFile::exists(aFile)) {
+ QFile::remove(aFile);
+ }
+ }
+}
+
+
+/*!
+ \brief Called when study is closed.
+
+ Removes data model from the \a study.
+
+ \param study study being closed
+*/
+void PVGUI_Module::studyClosed(SUIT_Study* study)
+{
+ showView(false);
+ clearParaviewState();
+
+ LightApp_Module::studyClosed(study);
+}
+
+/*!
+ \brief Called when study is opened.
+*/
+void PVGUI_Module::onModelOpened()
+{
+ _PTR(Study) studyDS = PARAVIS::GetCStudy(this);
+ if(!studyDS) {
+ return;
+ }
+
+ _PTR(SComponent) paravisComp =
+ studyDS->FindComponent(PARAVIS_MODULE_NAME);
+ if(!paravisComp) {
+ return;
+ }
+
+ _PTR(ChildIterator) anIter(studyDS->NewChildIterator(paravisComp));
+ for (; anIter->More(); anIter->Next()) {
+ _PTR(SObject) aSObj = anIter->Value();
+ _PTR(GenericAttribute) anAttr;
+ if (!aSObj->FindAttribute(anAttr, "AttributeLocalID")) {
+ continue;
+ }
+ _PTR(AttributeLocalID) anID(anAttr);
+ if (anID->Value() == PVSTATEID) {
+ myStateCounter++;
+ }
+ }
+}
+
+/*!
+\brief Returns IOR of current engine
+*/
+QString PVGUI_Module::engineIOR() const
+{
+ CORBA::String_var anIOR = GetCPPEngine()->GetIOR();
+ return QString(anIOR.in());
+}
+
+/*!
+ \brief Open file of format supported by ParaView
+*/
+void PVGUI_Module::openFile(const char* theName)
+{
+ QStringList aFiles;
+ aFiles<<theName;
+ pqLoadDataReaction::loadData(aFiles);
+}
+
+/**!
+ * \brief Starts Python trace.
+ *
+ * Start trace invoking the newly introduced C++ API (PV 4.2)
+ * (inspired from pqTraceReaction::start())
+ */
+void PVGUI_Module::startTrace()
+{
+ vtkSMSessionProxyManager* pxm = pqActiveObjects::instance().activeServer()->proxyManager();
+
+ vtkSmartPointer<vtkSMProxy> proxy;
+ proxy.TakeReference(pxm->NewProxy("pythontracing", "PythonTraceOptions"));
+ if (proxy)
+ {
+ vtkNew<vtkSMParaViewPipelineController> controller;
+ controller->InitializeProxy(proxy);
+ }
+ vtkSMTrace* trace = vtkSMTrace::StartTrace();
+ if (proxy)
+ {
+ // Set manually the properties entered via the dialog box poping-up when requiring
+ // a trace start in PV4.2 (trace options)
+ trace->SetPropertiesToTraceOnCreate(vtkSMTrace::RECORD_USER_MODIFIED_PROPERTIES);
+ trace->SetFullyTraceSupplementalProxies(false);
+ }
+}
+
+/**!
+ * \brief Stops Python trace.
+ */
+void PVGUI_Module::stopTrace()
+{
+ vtkSMTrace::StopTrace();
+}
+
+/**!
+ * \brief Execute a Python script.
+ */
+void PVGUI_Module::executeScript(const char *script)
+{
+#ifndef WNT
+ pqPythonManager* manager = qobject_cast<pqPythonManager*>(
+ pqApplicationCore::instance()->manager("PYTHON_MANAGER"));
+ if (manager) {
+ pqPythonDialog* pyDiag = manager->pythonShellDialog();
+ if (pyDiag) {
+ pyDiag->runString(script);
+ }
+ }
+#endif
+}
+
+///**
+// * Debug function printing out the given interpreter's execution context
+// */
+//void printInterpContext(PyInterp_Interp * interp )
+//{
+// // Extract __smtraceString from interpreter's context
+// const PyObject* ctxt = interp->getExecutionContext();
+//
+// PyObject* lst = PyDict_Keys((PyObject *)ctxt);
+// Py_ssize_t siz = PyList_GET_SIZE(lst);
+// for (Py_ssize_t i = 0; i < siz; i++)
+// {
+// PyObject * elem = PyList_GetItem(lst, i);
+// if (PyString_Check(elem))
+// {
+// std::cout << "At pos:" << i << ", " << PyString_AsString(elem) << std::endl;
+// }
+// else
+// std::cout << "At pos:" << i << ", not a string!" << std::endl;
+// }
+// Py_XDECREF(lst);
+//}
+
+/*!
+ \brief Returns trace string
+*/
+static const QString MYReplaceStr("paraview.simple");
+QString PVGUI_Module::getTraceString()
+{
+ vtkSMTrace *tracer = vtkSMTrace::GetActiveTracer();
+ if (!tracer) // trace is not started
+ return QString("");
+
+ QString traceString(tracer->GetCurrentTrace());
+ std::stringstream nl; nl << std::endl; // surely there is some Qt trick to do that in a portable way??
+ QString end_line(nl.str().c_str());
+ // 'import pvsimple' is necessary to fix the first call to DisableFirstRenderCamera in the paraview trace
+ // 'ShowParaviewView()' ensure there is an opened viewing window (otherwise SEGFAULT!)
+ traceString = "import pvsimple" + end_line +
+ "pvsimple.ShowParaviewView()" + end_line + traceString;
+
+ // Replace import "paraview.simple" by "pvsimple"
+ if ((!traceString.isNull()) && traceString.length() != 0) {
+ int aPos = traceString.indexOf(MYReplaceStr);
+ while (aPos != -1) {
+ traceString = traceString.replace(aPos, MYReplaceStr.length(), "pvsimple");
+ aPos = traceString.indexOf(MYReplaceStr, aPos);
+ }
+ }
+
+ return traceString;
+}
+
+/*!
+ \brief Saves trace string to disk file
+*/
+void PVGUI_Module::saveTrace(const char* theName)
+{
+ QFile file(theName);
+ if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
+ MESSAGE( "Could not open file:" << theName );
+ return;
+ }
+ QTextStream out(&file);
+ out << getTraceString();
+ file.close();
+}
+
+/*!
+ \brief Saves ParaView state to a disk file
+*/
+void PVGUI_Module::saveParaviewState(const char* theFileName)
+{
+ pqApplicationCore::instance()->saveState(theFileName);
+}
+
+/*!
+ \brief Delete all objects for Paraview Pipeline Browser
+*/
+void PVGUI_Module::clearParaviewState()
+{
+ QAction* deleteAllAction = action(DeleteAllId);
+ if (deleteAllAction) {
+ deleteAllAction->activate(QAction::Trigger);
+ }
+}
+
+/*!
+ \brief Restores ParaView state from a disk file
+
+ If toClear == true, the current ojects will be deleted
+*/
+void PVGUI_Module::loadParaviewState(const char* theFileName)
+{
+ pqApplicationCore::instance()->loadState(theFileName, getActiveServer());
+}
+
+/*!
+ \brief Returns current active ParaView server
+*/
+pqServer* PVGUI_Module::getActiveServer()
+{
+ return pqApplicationCore::instance()->getActiveServer();
+}
+
+
+/*!
+ \brief Creates PARAVIS preferences panel.
+*/
+void PVGUI_Module::createPreferences()
+{
+ // Paraview settings tab
+ int aParaViewSettingsTab = addPreference( tr( "TIT_PVIEWSETTINGS" ) );
+
+ setPreferenceProperty(aParaViewSettingsTab, "stretch", false );
+ int aPanel = addPreference(QString(), aParaViewSettingsTab, LightApp_Preferences::UserDefined, PARAVIS_MODULE_NAME, "");
+
+ setPreferenceProperty(aPanel, "content", (qint64)(new PVGUI_ParaViewSettingsPane(0, getApp())));
+
+ // Paravis settings tab
+ int aParaVisSettingsTab = addPreference( tr( "TIT_PVISSETTINGS" ) );
+ addPreference( tr( "PREF_STOP_TRACE" ), aParaVisSettingsTab, LightApp_Preferences::Bool, PARAVIS_MODULE_NAME, "stop_trace");
+
+ addPreference( tr( "PREF_NO_EXT_PVSERVER" ), aParaVisSettingsTab, LightApp_Preferences::Bool, PARAVIS_MODULE_NAME, "no_ext_pv_server");
+
+ int aSaveType = addPreference(tr( "PREF_SAVE_TYPE_LBL" ), aParaVisSettingsTab,
+ LightApp_Preferences::Selector,
+ PARAVIS_MODULE_NAME, "savestate_type");
+ QList<QVariant> aIndices;
+ QStringList aStrings;
+ aIndices<<0<<1<<2;
+ aStrings<<tr("PREF_SAVE_TYPE_0");
+ aStrings<<tr("PREF_SAVE_TYPE_1");
+ aStrings<<tr("PREF_SAVE_TYPE_2");
+ setPreferenceProperty(aSaveType, "strings", aStrings);
+ setPreferenceProperty(aSaveType, "indexes", aIndices);
+}
+
+/*!
+ \brief Creates ParaViS context menu popup
+*/
+void PVGUI_Module::contextMenuPopup(const QString& theClient, QMenu* theMenu, QString& theTitle)
+{
+ LightApp_Module::contextMenuPopup(theClient, theMenu, theTitle);
+
+ // Check if we are in Object Browser
+ SUIT_DataBrowser* ob = getApp()->objectBrowser();
+ bool isOBClient = (ob && theClient == ob->popupClientType());
+ if (!isOBClient) {
+ return;
+ }
+
+ // Get list of selected objects
+ LightApp_SelectionMgr* aSelectionMgr = getApp()->selectionMgr();
+ SALOME_ListIO aListIO;
+ aSelectionMgr->selectedObjects(aListIO);
+ if (aListIO.Extent() == 1 && aListIO.First()->hasEntry()) {
+ QString entry = QString(aListIO.First()->getEntry());
+
+ // Get active study
+ SalomeApp_Study* activeStudy =
+ dynamic_cast<SalomeApp_Study*>(getApp()->activeStudy());
+ if(!activeStudy) {
+ return;
+ }
+
+ // Get SALOMEDS client study
+ _PTR(Study) studyDS = activeStudy->studyDS();
+ if(!studyDS) {
+ return;
+ }
+
+ QString paravisDataType(PARAVIS_MODULE_NAME);
+ if(activeStudy && activeStudy->isComponent(entry) &&
+ activeStudy->componentDataType(entry) == paravisDataType) {
+ // ParaViS module object
+ theMenu->addSeparator();
+ theMenu->addAction(action(SaveStatePopupId));
+ }
+ else {
+ // Try to get state object
+ _PTR(SObject) stateSObj =
+ studyDS->FindObjectID(entry.toLatin1().constData());
+ if (!stateSObj) {
+ return;
+ }
+
+ // Check local id
+ _PTR(GenericAttribute) anAttr;
+ if (!stateSObj->FindAttribute(anAttr, "AttributeLocalID")) {
+ return;
+ }
+
+ _PTR(AttributeLocalID) anID(anAttr);
+
+ if (anID->Value() == PVSTATEID) {
+ // Paraview state object
+ theMenu->addSeparator();
+ theMenu->addAction(action(AddStatePopupId));
+ theMenu->addAction(action(CleanAndAddStatePopupId));
+ theMenu->addSeparator();
+ theMenu->addAction(action(ParaVisRenameId));
+ theMenu->addAction(action(ParaVisDeleteId));
+ }
+ }
+ }
+}
+
+/*!
+ \brief. Show ParaView python trace.
+*/
+void PVGUI_Module::onShowTrace()
+{
+ if (!myTraceWindow) {
+ myTraceWindow = new pqPythonScriptEditor(getApp()->desktop());
+ }
+ myTraceWindow->setText(getTraceString());
+ myTraceWindow->show();
+ myTraceWindow->raise();
+ myTraceWindow->activateWindow();
+}
+
+
+/*!
+ \brief. Re-initialize ParaView python trace.
+*/
+void PVGUI_Module::onRestartTrace()
+{
+ stopTrace();
+ startTrace();
+}
+
+/*!
+ \brief Save state under the module root object.
+*/
+void PVGUI_Module::onSaveMultiState()
+{
+ // Create state study object
+
+ // Get SALOMEDS client study
+ _PTR(Study) studyDS = PARAVIS::GetCStudy(this);
+ if(!studyDS) {
+ return;
+ }
+
+ _PTR(SComponent) paravisComp =
+ studyDS->FindComponent(PARAVIS_MODULE_NAME);
+ if(!paravisComp) {
+ return;
+ }
+
+ // Unlock the study if it is locked
+ bool isLocked = studyDS->GetProperties()->IsLocked();
+ if (isLocked) {
+ studyDS->GetProperties()->SetLocked(false);
+ }
+
+ QString stateName = tr("SAVED_PARAVIEW_STATE_NAME") +
+ QString::number(myStateCounter + 1);
+
+ _PTR(StudyBuilder) studyBuilder = studyDS->NewBuilder();
+ _PTR(SObject) newSObj = studyBuilder->NewObject(paravisComp);
+
+ // Set name
+ _PTR(GenericAttribute) anAttr;
+ anAttr = studyBuilder->FindOrCreateAttribute(newSObj, "AttributeName");
+ _PTR(AttributeName) nameAttr(anAttr);
+
+ nameAttr->SetValue(stateName.toLatin1().constData());
+
+ // Set local id
+ anAttr = studyBuilder->FindOrCreateAttribute(newSObj, "AttributeLocalID");
+ _PTR(AttributeLocalID) localIdAttr(anAttr);
+
+ localIdAttr->SetValue(PVSTATEID);
+
+ // Set file name
+ QString stateEntry = QString::fromStdString(newSObj->GetID());
+
+ // File name for state saving
+ QString tmpDir = QString::fromStdString(SALOMEDS_Tool::GetTmpDir());
+ QString fileName = QString("%1_paravisstate:%2").arg(tmpDir, stateEntry);
+
+ anAttr = studyBuilder->FindOrCreateAttribute(newSObj, "AttributeString");
+ _PTR(AttributeString) stringAttr(anAttr);
+
+ stringAttr->SetValue(fileName.toLatin1().constData());
+
+ // Lock the study back if necessary
+ if (isLocked) {
+ studyDS->GetProperties()->SetLocked(true);
+ }
+
+ // Save state
+ saveParaviewState(fileName.toLatin1().constData());
+ myTemporaryFiles.append(fileName);
+
+ // Increment the counter
+ myStateCounter++;
+
+ updateObjBrowser();
+}
+
+/*!
+ \brief Restore the selected state by merging with the current one.
+*/
+void PVGUI_Module::onAddState()
+{
+ loadSelectedState(false);
+}
+
+/*!
+ \brief Clean the current state and restore the selected one.
+*/
+void PVGUI_Module::onCleanAddState()
+{
+ loadSelectedState(true);
+}
+
+/*!
+ \brief Rename the selected object.
+*/
+void PVGUI_Module::onRename()
+{
+ LightApp_SelectionMgr* aSelectionMgr = getApp()->selectionMgr();
+ SALOME_ListIO aListIO;
+ aSelectionMgr->selectedObjects(aListIO);
+
+ if (aListIO.Extent() == 1 && aListIO.First()->hasEntry()) {
+ std::string entry = aListIO.First()->getEntry();
+
+ // Get SALOMEDS client study
+ _PTR(Study) studyDS = PARAVIS::GetCStudy(this);
+ if(!studyDS) {
+ return;
+ }
+
+ // Unlock the study if it is locked
+ bool isLocked = studyDS->GetProperties()->IsLocked();
+ if (isLocked) {
+ studyDS->GetProperties()->SetLocked(false);
+ }
+
+ // Rename the selected state object
+ _PTR(SObject) stateSObj = studyDS->FindObjectID(entry);
+ if (!stateSObj) {
+ return;
+ }
+
+ _PTR(GenericAttribute) anAttr;
+ if (stateSObj->FindAttribute(anAttr, "AttributeName")) {
+ _PTR(AttributeName) nameAttr (anAttr);
+ QString newName =
+ LightApp_NameDlg::getName(getApp()->desktop(), nameAttr->Value().c_str());
+ if (!newName.isEmpty()) {
+ nameAttr->SetValue(newName.toLatin1().constData());
+ aListIO.First()->setName(newName.toLatin1().constData());
+ }
+ }
+
+ // Lock the study back if necessary
+ if (isLocked) {
+ studyDS->GetProperties()->SetLocked(true);
+ }
+
+ // Update object browser
+ updateObjBrowser();
+
+ }
+}
+
+/*!
+ \brief Delete the selected objects.
+*/
+void PVGUI_Module::onDelete()
+{
+ LightApp_SelectionMgr* aSelectionMgr = getApp()->selectionMgr();
+ SALOME_ListIO aListIO;
+ aSelectionMgr->selectedObjects(aListIO);
+
+ if (aListIO.Extent() == 1 && aListIO.First()->hasEntry()) {
+ std::string entry = aListIO.First()->getEntry();
+
+ // Get SALOMEDS client study
+ _PTR(Study) studyDS = PARAVIS::GetCStudy(this);
+ if(!studyDS) {
+ return;
+ }
+
+ // Unlock the study if it is locked
+ bool isLocked = studyDS->GetProperties()->IsLocked();
+ if (isLocked) {
+ studyDS->GetProperties()->SetLocked(false);
+ }
+
+ // Remove the selected state from the study
+ _PTR(StudyBuilder) studyBuilder = studyDS->NewBuilder();
+ _PTR(SObject) stateSObj = studyDS->FindObjectID(entry);
+ studyBuilder->RemoveObject(stateSObj);
+
+ // Lock the study back if necessary
+ if (isLocked) {
+ studyDS->GetProperties()->SetLocked(true);
+ }
+
+ // Update object browser
+ updateObjBrowser();
+ }
+}
+
+/*!
+ * \brief Slot called everytime the Python trace is pushed onto the CORBA engine.
+ */
+void PVGUI_Module::onPushTraceTimer()
+{
+ //MESSAGE("onPushTraceTimer(): Pushing trace to engine...");
+ GetEngine()->PutPythonTraceStringToEngine(getTraceString().toStdString().c_str());
+}
+
+/*!
+ \brief Discover help project files from the resources.
+ \return name of the help file.
+*/
+QString PVGUI_Module::getHelpFileName() {
+ QString aPVHome(getenv("PVHOME"));
+ if (aPVHome.isNull()) {
+ qWarning("Wariable PVHOME is not defined");
+ return QString();
+ }
+ QChar aSep = QDir::separator();
+ //PARAVIEW_VERSION from the vtkPVConfig.h file
+ QString aFileName = aPVHome + aSep + "share" + aSep + "doc" + aSep + "paraview-"+ PARAVIEW_VERSION + aSep + "paraview.qch";
+ return aFileName;
+}
+
+
+/*!
+ \brief Load selected paraview state
+
+ If toClear == true, the current state will be cleared
+*/
+void PVGUI_Module::loadSelectedState(bool toClear)
+{
+ QString fileName;
+
+ LightApp_SelectionMgr* aSelectionMgr = getApp()->selectionMgr();
+ SALOME_ListIO aListIO;
+ aSelectionMgr->selectedObjects(aListIO);
+
+ if (aListIO.Extent() == 1 && aListIO.First()->hasEntry()) {
+ std::string entry = aListIO.First()->getEntry();
+
+ // Get SALOMEDS client study
+ _PTR(Study) studyDS = PARAVIS::GetCStudy(this);
+ if(!studyDS) {
+ return;
+ }
+
+ // Check local id
+ _PTR(SObject) stateSObj = studyDS->FindObjectID(entry);
+ _PTR(GenericAttribute) anAttr;
+ if (!stateSObj->FindAttribute(anAttr, "AttributeLocalID")) {
+ return;
+ }
+ _PTR(AttributeLocalID) anID(anAttr);
+ if (!anID->Value() == PVSTATEID) {
+ return;
+ }
+
+ // Get state file name
+ if (stateSObj->FindAttribute(anAttr, "AttributeString")) {
+ _PTR(AttributeString) aStringAttr(anAttr);
+ QString stringValue(aStringAttr->Value().c_str());
+
+ if (QFile::exists(stringValue)) {
+ fileName = stringValue;
+ }
+ }
+ }
+
+ if (!fileName.isEmpty()) {
+ if (toClear) {
+ clearParaviewState();
+ }
+
+ loadParaviewState(fileName.toLatin1().constData());
+ }
+ else {
+ SUIT_MessageBox::critical(getApp()->desktop(),
+ tr("ERR_ERROR"),
+ tr("ERR_STATE_CANNOT_BE_RESTORED"));
+ }
+}
+