1 // Copyright (C) 2007-2012 CEA/DEN, EDF R&D, OPEN CASCADE
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 // File: SalomeApp_NoteBookDlg.cxx
21 // Author : Roman NIKOLAEV, Open CASCADE S.A.S.
24 #include <PyConsole_Interp.h> // this include must be first (see PyInterp_base.h)!
25 #include <PyConsole_Console.h>
27 #include "SalomeApp_NoteBookDlg.h"
28 #include "SalomeApp_Application.h"
29 #include "SalomeApp_Study.h"
30 #include "SalomeApp_VisualState.h"
34 #include <CAM_Module.h>
36 #include <SUIT_Desktop.h>
37 #include <SUIT_MessageBox.h>
38 #include <SUIT_ResourceMgr.h>
39 #include <SUIT_Session.h>
41 #include <SALOMEDS_Tool.hxx>
45 #include <QGridLayout>
46 #include <QTableWidget>
47 #include <QTableWidgetItem>
48 #include <QPushButton>
52 #include <QApplication>
58 #define DEFAULT_MARGIN 11
59 #define DEFAULT_SPACING 6
60 #define SPACER_SIZE 120
61 #define COLUMN_SIZE 180
64 #define VALUE_COLUMN 1
67 ///////////////////////////////////////////////////////////////////////////
68 // NoteBook_TableRow class //
69 ///////////////////////////////////////////////////////////////////////////
70 //============================================================================
71 /*! Function : NoteBook_TableRow
72 * Purpose : Constructor
74 //============================================================================
75 NoteBook_TableRow::NoteBook_TableRow(int index, NoteBook_Table* parentTable, QWidget* parent):
77 myParentTable(parentTable),
79 myRowHeader(new QTableWidgetItem()),
80 myVariableName(new QTableWidgetItem()),
81 myVariableValue(new QTableWidgetItem())
85 //============================================================================
86 /*! Function : ~NoteBook_TableRow
87 * Purpose : Destructor
89 //============================================================================
90 NoteBook_TableRow::~NoteBook_TableRow()
94 //============================================================================
95 /*! Function : AddToTable
96 * Purpose : Add this row to the table theTable
98 //============================================================================
99 void NoteBook_TableRow::AddToTable(QTableWidget *theTable)
101 int aPosition = theTable->rowCount();
102 int aRowCount = aPosition+1;
103 theTable->setRowCount(aRowCount);
104 myRowHeader->setText(QString::number(aRowCount));
106 theTable->setVerticalHeaderItem(aPosition,myRowHeader);
107 theTable->setItem(aPosition, NAME_COLUMN, myVariableName);
108 theTable->setItem(aPosition, VALUE_COLUMN, myVariableValue);
111 //============================================================================
112 /*! Function : SetName
115 //============================================================================
116 void NoteBook_TableRow::SetName(const QString theName)
118 myVariableName->setText(theName);
121 //============================================================================
122 /*! Function : SetValue
125 //============================================================================
126 void NoteBook_TableRow::SetValue(const QString theValue)
128 myVariableValue->setText(theValue);
131 //============================================================================
132 /*! Function : GetName
133 * Purpose : Return variable name
135 //============================================================================
136 QString NoteBook_TableRow::GetName() const
138 return myVariableName->text();
141 //============================================================================
142 /*! Function : GetValue
143 * Purpose : Return variable value
145 //============================================================================
146 QString NoteBook_TableRow::GetValue() const
148 return myVariableValue->text();
151 //============================================================================
152 /*! Function : CheckName
153 * Purpose : Return true if variable name correct, otherwise return false
155 //============================================================================
156 bool NoteBook_TableRow::CheckName()
158 QString aName = GetName();
160 QRegExpValidator aValidator( QRegExp("^([a-zA-Z]+)([a-zA-Z0-9_]*)$"), 0 );
161 if( aName.isEmpty() || !aValidator.validate( aName, aPos ) )
166 //============================================================================
167 /*! Function : CheckValue
168 * Purpose : Return true if variable value correct, otherwise return false
170 //============================================================================
171 bool NoteBook_TableRow::CheckValue()
173 bool aResult = false;
174 QString aValue = GetValue();
175 if(!aValue.isEmpty() &&
176 (IsRealValue(aValue) ||
177 IsIntegerValue(aValue) ||
178 IsBooleanValue(aValue) ||
179 IsValidStringValue(aValue)))
185 //============================================================================
186 /*! Function : GetVariableItem
189 //============================================================================
190 QTableWidgetItem* NoteBook_TableRow::GetVariableItem()
192 return myVariableValue;
195 //============================================================================
196 /*! Function : GetNameItem
199 //============================================================================
200 QTableWidgetItem* NoteBook_TableRow::GetNameItem()
202 return myVariableName;
205 //============================================================================
206 /*! Function : GetHeaderItem
209 //============================================================================
210 QTableWidgetItem* NoteBook_TableRow::GetHeaderItem()
215 //============================================================================
216 /*! Function : IsRealValue
217 * Purpose : Return true if theValue string is real value, otherwise return
220 //============================================================================
221 bool NoteBook_TableRow::IsRealValue(const QString theValue, double* theResult)
223 bool aResult = false;
224 double aDResult = theValue.toDouble(&aResult);
225 if(aResult && theResult)
226 *theResult = aDResult;
231 //============================================================================
232 /*! Function : IsBooleanValue
233 * Purpose : Return true if theValue String is boolean value, otherwise return
236 //============================================================================
237 bool NoteBook_TableRow::IsBooleanValue(const QString theValue, bool* theResult){
238 bool aResult = false;
240 if(theValue.compare("True") == 0) {
244 else if(theValue.compare("False") == 0) {
248 if(aResult && theResult)
249 *theResult = aBResult;
254 //============================================================================
255 /*! Function : IsIntegerValue
256 * Purpose : Return true if theValue string is integer value, otherwise return
259 //============================================================================
260 bool NoteBook_TableRow::IsIntegerValue(const QString theValue, int* theResult)
262 bool aResult = false;
264 anIResult = theValue.toInt(&aResult);
266 if(aResult && theResult)
267 *theResult = anIResult;
272 //============================================================================
273 /*! Function : IsValidStringValue
274 * Purpose : Return true if theValue string is valid, otherwise return
276 * The string are always valid for the moment
277 * The whole notebook is verified on apply
279 //============================================================================
280 bool NoteBook_TableRow::IsValidStringValue(const QString theValue)
282 int aNumRows = myParentTable->myRows.count();
286 bool aLastRowIsEmpty = myParentTable->myRows[ aNumRows - 1 ]->GetName().isEmpty() &&
287 myParentTable->myRows[ aNumRows - 1 ]->GetValue().isEmpty();
289 SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>( SUIT_Session::session()->activeApplication() );
290 PyConsole_Console* pyConsole = app->pythonConsole();
291 PyConsole_Interp* pyInterp = pyConsole->getInterp();
292 PyLockWrapper aLock = pyInterp->GetLockWrapper();
293 std::string command = "import salome_notebook ; ";
294 command += "salome_notebook.checkThisNoteBook(";
295 for( int i = 0, n = aLastRowIsEmpty ? aNumRows - 1 : aNumRows; i < n; i++ ) {
296 command += myParentTable->myRows[i]->GetName().toStdString();
298 command += myParentTable->myRows[i]->GetValue().toStdString();
303 //rnv: fix for bug 21947 WinTC5.1.4: Wrong error management of "Salome NoteBook"
304 bool oldSuppressValue = pyConsole->isSuppressOutput();
305 pyConsole->setIsSuppressOutput(true);
306 bool aResult = pyInterp->run(command.c_str());
307 pyConsole->setIsSuppressOutput(oldSuppressValue);
311 ///////////////////////////////////////////////////////////////////////////
312 // NoteBook_Table class //
313 ///////////////////////////////////////////////////////////////////////////
314 //============================================================================
315 /*! Function : NoteBook_Table
316 * Purpose : Constructor
318 //============================================================================
319 NoteBook_Table::NoteBook_Table(QWidget * parent)
320 :QTableWidget(parent),
321 isProcessItemChangedSignal(false),
325 setSelectionMode(QAbstractItemView::SingleSelection);
327 //Add Headers Columns
328 QFont aFont = QFont();
330 aFont.setPointSize(10);
333 QTableWidgetItem * aNameHeader = new QTableWidgetItem();
334 aNameHeader->setText(tr("VARNAME_COLUMN"));
335 aNameHeader->setFont(aFont);
336 setHorizontalHeaderItem(0,aNameHeader);
337 setColumnWidth ( 0, COLUMN_SIZE);
340 QTableWidgetItem * aValueHeader = new QTableWidgetItem();
341 aValueHeader->setText(tr("VARVALUE_COLUMN"));
342 aValueHeader->setFont(aFont);
343 setHorizontalHeaderItem(1,aValueHeader);
344 setColumnWidth ( 1, COLUMN_SIZE);
345 setSortingEnabled(false);
347 connect(this,SIGNAL(itemChanged(QTableWidgetItem*)),this,SLOT(onItemChanged(QTableWidgetItem*)));
350 //============================================================================
351 /*! Function : ~NoteBook_Table
352 * Purpose : Destructor
354 //============================================================================
355 NoteBook_Table::~NoteBook_Table(){}
357 //============================================================================
358 /*! Function : getUniqueIndex
359 * Purpose : Get a unique index for the new row
361 //============================================================================
362 int NoteBook_Table::getUniqueIndex() const
365 if( !myRows.isEmpty() )
366 if( NoteBook_TableRow* aRow = myRows.last() )
367 anIndex = aRow->GetIndex();
369 int aMaxRemovedRow = 0;
370 for( QListIterator<int> anIter( myRemovedRows ); anIter.hasNext(); )
372 int aRemovedRow = anIter.next();
373 aMaxRemovedRow = qMax( aRemovedRow, aMaxRemovedRow );
376 anIndex = qMax( anIndex, aMaxRemovedRow ) + 1;
380 //============================================================================
382 * Purpose : Add variables in the table from theStudy
384 //============================================================================
385 void NoteBook_Table::Init(_PTR(Study) theStudy)
387 isProcessItemChangedSignal = false;
389 int aNumRows = myRows.count();
392 for( int i = 0; i < myRows.size(); i++ )
394 NoteBook_TableRow* aRow = myRows[ i ];
405 myRemovedRows.clear();
406 myVariableMapRef.clear();
407 myVariableMap.clear();
409 //Add all variables into the table
410 std::vector<std::string> aVariables = theStudy->GetVariableNames();
411 for(int iVar = 0; iVar < aVariables.size(); iVar++ ) {
412 AddRow(QString(aVariables[iVar].c_str()),
413 Variable2String(aVariables[iVar],theStudy));
418 isProcessItemChangedSignal = true;
425 //============================================================================
426 /*! Function : Variable2String
427 * Purpose : Convert variable values to QString
429 //============================================================================
430 QString NoteBook_Table::Variable2String(const std::string& theVarName,
431 _PTR(Study) theStudy)
434 if( theStudy->IsReal(theVarName) )
435 aResult = QString::number(theStudy->GetReal(theVarName));
436 else if( theStudy->IsInteger(theVarName) )
437 aResult = QString::number(theStudy->GetInteger(theVarName));
438 else if( theStudy->IsBoolean(theVarName) )
439 aResult = theStudy->GetBoolean(theVarName) ? QString("True") : QString("False");
440 else if( theStudy->IsString(theVarName) )
441 aResult = theStudy->GetString(theVarName).c_str();
446 //============================================================================
447 /*! Function : IsValid
448 * Purpose : Check validity of the table data
450 //============================================================================
451 bool NoteBook_Table::IsValid() const
453 int aNumRows = myRows.count();
457 bool aLastRowIsEmpty = myRows[ aNumRows - 1 ]->GetName().isEmpty() &&
458 myRows[ aNumRows - 1 ]->GetValue().isEmpty();
460 for( int i = 0, n = aLastRowIsEmpty ? aNumRows - 1 : aNumRows; i < n; i++ )
461 if( !myRows[i]->CheckName() || !IsUniqueName( myRows[i] ) || !myRows[i]->CheckValue() )
464 SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>( SUIT_Session::session()->activeApplication() );
465 PyConsole_Console* pyConsole = app->pythonConsole();
466 PyConsole_Interp* pyInterp = pyConsole->getInterp();
467 PyLockWrapper aLock = pyInterp->GetLockWrapper();
468 std::string command = "import salome_notebook ; ";
469 command += "salome_notebook.checkThisNoteBook(";
470 for( int i = 0, n = aLastRowIsEmpty ? aNumRows - 1 : aNumRows; i < n; i++ )
472 command += myRows[i]->GetName().toStdString();
474 command += myRows[i]->GetValue().toStdString();
479 //rnv: fix for bug 21947 WinTC5.1.4: Wrong error management of "Salome NoteBook"
480 bool oldSuppressValue = pyConsole->isSuppressOutput();
481 pyConsole->setIsSuppressOutput(true);
482 bool aResult = pyInterp->run(command.c_str());
483 pyConsole->setIsSuppressOutput(oldSuppressValue);
488 //============================================================================
489 /*! Function : RenamberRowItems
490 * Purpose : renumber row items
492 //============================================================================
493 void NoteBook_Table::RenamberRowItems() {
494 for(int i=0; i<myRows.size();i++){
495 myRows[i]->GetHeaderItem()->setText(QString::number(i+1));
499 //============================================================================
500 /*! Function : AddRow
501 * Purpose : Add a row into the table
503 //============================================================================
504 void NoteBook_Table::AddRow(const QString& theName, const QString& theValue)
506 int anIndex = getUniqueIndex();
507 NoteBook_TableRow* aRow = new NoteBook_TableRow(anIndex, this, this);
508 aRow->SetName(theName);
509 aRow->SetValue(theValue);
510 aRow->AddToTable(this);
513 myVariableMap.insert( anIndex, NoteBoox_Variable( theName, theValue ) );
516 //============================================================================
517 /*! Function : AddEmptyRow
518 * Purpose : Add an empty row into the end of the table
520 //============================================================================
521 void NoteBook_Table::AddEmptyRow()
523 isProcessItemChangedSignal = false;
525 isProcessItemChangedSignal = true;
528 //============================================================================
529 /*! Function : GetRowByItem
532 //============================================================================
533 NoteBook_TableRow* NoteBook_Table::GetRowByItem(const QTableWidgetItem* theItem) const
535 int aCurrentRow = row(theItem);
537 if( (myRows.size() <= aCurrentRow ) && (aCurrentRow < 0))
540 return myRows.at(aCurrentRow);
543 //============================================================================
544 /*! Function : IsLastRow
545 * Purpose : Return true if theRow is last row in the table
547 //============================================================================
548 bool NoteBook_Table::IsLastRow(const NoteBook_TableRow* theRow) const
550 return (myRows.last() == theRow);
553 //============================================================================
554 /*! Function : onItemChanged
555 * Purpose : [slot] called then table item changed
557 //============================================================================
558 void NoteBook_Table::onItemChanged(QTableWidgetItem* theItem)
560 if(isProcessItemChangedSignal) {
561 bool isModified = true;
562 NoteBook_TableRow* aRow = GetRowByItem(theItem);
564 int aCurrentColumn = column(theItem);
565 bool IsCorrect = true, IsVariableComplited = false;
568 if(aCurrentColumn == NAME_COLUMN) {
569 int anIndex = aRow->GetIndex();
570 if( myVariableMap.contains( anIndex ) )
572 const NoteBoox_Variable& aVariable = myVariableMap[ anIndex ];
573 if( !aVariable.Name.isEmpty() && myStudy->IsVariableUsed( std::string( aVariable.Name.toLatin1().constData() ) ) )
575 if( QMessageBox::warning( parentWidget(), tr( "WARNING" ),
576 tr( "RENAME_VARIABLE_IS_USED" ).arg( aVariable.Name ),
577 QMessageBox::Yes, QMessageBox::No ) == QMessageBox::No )
579 bool isBlocked = blockSignals( true );
580 aRow->SetName( aVariable.Name );
581 blockSignals( isBlocked );
588 //Case then varible name changed.
589 if(aCurrentColumn == NAME_COLUMN) {
590 if(!aRow->CheckName()) {
592 aMsg = tr( "VARNAME_INCORRECT" ).arg(aRow->GetName());
594 else if(!IsUniqueName(aRow)) {
596 aMsg = tr( "VARNAME_EXISTS" ).arg(aRow->GetName());
599 IsVariableComplited = aRow->CheckValue();
602 //Case then varible value changed.
603 else if(aCurrentColumn == VALUE_COLUMN){
604 if(!aRow->CheckValue()) {
606 aMsg = tr( "VARVALUE_INCORRECT" ).arg(aRow->GetName());
609 IsVariableComplited = aRow->CheckName() && IsUniqueName(aRow);
612 if(!IsCorrect && !aMsg.isEmpty())
613 SUIT_MessageBox::warning( parentWidget(), tr( "WARNING" ), aMsg );
615 bool isBlocked = blockSignals( true );
616 theItem->setForeground( QBrush( IsCorrect ? Qt::black : Qt::red ) );
617 blockSignals( isBlocked );
619 int anIndex = aRow->GetIndex();
620 if( myVariableMap.contains( anIndex ) )
622 NoteBoox_Variable& aVariable = myVariableMap[ anIndex ];
623 if( aVariable.Name.compare( aRow->GetName() ) != 0 ||
624 aVariable.Value.compare( aRow->GetValue() ) != 0 )
626 aVariable.Name = aRow->GetName();
627 aVariable.Value = aRow->GetValue();
633 if(IsCorrect && IsVariableComplited && IsLastRow(aRow))
638 myIsModified = isModified;
642 //============================================================================
643 /*! Function : IsUniqueName
644 * Purpose : Return true if theName is unique name of the Variable
646 //============================================================================
647 bool NoteBook_Table::IsUniqueName(const NoteBook_TableRow* theRow) const
649 for(int i=0; i<myRows.size();i++) {
650 if(myRows[i] == theRow )
652 if(myRows[i]->GetName().compare(theRow->GetName()) == 0)
658 //============================================================================
659 /*! Function : RemoveSelected
660 * Purpose : Remove selected rows in the table
662 //============================================================================
663 void NoteBook_Table::RemoveSelected()
665 isProcessItemChangedSignal = false;
666 QList<QTableWidgetItem*> aSelectedItems = selectedItems();
667 if( !(aSelectedItems.size() > 0)) {
668 isProcessItemChangedSignal = true;
671 bool removedFromStudy = false;
672 for(int i=0; i < aSelectedItems.size(); i++ ) {
673 NoteBook_TableRow* aRow = GetRowByItem(aSelectedItems[i]);
675 if(IsLastRow(aRow)) {
676 aRow->SetName(QString());
677 aRow->SetValue(QString());
680 int nRow = row(aSelectedItems[i]);
682 if( myStudy->IsVariableUsed( std::string( aRow->GetName().toLatin1().constData() ) ) )
684 if( QMessageBox::warning( parentWidget(), tr( "WARNING" ),
685 tr( "REMOVE_VARIABLE_IS_USED" ).arg( aRow->GetName() ),
686 QMessageBox::Yes, QMessageBox::No ) == QMessageBox::No )
688 isProcessItemChangedSignal = true;
693 int index = aRow->GetIndex();
694 QString aVarName = aRow->GetName();
695 myRemovedRows.append( index );
696 if( myVariableMap.contains( index ) )
697 myVariableMap.remove( index );
699 myRows.removeAt(nRow);
700 if(myStudy->IsVariable(aVarName.toLatin1().constData()))
701 removedFromStudy = true;
708 isProcessItemChangedSignal = true;
711 //============================================================================
712 /*! Function : SetProcessItemChangedSignalFlag
715 //============================================================================
716 void NoteBook_Table::SetProcessItemChangedSignalFlag(const bool enable)
718 isProcessItemChangedSignal = enable;
721 //============================================================================
722 /*! Function : GetProcessItemChangedSignalFlag
725 //============================================================================
726 bool NoteBook_Table::GetProcessItemChangedSignalFlag() const
728 return isProcessItemChangedSignal;
731 //============================================================================
732 /*! Function : GetRows
735 //============================================================================
736 QList<NoteBook_TableRow*> NoteBook_Table::GetRows() const
741 //============================================================================
742 /*! Function : ResetMaps
743 * Purpose : Reset variable maps
745 //============================================================================
746 void NoteBook_Table::ResetMaps()
748 myIsModified = false;
749 myVariableMapRef = myVariableMap;
750 myRemovedRows.clear();
753 ///////////////////////////////////////////////////////////////////////////
754 // SalomeApp_NoteBookDlg class //
755 ///////////////////////////////////////////////////////////////////////////
756 //============================================================================
757 /*! Function : SalomeApp_NoteBookDlg
758 * Purpose : Constructor
760 //============================================================================
761 SalomeApp_NoteBookDlg::SalomeApp_NoteBookDlg(QWidget * parent, _PTR(Study) theStudy):
762 QDialog(parent, Qt::WindowTitleHint | Qt::WindowSystemMenuHint),
766 setObjectName("SalomeApp_NoteBookDlg");
767 setWindowTitle(tr("NOTEBOOK_TITLE"));
768 QGridLayout* aLayout = new QGridLayout(this);
769 aLayout->setMargin(DEFAULT_MARGIN);
770 aLayout->setSpacing(DEFAULT_SPACING);
773 myTable = new NoteBook_Table(this);
774 aLayout->addWidget(myTable, 0, 0, 1, 3);
777 myRemoveButton = new QPushButton(tr("BUT_REMOVE"));
778 aLayout->addWidget(myRemoveButton, 1, 0, 1, 1);
780 QSpacerItem* spacer =
781 new QSpacerItem(DEFAULT_SPACING, 5 , QSizePolicy::Expanding, QSizePolicy::Minimum);
782 aLayout->addItem(spacer, 1, 1, 2, 1);
784 myUpdateStudyBtn = new QPushButton(tr("BUT_UPDATE_STUDY"));
785 aLayout->addWidget(myUpdateStudyBtn, 1, 2, 1, 1);
787 QGroupBox* groupBox = new QGroupBox(this);
789 QGridLayout* aLayout1 = new QGridLayout(groupBox);
791 aLayout1->setMargin(DEFAULT_MARGIN);
792 aLayout1->setSpacing(DEFAULT_SPACING);
794 myOkBtn = new QPushButton(tr("BUT_APPLY_AND_CLOSE"));
795 aLayout1->addWidget(myOkBtn, 0, 0, 1, 1);
797 myApplyBtn = new QPushButton(tr("BUT_APPLY"));
798 aLayout1->addWidget(myApplyBtn, 0, 1, 1, 1);
800 QSpacerItem* spacer1 =
801 new QSpacerItem(DEFAULT_SPACING, 5, QSizePolicy::Expanding, QSizePolicy::Minimum);
802 aLayout1->addItem(spacer1, 0, 2, 1, 1);
804 myCancelBtn = new QPushButton(tr("BUT_CLOSE"));
805 aLayout1->addWidget(myCancelBtn, 0, 3, 1, 1);
807 myHelpBtn = new QPushButton(tr("BUT_HELP"));
808 aLayout1->addWidget(myHelpBtn, 0, 4, 1, 1);
810 aLayout->addWidget(groupBox, 2, 0, 1, 3);
812 QWidgetList aWidgetList;
813 aWidgetList.append( myTable );
814 aWidgetList.append( myOkBtn );
815 aWidgetList.append( myApplyBtn );
816 aWidgetList.append( myCancelBtn );
817 aWidgetList.append( myHelpBtn );
818 aWidgetList.append( myUpdateStudyBtn );
819 aWidgetList.append( myRemoveButton );
820 Qtx::setTabOrder( aWidgetList );
822 connect( myOkBtn, SIGNAL(clicked()), this, SLOT(onOK()) );
823 connect( myApplyBtn, SIGNAL(clicked()), this, SLOT(onApply()) );
824 connect( myCancelBtn, SIGNAL(clicked()), this, SLOT(onCancel()) );
825 connect( myUpdateStudyBtn, SIGNAL(clicked()), this, SLOT(onUpdateStudy()) );
826 connect( myRemoveButton, SIGNAL(clicked()), this, SLOT(onRemove()));
827 connect( myHelpBtn, SIGNAL(clicked()), this, SLOT(onHelp()));
829 myTable->Init(myStudy);
832 //============================================================================
833 /*! Function : ~SalomeApp_NoteBookDlg
834 * Purpose : Destructor
836 //============================================================================
837 SalomeApp_NoteBookDlg::~SalomeApp_NoteBookDlg(){}
840 //============================================================================
841 /*! Function : Init()
842 * Purpose : init variable table
844 //============================================================================
845 void SalomeApp_NoteBookDlg::Init(_PTR(Study) theStudy){
846 if(myStudy!= theStudy)
848 myTable->Init(myStudy);
852 //============================================================================
856 //============================================================================
857 void SalomeApp_NoteBookDlg::onOK()
860 if( myTable->IsValid() )
864 //============================================================================
865 /*! Function : onHelp
868 //============================================================================
869 void SalomeApp_NoteBookDlg::onHelp()
871 QString aHelpFileName("using_notebook.html");
872 LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
874 app->onHelpContextModule("GUI",aHelpFileName);
878 platform = "winapplication";
880 platform = "application";
882 SUIT_MessageBox::warning(this, tr("WRN_WARNING"),
883 tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
884 arg(app->resourceMgr()->stringValue("ExternalBrowser",
890 //============================================================================
891 /*! Function : onApply
894 //============================================================================
895 void SalomeApp_NoteBookDlg::onApply()
897 if( !myTable->IsValid() )
899 SUIT_MessageBox::warning( this, tr( "WARNING" ), tr( "INCORRECT_DATA" ) );
907 const QList<int>& aRemovedRows = myTable->GetRemovedRows();
908 const VariableMap& aVariableMap = myTable->GetVariableMap();
909 const VariableMap& aVariableMapRef = myTable->GetVariableMapRef();
911 for( QListIterator<int> anIter( aRemovedRows ); anIter.hasNext(); )
913 int anIndex = anIter.next();
914 if( aVariableMapRef.contains( anIndex ) )
916 QString aRemovedVariable = aVariableMapRef[ anIndex ].Name;
917 myStudy->RemoveVariable( std::string( aRemovedVariable.toLatin1().constData() ) );
921 VariableMap::const_iterator it = aVariableMap.constBegin(), itEnd = aVariableMap.constEnd();
922 for( ; it != itEnd; ++it )
924 int anIndex = it.key();
925 const NoteBoox_Variable& aVariable = it.value();
926 QString aName = aVariable.Name;
927 QString aValue = aVariable.Value;
929 if( !aName.isEmpty() && !aValue.isEmpty() )
931 if( aVariableMapRef.contains( anIndex ) )
933 const NoteBoox_Variable& aVariableRef = aVariableMapRef[ anIndex ];
934 QString aNameRef = aVariableRef.Name;
935 QString aValueRef = aVariableRef.Value;
937 if( !aNameRef.isEmpty() && !aValueRef.isEmpty() && aNameRef != aName )
939 myStudy->RenameVariable( std::string( aNameRef.toLatin1().constData() ),
940 std::string( aName.toLatin1().constData() ) );
944 if( NoteBook_TableRow::IsIntegerValue(aValue,&anIVal) )
945 myStudy->SetInteger(std::string(aName.toLatin1().constData()),anIVal);
947 else if( NoteBook_TableRow::IsRealValue(aValue,&aDVal) )
948 myStudy->SetReal(std::string(aName.toLatin1().constData()),aDVal);
950 else if( NoteBook_TableRow::IsBooleanValue(aValue,&aBVal) )
951 myStudy->SetBoolean(std::string(aName.toLatin1().constData()),aBVal);
954 myStudy->SetString(std::string(aName.toLatin1().constData()),aValue.toStdString());
957 myTable->ResetMaps();
959 SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>( SUIT_Session::session()->activeApplication() );
961 app->updateActions();
965 //============================================================================
966 /*! Function : onCancel
969 //============================================================================
970 void SalomeApp_NoteBookDlg::onCancel()
972 if( myTable->IsModified() )
974 int answer = QMessageBox::question( this, tr( "CLOSE_CAPTION" ), tr( "CLOSE_DESCRIPTION" ),
975 QMessageBox::Yes, QMessageBox::No, QMessageBox::Cancel );
978 case QMessageBox::Yes : onOK(); return;
979 case QMessageBox::No : break;
980 case QMessageBox::Cancel : return;
987 //============================================================================
988 /*! Function : onRemove
991 //============================================================================
992 void SalomeApp_NoteBookDlg::onRemove()
994 myTable->RemoveSelected();
997 //============================================================================
998 /*! Function : onUpdateStudy
1001 //============================================================================
1002 void SalomeApp_NoteBookDlg::onUpdateStudy()
1005 if( !myTable->IsValid() )
1008 QApplication::setOverrideCursor( Qt::WaitCursor );
1010 if( !updateStudy() )
1011 SUIT_MessageBox::warning( this, tr( "ERROR" ), tr( "ERR_UPDATE_STUDY_FAILED" ) );
1013 QApplication::restoreOverrideCursor();
1016 //============================================================================
1017 /*! Function : updateStudy
1020 //============================================================================
1021 bool SalomeApp_NoteBookDlg::updateStudy()
1023 SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>( SUIT_Session::session()->activeApplication() );
1027 SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
1031 bool isStudySaved = study->isSaved();
1032 QString aStudyName = study->studyName();
1034 _PTR(Study) studyDS = study->studyDS();
1036 // get unique temporary directory name
1037 QString aTmpDir = QString::fromStdString( SALOMEDS_Tool::GetTmpDir() );
1038 if( aTmpDir.isEmpty() )
1041 if( aTmpDir.right( 1 ).compare( QDir::separator() ) == 0 )
1042 aTmpDir.remove( aTmpDir.length() - 1, 1 );
1044 // dump study to the temporary directory
1045 QString aFileName( "notebook" );
1046 bool toPublish = true;
1047 bool isMultiFile = false;
1048 bool toSaveGUI = true;
1051 _PTR(AttributeParameter) ap;
1052 _PTR(IParameters) ip = ClientFactory::getIParameters(ap);
1053 if(ip->isDumpPython(studyDS)) ip->setDumpPython(studyDS); //Unset DumpPython flag.
1054 if ( toSaveGUI ) { //SRN: Store a visual state of the study at the save point for DumpStudy method
1055 ip->setDumpPython(studyDS);
1056 savePoint = SalomeApp_VisualState( app ).storeState(); //SRN: create a temporary save point
1058 bool ok = studyDS->DumpStudy( aTmpDir.toStdString(), aFileName.toStdString(), toPublish, isMultiFile );
1060 study->removeSavePoint(savePoint); //SRN: remove the created temporary save point.
1065 // clear a study (delete all objects)
1068 // get active application
1069 app = dynamic_cast<SalomeApp_Application*>( SUIT_Session::session()->activeApplication() );
1071 // load study from the temporary directory
1072 QString command = QString( "execfile(r\"%1\")" ).arg( aTmpDir + QDir::separator() + aFileName + ".py" );
1074 PyConsole_Console* pyConsole = app->pythonConsole();
1076 pyConsole->execAndWait( command );
1078 // remove temporary directory
1079 QDir aDir( aTmpDir );
1080 QStringList aFiles = aDir.entryList( QStringList( "*.py*" ) );
1081 for( QStringList::iterator it = aFiles.begin(), itEnd = aFiles.end(); it != itEnd; ++it )
1082 ok = aDir.remove( *it ) && ok;
1084 ok = aDir.rmdir( aTmpDir );
1086 if( SalomeApp_Study* newStudy = dynamic_cast<SalomeApp_Study*>( app->activeStudy() ) )
1088 myStudy = newStudy->studyDS();
1089 myTable->Init(myStudy);
1091 newStudy->markAsSavedIn(aStudyName);
1100 //============================================================================
1101 /*! Function : clearStudy
1104 //============================================================================
1105 void SalomeApp_NoteBookDlg::clearStudy()
1107 SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>( SUIT_Session::session()->activeApplication() );
1111 QList<SUIT_Application*> aList = SUIT_Session::session()->applications();
1112 int anIndex = aList.indexOf( app );
1114 //Store position and size of the this dialog
1120 // Disconnect dialog from application desktop in case if:
1121 // 1) Application is not the first application in the session
1122 // 2) Application is the first application in session but not the only.
1123 bool changeDesktop = ((anIndex > 0) || (anIndex == 0 && aList.count() > 1));
1128 app->onCloseDoc( false );
1130 if( anIndex > 0 && anIndex < aList.count() )
1131 app = dynamic_cast<SalomeApp_Application*>( aList[ anIndex - 1 ] );
1132 else if(anIndex == 0 && aList.count() > 1)
1133 app = dynamic_cast<SalomeApp_Application*>( aList[ 1 ] );
1140 app = dynamic_cast<SalomeApp_Application*>( SUIT_Session::session()->activeApplication() );
1141 if( changeDesktop && app ) {
1142 setParent( app->desktop(), Qt::Dialog );
1143 app->setNoteBook(this);
1145 //Set position and size of the this dialog