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.htm";
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_OK ] = new QPushButton(tr("SMESH_BUT_OK" ), aGrp);
245 myButtons[ BTN_Apply ] = new QPushButton(tr("SMESH_BUT_APPLY"), aGrp);
247 QLabel* aLbl = new QLabel(aGrp);
248 aLbl->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed));
250 myButtons[ BTN_Cancel ] = new QPushButton(tr("SMESH_BUT_CANCEL"), aGrp);
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);
481 SUIT_MessageBox::warn1(0, QObject::tr("WRN_WARNING"),
482 QObject::tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
483 arg(app->resourceMgr()->stringValue("ExternalBrowser", "application")).arg(myHelpFileName),
484 QObject::tr("BUT_OK"));
488 //=======================================================================
489 // name : SMESHGUI_FilterLibraryDlg::onDeactivate
490 // Purpose : SLOT called when dialog must be deativated
491 //=======================================================================
492 void SMESHGUI_FilterLibraryDlg::onDeactivate()
497 //=======================================================================
498 // name : SMESHGUI_FilterLibraryDlg::enterEvent
499 // Purpose : Event filter
500 //=======================================================================
501 void SMESHGUI_FilterLibraryDlg::enterEvent(QEvent*)
506 //=================================================================================
507 // function : closeEvent()
508 // purpose : Close dialog
509 //=================================================================================
510 void SMESHGUI_FilterLibraryDlg::closeEvent(QCloseEvent* e)
515 //=======================================================================
516 // name : SMESHGUI_FilterLibraryDlg::getFileName
517 // Purpose : Get file name
518 //=======================================================================
519 QString SMESHGUI_FilterLibraryDlg::getFileName() const
521 return myFileName != 0 ? myFileName->text() : QString("");
524 //================================================================
525 // Function : setFileName
526 // Purpose : Set file name to line edit
527 //================================================================
528 void SMESHGUI_FilterLibraryDlg::setFileName(const QString& txt, const bool autoExt)
532 myFileName->setText(autoExt ? autoExtension(txt) : txt);
535 //================================================================
536 // Function : autoExtension
537 // Purpose : Append extension to the file name
538 //================================================================
539 QString SMESHGUI_FilterLibraryDlg::autoExtension(const QString& theFileName) const
541 QString anExt = theFileName.section('.', -1);
542 return anExt != "xml" && anExt != "XML" ? theFileName + ".xml" : theFileName;
545 //================================================================
546 // Function : filterWildCards
548 //================================================================
549 QStringList SMESHGUI_FilterLibraryDlg::filterWildCards(const QString& theFilter) const
553 int b = theFilter.findRev("(");
554 int e = theFilter.findRev(")");
555 if (b != -1 && e != -1)
557 QString content = theFilter.mid(b + 1, e - b - 1).stripWhiteSpace();
558 QStringList lst = QStringList::split(" ", content);
559 for (QStringList::const_iterator it = lst.begin(); it != lst.end(); ++it)
560 if ((*it).find(".") != -1)
561 res.append((*it).stripWhiteSpace());
566 //=======================================================================
567 // name : SMESHGUI_FilterLibraryDlg::prepareFilters
568 // Purpose : Prepare filters for dialog
569 //=======================================================================
570 QStringList SMESHGUI_FilterLibraryDlg::prepareFilters() const
572 static QStringList aList;
575 aList.append(tr("XML_FILT"));
576 //aList.append(tr("ALL_FILES_FILTER"));
582 //================================================================
583 // Function : onBrowse
584 // Purpose : SLOT. Display "Open file" dialog for chosing library name
585 //================================================================
586 void SMESHGUI_FilterLibraryDlg::onBrowse()
588 Dialog* aDlg = new Dialog(this, true);
589 aDlg->setCaption(tr("OPEN_LIBRARY"));
591 //aDlg->setMode(myMode == COPY_FROM ? QFileDialogP::ExistingFile : QFileDialogP::AnyFile);
592 aDlg->setMode(myMode == COPY_FROM ? QFileDialog::ExistingFile : QFileDialog::AnyFile);
593 aDlg->setFilters(prepareFilters());
594 aDlg->setSelection(getFileName());
596 QPushButton* anOkBtn = (QPushButton*)aDlg->child("OK", "QPushButton");
598 anOkBtn->setText(tr("SMESH_BUT_OK"));
600 if (aDlg->exec() != Accepted)
603 QString fName = aDlg->selectedFile();
608 if (QFileInfo(fName).extension().isEmpty())
609 fName = autoExtension(fName);
611 fName = QDir::convertSeparators(fName);
612 QString prev = QDir::convertSeparators(getFileName());
619 QString aName = myListBox->text(myListBox->count() - 1);
622 if (myMode == ADD_TO)
624 myTable->Copy((SMESHGUI_FilterTable*)parentWidget());
625 myCurrFilterName = "";
627 addFilterToLib(aName);
630 isPermissionValid(false);
633 //=======================================================================
634 // name : SMESHGUI_FilterLibraryDlg::processNewLibrary
635 // Purpose : SLOT. Calleds when file name changed
636 //=======================================================================
637 void SMESHGUI_FilterLibraryDlg::processNewLibrary()
639 SMESH::FilterManager_var aFilterMgr = SMESH::GetFilterManager();
640 if (aFilterMgr->_is_nil())
643 myLibrary = aFilterMgr->LoadLibrary(autoExtension(getFileName()));
644 if (myLibrary->_is_nil()) {
645 if (myMode == COPY_FROM) {
646 QMessageBox::information(SMESHGUI::desktop(), tr("SMESH_ERROR"),
647 tr("ERROR_LOAD"), QMessageBox::Ok);
650 myLibrary = aFilterMgr->CreateLibrary();
651 myLibrary->SetFileName(getFileName().latin1());
658 //=======================================================================
659 // name : SMESHGUI_FilterLibraryDlg::updateList
660 // Purpose : Fill list box with filter names
661 //=======================================================================
662 void SMESHGUI_FilterLibraryDlg::updateList()
665 SMESH::string_array_var aNames = myLibrary->GetNames((SMESH::ElementType)myTable->GetType());
666 for (int i = 0, n = aNames->length(); i < n; i++)
667 aList.append(QString(aNames[ i ]));
669 myListBox->insertStringList(aList);
670 if (myListBox->count() == 0)
672 myTable->Clear(myTable->GetType());
674 myName->setEnabled(false);
675 myTable->SetEnabled(false);
679 myName->setEnabled(true);
680 myTable->SetEnabled(true);
681 if (myListBox->count())
683 myCurrFilterName = "";
684 myListBox->setCurrentItem(0);
689 //=======================================================================
690 // name : SMESHGUI_FilterLibraryDlg::isNameValid
691 // Purpose : Verify validity if entered data
692 //=======================================================================
693 bool SMESHGUI_FilterLibraryDlg::isNameValid(const bool theMess) const
695 // verify validity of filter name
696 if (myName->isEnabled() && !myCurrFilterName.isEmpty()) {
697 QString aCurrName = myName->text();
698 if (aCurrName.isEmpty()) {
700 QMessageBox::information(SMESHGUI::desktop(), tr("SMESH_INSUFFICIENT_DATA"),
701 tr("EMPTY_FILTER_NAME"), QMessageBox::Ok);
705 SMESH::string_array_var aNames = myLibrary->GetAllNames();
706 for (int f = 0, n = aNames->length(); f < n; f++) {
707 if (aNames[ f ] == aCurrName && aNames[ f ] != myCurrFilterName) {
709 QMessageBox::information(SMESHGUI::desktop(), tr("SMESH_INSUFFICIENT_DATA"),
710 tr("ERROR_FILTER_NAME"), QMessageBox::Ok);
719 //=======================================================================
720 // name : SMESHGUI_FilterLibraryDlg::isPermissionValid
721 // Purpose : Verify write permission on file
722 //=======================================================================
723 bool SMESHGUI_FilterLibraryDlg::isPermissionValid(const bool theIsExistingOnly)
725 if (myMode == COPY_FROM)
728 // Verify write permission
729 bool isWritable = false;
731 QString fName(myFileName->text());
732 if (QFileInfo(fName).extension().isEmpty())
733 fName = autoExtension(fName);
735 fName = QDir::convertSeparators(fName);
737 if (QFileInfo(fName).exists()) {
738 isWritable = QFileInfo(fName).isWritable();
739 } else if (!theIsExistingOnly) {
740 QFileInfo aDirInfo(QFileInfo(fName).dirPath(true));
741 isWritable = aDirInfo.isWritable();
742 /*if (QDir(QFileInfo(fName).dirPath(true)).exists() ||
743 QDir().mkdir(QFileInfo(fName).dirPath(true)))
746 if (aFile.open(IO_WriteOnly))
757 QMessageBox::information(SMESHGUI::desktop(), tr("SMESH_WRN_WARNING"),
758 tr("NO_PERMISSION"), QMessageBox::Ok);
765 //=======================================================================
766 // name : SMESHGUI_FilterLibraryDlg::isValid
767 // Purpose : Verify validity if entered data
768 //=======================================================================
769 bool SMESHGUI_FilterLibraryDlg::isValid(const bool theMess) const
771 // verify validity of table
772 if (!myTable->IsValid(theMess) || !isNameValid(theMess))
778 //=======================================================================
779 // name : SMESHGUI_FilterLibraryDlg::onFilterChanged
780 // Purpose : SLOT. Called when selected filter of library changed
781 //=======================================================================
782 void SMESHGUI_FilterLibraryDlg::onFilterChanged(const QString& theName)
784 if (myLibrary->_is_nil())
787 // Save parameters of filter if it was changed
789 if (!myCurrFilterName.isEmpty() && myTable->IsEditable())
793 myListBox->blockSignals(true);
794 myListBox->setCurrentItem(myCurrFilter);
795 myListBox->blockSignals(false);
799 SMESH::Filter_var aFilter = createFilter();
800 myLibrary->Replace(myCurrFilterName.latin1(), myName->text().latin1(), aFilter);
803 // Fill table with filter parameters
805 SMESH::Filter_var aFilter = myLibrary->Copy(theName);
806 myCurrFilterName = theName;
807 myCurrFilter = myListBox->currentItem();
808 myName->setText(theName);
811 SMESH::Filter::Criteria_var aCriteria;
813 myTable->Clear(myTable->GetType());
815 if (!aFilter->GetCriteria(aCriteria))
818 for (int i = 0, n = aCriteria->length(); i < n; i++)
819 myTable->AddCriterion(aCriteria[ i ], myTable->GetType());
824 //=======================================================================
825 // name : SMESHGUI_FilterLibraryDlg::onReturnPressed
826 // Purpose : SLOT. Called when enter button is pressed in library name field
828 //=======================================================================
829 void SMESHGUI_FilterLibraryDlg::onReturnPressed()
831 QString aName = myListBox->text(myListBox->count() - 1);
835 if (myMode == ADD_TO)
837 myTable->Copy((SMESHGUI_FilterTable*)parentWidget());
838 myCurrFilterName = "";
840 addFilterToLib(aName);
843 isPermissionValid(false);
846 //=======================================================================
847 // name : SMESHGUI_FilterLibraryDlg::enableMainButtons
848 // Purpose : Update state of "OK", "Cancel" buttons
849 //=======================================================================
850 void SMESHGUI_FilterLibraryDlg::enableMainButtons()
852 /*bool isEnabled = isValid(false);
853 if (myButtons.contains(BTN_OK))
854 myButtons[ BTN_OK ]->setEnabled(isEnabled);
855 else if (myButtons.contains(BTN_Apply))
856 myButtons[ BTN_OK ]->setEnabled(isEnabled);
857 if (myButtons.contains(BTN_Cancel))
858 myButtons[ BTN_Cancel ]->setEnabled(isEnabled);
859 else if (myButtons.contains(BTN_Close))
860 myButtons[ BTN_Cancel ]->setEnabled(isEnabled);
864 //=======================================================================
865 // name : SMESHGUI_FilterLibraryDlg::createFilter
866 // Purpose : Cerate filter in accordance with library
867 //=======================================================================
868 SMESH::Filter_ptr SMESHGUI_FilterLibraryDlg::createFilter(const int theType)
870 int n = myTable->NumRows(theType);
872 SMESH::Filter::Criteria_var aCriteria = new SMESH::Filter::Criteria;
873 aCriteria->length(n);
875 for (int i = 0; i < n; i++)
877 SMESH::Filter::Criterion aCriterion = SMESHGUI_FilterDlg::createCriterion();
878 myTable->GetCriterion(i, aCriterion);
879 aCriteria[ i ] = aCriterion;
882 SMESH::FilterManager_var aFilterMgr = SMESH::GetFilterManager();
883 SMESH::Filter_var aFilter = aFilterMgr->CreateFilter();
884 aFilter->SetCriteria(aCriteria.in());
886 return aFilter._retn();
889 //=======================================================================
890 // name : SMESHGUI_FilterLibraryDlg::onAddBtnPressed
891 // Purpose : SLOT. Called when "Add" button pressed
892 // Add new filter to the end of library
893 //=======================================================================
894 void SMESHGUI_FilterLibraryDlg::onAddBtnPressed()
896 // Save parameters of filter if it was changed
897 if (!myCurrFilterName.isEmpty() && myTable->IsEditable())
902 SMESH::Filter_var aFilter = createFilter();
903 myLibrary->Replace(myCurrFilterName.latin1(), myName->text().latin1(), aFilter);
906 addFilterToLib(getDefaultFilterName());
909 //=======================================================================
910 // name : onAddBtnPressed()
911 // Purpose : SLOT. Called when "Add" button pressed
912 // Add new filter to the end of library
913 //=======================================================================
914 void SMESHGUI_FilterLibraryDlg::addFilterToLib (const QString& theName)
916 if (myLibrary->_is_nil()) {
917 QMessageBox::information(SMESHGUI::desktop(), tr("SMESH_WRN_WARNING"),
918 tr("LIBRARY_IS_NOT_LOADED"), QMessageBox::Ok);
923 SMESH::Filter_var aFilter = createFilter();
925 // if name of filter already exist in the library assign default name for the filter
926 QString aName(theName);
927 SMESH::string_array_var aNames = myLibrary->GetAllNames();
928 for (int i = 0, n = aNames->length(); i < n; i++)
929 if (aName == QString(aNames[ i ]))
931 aName = getDefaultFilterName();
935 // add new filter in library
936 bool aResult = !aFilter->GetPredicate()->_is_nil()
937 ? myLibrary->Add(aName.latin1(), aFilter)
938 : myLibrary->AddEmpty(aName.latin1(), (SMESH::ElementType)myTable->GetType());
941 QMessageBox::information(SMESHGUI::desktop(), tr("SMESH_ERROR"),
942 tr("ERROR_OF_ADDING"), QMessageBox::Ok);
946 myCurrFilterName = "";
950 if (theName != aName)
951 QMessageBox::information(SMESHGUI::desktop(), tr("SMESH_WARNING"),
952 tr("ASSIGN_NEW_NAME").arg(theName).arg(aName), QMessageBox::Ok);
955 //=======================================================================
956 // name : SMESHGUI_FilterLibraryDlg::getDefaultLibraryName
957 // Purpose : Get default library name
958 //=======================================================================
959 QString& SMESHGUI_FilterLibraryDlg::getDefaultLibraryName() const
961 static QString aName;
964 QString aHomeDir = QDir(QDir::home()).absPath();
965 aName = aHomeDir + "/" + tr ("LIB_NAME");
970 //=======================================================================
971 // name : SMESHGUI_FilterLibraryDlg::getDefaultFilterName
972 // Purpose : Create default filter name
973 //=======================================================================
974 QString SMESHGUI_FilterLibraryDlg::getDefaultFilterName() const
978 if (myTable->GetType() == SMESH::NODE)
980 else if (myTable->GetType() == SMESH::EDGE)
982 else if (myTable->GetType() == SMESH::FACE)
984 else if (myTable->GetType() == SMESH::VOLUME)
985 aName = tr("VOLUME");
986 else if (myTable->GetType() == SMESH::ALL)
987 aName = tr("ELEMENT");
989 aName = tr("SELECTION");
991 aName += tr("FILTER");
994 QMap< QString, int > anAllNames;
995 SMESH::string_array_var aNames = myLibrary->GetAllNames();
996 for(int i = 0, n = aNames->length(); i < n; i++)
997 anAllNames[ QString(aNames[ i ]) ] = -1;
999 bool isNotValid = true;
1005 aNewName = aName + "_" + QString("%1").arg(k);
1006 if (anAllNames.contains(aNewName))
1016 //=======================================================================
1017 // name : SMESHGUI_FilterLibraryDlg::setSelected
1018 // Purpose : set selected item in list box containing filters
1019 //=======================================================================
1020 bool SMESHGUI_FilterLibraryDlg::setSelected(const QString& theName)
1022 int anIndex = getIndex(theName);
1025 myListBox->setCurrentItem(anIndex);
1026 myCurrFilterName = theName;
1027 myCurrFilter = anIndex;
1029 return anIndex != -1;
1032 //=======================================================================
1033 // name : SMESHGUI_FilterLibraryDlg::getIndex
1034 // Purpose : Get index of the filter in list box
1035 //=======================================================================
1036 int SMESHGUI_FilterLibraryDlg::getIndex(const QString& theName) const
1038 for (int i = 0, n = myListBox->count(); i < n; i++)
1039 if (myListBox->text(i) == theName)
1044 //=======================================================================
1045 // name : SMESHGUI_FilterLibraryDlg::onDeleteBtnPressed
1046 // Purpose : SLOT. Called when "Delete" button pressed
1047 //=======================================================================
1048 void SMESHGUI_FilterLibraryDlg::onDeleteBtnPressed()
1050 if (myLibrary->_is_nil()) {
1051 QMessageBox::information(SMESHGUI::desktop(), tr("SMESH_WRN_WARNING"),
1052 tr("LIBRARY_IS_NOT_LOADED"), QMessageBox::Ok);
1056 int anIndex = getIndex(myCurrFilterName);
1058 if (anIndex == -1 || !myLibrary->Delete(myCurrFilterName.latin1())) {
1059 QMessageBox::information(SMESHGUI::desktop(), tr("SMESH_ERROR"),
1060 tr("ERROR_OF_DELETING"), QMessageBox::Ok);
1062 myCurrFilterName = "";
1064 myListBox->removeItem(anIndex);
1067 myListBox->setSelected(anIndex - 1, true);
1068 else if (anIndex == 0 && myListBox->count() > 0)
1069 myListBox->setSelected(0, true);
1074 myTable->SetEnabled(myListBox->count() > 0);
1075 if (myListBox->count() == 0) {
1076 myName->setText("");
1077 myName->setEnabled(false);
1081 //=======================================================================
1082 // name : onFilterNameChanged()
1083 // Purpose : SLOT. Called when name of filter changed
1084 // Change filter name in list box
1085 //=======================================================================
1086 void SMESHGUI_FilterLibraryDlg::onFilterNameChanged (const QString& theName)
1088 int aCurrItem = myListBox->currentItem();
1089 if (aCurrItem == -1)
1092 myListBox->blockSignals(true);
1093 myListBox->changeItem(theName, aCurrItem);
1094 myListBox->blockSignals(false);
1097 //=======================================================================
1098 // name : SMESHGUI_FilterLibraryDlg::SetTable
1099 // Purpose : Set table
1100 //=======================================================================
1101 void SMESHGUI_FilterLibraryDlg::SetTable(const SMESHGUI_FilterTable* theTable)
1103 myTable->Copy(theTable);
1104 myName->setText(getDefaultFilterName());
1105 addFilterToLib(myName->text());
1109 //=======================================================================
1110 // name : SMESHGUI_FilterLibraryDlg::GetTable
1111 // Purpose : Get table
1112 //=======================================================================
1113 const SMESHGUI_FilterTable* SMESHGUI_FilterLibraryDlg::GetTable() const
1119 //=======================================================================
1120 // name : SMESHGUI_FilterLibraryDlg::onEntityTypeChanged
1121 // Purpose : SLOT. Called when entiyt type changed
1122 //=======================================================================
1123 void SMESHGUI_FilterLibraryDlg::onEntityTypeChanged(const int theType)
1125 if (myLibrary->_is_nil())
1129 myCurrFilterName = "";
1132 if (myListBox->count())
1133 myListBox->setCurrentItem(0);
1136 //=======================================================================
1137 // name : SMESHGUI_FilterLibraryDlg::onNeedValidation
1139 //=======================================================================
1140 void SMESHGUI_FilterLibraryDlg::onNeedValidation()
1142 if (!myCurrFilterName.isEmpty())
1144 bool valid = isValid(true);
1145 myTable->SetValidity(valid);
1149 SMESH::Filter_var aFilter = createFilter(myTable->GetType());
1150 myLibrary->Replace(myCurrFilterName.latin1(), myName->text().latin1(), aFilter);
1155 //=================================================================================
1156 // function : keyPressEvent()
1158 //=================================================================================
1159 void SMESHGUI_FilterLibraryDlg::keyPressEvent( QKeyEvent* e )
1161 QDialog::keyPressEvent( e );
1162 if ( e->isAccepted() )
1165 if ( e->key() == Key_F1 )