1 // Copyright (C) 2007-2008 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 // SMESH SMESHGUI : GUI for SMESH component
23 // File : SMESHGUI_FilterLibraryDlg.cxx
24 // Author : Sergey LITONIN
27 #include "SMESHGUI_FilterLibraryDlg.h"
30 #include "SMESHGUI_Utils.h"
31 #include "SMESHGUI_FilterUtils.h"
33 #include "SUIT_Session.h"
34 #include "SUIT_Desktop.h"
35 #include "SUIT_FileDlg.h"
36 #include "SUIT_MessageBox.h"
38 #include "LightApp_Application.h"
41 #include <qapplication.h>
42 #include <qmessagebox.h>
45 #include <qlineedit.h>
46 #include <qpushbutton.h>
47 #include <qgroupbox.h>
51 #include <qfileinfo.h>
58 * Class : SMESHGUI_FilterLibraryDlg::Dialog
59 * Description : Dialog for opening filter library
62 class SMESHGUI_FilterLibraryDlg::Dialog : public SUIT_FileDlg
65 Dialog(QWidget* theParent, const bool theToOpen);
69 virtual bool acceptData();
72 SMESHGUI_FilterLibraryDlg::Dialog::Dialog (QWidget* theParent,
74 : SUIT_FileDlg(theParent, theToOpen)
78 SMESHGUI_FilterLibraryDlg::Dialog::~Dialog()
82 bool SMESHGUI_FilterLibraryDlg::Dialog::acceptData()
84 // if (mode() != QFileDialogP::AnyFile)
85 // return SUIT_FileDlg::acceptData();
91 * Class : SMESHGUI_FilterLibraryDlg
92 * Description : Dialog to specify filters for VTK viewer
95 //=======================================================================
96 // name : SMESHGUI_FilterLibraryDlg::SMESHGUI_FilterLibraryDlg
97 // Purpose : Constructor
98 //=======================================================================
99 SMESHGUI_FilterLibraryDlg::SMESHGUI_FilterLibraryDlg (SMESHGUI* theModule,
101 const QValueList<int>& theTypes,
104 : QDialog( parent, theName, false, WStyle_Customize |
105 WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu ),
106 mySMESHGUI( theModule )
108 construct(theTypes, theMode);
111 //=======================================================================
112 // name : SMESHGUI_FilterLibraryDlg::SMESHGUI_FilterLibraryDlg
113 // Purpose : Constructor
114 //=======================================================================
115 SMESHGUI_FilterLibraryDlg::SMESHGUI_FilterLibraryDlg (SMESHGUI* theModule,
120 : QDialog( parent, theName, true, WStyle_Customize |
121 WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu ),
122 mySMESHGUI( theModule )
124 QValueList<int> aTypes;
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 QValueList<int>& theTypes,
139 QVBoxLayout* aDlgLay = new QVBoxLayout(this, MARGIN, SPACING);
141 myMainFrame = createMainFrame (this);
142 QFrame* aBtnFrame = createButtonFrame(this);
144 aDlgLay->addWidget(myMainFrame);
145 aDlgLay->addWidget(aBtnFrame);
147 aDlgLay->setStretchFactor(myMainFrame, 1);
149 myHelpFileName = "selection_filter_library_page.html";
151 Init(myTypes, myMode);
154 //=======================================================================
155 // name : SMESHGUI_FilterLibraryDlg::createMainFrame
156 // Purpose : Create frame containing dialog's input fields
157 //=======================================================================
158 QFrame* SMESHGUI_FilterLibraryDlg::createMainFrame (QWidget* theParent)
160 QGroupBox* aMainFrame = new QGroupBox(1, Qt::Horizontal, theParent);
161 aMainFrame->setFrameStyle(QFrame::NoFrame);
162 aMainFrame->setInsideMargin(0);
166 QGroupBox* aGrp = new QGroupBox(1, Qt::Vertical, aMainFrame);
167 aGrp->setFrameStyle(QFrame::NoFrame);
168 aGrp->setInsideMargin(0);
170 new QLabel(tr("LIBRARY_FILE"), aGrp);
171 myFileName = new QLineEdit(aGrp);
172 myOpenBtn = new QPushButton(aGrp);
173 myOpenBtn->setPixmap(SUIT_Session::session()->resourceMgr()->loadPixmap(
174 "SUIT", tr("ICON_FILE_OPEN")));
178 aGrp = new QGroupBox(1, Qt::Vertical, tr("FILTER_NAMES"), aMainFrame);
179 QFrame* aFrame = new QFrame(aGrp);
180 myListBox = new QListBox(aFrame);
181 myAddBtn = new QPushButton(tr("ADD"), aFrame);
182 myDeleteBtn = new QPushButton(tr("DELETE"), aFrame);
184 QGridLayout* aLay = new QGridLayout(aFrame, 3, 2, 0, 5);
185 aLay->addMultiCellWidget(myListBox, 0, 2, 0, 0);
186 aLay->addWidget(myAddBtn, 0, 1);
187 aLay->addWidget(myDeleteBtn, 1, 1);
188 QSpacerItem* aVSpacer = new QSpacerItem(0, 0, QSizePolicy::Minimum, QSizePolicy::Expanding);
189 aLay->addItem(aVSpacer, 2, 1);
193 myNameGrp = new QGroupBox(1, Qt::Vertical, aMainFrame);
194 myNameGrp->setFrameStyle(QFrame::NoFrame);
195 myNameGrp->setInsideMargin(0);
196 new QLabel(tr("FILTER_NAME"), myNameGrp);
197 myName = new QLineEdit(myNameGrp);
201 myTable = new SMESHGUI_FilterTable( mySMESHGUI, aMainFrame, myTypes);
202 myTable->SetEditable(myMode == EDIT);
203 myTable->SetLibsEnabled(false);
205 myListBox->setMinimumHeight((int)(myTable->sizeHint().height() * 0.5));
206 myListBox->setRowMode(QListBox::FitToWidth);
207 myListBox->setSelectionMode(QListBox::Single);
209 myOpenBtn->setAutoDefault(false);
210 myAddBtn->setAutoDefault(false);
211 myDeleteBtn->setAutoDefault(false);
213 // connect signals and slots
215 connect(myFileName, SIGNAL(returnPressed()), this, SLOT(onReturnPressed()));
216 connect(myOpenBtn , SIGNAL(clicked()), this, SLOT(onBrowse()));
218 connect(myListBox, SIGNAL(highlighted(const QString&)),
219 this, SLOT(onFilterChanged(const QString&)));
221 connect(myAddBtn, SIGNAL(clicked()), this, SLOT(onAddBtnPressed()));
222 connect(myDeleteBtn, SIGNAL(clicked()), this, SLOT(onDeleteBtnPressed()));
224 connect(myName, SIGNAL(textChanged(const QString&)),
225 this, SLOT(onFilterNameChanged(const QString&)));
227 connect(myTable, SIGNAL(EntityTypeChanged(const int)),
228 this, SLOT(onEntityTypeChanged(const int)));
230 connect(myTable, SIGNAL(NeedValidation()), this, SLOT(onNeedValidation()));
235 //=======================================================================
236 // name : SMESHGUI_FilterLibraryDlg::createButtonFrame
237 // Purpose : Create frame containing buttons
238 //=======================================================================
239 QFrame* SMESHGUI_FilterLibraryDlg::createButtonFrame (QWidget* theParent)
241 QGroupBox* aGrp = new QGroupBox(1, Qt::Vertical, theParent);
243 myButtons[ BTN_OK ] = new QPushButton(tr("SMESH_BUT_APPLY_AND_CLOSE"), aGrp);
244 myButtons[ BTN_Apply ] = new QPushButton(tr("SMESH_BUT_APPLY"), aGrp);
246 QLabel* aLbl = new QLabel(aGrp);
247 aLbl->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed));
249 myButtons[ BTN_Cancel ] = new QPushButton(tr("SMESH_BUT_CANCEL"), aGrp);
250 myButtons[ BTN_Close ] = new QPushButton(tr("SMESH_BUT_CLOSE"), aGrp);
251 myButtons[ BTN_Help ] = new QPushButton(tr("SMESH_BUT_HELP"), aGrp);
253 connect(myButtons[ BTN_OK ], SIGNAL(clicked()), SLOT(onOk()));
254 connect(myButtons[ BTN_Cancel ], SIGNAL(clicked()), SLOT(onClose()));
255 connect(myButtons[ BTN_Close ], SIGNAL(clicked()), SLOT(onClose()));
256 connect(myButtons[ BTN_Apply ], SIGNAL(clicked()), SLOT(onApply()));
257 connect(myButtons[ BTN_Help ], SIGNAL(clicked()), SLOT(onHelp()));
259 QMap<int, QPushButton*>::iterator anIter;
260 for (anIter = myButtons.begin(); anIter != myButtons.end(); ++anIter)
261 anIter.data()->setAutoDefault(false);
268 //=======================================================================
269 // name : SMESHGUI_FilterLibraryDlg::updateMainButtons
270 // Purpose : Update visibility of main buttons (OK, Cancel, Close ...)
271 //=======================================================================
272 void SMESHGUI_FilterLibraryDlg::updateMainButtons()
274 if (myTypes.count() == 1) {
275 myButtons[ BTN_Cancel ]->show();
276 myButtons[ BTN_Apply ]->hide();
277 myButtons[ BTN_Close ]->hide();
279 myButtons[ BTN_Cancel ]->hide();
280 myButtons[ BTN_Apply ]->show();
281 myButtons[ BTN_Close ]->show();
285 //=======================================================================
286 // name : SMESHGUI_FilterLibraryDlg::~SMESHGUI_FilterLibraryDlg
287 // Purpose : Destructor
288 //=======================================================================
289 SMESHGUI_FilterLibraryDlg::~SMESHGUI_FilterLibraryDlg()
293 //=======================================================================
294 // name : SMESHGUI_FilterLibraryDlg::Init
295 // Purpose : Init dialog fields, connect signals and slots, show dialog
296 //=======================================================================
297 void SMESHGUI_FilterLibraryDlg::Init (const int type, const int theMode)
299 QValueList<int> aTypes;
301 Init(aTypes, theMode);
304 //=======================================================================
305 // name : SMESHGUI_FilterLibraryDlg::Init
306 // Purpose : Init dialog fields, connect signals and slots, show dialog
307 //=======================================================================
308 void SMESHGUI_FilterLibraryDlg::Init (const QValueList<int>& theTypes,
313 myTable->Init(theTypes);
314 myCurrFilterName = "";
320 updateControlsVisibility();
323 connect( mySMESHGUI, SIGNAL(SignalDeactivateActiveDialog()), SLOT(onDeactivate()));
324 connect( mySMESHGUI, SIGNAL(SignalCloseAllDialogs()), SLOT(onClose()));
326 if (myMode == ADD_TO)
328 setCaption(tr("ADD_TO_TLT"));
329 if (myFileName->text().isEmpty())
330 myFileName->setText(getDefaultLibraryName());
333 else if (myMode == COPY_FROM)
335 setCaption(tr("COPY_FROM_TLT"));
336 if (myFileName->text().isEmpty())
337 myFileName->setText(getDefaultLibraryName());
339 if (myListBox->count() > 0)
340 myListBox->setCurrentItem(0);
344 setCaption(tr("EDIT_LIB_TLT"));
345 if (myFileName->text().isEmpty())
346 myFileName->setText(getDefaultLibraryName());
348 if (myListBox->count() > 0)
349 myListBox->setCurrentItem(0);
355 isPermissionValid(false);
358 //=======================================================================
359 // name : SMESHGUI_FilterLibraryDlg::updateControlsVisibility
360 // Purpose : Update visibility of controls in accordance with myMode
361 //=======================================================================
362 void SMESHGUI_FilterLibraryDlg::updateControlsVisibility()
364 if (myMode == ADD_TO)
367 myNameGrp->setEnabled(true);
370 myTable->SetEditable(false);
372 else if (myMode == COPY_FROM)
375 myNameGrp->setEnabled(false);
378 myTable->SetEditable(false);
380 else if (myMode == EDIT)
383 myNameGrp->setEnabled(true);
386 myTable->SetEditable(true);
389 qApp->processEvents();
394 //=======================================================================
395 // name : SMESHGUI_FilterLibraryDlg::onApply
396 // Purpose : SLOT called when "Apply" button pressed.
397 //=======================================================================
398 bool SMESHGUI_FilterLibraryDlg::onApply()
400 if (!isValid(true) || !isPermissionValid(false))
403 if (myLibrary->_is_nil()) {
404 QMessageBox::information(SMESHGUI::desktop(), tr("SMESH_WRN_WARNING"),
405 tr("LIBRARY_IS_NOT_LOADED"), QMessageBox::Ok);
409 const char* aName = myFileName->text().latin1();
410 if (strcmp(myLibrary->GetFileName(), aName) != 0)
411 myLibrary->SetFileName(aName);
413 bool aResult = false;
415 if (myMode == COPY_FROM || myListBox->count() == 0) {
417 } else if (myMode == EDIT || myMode == ADD_TO) {
418 SMESH::Filter_var aFilter = createFilter();
419 if (!myLibrary->Replace(myCurrFilterName, myName->text(), aFilter.in())) {
420 QMessageBox::information(SMESHGUI::desktop(), tr("SMESH_ERROR"),
421 tr("ERROR_OF_EDITING"), QMessageBox::Ok);
428 if (aResult && myMode != COPY_FROM)
429 aResult = myLibrary->Save();
432 char* aFileName = myLibrary->GetFileName();
433 getDefaultLibraryName() = QString(aFileName);
435 } else if (myMode != COPY_FROM) {
436 QMessageBox::information(SMESHGUI::desktop(), tr("SMESH_ERROR"),
437 tr("ERROR_OF_SAVING"), QMessageBox::Ok);
444 //=======================================================================
445 // name : SMESHGUI_FilterLibraryDlg::onOk
446 // Purpose : SLOT called when "Ok" button pressed.
447 // Assign filters VTK viewer and close dialog
448 //=======================================================================
449 void SMESHGUI_FilterLibraryDlg::onOk()
453 disconnect( mySMESHGUI, 0, this, 0);
454 mySMESHGUI->ResetState();
459 //=======================================================================
460 // name : SMESHGUI_FilterLibraryDlg::onClose
461 // Purpose : SLOT called when "Close" button pressed. Close dialog
462 //=======================================================================
463 void SMESHGUI_FilterLibraryDlg::onClose()
465 disconnect( mySMESHGUI, 0, this, 0);
466 mySMESHGUI->ResetState();
470 //=================================================================================
471 // function : onHelp()
473 //=================================================================================
474 void SMESHGUI_FilterLibraryDlg::onHelp()
476 LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
478 app->onHelpContextModule(mySMESHGUI ? app->moduleName(mySMESHGUI->moduleName()) : QString(""), myHelpFileName);
482 platform = "winapplication";
484 platform = "application";
486 SUIT_MessageBox::warn1(0, QObject::tr("WRN_WARNING"),
487 QObject::tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
488 arg(app->resourceMgr()->stringValue("ExternalBrowser", platform)).arg(myHelpFileName),
489 QObject::tr("BUT_OK"));
493 //=======================================================================
494 // name : SMESHGUI_FilterLibraryDlg::onDeactivate
495 // Purpose : SLOT called when dialog must be deativated
496 //=======================================================================
497 void SMESHGUI_FilterLibraryDlg::onDeactivate()
502 //=======================================================================
503 // name : SMESHGUI_FilterLibraryDlg::enterEvent
504 // Purpose : Event filter
505 //=======================================================================
506 void SMESHGUI_FilterLibraryDlg::enterEvent(QEvent*)
511 //=================================================================================
512 // function : closeEvent()
513 // purpose : Close dialog
514 //=================================================================================
515 void SMESHGUI_FilterLibraryDlg::closeEvent(QCloseEvent* e)
520 //=======================================================================
521 // name : SMESHGUI_FilterLibraryDlg::getFileName
522 // Purpose : Get file name
523 //=======================================================================
524 QString SMESHGUI_FilterLibraryDlg::getFileName() const
526 return myFileName != 0 ? myFileName->text() : QString("");
529 //================================================================
530 // Function : setFileName
531 // Purpose : Set file name to line edit
532 //================================================================
533 void SMESHGUI_FilterLibraryDlg::setFileName(const QString& txt, const bool autoExt)
537 myFileName->setText(autoExt ? autoExtension(txt) : txt);
540 //================================================================
541 // Function : autoExtension
542 // Purpose : Append extension to the file name
543 //================================================================
544 QString SMESHGUI_FilterLibraryDlg::autoExtension(const QString& theFileName) const
546 QString anExt = theFileName.section('.', -1);
547 return anExt != "xml" && anExt != "XML" ? theFileName + ".xml" : theFileName;
550 //================================================================
551 // Function : filterWildCards
553 //================================================================
554 QStringList SMESHGUI_FilterLibraryDlg::filterWildCards(const QString& theFilter) const
558 int b = theFilter.findRev("(");
559 int e = theFilter.findRev(")");
560 if (b != -1 && e != -1)
562 QString content = theFilter.mid(b + 1, e - b - 1).stripWhiteSpace();
563 QStringList lst = QStringList::split(" ", content);
564 for (QStringList::const_iterator it = lst.begin(); it != lst.end(); ++it)
565 if ((*it).find(".") != -1)
566 res.append((*it).stripWhiteSpace());
571 //=======================================================================
572 // name : SMESHGUI_FilterLibraryDlg::prepareFilters
573 // Purpose : Prepare filters for dialog
574 //=======================================================================
575 QStringList SMESHGUI_FilterLibraryDlg::prepareFilters() const
577 static QStringList aList;
580 aList.append(tr("XML_FILT"));
581 //aList.append(tr("ALL_FILES_FILTER"));
587 //================================================================
588 // Function : onBrowse
589 // Purpose : SLOT. Display "Open file" dialog for chosing library name
590 //================================================================
591 void SMESHGUI_FilterLibraryDlg::onBrowse()
593 Dialog* aDlg = new Dialog(this, true);
594 aDlg->setCaption(tr("OPEN_LIBRARY"));
596 //aDlg->setMode(myMode == COPY_FROM ? QFileDialogP::ExistingFile : QFileDialogP::AnyFile);
597 aDlg->setMode(myMode == COPY_FROM ? QFileDialog::ExistingFile : QFileDialog::AnyFile);
598 aDlg->setFilters(prepareFilters());
599 aDlg->setSelection(getFileName());
601 QPushButton* anOkBtn = (QPushButton*)aDlg->child("OK", "QPushButton");
603 anOkBtn->setText(tr("SMESH_BUT_OK"));
605 if (aDlg->exec() != Accepted)
608 QString fName = aDlg->selectedFile();
613 if (QFileInfo(fName).extension().isEmpty())
614 fName = autoExtension(fName);
616 fName = QDir::convertSeparators(fName);
617 QString prev = QDir::convertSeparators(getFileName());
624 QString aName = myListBox->text(myListBox->count() - 1);
627 if (myMode == ADD_TO)
629 myTable->Copy((SMESHGUI_FilterTable*)parentWidget());
630 myCurrFilterName = "";
632 addFilterToLib(aName);
635 isPermissionValid(false);
638 //=======================================================================
639 // name : SMESHGUI_FilterLibraryDlg::processNewLibrary
640 // Purpose : SLOT. Calleds when file name changed
641 //=======================================================================
642 void SMESHGUI_FilterLibraryDlg::processNewLibrary()
644 SMESH::FilterManager_var aFilterMgr = SMESH::GetFilterManager();
645 if (aFilterMgr->_is_nil())
648 myLibrary = aFilterMgr->LoadLibrary(autoExtension(getFileName()));
649 if (myLibrary->_is_nil()) {
650 if (myMode == COPY_FROM) {
651 QMessageBox::information(SMESHGUI::desktop(), tr("SMESH_ERROR"),
652 tr("ERROR_LOAD"), QMessageBox::Ok);
655 myLibrary = aFilterMgr->CreateLibrary();
656 myLibrary->SetFileName(getFileName().latin1());
663 //=======================================================================
664 // name : SMESHGUI_FilterLibraryDlg::updateList
665 // Purpose : Fill list box with filter names
666 //=======================================================================
667 void SMESHGUI_FilterLibraryDlg::updateList()
670 SMESH::string_array_var aNames = myLibrary->GetNames((SMESH::ElementType)myTable->GetType());
671 for (int i = 0, n = aNames->length(); i < n; i++)
672 aList.append(QString(aNames[ i ]));
674 myListBox->insertStringList(aList);
675 if (myListBox->count() == 0)
677 myTable->Clear(myTable->GetType());
679 myName->setEnabled(false);
680 myTable->SetEnabled(false);
684 myName->setEnabled(true);
685 myTable->SetEnabled(true);
686 if (myListBox->count())
688 myCurrFilterName = "";
689 myListBox->setCurrentItem(0);
694 //=======================================================================
695 // name : SMESHGUI_FilterLibraryDlg::isNameValid
696 // Purpose : Verify validity if entered data
697 //=======================================================================
698 bool SMESHGUI_FilterLibraryDlg::isNameValid(const bool theMess) const
700 // verify validity of filter name
701 if (myName->isEnabled() && !myCurrFilterName.isEmpty()) {
702 QString aCurrName = myName->text();
703 if (aCurrName.isEmpty()) {
705 QMessageBox::information(SMESHGUI::desktop(), tr("SMESH_INSUFFICIENT_DATA"),
706 tr("EMPTY_FILTER_NAME"), QMessageBox::Ok);
710 SMESH::string_array_var aNames = myLibrary->GetAllNames();
711 for (int f = 0, n = aNames->length(); f < n; f++) {
712 if (aNames[ f ] == aCurrName && aNames[ f ] != myCurrFilterName) {
714 QMessageBox::information(SMESHGUI::desktop(), tr("SMESH_INSUFFICIENT_DATA"),
715 tr("ERROR_FILTER_NAME"), QMessageBox::Ok);
724 //=======================================================================
725 // name : SMESHGUI_FilterLibraryDlg::isPermissionValid
726 // Purpose : Verify write permission on file
727 //=======================================================================
728 bool SMESHGUI_FilterLibraryDlg::isPermissionValid(const bool theIsExistingOnly)
730 if (myMode == COPY_FROM)
733 // Verify write permission
734 bool isWritable = false;
736 QString fName(myFileName->text());
737 if (QFileInfo(fName).extension().isEmpty())
738 fName = autoExtension(fName);
740 fName = QDir::convertSeparators(fName);
742 if (QFileInfo(fName).exists()) {
743 isWritable = QFileInfo(fName).isWritable();
744 } else if (!theIsExistingOnly) {
745 QFileInfo aDirInfo(QFileInfo(fName).dirPath(true));
746 isWritable = aDirInfo.isWritable();
747 /*if (QDir(QFileInfo(fName).dirPath(true)).exists() ||
748 QDir().mkdir(QFileInfo(fName).dirPath(true)))
751 if (aFile.open(IO_WriteOnly))
762 QMessageBox::information(SMESHGUI::desktop(), tr("SMESH_WRN_WARNING"),
763 tr("NO_PERMISSION"), QMessageBox::Ok);
770 //=======================================================================
771 // name : SMESHGUI_FilterLibraryDlg::isValid
772 // Purpose : Verify validity if entered data
773 //=======================================================================
774 bool SMESHGUI_FilterLibraryDlg::isValid(const bool theMess) const
776 // verify validity of table
777 if (!myTable->IsValid(theMess) || !isNameValid(theMess))
783 //=======================================================================
784 // name : SMESHGUI_FilterLibraryDlg::onFilterChanged
785 // Purpose : SLOT. Called when selected filter of library changed
786 //=======================================================================
787 void SMESHGUI_FilterLibraryDlg::onFilterChanged(const QString& theName)
789 if (myLibrary->_is_nil())
792 // Save parameters of filter if it was changed
794 if (!myCurrFilterName.isEmpty() && myTable->IsEditable())
798 myListBox->blockSignals(true);
799 myListBox->setCurrentItem(myCurrFilter);
800 myListBox->blockSignals(false);
804 SMESH::Filter_var aFilter = createFilter();
805 myLibrary->Replace(myCurrFilterName.latin1(), myName->text().latin1(), aFilter);
808 // Fill table with filter parameters
810 SMESH::Filter_var aFilter = myLibrary->Copy(theName);
811 myCurrFilterName = theName;
812 myCurrFilter = myListBox->currentItem();
813 myName->setText(theName);
816 SMESH::Filter::Criteria_var aCriteria;
818 myTable->Clear(myTable->GetType());
820 if (!aFilter->GetCriteria(aCriteria))
823 for (int i = 0, n = aCriteria->length(); i < n; i++)
824 myTable->AddCriterion(aCriteria[ i ], myTable->GetType());
829 //=======================================================================
830 // name : SMESHGUI_FilterLibraryDlg::onReturnPressed
831 // Purpose : SLOT. Called when enter button is pressed in library name field
833 //=======================================================================
834 void SMESHGUI_FilterLibraryDlg::onReturnPressed()
836 QString aName = myListBox->text(myListBox->count() - 1);
840 if (myMode == ADD_TO)
842 myTable->Copy((SMESHGUI_FilterTable*)parentWidget());
843 myCurrFilterName = "";
845 addFilterToLib(aName);
848 isPermissionValid(false);
851 //=======================================================================
852 // name : SMESHGUI_FilterLibraryDlg::enableMainButtons
853 // Purpose : Update state of "OK", "Cancel" buttons
854 //=======================================================================
855 void SMESHGUI_FilterLibraryDlg::enableMainButtons()
857 /*bool isEnabled = isValid(false);
858 if (myButtons.contains(BTN_OK))
859 myButtons[ BTN_OK ]->setEnabled(isEnabled);
860 else if (myButtons.contains(BTN_Apply))
861 myButtons[ BTN_OK ]->setEnabled(isEnabled);
862 if (myButtons.contains(BTN_Cancel))
863 myButtons[ BTN_Cancel ]->setEnabled(isEnabled);
864 else if (myButtons.contains(BTN_Close))
865 myButtons[ BTN_Cancel ]->setEnabled(isEnabled);
869 //=======================================================================
870 // name : SMESHGUI_FilterLibraryDlg::createFilter
871 // Purpose : Cerate filter in accordance with library
872 //=======================================================================
873 SMESH::Filter_ptr SMESHGUI_FilterLibraryDlg::createFilter(const int theType)
875 int n = myTable->NumRows(theType);
877 SMESH::Filter::Criteria_var aCriteria = new SMESH::Filter::Criteria;
878 aCriteria->length(n);
880 for (int i = 0; i < n; i++)
882 SMESH::Filter::Criterion aCriterion = SMESHGUI_FilterDlg::createCriterion();
883 myTable->GetCriterion(i, aCriterion);
884 aCriteria[ i ] = aCriterion;
887 SMESH::FilterManager_var aFilterMgr = SMESH::GetFilterManager();
888 SMESH::Filter_var aFilter = aFilterMgr->CreateFilter();
889 aFilter->SetCriteria(aCriteria.in());
891 return aFilter._retn();
894 //=======================================================================
895 // name : SMESHGUI_FilterLibraryDlg::onAddBtnPressed
896 // Purpose : SLOT. Called when "Add" button pressed
897 // Add new filter to the end of library
898 //=======================================================================
899 void SMESHGUI_FilterLibraryDlg::onAddBtnPressed()
901 // Save parameters of filter if it was changed
902 if (!myCurrFilterName.isEmpty() && myTable->IsEditable())
907 SMESH::Filter_var aFilter = createFilter();
908 myLibrary->Replace(myCurrFilterName.latin1(), myName->text().latin1(), aFilter);
911 addFilterToLib(getDefaultFilterName());
914 //=======================================================================
915 // name : onAddBtnPressed()
916 // Purpose : SLOT. Called when "Add" button pressed
917 // Add new filter to the end of library
918 //=======================================================================
919 void SMESHGUI_FilterLibraryDlg::addFilterToLib (const QString& theName)
921 if (myLibrary->_is_nil()) {
922 QMessageBox::information(SMESHGUI::desktop(), tr("SMESH_WRN_WARNING"),
923 tr("LIBRARY_IS_NOT_LOADED"), QMessageBox::Ok);
928 SMESH::Filter_var aFilter = createFilter();
930 // if name of filter already exist in the library assign default name for the filter
931 QString aName(theName);
932 SMESH::string_array_var aNames = myLibrary->GetAllNames();
933 for (int i = 0, n = aNames->length(); i < n; i++)
934 if (aName == QString(aNames[ i ]))
936 aName = getDefaultFilterName();
940 // add new filter in library
941 bool aResult = !aFilter->GetPredicate()->_is_nil()
942 ? myLibrary->Add(aName.latin1(), aFilter)
943 : myLibrary->AddEmpty(aName.latin1(), (SMESH::ElementType)myTable->GetType());
946 QMessageBox::information(SMESHGUI::desktop(), tr("SMESH_ERROR"),
947 tr("ERROR_OF_ADDING"), QMessageBox::Ok);
951 myCurrFilterName = "";
955 if (theName != aName)
956 QMessageBox::information(SMESHGUI::desktop(), tr("SMESH_WARNING"),
957 tr("ASSIGN_NEW_NAME").arg(theName).arg(aName), QMessageBox::Ok);
960 //=======================================================================
961 // name : SMESHGUI_FilterLibraryDlg::getDefaultLibraryName
962 // Purpose : Get default library name
963 //=======================================================================
964 QString& SMESHGUI_FilterLibraryDlg::getDefaultLibraryName() const
966 static QString aName;
969 QString aHomeDir = QDir(QDir::home()).absPath();
970 aName = aHomeDir + "/" + tr ("LIB_NAME");
975 //=======================================================================
976 // name : SMESHGUI_FilterLibraryDlg::getDefaultFilterName
977 // Purpose : Create default filter name
978 //=======================================================================
979 QString SMESHGUI_FilterLibraryDlg::getDefaultFilterName() const
983 if (myTable->GetType() == SMESH::NODE)
985 else if (myTable->GetType() == SMESH::EDGE)
987 else if (myTable->GetType() == SMESH::FACE)
989 else if (myTable->GetType() == SMESH::VOLUME)
990 aName = tr("VOLUME");
991 else if (myTable->GetType() == SMESH::ALL)
992 aName = tr("ELEMENT");
994 aName = tr("SELECTION");
996 aName += tr("FILTER");
999 QMap< QString, int > anAllNames;
1000 SMESH::string_array_var aNames = myLibrary->GetAllNames();
1001 for(int i = 0, n = aNames->length(); i < n; i++)
1002 anAllNames[ QString(aNames[ i ]) ] = -1;
1004 bool isNotValid = true;
1010 aNewName = aName + "_" + QString("%1").arg(k);
1011 if (anAllNames.contains(aNewName))
1021 //=======================================================================
1022 // name : SMESHGUI_FilterLibraryDlg::setSelected
1023 // Purpose : set selected item in list box containing filters
1024 //=======================================================================
1025 bool SMESHGUI_FilterLibraryDlg::setSelected(const QString& theName)
1027 int anIndex = getIndex(theName);
1030 myListBox->setCurrentItem(anIndex);
1031 myCurrFilterName = theName;
1032 myCurrFilter = anIndex;
1034 return anIndex != -1;
1037 //=======================================================================
1038 // name : SMESHGUI_FilterLibraryDlg::getIndex
1039 // Purpose : Get index of the filter in list box
1040 //=======================================================================
1041 int SMESHGUI_FilterLibraryDlg::getIndex(const QString& theName) const
1043 for (int i = 0, n = myListBox->count(); i < n; i++)
1044 if (myListBox->text(i) == theName)
1049 //=======================================================================
1050 // name : SMESHGUI_FilterLibraryDlg::onDeleteBtnPressed
1051 // Purpose : SLOT. Called when "Delete" button pressed
1052 //=======================================================================
1053 void SMESHGUI_FilterLibraryDlg::onDeleteBtnPressed()
1055 if (myLibrary->_is_nil()) {
1056 QMessageBox::information(SMESHGUI::desktop(), tr("SMESH_WRN_WARNING"),
1057 tr("LIBRARY_IS_NOT_LOADED"), QMessageBox::Ok);
1061 int anIndex = getIndex(myCurrFilterName);
1063 if (anIndex == -1 || !myLibrary->Delete(myCurrFilterName.latin1())) {
1064 QMessageBox::information(SMESHGUI::desktop(), tr("SMESH_ERROR"),
1065 tr("ERROR_OF_DELETING"), QMessageBox::Ok);
1067 myCurrFilterName = "";
1069 myListBox->removeItem(anIndex);
1072 myListBox->setSelected(anIndex - 1, true);
1073 else if (anIndex == 0 && myListBox->count() > 0)
1074 myListBox->setSelected(0, true);
1079 myTable->SetEnabled(myListBox->count() > 0);
1080 if (myListBox->count() == 0) {
1081 myName->setText("");
1082 myName->setEnabled(false);
1086 //=======================================================================
1087 // name : onFilterNameChanged()
1088 // Purpose : SLOT. Called when name of filter changed
1089 // Change filter name in list box
1090 //=======================================================================
1091 void SMESHGUI_FilterLibraryDlg::onFilterNameChanged (const QString& theName)
1093 int aCurrItem = myListBox->currentItem();
1094 if (aCurrItem == -1)
1097 myListBox->blockSignals(true);
1098 myListBox->changeItem(theName, aCurrItem);
1099 myListBox->blockSignals(false);
1102 //=======================================================================
1103 // name : SMESHGUI_FilterLibraryDlg::SetTable
1104 // Purpose : Set table
1105 //=======================================================================
1106 void SMESHGUI_FilterLibraryDlg::SetTable(const SMESHGUI_FilterTable* theTable)
1108 myTable->Copy(theTable);
1109 myName->setText(getDefaultFilterName());
1110 addFilterToLib(myName->text());
1114 //=======================================================================
1115 // name : SMESHGUI_FilterLibraryDlg::GetTable
1116 // Purpose : Get table
1117 //=======================================================================
1118 const SMESHGUI_FilterTable* SMESHGUI_FilterLibraryDlg::GetTable() const
1124 //=======================================================================
1125 // name : SMESHGUI_FilterLibraryDlg::onEntityTypeChanged
1126 // Purpose : SLOT. Called when entiyt type changed
1127 //=======================================================================
1128 void SMESHGUI_FilterLibraryDlg::onEntityTypeChanged(const int theType)
1130 if (myLibrary->_is_nil())
1134 myCurrFilterName = "";
1137 if (myListBox->count())
1138 myListBox->setCurrentItem(0);
1141 //=======================================================================
1142 // name : SMESHGUI_FilterLibraryDlg::onNeedValidation
1144 //=======================================================================
1145 void SMESHGUI_FilterLibraryDlg::onNeedValidation()
1147 if (!myCurrFilterName.isEmpty())
1149 bool valid = isValid(true);
1150 myTable->SetValidity(valid);
1154 SMESH::Filter_var aFilter = createFilter(myTable->GetType());
1155 myLibrary->Replace(myCurrFilterName.latin1(), myName->text().latin1(), aFilter);
1160 //=================================================================================
1161 // function : keyPressEvent()
1163 //=================================================================================
1164 void SMESHGUI_FilterLibraryDlg::keyPressEvent( QKeyEvent* e )
1166 QDialog::keyPressEvent( e );
1167 if ( e->isAccepted() )
1170 if ( e->key() == Key_F1 )