1 // SMESH SMESHGUI : GUI for SMESH component
3 // Copyright (C) 2003 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 // File : SMESHGUI_FilterLibraryDlg.cxx
23 // 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(currentTextChanged(const QString&)),
229 this, SLOT(onFilterChanged(const QString&)));
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_OK" ), aGrp);
257 myButtons[ BTN_Apply ] = new QPushButton(tr("SMESH_BUT_APPLY"), aGrp);
259 QLabel* aLbl = new QLabel(aGrp);
260 aLbl->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed));
262 myButtons[ BTN_Cancel ] = new QPushButton(tr("SMESH_BUT_CANCEL"), aGrp);
263 myButtons[ BTN_Close ] = new QPushButton(tr("SMESH_BUT_CLOSE"), aGrp);
264 myButtons[ BTN_Help ] = new QPushButton(tr("SMESH_BUT_HELP"), aGrp);
266 aLay->addWidget(myButtons[ BTN_OK ]);
267 aLay->addSpacing(10);
268 aLay->addWidget(myButtons[ BTN_Apply ]);
269 aLay->addSpacing(10);
271 aLay->addWidget(myButtons[ BTN_Cancel ]);
272 aLay->addWidget(myButtons[ BTN_Close ]);
273 aLay->addWidget(myButtons[ BTN_Help ]);
275 connect(myButtons[ BTN_OK ], SIGNAL(clicked()), SLOT(onOk()));
276 connect(myButtons[ BTN_Cancel ], SIGNAL(clicked()), SLOT(onClose()));
277 connect(myButtons[ BTN_Close ], SIGNAL(clicked()), SLOT(onClose()));
278 connect(myButtons[ BTN_Apply ], SIGNAL(clicked()), SLOT(onApply()));
279 connect(myButtons[ BTN_Help ], SIGNAL(clicked()), SLOT(onHelp()));
281 QMap<int, QPushButton*>::iterator anIter;
282 for (anIter = myButtons.begin(); anIter != myButtons.end(); ++anIter)
283 anIter.value()->setAutoDefault(false);
290 //=======================================================================
291 // name : SMESHGUI_FilterLibraryDlg::updateMainButtons
292 // Purpose : Update visibility of main buttons (OK, Cancel, Close ...)
293 //=======================================================================
294 void SMESHGUI_FilterLibraryDlg::updateMainButtons()
296 if (myTypes.count() == 1) {
297 myButtons[ BTN_Cancel ]->show();
298 myButtons[ BTN_Apply ]->hide();
299 myButtons[ BTN_Close ]->hide();
301 myButtons[ BTN_Cancel ]->hide();
302 myButtons[ BTN_Apply ]->show();
303 myButtons[ BTN_Close ]->show();
307 //=======================================================================
308 // name : SMESHGUI_FilterLibraryDlg::~SMESHGUI_FilterLibraryDlg
309 // Purpose : Destructor
310 //=======================================================================
311 SMESHGUI_FilterLibraryDlg::~SMESHGUI_FilterLibraryDlg()
315 //=======================================================================
316 // name : SMESHGUI_FilterLibraryDlg::Init
317 // Purpose : Init dialog fields, connect signals and slots, show dialog
318 //=======================================================================
319 void SMESHGUI_FilterLibraryDlg::Init (const int type, const int theMode)
323 Init(aTypes, theMode);
326 //=======================================================================
327 // name : SMESHGUI_FilterLibraryDlg::Init
328 // Purpose : Init dialog fields, connect signals and slots, show dialog
329 //=======================================================================
330 void SMESHGUI_FilterLibraryDlg::Init (const QList<int>& theTypes,
335 myTable->Init(theTypes);
336 myCurrFilterName = "";
338 myListBox->blockSignals(true);
340 myListBox->blockSignals(false);
344 updateControlsVisibility();
347 connect( mySMESHGUI, SIGNAL(SignalDeactivateActiveDialog()), SLOT(onDeactivate()));
348 connect( mySMESHGUI, SIGNAL(SignalCloseAllDialogs()), SLOT(onClose()));
350 if (myMode == ADD_TO)
352 setWindowTitle(tr("ADD_TO_TLT"));
353 if (myFileName->text().isEmpty())
354 myFileName->setText(getDefaultLibraryName());
357 else if (myMode == COPY_FROM)
359 setWindowTitle(tr("COPY_FROM_TLT"));
360 if (myFileName->text().isEmpty())
361 myFileName->setText(getDefaultLibraryName());
363 if (myListBox->count() > 0)
364 myListBox->setCurrentItem(0);
368 setWindowTitle(tr("EDIT_LIB_TLT"));
369 if (myFileName->text().isEmpty())
370 myFileName->setText(getDefaultLibraryName());
372 if (myListBox->count() > 0)
373 myListBox->setCurrentItem(0);
379 isPermissionValid(false);
382 //=======================================================================
383 // name : SMESHGUI_FilterLibraryDlg::updateControlsVisibility
384 // Purpose : Update visibility of controls in accordance with myMode
385 //=======================================================================
386 void SMESHGUI_FilterLibraryDlg::updateControlsVisibility()
388 if (myMode == ADD_TO)
391 myNameGrp->setEnabled(true);
394 myTable->SetEditable(false);
396 else if (myMode == COPY_FROM)
399 myNameGrp->setEnabled(false);
402 myTable->SetEditable(false);
404 else if (myMode == EDIT)
407 myNameGrp->setEnabled(true);
410 myTable->SetEditable(true);
413 qApp->processEvents();
418 //=======================================================================
419 // name : SMESHGUI_FilterLibraryDlg::onApply
420 // Purpose : SLOT called when "Apply" button pressed.
421 //=======================================================================
422 bool SMESHGUI_FilterLibraryDlg::onApply()
424 if (!isValid(true) || !isPermissionValid(false))
427 if (myLibrary->_is_nil()) {
428 SUIT_MessageBox::information(SMESHGUI::desktop(), tr("SMESH_WRN_WARNING"),
429 tr("LIBRARY_IS_NOT_LOADED"));
433 if (myFileName->text() != myLibrary->GetFileName())
434 myLibrary->SetFileName(myFileName->text().toLatin1().data());
436 bool aResult = false;
438 if (myMode == COPY_FROM || myListBox->count() == 0) {
440 } else if (myMode == EDIT || myMode == ADD_TO) {
441 SMESH::Filter_var aFilter = createFilter();
442 if (!myLibrary->Replace(myCurrFilterName.toLatin1().data(),
443 myName->text().toLatin1().data(),
445 SUIT_MessageBox::information(SMESHGUI::desktop(), tr("SMESH_ERROR"),
446 tr("ERROR_OF_EDITING"));
453 if (aResult && myMode != COPY_FROM)
454 aResult = myLibrary->Save();
457 char* aFileName = myLibrary->GetFileName();
458 getDefaultLibraryName() = QString(aFileName);
460 } else if (myMode != COPY_FROM) {
461 SUIT_MessageBox::information(SMESHGUI::desktop(), tr("SMESH_ERROR"),
462 tr("ERROR_OF_SAVING"));
469 //=======================================================================
470 // name : SMESHGUI_FilterLibraryDlg::onOk
471 // Purpose : SLOT called when "Ok" button pressed.
472 // Assign filters VTK viewer and close dialog
473 //=======================================================================
474 void SMESHGUI_FilterLibraryDlg::onOk()
478 disconnect( mySMESHGUI, 0, this, 0);
479 mySMESHGUI->ResetState();
484 //=======================================================================
485 // name : SMESHGUI_FilterLibraryDlg::onClose
486 // Purpose : SLOT called when "Close" button pressed. Close dialog
487 //=======================================================================
488 void SMESHGUI_FilterLibraryDlg::onClose()
490 disconnect( mySMESHGUI, 0, this, 0);
491 mySMESHGUI->ResetState();
495 //=================================================================================
496 // function : onHelp()
498 //=================================================================================
499 void SMESHGUI_FilterLibraryDlg::onHelp()
501 LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
503 app->onHelpContextModule(mySMESHGUI ? app->moduleName(mySMESHGUI->moduleName()) : QString(""), myHelpFileName);
507 platform = "winapplication";
509 platform = "application";
511 SUIT_MessageBox::warning(this, tr("WRN_WARNING"),
512 tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
513 arg(app->resourceMgr()->stringValue("ExternalBrowser",
515 arg(myHelpFileName));
519 //=======================================================================
520 // name : SMESHGUI_FilterLibraryDlg::onDeactivate
521 // Purpose : SLOT called when dialog must be deativated
522 //=======================================================================
523 void SMESHGUI_FilterLibraryDlg::onDeactivate()
528 //=======================================================================
529 // name : SMESHGUI_FilterLibraryDlg::enterEvent
530 // Purpose : Event filter
531 //=======================================================================
532 void SMESHGUI_FilterLibraryDlg::enterEvent(QEvent*)
537 //=================================================================================
538 // function : closeEvent()
539 // purpose : Close dialog
540 //=================================================================================
541 void SMESHGUI_FilterLibraryDlg::closeEvent(QCloseEvent* e)
546 //=======================================================================
547 // name : SMESHGUI_FilterLibraryDlg::getFileName
548 // Purpose : Get file name
549 //=======================================================================
550 QString SMESHGUI_FilterLibraryDlg::getFileName() const
552 return myFileName != 0 ? myFileName->text() : QString("");
555 //================================================================
556 // Function : setFileName
557 // Purpose : Set file name to line edit
558 //================================================================
559 void SMESHGUI_FilterLibraryDlg::setFileName(const QString& txt, const bool autoExt)
563 myFileName->setText(autoExt ? autoExtension(txt) : txt);
566 //================================================================
567 // Function : autoExtension
568 // Purpose : Append extension to the file name
569 //================================================================
570 QString SMESHGUI_FilterLibraryDlg::autoExtension(const QString& theFileName) const
572 QString anExt = theFileName.section('.', -1);
573 return anExt != "xml" && anExt != "XML" ? theFileName + ".xml" : theFileName;
576 //================================================================
577 // Function : filterWildCards
579 //================================================================
580 QStringList SMESHGUI_FilterLibraryDlg::filterWildCards(const QString& theFilter) const
584 int b = theFilter.lastIndexOf("(");
585 int e = theFilter.lastIndexOf(")");
586 if (b != -1 && e != -1)
588 QString content = theFilter.mid(b + 1, e - b - 1).trimmed();
589 QStringList lst = content.split(" ", QString::SkipEmptyParts);
590 for (QStringList::const_iterator it = lst.begin(); it != lst.end(); ++it)
591 if ((*it).indexOf(".") != -1)
592 res.append((*it).trimmed());
597 //=======================================================================
598 // name : SMESHGUI_FilterLibraryDlg::prepareFilters
599 // Purpose : Prepare filters for dialog
600 //=======================================================================
601 QStringList SMESHGUI_FilterLibraryDlg::prepareFilters() const
603 static QStringList aList;
606 aList.append(tr("XML_FILT"));
607 //aList.append(tr("ALL_FILES_FILTER"));
613 //================================================================
614 // Function : onBrowse
615 // Purpose : SLOT. Display "Open file" dialog for chosing library name
616 //================================================================
617 void SMESHGUI_FilterLibraryDlg::onBrowse()
619 Dialog* aDlg = new Dialog(this, true);
620 aDlg->setWindowTitle(tr("OPEN_LIBRARY"));
622 //aDlg->setMode(myMode == COPY_FROM ? QFileDialogP::ExistingFile : QFileDialogP::AnyFile);
623 aDlg->setFileMode(myMode == COPY_FROM ? QFileDialog::ExistingFile : QFileDialog::AnyFile);
624 aDlg->setFilters(prepareFilters());
625 aDlg->selectFile(getFileName());
627 QPushButton* anOkBtn = (QPushButton*)aDlg->findChild<QPushButton*>("OK");
629 anOkBtn->setText(tr("SMESH_BUT_OK"));
631 if (aDlg->exec() != Accepted)
634 QString fName = aDlg->selectedFile();
639 if (QFileInfo(fName).suffix().isEmpty())
640 fName = autoExtension(fName);
642 fName = QDir::convertSeparators(fName);
643 QString prev = QDir::convertSeparators(getFileName());
650 QString aName = myListBox->item(myListBox->count() - 1)->text();
653 if (myMode == ADD_TO)
655 myTable->Copy((SMESHGUI_FilterTable*)parentWidget());
656 myCurrFilterName = "";
658 addFilterToLib(aName);
661 isPermissionValid(false);
664 //=======================================================================
665 // name : SMESHGUI_FilterLibraryDlg::processNewLibrary
666 // Purpose : SLOT. Calleds when file name changed
667 //=======================================================================
668 void SMESHGUI_FilterLibraryDlg::processNewLibrary()
670 SMESH::FilterManager_var aFilterMgr = SMESH::GetFilterManager();
671 if (aFilterMgr->_is_nil())
674 myLibrary = aFilterMgr->LoadLibrary(autoExtension(getFileName()).toLatin1().data());
675 if (myLibrary->_is_nil()) {
676 if (myMode == COPY_FROM) {
677 SUIT_MessageBox::information(SMESHGUI::desktop(), tr("SMESH_ERROR"),
681 myLibrary = aFilterMgr->CreateLibrary();
682 myLibrary->SetFileName(getFileName().toLatin1().data());
689 //=======================================================================
690 // name : SMESHGUI_FilterLibraryDlg::updateList
691 // Purpose : Fill list box with filter names
692 //=======================================================================
693 void SMESHGUI_FilterLibraryDlg::updateList()
696 SMESH::string_array_var aNames = myLibrary->GetNames((SMESH::ElementType)myTable->GetType());
697 for (int i = 0, n = aNames->length(); i < n; i++)
698 aList.append(QString(aNames[ i ]));
699 myListBox->blockSignals(true);
701 myListBox->blockSignals(false);
702 myListBox->addItems(aList);
703 if (myListBox->count() == 0)
705 myTable->Clear(myTable->GetType());
707 myName->setEnabled(false);
708 myTable->SetEnabled(false);
712 myName->setEnabled(true);
713 myTable->SetEnabled(true);
714 if (myListBox->count())
716 myCurrFilterName = "";
717 myListBox->setCurrentItem(0);
722 //=======================================================================
723 // name : SMESHGUI_FilterLibraryDlg::isNameValid
724 // Purpose : Verify validity if entered data
725 //=======================================================================
726 bool SMESHGUI_FilterLibraryDlg::isNameValid(const bool theMess) const
728 // verify validity of filter name
729 if (myName->isEnabled() && !myCurrFilterName.isEmpty()) {
730 QString aCurrName = myName->text();
731 if (aCurrName.isEmpty()) {
733 SUIT_MessageBox::information(SMESHGUI::desktop(), tr("SMESH_INSUFFICIENT_DATA"),
734 tr("EMPTY_FILTER_NAME"));
738 SMESH::string_array_var aNames = myLibrary->GetAllNames();
739 for (int f = 0, n = aNames->length(); f < n; f++) {
740 if (aNames[ f ] == aCurrName && aNames[ f ] != myCurrFilterName) {
742 SUIT_MessageBox::information(SMESHGUI::desktop(), tr("SMESH_INSUFFICIENT_DATA"),
743 tr("ERROR_FILTER_NAME"));
752 //=======================================================================
753 // name : SMESHGUI_FilterLibraryDlg::isPermissionValid
754 // Purpose : Verify write permission on file
755 //=======================================================================
756 bool SMESHGUI_FilterLibraryDlg::isPermissionValid(const bool theIsExistingOnly)
758 if (myMode == COPY_FROM)
761 // Verify write permission
762 bool isWritable = false;
764 QString fName(myFileName->text());
765 if (QFileInfo(fName).suffix().isEmpty())
766 fName = autoExtension(fName);
768 fName = QDir::convertSeparators(fName);
770 if (QFileInfo(fName).exists()) {
771 isWritable = QFileInfo(fName).isWritable();
772 } else if (!theIsExistingOnly) {
773 QFileInfo aDirInfo(QFileInfo(fName).absolutePath());
774 isWritable = aDirInfo.isWritable();
775 /*if (QDir(QFileInfo(fName).dirPath(true)).exists() ||
776 QDir().mkdir(QFileInfo(fName).dirPath(true)))
779 if (aFile.open(IO_WriteOnly))
790 SUIT_MessageBox::information(SMESHGUI::desktop(), tr("SMESH_WRN_WARNING"),
791 tr("NO_PERMISSION"));
798 //=======================================================================
799 // name : SMESHGUI_FilterLibraryDlg::isValid
800 // Purpose : Verify validity if entered data
801 //=======================================================================
802 bool SMESHGUI_FilterLibraryDlg::isValid(const bool theMess) const
804 // verify validity of table
805 if (!myTable->IsValid(theMess) || !isNameValid(theMess))
811 //=======================================================================
812 // name : SMESHGUI_FilterLibraryDlg::onFilterChanged
813 // Purpose : SLOT. Called when selected filter of library changed
814 //=======================================================================
815 void SMESHGUI_FilterLibraryDlg::onFilterChanged(const QString& theName)
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().data(),
834 myName->text().toLatin1().data(),
838 // Fill table with filter parameters
840 SMESH::Filter_var aFilter = myLibrary->Copy(theName.toLatin1().data());
841 myCurrFilterName = theName;
842 myCurrFilter = myListBox->currentRow();
843 myName->setText(theName);
846 SMESH::Filter::Criteria_var aCriteria;
848 myTable->Clear(myTable->GetType());
850 if (!aFilter->GetCriteria(aCriteria))
853 for (int i = 0, n = aCriteria->length(); i < n; i++)
854 myTable->AddCriterion(aCriteria[ i ], myTable->GetType());
859 //=======================================================================
860 // name : SMESHGUI_FilterLibraryDlg::onReturnPressed
861 // Purpose : SLOT. Called when enter button is pressed in library name field
863 //=======================================================================
864 void SMESHGUI_FilterLibraryDlg::onReturnPressed()
866 QString aName = myListBox->item(myListBox->count() - 1)->text();
870 if (myMode == ADD_TO)
872 myTable->Copy((SMESHGUI_FilterTable*)parentWidget());
873 myCurrFilterName = "";
875 addFilterToLib(aName);
878 isPermissionValid(false);
881 //=======================================================================
882 // name : SMESHGUI_FilterLibraryDlg::enableMainButtons
883 // Purpose : Update state of "OK", "Cancel" buttons
884 //=======================================================================
885 void SMESHGUI_FilterLibraryDlg::enableMainButtons()
887 /*bool isEnabled = isValid(false);
888 if (myButtons.contains(BTN_OK))
889 myButtons[ BTN_OK ]->setEnabled(isEnabled);
890 else if (myButtons.contains(BTN_Apply))
891 myButtons[ BTN_OK ]->setEnabled(isEnabled);
892 if (myButtons.contains(BTN_Cancel))
893 myButtons[ BTN_Cancel ]->setEnabled(isEnabled);
894 else if (myButtons.contains(BTN_Close))
895 myButtons[ BTN_Cancel ]->setEnabled(isEnabled);
899 //=======================================================================
900 // name : SMESHGUI_FilterLibraryDlg::createFilter
901 // Purpose : Cerate filter in accordance with library
902 //=======================================================================
903 SMESH::Filter_ptr SMESHGUI_FilterLibraryDlg::createFilter(const int theType)
905 int n = myTable->NumRows(theType);
907 SMESH::Filter::Criteria_var aCriteria = new SMESH::Filter::Criteria;
908 aCriteria->length(n);
910 for (int i = 0; i < n; i++)
912 SMESH::Filter::Criterion aCriterion = SMESHGUI_FilterDlg::createCriterion();
913 myTable->GetCriterion(i, aCriterion);
914 aCriteria[ i ] = aCriterion;
917 SMESH::FilterManager_var aFilterMgr = SMESH::GetFilterManager();
918 SMESH::Filter_var aFilter = aFilterMgr->CreateFilter();
919 aFilter->SetCriteria(aCriteria.in());
921 return aFilter._retn();
924 //=======================================================================
925 // name : SMESHGUI_FilterLibraryDlg::onAddBtnPressed
926 // Purpose : SLOT. Called when "Add" button pressed
927 // Add new filter to the end of library
928 //=======================================================================
929 void SMESHGUI_FilterLibraryDlg::onAddBtnPressed()
931 // Save parameters of filter if it was changed
932 if (!myCurrFilterName.isEmpty() && myTable->IsEditable())
937 SMESH::Filter_var aFilter = createFilter();
938 myLibrary->Replace(myCurrFilterName.toLatin1().data(),
939 myName->text().toLatin1().data(),
943 addFilterToLib(getDefaultFilterName());
946 //=======================================================================
947 // name : onAddBtnPressed()
948 // Purpose : SLOT. Called when "Add" button pressed
949 // Add new filter to the end of library
950 //=======================================================================
951 void SMESHGUI_FilterLibraryDlg::addFilterToLib (const QString& theName)
953 if (myLibrary->_is_nil()) {
954 SUIT_MessageBox::information(SMESHGUI::desktop(), tr("SMESH_WRN_WARNING"),
955 tr("LIBRARY_IS_NOT_LOADED"));
960 SMESH::Filter_var aFilter = createFilter();
962 // if name of filter already exist in the library assign default name for the filter
963 QString aName(theName);
964 SMESH::string_array_var aNames = myLibrary->GetAllNames();
965 for (int i = 0, n = aNames->length(); i < n; i++)
966 if (aName == QString(aNames[ i ]))
968 aName = getDefaultFilterName();
972 // add new filter in library
973 bool aResult = !aFilter->GetPredicate()->_is_nil()
974 ? myLibrary->Add(aName.toLatin1().data(), aFilter)
975 : myLibrary->AddEmpty(aName.toLatin1().data(), (SMESH::ElementType)myTable->GetType());
978 SUIT_MessageBox::information(SMESHGUI::desktop(), tr("SMESH_ERROR"),
979 tr("ERROR_OF_ADDING"));
983 myCurrFilterName = "";
987 if (theName != aName)
988 SUIT_MessageBox::information(SMESHGUI::desktop(), tr("SMESH_WARNING"),
989 tr("ASSIGN_NEW_NAME").arg(theName).arg(aName));
992 //=======================================================================
993 // name : SMESHGUI_FilterLibraryDlg::getDefaultLibraryName
994 // Purpose : Get default library name
995 //=======================================================================
996 QString& SMESHGUI_FilterLibraryDlg::getDefaultLibraryName() const
998 static QString aName;
1001 QString aHomeDir = QDir(QDir::home()).absolutePath();
1002 aName = aHomeDir + "/" + tr ("LIB_NAME");
1007 //=======================================================================
1008 // name : SMESHGUI_FilterLibraryDlg::getDefaultFilterName
1009 // Purpose : Create default filter name
1010 //=======================================================================
1011 QString SMESHGUI_FilterLibraryDlg::getDefaultFilterName() const
1015 if (myTable->GetType() == SMESH::NODE)
1017 else if (myTable->GetType() == SMESH::EDGE)
1019 else if (myTable->GetType() == SMESH::FACE)
1021 else if (myTable->GetType() == SMESH::VOLUME)
1022 aName = tr("VOLUME");
1023 else if (myTable->GetType() == SMESH::ALL)
1024 aName = tr("ELEMENT");
1026 aName = tr("SELECTION");
1028 aName += tr("FILTER");
1031 QMap< QString, int > anAllNames;
1032 SMESH::string_array_var aNames = myLibrary->GetAllNames();
1033 for(int i = 0, n = aNames->length(); i < n; i++)
1034 anAllNames[ QString(aNames[ i ]) ] = -1;
1036 bool isNotValid = true;
1042 aNewName = aName + "_" + QString("%1").arg(k);
1043 if (anAllNames.contains(aNewName))
1053 //=======================================================================
1054 // name : SMESHGUI_FilterLibraryDlg::setSelected
1055 // Purpose : set selected item in list box containing filters
1056 //=======================================================================
1057 bool SMESHGUI_FilterLibraryDlg::setSelected(const QString& theName)
1059 int anIndex = getIndex(theName);
1062 myListBox->setCurrentRow(anIndex);
1063 myCurrFilterName = theName;
1064 myCurrFilter = anIndex;
1066 return anIndex != -1;
1069 //=======================================================================
1070 // name : SMESHGUI_FilterLibraryDlg::getIndex
1071 // Purpose : Get index of the filter in list box
1072 //=======================================================================
1073 int SMESHGUI_FilterLibraryDlg::getIndex(const QString& theName) const
1075 for (int i = 0, n = myListBox->count(); i < n; i++)
1076 if (myListBox->item(i)->text() == theName)
1081 //=======================================================================
1082 // name : SMESHGUI_FilterLibraryDlg::onDeleteBtnPressed
1083 // Purpose : SLOT. Called when "Delete" button pressed
1084 //=======================================================================
1085 void SMESHGUI_FilterLibraryDlg::onDeleteBtnPressed()
1087 if (myLibrary->_is_nil()) {
1088 SUIT_MessageBox::information(SMESHGUI::desktop(), tr("SMESH_WRN_WARNING"),
1089 tr("LIBRARY_IS_NOT_LOADED"));
1093 int anIndex = getIndex(myCurrFilterName);
1095 if (anIndex == -1 || !myLibrary->Delete(myCurrFilterName.toLatin1().data())) {
1096 SUIT_MessageBox::information(SMESHGUI::desktop(), tr("SMESH_ERROR"),
1097 tr("ERROR_OF_DELETING"));
1099 myCurrFilterName = "";
1101 delete myListBox->item(anIndex);
1104 myListBox->item(anIndex - 1)->setSelected(true);
1105 else if (anIndex == 0 && myListBox->count() > 0)
1106 myListBox->item(0)->setSelected(true);
1111 myTable->SetEnabled(myListBox->count() > 0);
1112 if (myListBox->count() == 0) {
1113 myName->setText("");
1114 myName->setEnabled(false);
1118 //=======================================================================
1119 // name : onFilterNameChanged()
1120 // Purpose : SLOT. Called when name of filter changed
1121 // Change filter name in list box
1122 //=======================================================================
1123 void SMESHGUI_FilterLibraryDlg::onFilterNameChanged (const QString& theName)
1125 int aCurrItem = myListBox->currentRow();
1126 if (aCurrItem == -1)
1129 myListBox->blockSignals(true);
1130 myListBox->item(aCurrItem)->setText(theName);
1131 myListBox->blockSignals(false);
1134 //=======================================================================
1135 // name : SMESHGUI_FilterLibraryDlg::SetTable
1136 // Purpose : Set table
1137 //=======================================================================
1138 void SMESHGUI_FilterLibraryDlg::SetTable(const SMESHGUI_FilterTable* theTable)
1140 myTable->Copy(theTable);
1141 myName->setText(getDefaultFilterName());
1142 addFilterToLib(myName->text());
1146 //=======================================================================
1147 // name : SMESHGUI_FilterLibraryDlg::GetTable
1148 // Purpose : Get table
1149 //=======================================================================
1150 const SMESHGUI_FilterTable* SMESHGUI_FilterLibraryDlg::GetTable() const
1156 //=======================================================================
1157 // name : SMESHGUI_FilterLibraryDlg::onEntityTypeChanged
1158 // Purpose : SLOT. Called when entiyt type changed
1159 //=======================================================================
1160 void SMESHGUI_FilterLibraryDlg::onEntityTypeChanged(const int theType)
1162 if (myLibrary->_is_nil())
1166 myCurrFilterName = "";
1169 if (myListBox->count())
1170 myListBox->setCurrentItem(0);
1173 //=======================================================================
1174 // name : SMESHGUI_FilterLibraryDlg::onNeedValidation
1176 //=======================================================================
1177 void SMESHGUI_FilterLibraryDlg::onNeedValidation()
1179 if (!myCurrFilterName.isEmpty())
1181 bool valid = isValid(true);
1182 myTable->SetValidity(valid);
1186 SMESH::Filter_var aFilter = createFilter(myTable->GetType());
1187 myLibrary->Replace(myCurrFilterName.toLatin1().data(),
1188 myName->text().toLatin1().data(),
1194 //=================================================================================
1195 // function : keyPressEvent()
1197 //=================================================================================
1198 void SMESHGUI_FilterLibraryDlg::keyPressEvent( QKeyEvent* e )
1200 QDialog::keyPressEvent( e );
1201 if ( e->isAccepted() )
1204 if ( e->key() == Qt::Key_F1 ) {