1 // Copyright (C) 2007-2008 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 // SMESH SMESHGUI : GUI for SMESH component
23 // File : SMESHGUI_FilterLibraryDlg.cxx
24 // Author : Sergey LITONIN, Open CASCADE S.A.S.
27 #include "SMESHGUI_FilterLibraryDlg.h"
30 #include "SMESHGUI_FilterUtils.h"
31 #include "SMESHGUI_FilterDlg.h"
33 // SALOME GUI includes
34 #include <SUIT_Session.h>
35 #include <SUIT_Desktop.h>
36 #include <SUIT_FileDlg.h>
37 #include <SUIT_MessageBox.h>
38 #include <SUIT_ResourceMgr.h>
40 #include <LightApp_Application.h>
43 #include <QApplication>
44 #include <QVBoxLayout>
45 #include <QHBoxLayout>
46 #include <QGridLayout>
48 #include <QPushButton>
51 #include <QListWidget>
60 * Class : SMESHGUI_FilterLibraryDlg::Dialog
61 * Description : Dialog for opening filter library
64 class SMESHGUI_FilterLibraryDlg::Dialog : public SUIT_FileDlg
67 Dialog(QWidget* theParent, const bool theToOpen);
71 virtual bool acceptData();
74 SMESHGUI_FilterLibraryDlg::Dialog::Dialog (QWidget* theParent,
76 : SUIT_FileDlg(theParent, theToOpen)
80 SMESHGUI_FilterLibraryDlg::Dialog::~Dialog()
84 bool SMESHGUI_FilterLibraryDlg::Dialog::acceptData()
86 // if (mode() != QFileDialogP::AnyFile)
87 // return SUIT_FileDlg::acceptData();
93 * Class : SMESHGUI_FilterLibraryDlg
94 * Description : Dialog to specify filters for VTK viewer
97 //=======================================================================
98 // name : SMESHGUI_FilterLibraryDlg::SMESHGUI_FilterLibraryDlg
99 // Purpose : Constructor
100 //=======================================================================
101 SMESHGUI_FilterLibraryDlg::SMESHGUI_FilterLibraryDlg (SMESHGUI* theModule,
103 const QList<int>& theTypes,
106 mySMESHGUI( theModule )
109 construct(theTypes, theMode);
112 //=======================================================================
113 // name : SMESHGUI_FilterLibraryDlg::SMESHGUI_FilterLibraryDlg
114 // Purpose : Constructor
115 //=======================================================================
116 SMESHGUI_FilterLibraryDlg::SMESHGUI_FilterLibraryDlg (SMESHGUI* theModule,
121 mySMESHGUI( theModule )
125 aTypes.append(theType);
126 construct(aTypes, theMode);
129 //=======================================================================
130 // name : SMESHGUI_FilterLibraryDlg::construct
131 // Purpose : Construct dialog (called by constructor)
132 //=======================================================================
133 void SMESHGUI_FilterLibraryDlg::construct (const QList<int>& theTypes,
139 QVBoxLayout* aDlgLay = new QVBoxLayout(this);
140 aDlgLay->setMargin(MARGIN);
141 aDlgLay->setSpacing(SPACING);
143 myMainFrame = createMainFrame (this);
144 QWidget* aBtnFrame = createButtonFrame(this);
146 aDlgLay->addWidget(myMainFrame);
147 aDlgLay->addWidget(aBtnFrame);
149 aDlgLay->setStretchFactor(myMainFrame, 1);
151 myHelpFileName = "selection_filter_library_page.html";
153 Init(myTypes, myMode);
156 //=======================================================================
157 // name : SMESHGUI_FilterLibraryDlg::createMainFrame
158 // Purpose : Create frame containing dialog's input fields
159 //=======================================================================
160 QWidget* SMESHGUI_FilterLibraryDlg::createMainFrame (QWidget* theParent)
162 QWidget* aMainFrame = new QWidget(theParent);
163 QGridLayout* aMainLay = new QGridLayout(aMainFrame);
164 aMainLay->setMargin(0);
165 aMainLay->setSpacing(SPACING);
169 QLabel* aFileLab = new QLabel(tr("LIBRARY_FILE"), aMainFrame);
170 myFileName = new QLineEdit(aMainFrame);
171 myOpenBtn = new QPushButton(aMainFrame);
172 myOpenBtn->setIcon(SUIT_Session::session()->resourceMgr()->loadPixmap(
173 "SUIT", tr("ICON_FILE_OPEN")));
177 QGroupBox* aFiltersGrp = new QGroupBox(tr("FILTER_NAMES"), aMainFrame);
178 QGridLayout* aLay = new QGridLayout(aFiltersGrp);
179 aLay->setMargin(MARGIN);
180 aLay->setSpacing(SPACING);
182 myListBox = new QListWidget(aFiltersGrp);
184 myAddBtn = new QPushButton(tr("ADD"), aFiltersGrp);
185 myDeleteBtn = new QPushButton(tr("DELETE"), aFiltersGrp);
187 aLay->addWidget(myListBox, 0, 0, 3, 1);
188 aLay->addWidget(myAddBtn, 0, 1);
189 aLay->addWidget(myDeleteBtn, 1, 1);
190 aLay->setRowStretch(2, 5);
194 myNameGrp = new QWidget(aMainFrame);
195 QHBoxLayout* myNameGrpLayout = new QHBoxLayout(myNameGrp);
196 myNameGrpLayout->setMargin(0);
197 myNameGrpLayout->setSpacing(SPACING);
199 myNameGrpLayout->addWidget( new QLabel(tr("FILTER_NAME"), myNameGrp) );
200 myNameGrpLayout->addWidget( myName = new QLineEdit(myNameGrp) );
204 myTable = new SMESHGUI_FilterTable( mySMESHGUI, aMainFrame, myTypes);
205 myTable->SetEditable(myMode == EDIT);
206 myTable->SetLibsEnabled(false);
208 myListBox->setMinimumHeight((int)(myTable->sizeHint().height() * 0.5));
209 //myListBox->setRowMode(QListWidget::FitToWidth); //VSR : TODO ???
210 myListBox->setSelectionMode(QListWidget::SingleSelection);
212 myOpenBtn->setAutoDefault(false);
213 myAddBtn->setAutoDefault(false);
214 myDeleteBtn->setAutoDefault(false);
216 aMainLay->addWidget(aFileLab, 0, 0);
217 aMainLay->addWidget(myFileName, 0, 1);
218 aMainLay->addWidget(myOpenBtn, 0, 2);
219 aMainLay->addWidget(aFiltersGrp, 1, 0, 1, 3);
220 aMainLay->addWidget(myNameGrp, 2, 0, 1, 3);
221 aMainLay->addWidget(myTable, 3, 0, 1, 3);
223 // connect signals and slots
225 connect(myFileName, SIGNAL(returnPressed()), this, SLOT(onReturnPressed()));
226 connect(myOpenBtn, SIGNAL(clicked()), this, SLOT(onBrowse()));
228 connect(myListBox, SIGNAL(itemSelectionChanged()),
229 this, SLOT(onFilterChanged()));
231 connect(myAddBtn, SIGNAL(clicked()), this, SLOT(onAddBtnPressed()));
232 connect(myDeleteBtn, SIGNAL(clicked()), this, SLOT(onDeleteBtnPressed()));
234 connect(myName, SIGNAL(textChanged(const QString&)),
235 this, SLOT(onFilterNameChanged(const QString&)));
237 connect(myTable, SIGNAL(EntityTypeChanged(const int)),
238 this, SLOT(onEntityTypeChanged(const int)));
240 connect(myTable, SIGNAL(NeedValidation()), this, SLOT(onNeedValidation()));
245 //=======================================================================
246 // name : SMESHGUI_FilterLibraryDlg::createButtonFrame
247 // Purpose : Create frame containing buttons
248 //=======================================================================
249 QWidget* SMESHGUI_FilterLibraryDlg::createButtonFrame (QWidget* theParent)
251 QGroupBox* aGrp = new QGroupBox(theParent);
252 QHBoxLayout* aLay = new QHBoxLayout(aGrp);
253 aLay->setMargin(MARGIN);
254 aLay->setSpacing(SPACING);
256 myButtons[ BTN_OK ] = new QPushButton(tr("SMESH_BUT_APPLY_AND_CLOSE"), aGrp);
257 myButtons[ BTN_Apply ] = new QPushButton(tr("SMESH_BUT_APPLY"), aGrp);
259 myButtons[ BTN_Cancel ] = new QPushButton(tr("SMESH_BUT_CANCEL"), aGrp);
260 myButtons[ BTN_Close ] = new QPushButton(tr("SMESH_BUT_CLOSE"), aGrp);
261 myButtons[ BTN_Help ] = new QPushButton(tr("SMESH_BUT_HELP"), aGrp);
263 aLay->addWidget(myButtons[ BTN_OK ]);
264 aLay->addSpacing(10);
265 aLay->addWidget(myButtons[ BTN_Apply ]);
266 aLay->addSpacing(10);
268 aLay->addWidget(myButtons[ BTN_Cancel ]);
269 aLay->addWidget(myButtons[ BTN_Close ]);
270 aLay->addWidget(myButtons[ BTN_Help ]);
272 connect(myButtons[ BTN_OK ], SIGNAL(clicked()), SLOT(onOk()));
273 connect(myButtons[ BTN_Cancel ], SIGNAL(clicked()), SLOT(onClose()));
274 connect(myButtons[ BTN_Close ], SIGNAL(clicked()), SLOT(onClose()));
275 connect(myButtons[ BTN_Apply ], SIGNAL(clicked()), SLOT(onApply()));
276 connect(myButtons[ BTN_Help ], SIGNAL(clicked()), SLOT(onHelp()));
278 QMap<int, QPushButton*>::iterator anIter;
279 for (anIter = myButtons.begin(); anIter != myButtons.end(); ++anIter)
280 anIter.value()->setAutoDefault(false);
287 //=======================================================================
288 // name : SMESHGUI_FilterLibraryDlg::updateMainButtons
289 // Purpose : Update visibility of main buttons (OK, Cancel, Close ...)
290 //=======================================================================
291 void SMESHGUI_FilterLibraryDlg::updateMainButtons()
293 if (myTypes.count() == 1) {
294 myButtons[ BTN_Cancel ]->show();
295 myButtons[ BTN_Apply ]->hide();
296 myButtons[ BTN_Close ]->hide();
298 myButtons[ BTN_Cancel ]->hide();
299 myButtons[ BTN_Apply ]->show();
300 myButtons[ BTN_Close ]->show();
304 //=======================================================================
305 // name : SMESHGUI_FilterLibraryDlg::~SMESHGUI_FilterLibraryDlg
306 // Purpose : Destructor
307 //=======================================================================
308 SMESHGUI_FilterLibraryDlg::~SMESHGUI_FilterLibraryDlg()
312 //=======================================================================
313 // name : SMESHGUI_FilterLibraryDlg::Init
314 // Purpose : Init dialog fields, connect signals and slots, show dialog
315 //=======================================================================
316 void SMESHGUI_FilterLibraryDlg::Init (const int type, const int theMode)
320 Init(aTypes, theMode);
323 //=======================================================================
324 // name : SMESHGUI_FilterLibraryDlg::Init
325 // Purpose : Init dialog fields, connect signals and slots, show dialog
326 //=======================================================================
327 void SMESHGUI_FilterLibraryDlg::Init (const QList<int>& theTypes,
332 myTable->Init(theTypes);
333 myCurrFilterName = "";
335 myListBox->blockSignals(true);
337 myListBox->blockSignals(false);
341 updateControlsVisibility();
344 connect( mySMESHGUI, SIGNAL(SignalDeactivateActiveDialog()), SLOT(onDeactivate()));
345 connect( mySMESHGUI, SIGNAL(SignalCloseAllDialogs()), SLOT(onClose()));
347 if (myMode == ADD_TO)
349 setWindowTitle(tr("ADD_TO_TLT"));
350 if (myFileName->text().isEmpty())
351 myFileName->setText(getDefaultLibraryName());
354 else if (myMode == COPY_FROM)
356 setWindowTitle(tr("COPY_FROM_TLT"));
357 if (myFileName->text().isEmpty())
358 myFileName->setText(getDefaultLibraryName());
360 if (myListBox->count() > 0)
361 myListBox->setCurrentItem(0);
365 setWindowTitle(tr("EDIT_LIB_TLT"));
366 if (myFileName->text().isEmpty())
367 myFileName->setText(getDefaultLibraryName());
369 if (myListBox->count() > 0)
370 myListBox->setCurrentItem(0);
376 isPermissionValid(false);
379 //=======================================================================
380 // name : SMESHGUI_FilterLibraryDlg::updateControlsVisibility
381 // Purpose : Update visibility of controls in accordance with myMode
382 //=======================================================================
383 void SMESHGUI_FilterLibraryDlg::updateControlsVisibility()
385 if (myMode == ADD_TO)
388 myNameGrp->setEnabled(true);
391 myTable->SetEditable(false);
393 else if (myMode == COPY_FROM)
396 myNameGrp->setEnabled(false);
399 myTable->SetEditable(false);
401 else if (myMode == EDIT)
404 myNameGrp->setEnabled(true);
407 myTable->SetEditable(true);
410 qApp->processEvents();
415 //=======================================================================
416 // name : SMESHGUI_FilterLibraryDlg::onApply
417 // Purpose : SLOT called when "Apply" button pressed.
418 //=======================================================================
419 bool SMESHGUI_FilterLibraryDlg::onApply()
421 if (!isValid(true) || !isPermissionValid(false))
424 if (myLibrary->_is_nil()) {
425 SUIT_MessageBox::information(SMESHGUI::desktop(), tr("SMESH_WRN_WARNING"),
426 tr("LIBRARY_IS_NOT_LOADED"));
430 if (myFileName->text() != myLibrary->GetFileName())
431 myLibrary->SetFileName( myFileName->text().toLatin1().constData() );
433 bool aResult = false;
435 if (myMode == COPY_FROM || myListBox->count() == 0) {
437 } else if (myMode == EDIT || myMode == ADD_TO) {
438 SMESH::Filter_var aFilter = createFilter();
439 if (!myListBox->selectedItems().empty() &&
440 !myLibrary->Replace(myCurrFilterName.toLatin1().constData(),
441 myName->text().toLatin1().constData(),
443 SUIT_MessageBox::information(SMESHGUI::desktop(), tr("SMESH_ERROR"),
444 tr("ERROR_OF_EDITING"));
451 if (aResult && myMode != COPY_FROM)
452 aResult = myLibrary->Save();
455 char* aFileName = myLibrary->GetFileName();
456 getDefaultLibraryName() = QString(aFileName);
458 } else if (myMode != COPY_FROM) {
459 SUIT_MessageBox::information(SMESHGUI::desktop(), tr("SMESH_ERROR"),
460 tr("ERROR_OF_SAVING"));
467 //=======================================================================
468 // name : SMESHGUI_FilterLibraryDlg::onOk
469 // Purpose : SLOT called when "Ok" button pressed.
470 // Assign filters VTK viewer and close dialog
471 //=======================================================================
472 void SMESHGUI_FilterLibraryDlg::onOk()
476 disconnect( mySMESHGUI, 0, this, 0);
477 mySMESHGUI->ResetState();
482 //=======================================================================
483 // name : SMESHGUI_FilterLibraryDlg::onClose
484 // Purpose : SLOT called when "Close" button pressed. Close dialog
485 //=======================================================================
486 void SMESHGUI_FilterLibraryDlg::onClose()
488 disconnect( mySMESHGUI, 0, this, 0);
489 mySMESHGUI->ResetState();
493 //=================================================================================
494 // function : onHelp()
496 //=================================================================================
497 void SMESHGUI_FilterLibraryDlg::onHelp()
499 LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
501 app->onHelpContextModule(mySMESHGUI ? app->moduleName(mySMESHGUI->moduleName()) : QString(""), myHelpFileName);
505 platform = "winapplication";
507 platform = "application";
509 SUIT_MessageBox::warning(this, tr("WRN_WARNING"),
510 tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
511 arg(app->resourceMgr()->stringValue("ExternalBrowser",
513 arg(myHelpFileName));
517 //=======================================================================
518 // name : SMESHGUI_FilterLibraryDlg::onDeactivate
519 // Purpose : SLOT called when dialog must be deativated
520 //=======================================================================
521 void SMESHGUI_FilterLibraryDlg::onDeactivate()
526 //=======================================================================
527 // name : SMESHGUI_FilterLibraryDlg::enterEvent
528 // Purpose : Event filter
529 //=======================================================================
530 void SMESHGUI_FilterLibraryDlg::enterEvent(QEvent*)
535 //=================================================================================
536 // function : closeEvent()
537 // purpose : Close dialog
538 //=================================================================================
539 void SMESHGUI_FilterLibraryDlg::closeEvent(QCloseEvent* e)
544 //=======================================================================
545 // name : SMESHGUI_FilterLibraryDlg::getFileName
546 // Purpose : Get file name
547 //=======================================================================
548 QString SMESHGUI_FilterLibraryDlg::getFileName() const
550 return myFileName != 0 ? myFileName->text() : QString("");
553 //================================================================
554 // Function : setFileName
555 // Purpose : Set file name to line edit
556 //================================================================
557 void SMESHGUI_FilterLibraryDlg::setFileName(const QString& txt, const bool autoExt)
561 myFileName->setText(autoExt ? autoExtension(txt) : txt);
564 //================================================================
565 // Function : autoExtension
566 // Purpose : Append extension to the file name
567 //================================================================
568 QString SMESHGUI_FilterLibraryDlg::autoExtension(const QString& theFileName) const
570 QString anExt = theFileName.section('.', -1);
571 return anExt != "xml" && anExt != "XML" ? theFileName + ".xml" : theFileName;
574 //================================================================
575 // Function : filterWildCards
577 //================================================================
578 QStringList SMESHGUI_FilterLibraryDlg::filterWildCards(const QString& theFilter) const
582 int b = theFilter.lastIndexOf("(");
583 int e = theFilter.lastIndexOf(")");
584 if (b != -1 && e != -1)
586 QString content = theFilter.mid(b + 1, e - b - 1).trimmed();
587 QStringList lst = content.split(" ", QString::SkipEmptyParts);
588 for (QStringList::const_iterator it = lst.begin(); it != lst.end(); ++it)
589 if ((*it).indexOf(".") != -1)
590 res.append((*it).trimmed());
595 //=======================================================================
596 // name : SMESHGUI_FilterLibraryDlg::prepareFilters
597 // Purpose : Prepare filters for dialog
598 //=======================================================================
599 QStringList SMESHGUI_FilterLibraryDlg::prepareFilters() const
601 static QStringList aList;
604 aList.append(tr("XML_FILT"));
605 //aList.append(tr("ALL_FILES_FILTER"));
611 //================================================================
612 // Function : onBrowse
613 // Purpose : SLOT. Display "Open file" dialog for chosing library name
614 //================================================================
615 void SMESHGUI_FilterLibraryDlg::onBrowse()
617 Dialog* aDlg = new Dialog(this, true);
618 aDlg->setWindowTitle(tr("OPEN_LIBRARY"));
620 //aDlg->setMode(myMode == COPY_FROM ? QFileDialogP::ExistingFile : QFileDialogP::AnyFile);
621 aDlg->setFileMode(myMode == COPY_FROM ? QFileDialog::ExistingFile : QFileDialog::AnyFile);
622 aDlg->setFilters(prepareFilters());
623 aDlg->selectFile(getFileName());
625 QPushButton* anOkBtn = (QPushButton*)aDlg->findChild<QPushButton*>("OK");
627 anOkBtn->setText(tr("SMESH_BUT_OK"));
629 if (aDlg->exec() != Accepted)
632 QString fName = aDlg->selectedFile();
637 if (QFileInfo(fName).suffix().isEmpty())
638 fName = autoExtension(fName);
640 fName = QDir::convertSeparators(fName);
641 QString prev = QDir::convertSeparators(getFileName());
648 QListWidgetItem* item = myListBox->item( myListBox->count()-1 );
649 QString aName = item ? item->text() : QString::null;
652 if (myMode == ADD_TO)
654 myTable->Copy((SMESHGUI_FilterTable*)parentWidget());
655 myCurrFilterName = "";
657 addFilterToLib(aName);
660 isPermissionValid(false);
663 //=======================================================================
664 // name : SMESHGUI_FilterLibraryDlg::processNewLibrary
665 // Purpose : SLOT. Calleds when file name changed
666 //=======================================================================
667 void SMESHGUI_FilterLibraryDlg::processNewLibrary()
669 SMESH::FilterManager_var aFilterMgr = SMESH::GetFilterManager();
670 if (aFilterMgr->_is_nil())
673 myLibrary = aFilterMgr->LoadLibrary(autoExtension(getFileName()).toLatin1().constData());
674 if (myLibrary->_is_nil()) {
675 if (myMode == COPY_FROM) {
676 SUIT_MessageBox::information(SMESHGUI::desktop(), tr("SMESH_ERROR"),
680 myLibrary = aFilterMgr->CreateLibrary();
681 myLibrary->SetFileName(getFileName().toLatin1().constData());
688 //=======================================================================
689 // name : SMESHGUI_FilterLibraryDlg::updateList
690 // Purpose : Fill list box with filter names
691 //=======================================================================
692 void SMESHGUI_FilterLibraryDlg::updateList()
695 SMESH::string_array_var aNames = myLibrary->GetNames((SMESH::ElementType)myTable->GetType());
696 for (int i = 0, n = aNames->length(); i < n; i++)
697 aList.append(QString(aNames[ i ]));
698 myListBox->blockSignals(true);
700 myListBox->blockSignals(false);
701 myListBox->addItems(aList);
702 if (myListBox->count() == 0)
704 myTable->Clear(myTable->GetType());
706 myName->setEnabled(false);
707 myTable->SetEnabled(false);
711 myName->setEnabled(true);
712 myTable->SetEnabled(true);
713 if (myListBox->count())
715 myCurrFilterName = "";
716 myListBox->setCurrentItem(0);
721 //=======================================================================
722 // name : SMESHGUI_FilterLibraryDlg::isNameValid
723 // Purpose : Verify validity if entered data
724 //=======================================================================
725 bool SMESHGUI_FilterLibraryDlg::isNameValid(const bool theMess) const
727 // verify validity of filter name
728 if (myName->isEnabled() && !myCurrFilterName.isEmpty()) {
729 QString aCurrName = myName->text();
730 if (aCurrName.isEmpty()) {
732 SUIT_MessageBox::information(SMESHGUI::desktop(), tr("SMESH_INSUFFICIENT_DATA"),
733 tr("EMPTY_FILTER_NAME"));
737 SMESH::string_array_var aNames = myLibrary->GetAllNames();
738 for (int f = 0, n = aNames->length(); f < n; f++) {
739 if (aNames[ f ] == aCurrName && aNames[ f ] != myCurrFilterName) {
741 SUIT_MessageBox::information(SMESHGUI::desktop(), tr("SMESH_INSUFFICIENT_DATA"),
742 tr("ERROR_FILTER_NAME"));
751 //=======================================================================
752 // name : SMESHGUI_FilterLibraryDlg::isPermissionValid
753 // Purpose : Verify write permission on file
754 //=======================================================================
755 bool SMESHGUI_FilterLibraryDlg::isPermissionValid(const bool theIsExistingOnly)
757 if (myMode == COPY_FROM)
760 // Verify write permission
761 bool isWritable = false;
763 QString fName(myFileName->text());
764 if (QFileInfo(fName).suffix().isEmpty())
765 fName = autoExtension(fName);
767 fName = QDir::convertSeparators(fName);
769 if (QFileInfo(fName).exists()) {
770 isWritable = QFileInfo(fName).isWritable();
771 } else if (!theIsExistingOnly) {
772 QFileInfo aDirInfo(QFileInfo(fName).absolutePath());
773 isWritable = aDirInfo.isWritable();
774 /*if (QDir(QFileInfo(fName).dirPath(true)).exists() ||
775 QDir().mkdir(QFileInfo(fName).dirPath(true)))
778 if (aFile.open(IO_WriteOnly))
789 SUIT_MessageBox::information(SMESHGUI::desktop(), tr("SMESH_WRN_WARNING"),
790 tr("NO_PERMISSION"));
797 //=======================================================================
798 // name : SMESHGUI_FilterLibraryDlg::isValid
799 // Purpose : Verify validity if entered data
800 //=======================================================================
801 bool SMESHGUI_FilterLibraryDlg::isValid(const bool theMess) const
803 // verify validity of table
804 if (!myTable->IsValid(theMess) || !isNameValid(theMess))
810 //=======================================================================
811 // name : SMESHGUI_FilterLibraryDlg::onFilterChanged
812 // Purpose : SLOT. Called when selected filter of library is changed
813 //=======================================================================
814 void SMESHGUI_FilterLibraryDlg::onFilterChanged()
816 QString theName = myListBox->currentItem() ? myListBox->currentItem()->text() : QString::null;
817 if (myLibrary->_is_nil())
820 // Save parameters of filter if it was changed
822 if (!myCurrFilterName.isEmpty() && myTable->IsEditable())
826 myListBox->blockSignals(true);
827 myListBox->setCurrentRow(myCurrFilter);
828 myListBox->blockSignals(false);
832 SMESH::Filter_var aFilter = createFilter();
833 myLibrary->Replace(myCurrFilterName.toLatin1().constData(),
834 myName->text().toLatin1().constData(),
838 // Fill table with filter parameters
840 SMESH::Filter_var aFilter = myLibrary->Copy(theName.toLatin1().constData());
841 myCurrFilterName = theName;
842 myCurrFilter = myListBox->currentRow();
843 myName->setText(theName);
846 SMESH::Filter::Criteria_var aCriteria;
848 myTable->Clear(myTable->GetType());
850 if (CORBA::is_nil( aFilter ) || !aFilter->GetCriteria(aCriteria))
853 for (int i = 0, n = aCriteria->length(); i < n; i++)
854 myTable->AddCriterion(aCriteria[ i ], myTable->GetType());
857 updateControlsVisibility(); // IPAL19974
860 //=======================================================================
861 // name : SMESHGUI_FilterLibraryDlg::onReturnPressed
862 // Purpose : SLOT. Called when enter button is pressed in library name field
864 //=======================================================================
865 void SMESHGUI_FilterLibraryDlg::onReturnPressed()
867 QListWidgetItem* item = myListBox->item( myListBox->count()-1 );
868 QString aName = item ? item->text() : QString::null;
872 if (myMode == ADD_TO)
874 myTable->Copy((SMESHGUI_FilterTable*)parentWidget());
875 myCurrFilterName = "";
877 addFilterToLib(aName);
880 isPermissionValid(false);
883 //=======================================================================
884 // name : SMESHGUI_FilterLibraryDlg::enableMainButtons
885 // Purpose : Update state of "OK", "Cancel" buttons
886 //=======================================================================
887 void SMESHGUI_FilterLibraryDlg::enableMainButtons()
889 /*bool isEnabled = isValid(false);
890 if (myButtons.contains(BTN_OK))
891 myButtons[ BTN_OK ]->setEnabled(isEnabled);
892 else if (myButtons.contains(BTN_Apply))
893 myButtons[ BTN_OK ]->setEnabled(isEnabled);
894 if (myButtons.contains(BTN_Cancel))
895 myButtons[ BTN_Cancel ]->setEnabled(isEnabled);
896 else if (myButtons.contains(BTN_Close))
897 myButtons[ BTN_Cancel ]->setEnabled(isEnabled);
901 //=======================================================================
902 // name : SMESHGUI_FilterLibraryDlg::createFilter
903 // Purpose : Cerate filter in accordance with library
904 //=======================================================================
905 SMESH::Filter_ptr SMESHGUI_FilterLibraryDlg::createFilter(const int theType)
907 int n = myTable->NumRows(theType);
909 SMESH::Filter::Criteria_var aCriteria = new SMESH::Filter::Criteria;
910 aCriteria->length(n);
912 for (int i = 0; i < n; i++)
914 SMESH::Filter::Criterion aCriterion = SMESHGUI_FilterDlg::createCriterion();
915 myTable->GetCriterion(i, aCriterion);
916 aCriteria[ i ] = aCriterion;
919 SMESH::FilterManager_var aFilterMgr = SMESH::GetFilterManager();
920 SMESH::Filter_var aFilter = aFilterMgr->CreateFilter();
921 aFilter->SetCriteria(aCriteria.in());
923 return aFilter._retn();
926 //=======================================================================
927 // name : SMESHGUI_FilterLibraryDlg::onAddBtnPressed
928 // Purpose : SLOT. Called when "Add" button pressed
929 // Add new filter to the end of library
930 //=======================================================================
931 void SMESHGUI_FilterLibraryDlg::onAddBtnPressed()
933 // Save parameters of filter if it was changed
934 if (!myCurrFilterName.isEmpty() && myTable->IsEditable())
939 SMESH::Filter_var aFilter = createFilter();
940 myLibrary->Replace(myCurrFilterName.toLatin1().constData(),
941 myName->text().toLatin1().constData(),
944 myTable->Clear(myTable->GetType());
946 addFilterToLib(getDefaultFilterName());
949 //=======================================================================
950 // name : onAddBtnPressed()
951 // Purpose : SLOT. Called when "Add" button pressed
952 // Add new filter to the end of library
953 //=======================================================================
954 void SMESHGUI_FilterLibraryDlg::addFilterToLib (const QString& theName)
956 if (myLibrary->_is_nil()) {
957 SUIT_MessageBox::information(SMESHGUI::desktop(), tr("SMESH_WRN_WARNING"),
958 tr("LIBRARY_IS_NOT_LOADED"));
963 SMESH::Filter_var aFilter = createFilter();
965 // if name of filter already exist in the library assign default name for the filter
966 QString aName(theName);
967 SMESH::string_array_var aNames = myLibrary->GetAllNames();
968 for (int i = 0, n = aNames->length(); i < n; i++)
969 if (aName == QString(aNames[ i ]))
971 aName = getDefaultFilterName();
975 // add new filter in library
976 bool aResult = !aFilter->GetPredicate()->_is_nil()
977 ? myLibrary->Add(aName.toLatin1().constData(), aFilter)
978 : myLibrary->AddEmpty(aName.toLatin1().constData(), (SMESH::ElementType)myTable->GetType());
981 SUIT_MessageBox::information(SMESHGUI::desktop(), tr("SMESH_ERROR"),
982 tr("ERROR_OF_ADDING"));
986 myCurrFilterName = "";
990 if (theName != aName)
991 SUIT_MessageBox::information(SMESHGUI::desktop(), tr("SMESH_WARNING"),
992 tr("ASSIGN_NEW_NAME").arg(theName).arg(aName));
995 //=======================================================================
996 // name : SMESHGUI_FilterLibraryDlg::getDefaultLibraryName
997 // Purpose : Get default library name
998 //=======================================================================
999 QString& SMESHGUI_FilterLibraryDlg::getDefaultLibraryName() const
1001 static QString aName;
1002 if (aName.isEmpty())
1004 QString aHomeDir = QDir(QDir::home()).absolutePath();
1005 aName = aHomeDir + "/" + tr ("LIB_NAME");
1010 //=======================================================================
1011 // name : SMESHGUI_FilterLibraryDlg::getDefaultFilterName
1012 // Purpose : Create default filter name
1013 //=======================================================================
1014 QString SMESHGUI_FilterLibraryDlg::getDefaultFilterName() const
1018 if (myTable->GetType() == SMESH::NODE)
1020 else if (myTable->GetType() == SMESH::EDGE)
1022 else if (myTable->GetType() == SMESH::FACE)
1024 else if (myTable->GetType() == SMESH::VOLUME)
1025 aName = tr("VOLUME");
1026 else if (myTable->GetType() == SMESH::ALL)
1027 aName = tr("ELEMENT");
1029 aName = tr("SELECTION");
1031 aName += tr("FILTER");
1034 QMap< QString, int > anAllNames;
1035 SMESH::string_array_var aNames = myLibrary->GetAllNames();
1036 for(int i = 0, n = aNames->length(); i < n; i++)
1037 anAllNames[ QString(aNames[ i ]) ] = -1;
1039 bool isNotValid = true;
1045 aNewName = aName + "_" + QString("%1").arg(k);
1046 if (anAllNames.contains(aNewName))
1056 //=======================================================================
1057 // name : SMESHGUI_FilterLibraryDlg::setSelected
1058 // Purpose : set selected item in list box containing filters
1059 //=======================================================================
1060 bool SMESHGUI_FilterLibraryDlg::setSelected(const QString& theName)
1062 int anIndex = getIndex(theName);
1065 myListBox->setCurrentRow(anIndex);
1066 myCurrFilterName = theName;
1067 myCurrFilter = anIndex;
1069 return anIndex != -1;
1072 //=======================================================================
1073 // name : SMESHGUI_FilterLibraryDlg::getIndex
1074 // Purpose : Get index of the filter in list box
1075 //=======================================================================
1076 int SMESHGUI_FilterLibraryDlg::getIndex(const QString& theName) const
1078 for (int i = 0, n = myListBox->count(); i < n; i++)
1079 if (myListBox->item(i)->text() == theName)
1084 //=======================================================================
1085 // name : SMESHGUI_FilterLibraryDlg::onDeleteBtnPressed
1086 // Purpose : SLOT. Called when "Delete" button pressed
1087 //=======================================================================
1088 void SMESHGUI_FilterLibraryDlg::onDeleteBtnPressed()
1090 if (myLibrary->_is_nil()) {
1091 SUIT_MessageBox::information(SMESHGUI::desktop(), tr("SMESH_WRN_WARNING"),
1092 tr("LIBRARY_IS_NOT_LOADED"));
1096 int anIndex = getIndex(myCurrFilterName);
1098 if (anIndex == -1 || !myLibrary->Delete(myCurrFilterName.toLatin1().constData())) {
1099 SUIT_MessageBox::information(SMESHGUI::desktop(), tr("SMESH_ERROR"),
1100 tr("ERROR_OF_DELETING"));
1102 myCurrFilterName = "";
1104 delete myListBox->item(anIndex);
1107 myListBox->item(anIndex - 1)->setSelected(true);
1108 else if (anIndex == 0 && myListBox->count() > 0)
1109 myListBox->item(0)->setSelected(true);
1114 myTable->SetEnabled(myListBox->count() > 0);
1115 if (myListBox->count() == 0) {
1116 myName->setText("");
1117 myName->setEnabled(false);
1121 //=======================================================================
1122 // name : onFilterNameChanged()
1123 // Purpose : SLOT. Called when name of filter changed
1124 // Change filter name in list box
1125 //=======================================================================
1126 void SMESHGUI_FilterLibraryDlg::onFilterNameChanged (const QString& theName)
1128 int aCurrItem = myListBox->currentRow();
1129 if (aCurrItem == -1)
1132 myListBox->blockSignals(true);
1133 myListBox->item(aCurrItem)->setText(theName);
1134 myListBox->blockSignals(false);
1137 //=======================================================================
1138 // name : SMESHGUI_FilterLibraryDlg::SetTable
1139 // Purpose : Set table
1140 //=======================================================================
1141 void SMESHGUI_FilterLibraryDlg::SetTable(const SMESHGUI_FilterTable* theTable)
1143 myTable->Copy(theTable);
1144 myName->setText(getDefaultFilterName());
1145 addFilterToLib(myName->text());
1149 //=======================================================================
1150 // name : SMESHGUI_FilterLibraryDlg::GetTable
1151 // Purpose : Get table
1152 //=======================================================================
1153 const SMESHGUI_FilterTable* SMESHGUI_FilterLibraryDlg::GetTable() const
1159 //=======================================================================
1160 // name : SMESHGUI_FilterLibraryDlg::onEntityTypeChanged
1161 // Purpose : SLOT. Called when entiyt type changed
1162 //=======================================================================
1163 void SMESHGUI_FilterLibraryDlg::onEntityTypeChanged(const int theType)
1165 if (myLibrary->_is_nil())
1169 myCurrFilterName = "";
1172 if (myListBox->count())
1173 myListBox->setCurrentItem(0);
1176 //=======================================================================
1177 // name : SMESHGUI_FilterLibraryDlg::onNeedValidation
1179 //=======================================================================
1180 void SMESHGUI_FilterLibraryDlg::onNeedValidation()
1182 if (!myCurrFilterName.isEmpty())
1184 bool valid = isValid(true);
1185 myTable->SetValidity(valid);
1189 SMESH::Filter_var aFilter = createFilter(myTable->GetType());
1190 myLibrary->Replace(myCurrFilterName.toLatin1().constData(),
1191 myName->text().toLatin1().constData(),
1197 //=================================================================================
1198 // function : keyPressEvent()
1200 //=================================================================================
1201 void SMESHGUI_FilterLibraryDlg::keyPressEvent( QKeyEvent* e )
1203 QDialog::keyPressEvent( e );
1204 if ( e->isAccepted() )
1207 if ( e->key() == Qt::Key_F1 ) {