From: ouv Date: Fri, 14 Nov 2008 12:22:19 +0000 (+0000) Subject: Dump Python extension X-Git-Tag: TG_DumpPython_Extension_2 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=3b66605517acbab8927c99795c19b2cda8e6d726;p=modules%2Fgui.git Dump Python extension --- diff --git a/src/SalomeApp/SalomeApp_NoteBookDlg.cxx b/src/SalomeApp/SalomeApp_NoteBookDlg.cxx index cec076b0c..3e9ef3d69 100644 --- a/src/SalomeApp/SalomeApp_NoteBookDlg.cxx +++ b/src/SalomeApp/SalomeApp_NoteBookDlg.cxx @@ -21,7 +21,20 @@ // Module : GUI #include "SalomeApp_NoteBookDlg.h" +#include "SalomeApp_Application.h" +#include "SalomeApp_Study.h" +#include "SalomeApp_VisualState.h" + +#include + +#include + #include +#include + +#include + +#include #include #include @@ -32,6 +45,8 @@ #include #include #include +#include +#include #include #include @@ -60,8 +75,7 @@ NoteBook_TableRow::NoteBook_TableRow(int index, QWidget* parent): myIndex(index), myRowHeader(new QTableWidgetItem()), myVariableName(new QTableWidgetItem()), - myVariableValue(new QTableWidgetItem()), - isNameEditable(true) + myVariableValue(new QTableWidgetItem()) { } @@ -91,51 +105,6 @@ void NoteBook_TableRow::AddToTable(QTableWidget *theTable) theTable->setItem(aPosition, VALUE_COLUMN, myVariableValue); } -//============================================================================ -/*! Function : SetNameEditable - * Purpose : Set item with variable name editable/not editable - * and change flag isNameEditable - */ -//============================================================================ -void NoteBook_TableRow::SetNameEditable(bool enable) -{ - isNameEditable = enable; - Qt::ItemFlags f = myVariableName->flags(); - f = f & (isNameEditable ? Qt::ItemIsEditable : ~Qt::ItemIsEditable); - myVariableName->setFlags(f); - - //Mark all not editabe variable names - QFont aFont = QFont(); - aFont.setBold(true); - myVariableName->setFont(aFont); -} - -//============================================================================ -/*! Function : setValueEditable - * Purpose : Set item with variable value editable/not editable - */ -//============================================================================ -void NoteBook_TableRow::setValueEditable(bool enable) -{ - Qt::ItemFlags f = myVariableValue->flags(); - f = enable ? (f | Qt::ItemIsEditable) : (f & ~Qt::ItemIsEditable); - myVariableValue->setFlags(f); -} - -//============================================================================ -/*! Function : setNameEditable - * Purpose : Set item with variable name editable/not editable - */ -//============================================================================ -void NoteBook_TableRow::setNameEditable(bool enable) -{ - if(isNameEditable) { - Qt::ItemFlags f = myVariableName->flags(); - f = enable ? (f | Qt::ItemIsEditable) : (f & ~Qt::ItemIsEditable); - myVariableName->setFlags(f); - } -} - //============================================================================ /*! Function : SetName * Purpose : @@ -176,11 +145,11 @@ QString NoteBook_TableRow::GetValue() const return myVariableValue->text(); } -bool NoteBook_TableRow::IsNameEditable() -{ - return isNameEditable; -} - +//============================================================================ +/*! Function : CheckName + * Purpose : Return true if variable name correct, otherwise return false + */ +//============================================================================ bool NoteBook_TableRow::CheckName() { bool aResult = false; @@ -192,7 +161,7 @@ bool NoteBook_TableRow::CheckName() } //============================================================================ -/*! Function : GetValue +/*! Function : CheckValue * Purpose : Return true if variable value correct, otherwise return false */ //============================================================================ @@ -323,8 +292,6 @@ NoteBook_Table::NoteBook_Table(QWidget * parent) setSortingEnabled(false); connect(this,SIGNAL(itemChanged(QTableWidgetItem*)),this,SLOT(onItemChanged(QTableWidgetItem*))); - connect(this,SIGNAL(itemSelectionChanged()),this,SLOT(onItemSelectionChanged())); - } //============================================================================ @@ -364,6 +331,28 @@ int NoteBook_Table::getUniqueIndex() const //============================================================================ void NoteBook_Table::Init(_PTR(Study) theStudy) { + isProcessItemChangedSignal = false; + + int aNumRows = myRows.count(); + if( aNumRows > 0 ) + { + for( int i = 0; i < myRows.size(); i++ ) + { + NoteBook_TableRow* aRow = myRows[ i ]; + if( aRow ) + { + delete aRow; + aRow = 0; + } + } + myRows.clear(); + } + setRowCount( 0 ); + + myRemovedRows.clear(); + myVariableMapRef.clear(); + myVariableMap.clear(); + //Add all variables into the table vector aVariables = theStudy->GetVariableNames(); for(int iVar = 0; iVar < aVariables.size(); iVar++ ) { @@ -493,7 +482,7 @@ void NoteBook_Table::onItemChanged(QTableWidgetItem* theItem) if( myVariableMap.contains( anIndex ) ) { const NoteBoox_Variable& aVariable = myVariableMap[ anIndex ]; - if( myStudy->IsVariableUsed( string( aVariable.Name.toLatin1().constData() ) ) ) + if( !aVariable.Name.isEmpty() && myStudy->IsVariableUsed( string( aVariable.Name.toLatin1().constData() ) ) ) { if( QMessageBox::warning( parentWidget(), tr( "WARNING" ), tr( "RENAME_VARIABLE_IS_USED" ).arg( aVariable.Name ), @@ -569,27 +558,6 @@ bool NoteBook_Table::IsUniqueName(const NoteBook_TableRow* theRow) const return true; } -//============================================================================ -/*! Function : onItemSelectionChanged - * Purpose : [slot] - */ -//============================================================================ -void NoteBook_Table::onItemSelectionChanged() -{ - bool aResult = true; - QList aSelectedItems = selectedItems(); - aResult &= (aSelectedItems.size() > 0); - - if(aResult) { - for(int i=0; i < aSelectedItems.size(); i++ ) { - NoteBook_TableRow* aRow = GetRowByItem(aSelectedItems[i]); - if(aRow) - aResult &= aRow->IsNameEditable(); - } - } - emit selectionChanged(aResult); -} - //============================================================================ /*! Function : RemoveSelected * Purpose : Remove selected rows in the table @@ -742,10 +710,7 @@ SalomeApp_NoteBookDlg::SalomeApp_NoteBookDlg(QWidget * parent, _PTR(Study) theSt connect( myUpdateStudyBtn, SIGNAL(clicked()), this, SLOT(onUpdateStudy()) ); connect( myRemoveButton, SIGNAL(clicked()), this, SLOT(onRemove())); - connect( myTable, SIGNAL(selectionChanged(bool)),this, SLOT(onTableSelectionChanged(bool))); - myTable->Init(myStudy); - myUpdateStudyBtn->setEnabled(false); } //============================================================================ @@ -756,26 +721,6 @@ SalomeApp_NoteBookDlg::SalomeApp_NoteBookDlg(QWidget * parent, _PTR(Study) theSt SalomeApp_NoteBookDlg::~SalomeApp_NoteBookDlg(){} -//============================================================================ -/*! Function : onTableSelectionChanged - * Purpose : [slot] - */ -//============================================================================ -void SalomeApp_NoteBookDlg::onTableSelectionChanged(bool flag) -{ - //myRemoveButton->setEnabled(flag); -} - -//============================================================================ -/*! Function : onRemove - * Purpose : [slot] - */ -//============================================================================ -void SalomeApp_NoteBookDlg::onRemove() -{ - myTable->RemoveSelected(); -} - //============================================================================ /*! Function : onOK * Purpose : [slot] @@ -813,8 +758,8 @@ void SalomeApp_NoteBookDlg::onApply() int anIndex = anIter.next(); if( aVariableMapRef.contains( anIndex ) ) { - QString aNameRef = aVariableMapRef[ anIndex ].Name; - myStudy->RemoveVariable( string( aNameRef.toLatin1().constData() ) ); + QString aRemovedVariable = aVariableMapRef[ anIndex ].Name; + myStudy->RemoveVariable( string( aRemovedVariable.toLatin1().constData() ) ); } } @@ -865,6 +810,16 @@ void SalomeApp_NoteBookDlg::onCancel() reject(); } +//============================================================================ +/*! Function : onRemove + * Purpose : [slot] + */ +//============================================================================ +void SalomeApp_NoteBookDlg::onRemove() +{ + myTable->RemoveSelected(); +} + //============================================================================ /*! Function : onUpdateStudy * Purpose : [slot] @@ -872,5 +827,101 @@ void SalomeApp_NoteBookDlg::onCancel() //============================================================================ void SalomeApp_NoteBookDlg::onUpdateStudy() { + onApply(); + + QApplication::setOverrideCursor( Qt::WaitCursor ); + + if( !updateStudy() ) + SUIT_MessageBox::warning( this, tr( "ERROR" ), tr( "ERR_UPDATE_STUDY_FAILED" ) ); + + QApplication::restoreOverrideCursor(); +} + +//============================================================================ +/*! Function : updateStudy + * Purpose : + */ +//============================================================================ +bool SalomeApp_NoteBookDlg::updateStudy() +{ + SalomeApp_Application* app = dynamic_cast( SUIT_Session::session()->activeApplication() ); + if( !app ) + return false; + + SalomeApp_Study* study = dynamic_cast( app->activeStudy() ); + if( !study ) + return false; + + _PTR(Study) studyDS = study->studyDS(); + + // get unique temporary directory name + QString aTmpDir = QString::fromStdString( SALOMEDS_Tool::GetTmpDir() ); + if( aTmpDir.isEmpty() ) + return false; + + if( aTmpDir.right( 1 ).compare( QDir::separator() ) == 0 ) + aTmpDir.remove( aTmpDir.length() - 1, 1 ); + + // dump study to the temporary directory + QString aFileName( "notebook" ); + bool toPublish = true; + bool toSaveGUI = true; + + int savePoint; + _PTR(AttributeParameter) ap; + _PTR(IParameters) ip = ClientFactory::getIParameters(ap); + if(ip->isDumpPython(studyDS)) ip->setDumpPython(studyDS); //Unset DumpPython flag. + if ( toSaveGUI ) { //SRN: Store a visual state of the study at the save point for DumpStudy method + ip->setDumpPython(studyDS); + savePoint = SalomeApp_VisualState( app ).storeState(); //SRN: create a temporary save point + } + bool ok = studyDS->DumpStudy( aTmpDir.toStdString(), aFileName.toStdString(), toPublish ); + if ( toSaveGUI ) + study->removeSavePoint(savePoint); //SRN: remove the created temporary save point. + + if( !ok ) + return false; + + // clear a study (delete all objects) + clearStudy(); + + // load study from the temporary directory + QString command = QString( "execfile(\"%1\")" ).arg( aTmpDir + QDir::separator() + aFileName + ".py" ); + + PyConsole_Console* pyConsole = app->pythonConsole(); + if ( pyConsole ) + pyConsole->execAndWait( command ); + + // remove temporary directory + QDir aDir( aTmpDir ); + QStringList aFiles = aDir.entryList( QStringList( "*.py*" ) ); + for( QStringList::iterator it = aFiles.begin(), itEnd = aFiles.end(); it != itEnd; ++it ) + ok = aDir.remove( *it ) && ok; + if( ok ) + ok = aDir.rmdir( aTmpDir ); + + if( SalomeApp_Study* newStudy = dynamic_cast( app->activeStudy() ) ) + { + myStudy = newStudy->studyDS(); + myTable->Init( myStudy ); + } + else + ok = false; + + return ok; +} + +//============================================================================ +/*! Function : clearStudy + * Purpose : + */ +//============================================================================ +void SalomeApp_NoteBookDlg::clearStudy() +{ + SalomeApp_Application* app = dynamic_cast( SUIT_Session::session()->activeApplication() ); + if( !app ) + return; + app->onCloseDoc( false ); + app->onNewDoc(); } diff --git a/src/SalomeApp/SalomeApp_NoteBookDlg.h b/src/SalomeApp/SalomeApp_NoteBookDlg.h index a1f81b8d7..acb18f653 100644 --- a/src/SalomeApp/SalomeApp_NoteBookDlg.h +++ b/src/SalomeApp/SalomeApp_NoteBookDlg.h @@ -58,9 +58,6 @@ class SALOMEAPP_EXPORT NoteBook_TableRow : public QWidget int GetIndex() const { return myIndex; } - void SetNameEditable(bool enable); - bool IsNameEditable(); - void AddToTable(QTableWidget *theTable); void SetName(const QString theName); @@ -72,9 +69,6 @@ class SALOMEAPP_EXPORT NoteBook_TableRow : public QWidget bool CheckName(); bool CheckValue(); - void setValueEditable(bool enable); - void setNameEditable(bool enable); - QTableWidgetItem* GetVariableItem(); QTableWidgetItem* GetNameItem(); @@ -87,7 +81,6 @@ class SALOMEAPP_EXPORT NoteBook_TableRow : public QWidget QTableWidgetItem* myRowHeader; QTableWidgetItem* myVariableName; QTableWidgetItem* myVariableValue; - bool isNameEditable; }; class SALOMEAPP_EXPORT NoteBook_Table : public QTableWidget @@ -124,14 +117,10 @@ class SALOMEAPP_EXPORT NoteBook_Table : public QTableWidget public slots: void onItemChanged(QTableWidgetItem* theItem); - void onItemSelectionChanged(); private: int getUniqueIndex() const; - signals: - void selectionChanged(bool); - private: bool isProcessItemChangedSignal; QList myRows; @@ -154,9 +143,12 @@ class SALOMEAPP_EXPORT SalomeApp_NoteBookDlg : public QDialog void onOK(); void onApply(); void onCancel(); - void onUpdateStudy(); void onRemove(); - void onTableSelectionChanged(bool flag); + void onUpdateStudy(); + + protected: + bool updateStudy(); + void clearStudy(); private: NoteBook_Table* myTable; diff --git a/src/SalomeApp/resources/SalomeApp_msg_en.ts b/src/SalomeApp/resources/SalomeApp_msg_en.ts index 0d4a5e644..faf5d0e67 100644 --- a/src/SalomeApp/resources/SalomeApp_msg_en.ts +++ b/src/SalomeApp/resources/SalomeApp_msg_en.ts @@ -400,5 +400,9 @@ Do you really want to rename it? At least one variable has been defined incorrectly. Please edit its parameters or remove it from table. + + ERR_UPDATE_STUDY_FAILED + Failed to update study! +