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
24 // File : SMESHGUI_FilterLibraryDlg.cxx
25 // Author : Sergey LITONIN
28 #include "SMESHGUI_FilterLibraryDlg.h"
31 #include "SMESHGUI_Utils.h"
32 #include "SMESHGUI_FilterUtils.h"
34 #include "SUIT_Session.h"
35 #include "SUIT_Desktop.h"
36 #include "SUIT_FileDlg.h"
37 #include "SUIT_MessageBox.h"
39 #include "LightApp_Application.h"
42 #include <qapplication.h>
43 #include <qmessagebox.h>
46 #include <qlineedit.h>
47 #include <qpushbutton.h>
48 #include <qgroupbox.h>
52 #include <qfileinfo.h>
59 * Class : SMESHGUI_FilterLibraryDlg::Dialog
60 * Description : Dialog for opening filter library
63 class SMESHGUI_FilterLibraryDlg::Dialog : public SUIT_FileDlg
66 Dialog(QWidget* theParent, const bool theToOpen);
70 virtual bool acceptData();
73 SMESHGUI_FilterLibraryDlg::Dialog::Dialog (QWidget* theParent,
75 : SUIT_FileDlg(theParent, theToOpen)
79 SMESHGUI_FilterLibraryDlg::Dialog::~Dialog()
83 bool SMESHGUI_FilterLibraryDlg::Dialog::acceptData()
85 // if (mode() != QFileDialogP::AnyFile)
86 // return SUIT_FileDlg::acceptData();
92 * Class : SMESHGUI_FilterLibraryDlg
93 * Description : Dialog to specify filters for VTK viewer
96 //=======================================================================
97 // name : SMESHGUI_FilterLibraryDlg::SMESHGUI_FilterLibraryDlg
98 // Purpose : Constructor
99 //=======================================================================
100 SMESHGUI_FilterLibraryDlg::SMESHGUI_FilterLibraryDlg (SMESHGUI* theModule,
102 const QValueList<int>& theTypes,
105 : QDialog( parent, theName, false, WStyle_Customize |
106 WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu ),
107 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 : QDialog( parent, theName, true, WStyle_Customize |
122 WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu ),
123 mySMESHGUI( theModule )
125 QValueList<int> aTypes;
126 aTypes.append(theType);
127 construct(aTypes, theMode);
130 //=======================================================================
131 // name : SMESHGUI_FilterLibraryDlg::construct
132 // Purpose : Construct dialog (called by constructor)
133 //=======================================================================
134 void SMESHGUI_FilterLibraryDlg::construct (const QValueList<int>& theTypes,
140 QVBoxLayout* aDlgLay = new QVBoxLayout(this, MARGIN, SPACING);
142 myMainFrame = createMainFrame (this);
143 QFrame* aBtnFrame = createButtonFrame(this);
145 aDlgLay->addWidget(myMainFrame);
146 aDlgLay->addWidget(aBtnFrame);
148 aDlgLay->setStretchFactor(myMainFrame, 1);
150 myHelpFileName = "selection_filter_library_page.html";
152 Init(myTypes, myMode);
155 //=======================================================================
156 // name : SMESHGUI_FilterLibraryDlg::createMainFrame
157 // Purpose : Create frame containing dialog's input fields
158 //=======================================================================
159 QFrame* SMESHGUI_FilterLibraryDlg::createMainFrame (QWidget* theParent)
161 QGroupBox* aMainFrame = new QGroupBox(1, Qt::Horizontal, theParent);
162 aMainFrame->setFrameStyle(QFrame::NoFrame);
163 aMainFrame->setInsideMargin(0);
167 QGroupBox* aGrp = new QGroupBox(1, Qt::Vertical, aMainFrame);
168 aGrp->setFrameStyle(QFrame::NoFrame);
169 aGrp->setInsideMargin(0);
171 new QLabel(tr("LIBRARY_FILE"), aGrp);
172 myFileName = new QLineEdit(aGrp);
173 myOpenBtn = new QPushButton(aGrp);
174 myOpenBtn->setPixmap(SUIT_Session::session()->resourceMgr()->loadPixmap(
175 "SUIT", tr("ICON_FILE_OPEN")));
179 aGrp = new QGroupBox(1, Qt::Vertical, tr("FILTER_NAMES"), aMainFrame);
180 QFrame* aFrame = new QFrame(aGrp);
181 myListBox = new QListBox(aFrame);
182 myAddBtn = new QPushButton(tr("ADD"), aFrame);
183 myDeleteBtn = new QPushButton(tr("DELETE"), aFrame);
185 QGridLayout* aLay = new QGridLayout(aFrame, 3, 2, 0, 5);
186 aLay->addMultiCellWidget(myListBox, 0, 2, 0, 0);
187 aLay->addWidget(myAddBtn, 0, 1);
188 aLay->addWidget(myDeleteBtn, 1, 1);
189 QSpacerItem* aVSpacer = new QSpacerItem(0, 0, QSizePolicy::Minimum, QSizePolicy::Expanding);
190 aLay->addItem(aVSpacer, 2, 1);
194 myNameGrp = new QGroupBox(1, Qt::Vertical, aMainFrame);
195 myNameGrp->setFrameStyle(QFrame::NoFrame);
196 myNameGrp->setInsideMargin(0);
197 new QLabel(tr("FILTER_NAME"), myNameGrp);
198 myName = new QLineEdit(myNameGrp);
202 myTable = new SMESHGUI_FilterTable( mySMESHGUI, aMainFrame, myTypes);
203 myTable->SetEditable(myMode == EDIT);
204 myTable->SetLibsEnabled(false);
206 myListBox->setMinimumHeight((int)(myTable->sizeHint().height() * 0.5));
207 myListBox->setRowMode(QListBox::FitToWidth);
208 myListBox->setSelectionMode(QListBox::Single);
210 myOpenBtn->setAutoDefault(false);
211 myAddBtn->setAutoDefault(false);
212 myDeleteBtn->setAutoDefault(false);
214 // connect signals and slots
216 connect(myFileName, SIGNAL(returnPressed()), this, SLOT(onReturnPressed()));
217 connect(myOpenBtn , SIGNAL(clicked()), this, SLOT(onBrowse()));
219 connect(myListBox, SIGNAL(highlighted(const QString&)),
220 this, SLOT(onFilterChanged(const QString&)));
222 connect(myAddBtn, SIGNAL(clicked()), this, SLOT(onAddBtnPressed()));
223 connect(myDeleteBtn, SIGNAL(clicked()), this, SLOT(onDeleteBtnPressed()));
225 connect(myName, SIGNAL(textChanged(const QString&)),
226 this, SLOT(onFilterNameChanged(const QString&)));
228 connect(myTable, SIGNAL(EntityTypeChanged(const int)),
229 this, SLOT(onEntityTypeChanged(const int)));
231 connect(myTable, SIGNAL(NeedValidation()), this, SLOT(onNeedValidation()));
236 //=======================================================================
237 // name : SMESHGUI_FilterLibraryDlg::createButtonFrame
238 // Purpose : Create frame containing buttons
239 //=======================================================================
240 QFrame* SMESHGUI_FilterLibraryDlg::createButtonFrame (QWidget* theParent)
242 QGroupBox* aGrp = new QGroupBox(1, Qt::Vertical, theParent);
244 myButtons[ BTN_Apply ] = new QPushButton(tr("SMESH_BUT_APPLY"), aGrp);
245 myButtons[ BTN_Cancel ] = new QPushButton(tr("SMESH_BUT_CANCEL"), aGrp);
246 myButtons[ BTN_OK ] = new QPushButton(tr("SMESH_BUT_APPLY_AND_CLOSE"), aGrp);
248 QLabel* aLbl = new QLabel(aGrp);
249 aLbl->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed));
251 myButtons[ BTN_Close ] = new QPushButton(tr("SMESH_BUT_CLOSE"), aGrp);
252 myButtons[ BTN_Help ] = new QPushButton(tr("SMESH_BUT_HELP"), aGrp);
254 connect(myButtons[ BTN_OK ], SIGNAL(clicked()), SLOT(onOk()));
255 connect(myButtons[ BTN_Cancel ], SIGNAL(clicked()), SLOT(onClose()));
256 connect(myButtons[ BTN_Close ], SIGNAL(clicked()), SLOT(onClose()));
257 connect(myButtons[ BTN_Apply ], SIGNAL(clicked()), SLOT(onApply()));
258 connect(myButtons[ BTN_Help ], SIGNAL(clicked()), SLOT(onHelp()));
260 QMap<int, QPushButton*>::iterator anIter;
261 for (anIter = myButtons.begin(); anIter != myButtons.end(); ++anIter)
262 anIter.data()->setAutoDefault(false);
269 //=======================================================================
270 // name : SMESHGUI_FilterLibraryDlg::updateMainButtons
271 // Purpose : Update visibility of main buttons (OK, Cancel, Close ...)
272 //=======================================================================
273 void SMESHGUI_FilterLibraryDlg::updateMainButtons()
275 if (myTypes.count() == 1) {
276 myButtons[ BTN_Cancel ]->show();
277 myButtons[ BTN_Apply ]->hide();
278 myButtons[ BTN_Close ]->hide();
280 myButtons[ BTN_Cancel ]->hide();
281 myButtons[ BTN_Apply ]->show();
282 myButtons[ BTN_Close ]->show();
286 //=======================================================================
287 // name : SMESHGUI_FilterLibraryDlg::~SMESHGUI_FilterLibraryDlg
288 // Purpose : Destructor
289 //=======================================================================
290 SMESHGUI_FilterLibraryDlg::~SMESHGUI_FilterLibraryDlg()
294 //=======================================================================
295 // name : SMESHGUI_FilterLibraryDlg::Init
296 // Purpose : Init dialog fields, connect signals and slots, show dialog
297 //=======================================================================
298 void SMESHGUI_FilterLibraryDlg::Init (const int type, const int theMode)
300 QValueList<int> aTypes;
302 Init(aTypes, theMode);
305 //=======================================================================
306 // name : SMESHGUI_FilterLibraryDlg::Init
307 // Purpose : Init dialog fields, connect signals and slots, show dialog
308 //=======================================================================
309 void SMESHGUI_FilterLibraryDlg::Init (const QValueList<int>& theTypes,
314 myTable->Init(theTypes);
315 myCurrFilterName = "";
321 updateControlsVisibility();
324 connect( mySMESHGUI, SIGNAL(SignalDeactivateActiveDialog()), SLOT(onDeactivate()));
325 connect( mySMESHGUI, SIGNAL(SignalCloseAllDialogs()), SLOT(onClose()));
327 if (myMode == ADD_TO)
329 setCaption(tr("ADD_TO_TLT"));
330 if (myFileName->text().isEmpty())
331 myFileName->setText(getDefaultLibraryName());
334 else if (myMode == COPY_FROM)
336 setCaption(tr("COPY_FROM_TLT"));
337 if (myFileName->text().isEmpty())
338 myFileName->setText(getDefaultLibraryName());
340 if (myListBox->count() > 0)
341 myListBox->setCurrentItem(0);
345 setCaption(tr("EDIT_LIB_TLT"));
346 if (myFileName->text().isEmpty())
347 myFileName->setText(getDefaultLibraryName());
349 if (myListBox->count() > 0)
350 myListBox->setCurrentItem(0);
356 isPermissionValid(false);
359 //=======================================================================
360 // name : SMESHGUI_FilterLibraryDlg::updateControlsVisibility
361 // Purpose : Update visibility of controls in accordance with myMode
362 //=======================================================================
363 void SMESHGUI_FilterLibraryDlg::updateControlsVisibility()
365 if (myMode == ADD_TO)
368 myNameGrp->setEnabled(true);
371 myTable->SetEditable(false);
373 else if (myMode == COPY_FROM)
376 myNameGrp->setEnabled(false);
379 myTable->SetEditable(false);
381 else if (myMode == EDIT)
384 myNameGrp->setEnabled(true);
387 myTable->SetEditable(true);
390 qApp->processEvents();
395 //=======================================================================
396 // name : SMESHGUI_FilterLibraryDlg::onApply
397 // Purpose : SLOT called when "Apply" button pressed.
398 //=======================================================================
399 bool SMESHGUI_FilterLibraryDlg::onApply()
401 if (!isValid(true) || !isPermissionValid(false))
404 if (myLibrary->_is_nil()) {
405 QMessageBox::information(SMESHGUI::desktop(), tr("SMESH_WRN_WARNING"),
406 tr("LIBRARY_IS_NOT_LOADED"), QMessageBox::Ok);
410 const char* aName = myFileName->text().latin1();
411 if (strcmp(myLibrary->GetFileName(), aName) != 0)
412 myLibrary->SetFileName(aName);
414 bool aResult = false;
416 if (myMode == COPY_FROM || myListBox->count() == 0) {
418 } else if (myMode == EDIT || myMode == ADD_TO) {
419 SMESH::Filter_var aFilter = createFilter();
420 if (!myLibrary->Replace(myCurrFilterName, myName->text(), aFilter.in())) {
421 QMessageBox::information(SMESHGUI::desktop(), tr("SMESH_ERROR"),
422 tr("ERROR_OF_EDITING"), QMessageBox::Ok);
429 if (aResult && myMode != COPY_FROM)
430 aResult = myLibrary->Save();
433 char* aFileName = myLibrary->GetFileName();
434 getDefaultLibraryName() = QString(aFileName);
436 } else if (myMode != COPY_FROM) {
437 QMessageBox::information(SMESHGUI::desktop(), tr("SMESH_ERROR"),
438 tr("ERROR_OF_SAVING"), QMessageBox::Ok);
445 //=======================================================================
446 // name : SMESHGUI_FilterLibraryDlg::onOk
447 // Purpose : SLOT called when "Ok" button pressed.
448 // Assign filters VTK viewer and close dialog
449 //=======================================================================
450 void SMESHGUI_FilterLibraryDlg::onOk()
454 disconnect( mySMESHGUI, 0, this, 0);
455 mySMESHGUI->ResetState();
460 //=======================================================================
461 // name : SMESHGUI_FilterLibraryDlg::onClose
462 // Purpose : SLOT called when "Close" button pressed. Close dialog
463 //=======================================================================
464 void SMESHGUI_FilterLibraryDlg::onClose()
466 disconnect( mySMESHGUI, 0, this, 0);
467 mySMESHGUI->ResetState();
471 //=================================================================================
472 // function : onHelp()
474 //=================================================================================
475 void SMESHGUI_FilterLibraryDlg::onHelp()
477 LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
479 app->onHelpContextModule(mySMESHGUI ? app->moduleName(mySMESHGUI->moduleName()) : QString(""), myHelpFileName);
483 platform = "winapplication";
485 platform = "application";
487 SUIT_MessageBox::warn1(0, QObject::tr("WRN_WARNING"),
488 QObject::tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
489 arg(app->resourceMgr()->stringValue("ExternalBrowser", platform)).arg(myHelpFileName),
490 QObject::tr("BUT_OK"));
494 //=======================================================================
495 // name : SMESHGUI_FilterLibraryDlg::onDeactivate
496 // Purpose : SLOT called when dialog must be deativated
497 //=======================================================================
498 void SMESHGUI_FilterLibraryDlg::onDeactivate()
503 //=======================================================================
504 // name : SMESHGUI_FilterLibraryDlg::enterEvent
505 // Purpose : Event filter
506 //=======================================================================
507 void SMESHGUI_FilterLibraryDlg::enterEvent(QEvent*)
512 //=================================================================================
513 // function : closeEvent()
514 // purpose : Close dialog
515 //=================================================================================
516 void SMESHGUI_FilterLibraryDlg::closeEvent(QCloseEvent* e)
521 //=======================================================================
522 // name : SMESHGUI_FilterLibraryDlg::getFileName
523 // Purpose : Get file name
524 //=======================================================================
525 QString SMESHGUI_FilterLibraryDlg::getFileName() const
527 return myFileName != 0 ? myFileName->text() : QString("");
530 //================================================================
531 // Function : setFileName
532 // Purpose : Set file name to line edit
533 //================================================================
534 void SMESHGUI_FilterLibraryDlg::setFileName(const QString& txt, const bool autoExt)
538 myFileName->setText(autoExt ? autoExtension(txt) : txt);
541 //================================================================
542 // Function : autoExtension
543 // Purpose : Append extension to the file name
544 //================================================================
545 QString SMESHGUI_FilterLibraryDlg::autoExtension(const QString& theFileName) const
547 QString anExt = theFileName.section('.', -1);
548 return anExt != "xml" && anExt != "XML" ? theFileName + ".xml" : theFileName;
551 //================================================================
552 // Function : filterWildCards
554 //================================================================
555 QStringList SMESHGUI_FilterLibraryDlg::filterWildCards(const QString& theFilter) const
559 int b = theFilter.findRev("(");
560 int e = theFilter.findRev(")");
561 if (b != -1 && e != -1)
563 QString content = theFilter.mid(b + 1, e - b - 1).stripWhiteSpace();
564 QStringList lst = QStringList::split(" ", content);
565 for (QStringList::const_iterator it = lst.begin(); it != lst.end(); ++it)
566 if ((*it).find(".") != -1)
567 res.append((*it).stripWhiteSpace());
572 //=======================================================================
573 // name : SMESHGUI_FilterLibraryDlg::prepareFilters
574 // Purpose : Prepare filters for dialog
575 //=======================================================================
576 QStringList SMESHGUI_FilterLibraryDlg::prepareFilters() const
578 static QStringList aList;
581 aList.append(tr("XML_FILT"));
582 //aList.append(tr("ALL_FILES_FILTER"));
588 //================================================================
589 // Function : onBrowse
590 // Purpose : SLOT. Display "Open file" dialog for chosing library name
591 //================================================================
592 void SMESHGUI_FilterLibraryDlg::onBrowse()
594 Dialog* aDlg = new Dialog(this, true);
595 aDlg->setCaption(tr("OPEN_LIBRARY"));
597 //aDlg->setMode(myMode == COPY_FROM ? QFileDialogP::ExistingFile : QFileDialogP::AnyFile);
598 aDlg->setMode(myMode == COPY_FROM ? QFileDialog::ExistingFile : QFileDialog::AnyFile);
599 aDlg->setFilters(prepareFilters());
600 aDlg->setSelection(getFileName());
602 QPushButton* anOkBtn = (QPushButton*)aDlg->child("OK", "QPushButton");
604 anOkBtn->setText(tr("SMESH_BUT_OK"));
606 if (aDlg->exec() != Accepted)
609 QString fName = aDlg->selectedFile();
614 if (QFileInfo(fName).extension().isEmpty())
615 fName = autoExtension(fName);
617 fName = QDir::convertSeparators(fName);
618 QString prev = QDir::convertSeparators(getFileName());
625 QString aName = myListBox->text(myListBox->count() - 1);
628 if (myMode == ADD_TO)
630 myTable->Copy((SMESHGUI_FilterTable*)parentWidget());
631 myCurrFilterName = "";
633 addFilterToLib(aName);
636 isPermissionValid(false);
639 //=======================================================================
640 // name : SMESHGUI_FilterLibraryDlg::processNewLibrary
641 // Purpose : SLOT. Calleds when file name changed
642 //=======================================================================
643 void SMESHGUI_FilterLibraryDlg::processNewLibrary()
645 SMESH::FilterManager_var aFilterMgr = SMESH::GetFilterManager();
646 if (aFilterMgr->_is_nil())
649 myLibrary = aFilterMgr->LoadLibrary(autoExtension(getFileName()));
650 if (myLibrary->_is_nil()) {
651 if (myMode == COPY_FROM) {
652 QMessageBox::information(SMESHGUI::desktop(), tr("SMESH_ERROR"),
653 tr("ERROR_LOAD"), QMessageBox::Ok);
656 myLibrary = aFilterMgr->CreateLibrary();
657 myLibrary->SetFileName(getFileName().latin1());
664 //=======================================================================
665 // name : SMESHGUI_FilterLibraryDlg::updateList
666 // Purpose : Fill list box with filter names
667 //=======================================================================
668 void SMESHGUI_FilterLibraryDlg::updateList()
671 SMESH::string_array_var aNames = myLibrary->GetNames((SMESH::ElementType)myTable->GetType());
672 for (int i = 0, n = aNames->length(); i < n; i++)
673 aList.append(QString(aNames[ i ]));
675 myListBox->insertStringList(aList);
676 if (myListBox->count() == 0)
678 myTable->Clear(myTable->GetType());
680 myName->setEnabled(false);
681 myTable->SetEnabled(false);
685 myName->setEnabled(true);
686 myTable->SetEnabled(true);
687 if (myListBox->count())
689 myCurrFilterName = "";
690 myListBox->setCurrentItem(0);
695 //=======================================================================
696 // name : SMESHGUI_FilterLibraryDlg::isNameValid
697 // Purpose : Verify validity if entered data
698 //=======================================================================
699 bool SMESHGUI_FilterLibraryDlg::isNameValid(const bool theMess) const
701 // verify validity of filter name
702 if (myName->isEnabled() && !myCurrFilterName.isEmpty()) {
703 QString aCurrName = myName->text();
704 if (aCurrName.isEmpty()) {
706 QMessageBox::information(SMESHGUI::desktop(), tr("SMESH_INSUFFICIENT_DATA"),
707 tr("EMPTY_FILTER_NAME"), QMessageBox::Ok);
711 SMESH::string_array_var aNames = myLibrary->GetAllNames();
712 for (int f = 0, n = aNames->length(); f < n; f++) {
713 if (aNames[ f ] == aCurrName && aNames[ f ] != myCurrFilterName) {
715 QMessageBox::information(SMESHGUI::desktop(), tr("SMESH_INSUFFICIENT_DATA"),
716 tr("ERROR_FILTER_NAME"), QMessageBox::Ok);
725 //=======================================================================
726 // name : SMESHGUI_FilterLibraryDlg::isPermissionValid
727 // Purpose : Verify write permission on file
728 //=======================================================================
729 bool SMESHGUI_FilterLibraryDlg::isPermissionValid(const bool theIsExistingOnly)
731 if (myMode == COPY_FROM)
734 // Verify write permission
735 bool isWritable = false;
737 QString fName(myFileName->text());
738 if (QFileInfo(fName).extension().isEmpty())
739 fName = autoExtension(fName);
741 fName = QDir::convertSeparators(fName);
743 if (QFileInfo(fName).exists()) {
744 isWritable = QFileInfo(fName).isWritable();
745 } else if (!theIsExistingOnly) {
746 QFileInfo aDirInfo(QFileInfo(fName).dirPath(true));
747 isWritable = aDirInfo.isWritable();
748 /*if (QDir(QFileInfo(fName).dirPath(true)).exists() ||
749 QDir().mkdir(QFileInfo(fName).dirPath(true)))
752 if (aFile.open(IO_WriteOnly))
763 QMessageBox::information(SMESHGUI::desktop(), tr("SMESH_WRN_WARNING"),
764 tr("NO_PERMISSION"), QMessageBox::Ok);
771 //=======================================================================
772 // name : SMESHGUI_FilterLibraryDlg::isValid
773 // Purpose : Verify validity if entered data
774 //=======================================================================
775 bool SMESHGUI_FilterLibraryDlg::isValid(const bool theMess) const
777 // verify validity of table
778 if (!myTable->IsValid(theMess) || !isNameValid(theMess))
784 //=======================================================================
785 // name : SMESHGUI_FilterLibraryDlg::onFilterChanged
786 // Purpose : SLOT. Called when selected filter of library changed
787 //=======================================================================
788 void SMESHGUI_FilterLibraryDlg::onFilterChanged(const QString& theName)
790 if (myLibrary->_is_nil())
793 // Save parameters of filter if it was changed
795 if (!myCurrFilterName.isEmpty() && myTable->IsEditable())
799 myListBox->blockSignals(true);
800 myListBox->setCurrentItem(myCurrFilter);
801 myListBox->blockSignals(false);
805 SMESH::Filter_var aFilter = createFilter();
806 myLibrary->Replace(myCurrFilterName.latin1(), myName->text().latin1(), aFilter);
809 // Fill table with filter parameters
811 SMESH::Filter_var aFilter = myLibrary->Copy(theName);
812 myCurrFilterName = theName;
813 myCurrFilter = myListBox->currentItem();
814 myName->setText(theName);
817 SMESH::Filter::Criteria_var aCriteria;
819 myTable->Clear(myTable->GetType());
821 if (!aFilter->GetCriteria(aCriteria))
824 for (int i = 0, n = aCriteria->length(); i < n; i++)
825 myTable->AddCriterion(aCriteria[ i ], myTable->GetType());
830 //=======================================================================
831 // name : SMESHGUI_FilterLibraryDlg::onReturnPressed
832 // Purpose : SLOT. Called when enter button is pressed in library name field
834 //=======================================================================
835 void SMESHGUI_FilterLibraryDlg::onReturnPressed()
837 QString aName = myListBox->text(myListBox->count() - 1);
841 if (myMode == ADD_TO)
843 myTable->Copy((SMESHGUI_FilterTable*)parentWidget());
844 myCurrFilterName = "";
846 addFilterToLib(aName);
849 isPermissionValid(false);
852 //=======================================================================
853 // name : SMESHGUI_FilterLibraryDlg::enableMainButtons
854 // Purpose : Update state of "OK", "Cancel" buttons
855 //=======================================================================
856 void SMESHGUI_FilterLibraryDlg::enableMainButtons()
858 /*bool isEnabled = isValid(false);
859 if (myButtons.contains(BTN_OK))
860 myButtons[ BTN_OK ]->setEnabled(isEnabled);
861 else if (myButtons.contains(BTN_Apply))
862 myButtons[ BTN_OK ]->setEnabled(isEnabled);
863 if (myButtons.contains(BTN_Cancel))
864 myButtons[ BTN_Cancel ]->setEnabled(isEnabled);
865 else if (myButtons.contains(BTN_Close))
866 myButtons[ BTN_Cancel ]->setEnabled(isEnabled);
870 //=======================================================================
871 // name : SMESHGUI_FilterLibraryDlg::createFilter
872 // Purpose : Cerate filter in accordance with library
873 //=======================================================================
874 SMESH::Filter_ptr SMESHGUI_FilterLibraryDlg::createFilter(const int theType)
876 int n = myTable->NumRows(theType);
878 SMESH::Filter::Criteria_var aCriteria = new SMESH::Filter::Criteria;
879 aCriteria->length(n);
881 for (int i = 0; i < n; i++)
883 SMESH::Filter::Criterion aCriterion = SMESHGUI_FilterDlg::createCriterion();
884 myTable->GetCriterion(i, aCriterion);
885 aCriteria[ i ] = aCriterion;
888 SMESH::FilterManager_var aFilterMgr = SMESH::GetFilterManager();
889 SMESH::Filter_var aFilter = aFilterMgr->CreateFilter();
890 aFilter->SetCriteria(aCriteria.in());
892 return aFilter._retn();
895 //=======================================================================
896 // name : SMESHGUI_FilterLibraryDlg::onAddBtnPressed
897 // Purpose : SLOT. Called when "Add" button pressed
898 // Add new filter to the end of library
899 //=======================================================================
900 void SMESHGUI_FilterLibraryDlg::onAddBtnPressed()
902 // Save parameters of filter if it was changed
903 if (!myCurrFilterName.isEmpty() && myTable->IsEditable())
908 SMESH::Filter_var aFilter = createFilter();
909 myLibrary->Replace(myCurrFilterName.latin1(), myName->text().latin1(), aFilter);
912 addFilterToLib(getDefaultFilterName());
915 //=======================================================================
916 // name : onAddBtnPressed()
917 // Purpose : SLOT. Called when "Add" button pressed
918 // Add new filter to the end of library
919 //=======================================================================
920 void SMESHGUI_FilterLibraryDlg::addFilterToLib (const QString& theName)
922 if (myLibrary->_is_nil()) {
923 QMessageBox::information(SMESHGUI::desktop(), tr("SMESH_WRN_WARNING"),
924 tr("LIBRARY_IS_NOT_LOADED"), QMessageBox::Ok);
929 SMESH::Filter_var aFilter = createFilter();
931 // if name of filter already exist in the library assign default name for the filter
932 QString aName(theName);
933 SMESH::string_array_var aNames = myLibrary->GetAllNames();
934 for (int i = 0, n = aNames->length(); i < n; i++)
935 if (aName == QString(aNames[ i ]))
937 aName = getDefaultFilterName();
941 // add new filter in library
942 bool aResult = !aFilter->GetPredicate()->_is_nil()
943 ? myLibrary->Add(aName.latin1(), aFilter)
944 : myLibrary->AddEmpty(aName.latin1(), (SMESH::ElementType)myTable->GetType());
947 QMessageBox::information(SMESHGUI::desktop(), tr("SMESH_ERROR"),
948 tr("ERROR_OF_ADDING"), QMessageBox::Ok);
952 myCurrFilterName = "";
956 if (theName != aName)
957 QMessageBox::information(SMESHGUI::desktop(), tr("SMESH_WARNING"),
958 tr("ASSIGN_NEW_NAME").arg(theName).arg(aName), QMessageBox::Ok);
961 //=======================================================================
962 // name : SMESHGUI_FilterLibraryDlg::getDefaultLibraryName
963 // Purpose : Get default library name
964 //=======================================================================
965 QString& SMESHGUI_FilterLibraryDlg::getDefaultLibraryName() const
967 static QString aName;
970 QString aHomeDir = QDir(QDir::home()).absPath();
971 aName = aHomeDir + "/" + tr ("LIB_NAME");
976 //=======================================================================
977 // name : SMESHGUI_FilterLibraryDlg::getDefaultFilterName
978 // Purpose : Create default filter name
979 //=======================================================================
980 QString SMESHGUI_FilterLibraryDlg::getDefaultFilterName() const
984 if (myTable->GetType() == SMESH::NODE)
986 else if (myTable->GetType() == SMESH::EDGE)
988 else if (myTable->GetType() == SMESH::FACE)
990 else if (myTable->GetType() == SMESH::VOLUME)
991 aName = tr("VOLUME");
992 else if (myTable->GetType() == SMESH::ALL)
993 aName = tr("ELEMENT");
995 aName = tr("SELECTION");
997 aName += tr("FILTER");
1000 QMap< QString, int > anAllNames;
1001 SMESH::string_array_var aNames = myLibrary->GetAllNames();
1002 for(int i = 0, n = aNames->length(); i < n; i++)
1003 anAllNames[ QString(aNames[ i ]) ] = -1;
1005 bool isNotValid = true;
1011 aNewName = aName + "_" + QString("%1").arg(k);
1012 if (anAllNames.contains(aNewName))
1022 //=======================================================================
1023 // name : SMESHGUI_FilterLibraryDlg::setSelected
1024 // Purpose : set selected item in list box containing filters
1025 //=======================================================================
1026 bool SMESHGUI_FilterLibraryDlg::setSelected(const QString& theName)
1028 int anIndex = getIndex(theName);
1031 myListBox->setCurrentItem(anIndex);
1032 myCurrFilterName = theName;
1033 myCurrFilter = anIndex;
1035 return anIndex != -1;
1038 //=======================================================================
1039 // name : SMESHGUI_FilterLibraryDlg::getIndex
1040 // Purpose : Get index of the filter in list box
1041 //=======================================================================
1042 int SMESHGUI_FilterLibraryDlg::getIndex(const QString& theName) const
1044 for (int i = 0, n = myListBox->count(); i < n; i++)
1045 if (myListBox->text(i) == theName)
1050 //=======================================================================
1051 // name : SMESHGUI_FilterLibraryDlg::onDeleteBtnPressed
1052 // Purpose : SLOT. Called when "Delete" button pressed
1053 //=======================================================================
1054 void SMESHGUI_FilterLibraryDlg::onDeleteBtnPressed()
1056 if (myLibrary->_is_nil()) {
1057 QMessageBox::information(SMESHGUI::desktop(), tr("SMESH_WRN_WARNING"),
1058 tr("LIBRARY_IS_NOT_LOADED"), QMessageBox::Ok);
1062 int anIndex = getIndex(myCurrFilterName);
1064 if (anIndex == -1 || !myLibrary->Delete(myCurrFilterName.latin1())) {
1065 QMessageBox::information(SMESHGUI::desktop(), tr("SMESH_ERROR"),
1066 tr("ERROR_OF_DELETING"), QMessageBox::Ok);
1068 myCurrFilterName = "";
1070 myListBox->removeItem(anIndex);
1073 myListBox->setSelected(anIndex - 1, true);
1074 else if (anIndex == 0 && myListBox->count() > 0)
1075 myListBox->setSelected(0, true);
1080 myTable->SetEnabled(myListBox->count() > 0);
1081 if (myListBox->count() == 0) {
1082 myName->setText("");
1083 myName->setEnabled(false);
1087 //=======================================================================
1088 // name : onFilterNameChanged()
1089 // Purpose : SLOT. Called when name of filter changed
1090 // Change filter name in list box
1091 //=======================================================================
1092 void SMESHGUI_FilterLibraryDlg::onFilterNameChanged (const QString& theName)
1094 int aCurrItem = myListBox->currentItem();
1095 if (aCurrItem == -1)
1098 myListBox->blockSignals(true);
1099 myListBox->changeItem(theName, aCurrItem);
1100 myListBox->blockSignals(false);
1103 //=======================================================================
1104 // name : SMESHGUI_FilterLibraryDlg::SetTable
1105 // Purpose : Set table
1106 //=======================================================================
1107 void SMESHGUI_FilterLibraryDlg::SetTable(const SMESHGUI_FilterTable* theTable)
1109 myTable->Copy(theTable);
1110 myName->setText(getDefaultFilterName());
1111 addFilterToLib(myName->text());
1115 //=======================================================================
1116 // name : SMESHGUI_FilterLibraryDlg::GetTable
1117 // Purpose : Get table
1118 //=======================================================================
1119 const SMESHGUI_FilterTable* SMESHGUI_FilterLibraryDlg::GetTable() const
1125 //=======================================================================
1126 // name : SMESHGUI_FilterLibraryDlg::onEntityTypeChanged
1127 // Purpose : SLOT. Called when entiyt type changed
1128 //=======================================================================
1129 void SMESHGUI_FilterLibraryDlg::onEntityTypeChanged(const int theType)
1131 if (myLibrary->_is_nil())
1135 myCurrFilterName = "";
1138 if (myListBox->count())
1139 myListBox->setCurrentItem(0);
1142 //=======================================================================
1143 // name : SMESHGUI_FilterLibraryDlg::onNeedValidation
1145 //=======================================================================
1146 void SMESHGUI_FilterLibraryDlg::onNeedValidation()
1148 if (!myCurrFilterName.isEmpty())
1150 bool valid = isValid(true);
1151 myTable->SetValidity(valid);
1155 SMESH::Filter_var aFilter = createFilter(myTable->GetType());
1156 myLibrary->Replace(myCurrFilterName.latin1(), myName->text().latin1(), aFilter);
1161 //=================================================================================
1162 // function : keyPressEvent()
1164 //=================================================================================
1165 void SMESHGUI_FilterLibraryDlg::keyPressEvent( QKeyEvent* e )
1167 QDialog::keyPressEvent( e );
1168 if ( e->isAccepted() )
1171 if ( e->key() == Key_F1 )