Back to index

salome-smesh  6.5.0
SMESHGUI_FilterLibraryDlg.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 // SMESH SMESHGUI : GUI for SMESH component
00024 // File   : SMESHGUI_FilterLibraryDlg.cxx
00025 // Author : Sergey LITONIN, Open CASCADE S.A.S.
00026 // SMESH includes
00027 //
00028 #include "SMESHGUI_FilterLibraryDlg.h"
00029 
00030 #include "SMESHGUI.h"
00031 #include "SMESHGUI_FilterUtils.h"
00032 #include "SMESHGUI_FilterDlg.h"
00033 
00034 // SALOME GUI includes
00035 #include <SUIT_Session.h>
00036 #include <SUIT_Desktop.h>
00037 #include <SUIT_FileDlg.h>
00038 #include <SUIT_MessageBox.h>
00039 #include <SUIT_ResourceMgr.h>
00040 
00041 #include <LightApp_Application.h>
00042 
00043 // Qt includes
00044 #include <QApplication>
00045 #include <QVBoxLayout>
00046 #include <QHBoxLayout>
00047 #include <QGridLayout>
00048 #include <QLineEdit>
00049 #include <QPushButton>
00050 #include <QGroupBox>
00051 #include <QLabel>
00052 #include <QListWidget>
00053 #include <QFileInfo>
00054 #include <QDir>
00055 #include <QKeyEvent>
00056 
00057 #define SPACING 6
00058 #define MARGIN  11
00059 
00065 class SMESHGUI_FilterLibraryDlg::Dialog : public SUIT_FileDlg
00066 {
00067 public:
00068   Dialog(QWidget* theParent, const bool theToOpen);
00069   virtual ~Dialog();
00070 
00071 protected:
00072   virtual bool acceptData();
00073 };
00074 
00075 SMESHGUI_FilterLibraryDlg::Dialog::Dialog (QWidget*   theParent,
00076                                            const bool theToOpen)
00077   : SUIT_FileDlg(theParent, theToOpen)
00078 {
00079 }
00080 
00081 SMESHGUI_FilterLibraryDlg::Dialog::~Dialog()
00082 {
00083 }
00084 
00085 bool SMESHGUI_FilterLibraryDlg::Dialog::acceptData()
00086 {
00087 //  if (mode() != QFileDialogP::AnyFile)
00088 //    return SUIT_FileDlg::acceptData();
00089 
00090   return true;
00091 }
00092 
00098 //=======================================================================
00099 // name    : SMESHGUI_FilterLibraryDlg::SMESHGUI_FilterLibraryDlg
00100 // Purpose : Constructor
00101 //=======================================================================
00102 SMESHGUI_FilterLibraryDlg::SMESHGUI_FilterLibraryDlg (SMESHGUI* theModule,
00103                                                       QWidget* parent,
00104                                                       const QList<int>& theTypes,
00105                                                       const int theMode)
00106   : QDialog( parent ),
00107     mySMESHGUI( theModule )
00108 {
00109   setModal(false);
00110   construct(theTypes, theMode);
00111 }
00112 
00113 //=======================================================================
00114 // name    : SMESHGUI_FilterLibraryDlg::SMESHGUI_FilterLibraryDlg
00115 // Purpose : Constructor
00116 //=======================================================================
00117 SMESHGUI_FilterLibraryDlg::SMESHGUI_FilterLibraryDlg (SMESHGUI* theModule,
00118                                                       QWidget* parent,
00119                                                       const int   theType,
00120                                                       const int   theMode)
00121   : QDialog( parent ),
00122     mySMESHGUI( theModule )
00123 {
00124   setModal(true);
00125   QList<int> aTypes;
00126   aTypes.append(theType);
00127   construct(aTypes, theMode);
00128 }
00129 
00130 //=======================================================================
00131 // name    : SMESHGUI_FilterLibraryDlg::construct
00132 // Purpose : Construct dialog (called by constructor)
00133 //=======================================================================
00134 void SMESHGUI_FilterLibraryDlg::construct (const QList<int>& theTypes,
00135                                            const int theMode)
00136 {
00137   myTypes = theTypes;
00138   myMode  = theMode;
00139 
00140   QVBoxLayout* aDlgLay = new QVBoxLayout(this);
00141   aDlgLay->setMargin(MARGIN);
00142   aDlgLay->setSpacing(SPACING);
00143 
00144   myMainFrame        = createMainFrame  (this);
00145   QWidget* aBtnFrame = createButtonFrame(this);
00146 
00147   aDlgLay->addWidget(myMainFrame);
00148   aDlgLay->addWidget(aBtnFrame);
00149 
00150   aDlgLay->setStretchFactor(myMainFrame, 1);
00151 
00152   myHelpFileName = "selection_filter_library_page.html";
00153   
00154   Init(myTypes, myMode);
00155 }
00156 
00157 //=======================================================================
00158 // name    : SMESHGUI_FilterLibraryDlg::createMainFrame
00159 // Purpose : Create frame containing dialog's input fields
00160 //=======================================================================
00161 QWidget* SMESHGUI_FilterLibraryDlg::createMainFrame (QWidget* theParent)
00162 {
00163   QWidget* aMainFrame = new QWidget(theParent);
00164   QGridLayout* aMainLay = new QGridLayout(aMainFrame);
00165   aMainLay->setMargin(0);
00166   aMainLay->setSpacing(SPACING);
00167 
00168   // library name
00169 
00170   QLabel* aFileLab = new QLabel(tr("LIBRARY_FILE"), aMainFrame);
00171   myFileName = new QLineEdit(aMainFrame);
00172   myOpenBtn = new QPushButton(aMainFrame);
00173   myOpenBtn->setIcon(SUIT_Session::session()->resourceMgr()->loadPixmap(
00174     "SUIT", tr("ICON_FILE_OPEN")));
00175 
00176   // filters list box
00177 
00178   QGroupBox* aFiltersGrp = new QGroupBox(tr("FILTER_NAMES"), aMainFrame);
00179   QGridLayout* aLay = new QGridLayout(aFiltersGrp);
00180   aLay->setMargin(MARGIN);
00181   aLay->setSpacing(SPACING);
00182 
00183   myListBox = new QListWidget(aFiltersGrp);
00184 
00185   myAddBtn    = new QPushButton(tr("ADD"), aFiltersGrp);
00186   myDeleteBtn = new QPushButton(tr("DELETE"), aFiltersGrp);
00187 
00188   aLay->addWidget(myListBox,   0, 0, 3, 1);
00189   aLay->addWidget(myAddBtn,    0, 1);
00190   aLay->addWidget(myDeleteBtn, 1, 1);
00191   aLay->setRowStretch(2, 5);
00192 
00193   // filter name
00194 
00195   myNameGrp = new QWidget(aMainFrame);
00196   QHBoxLayout* myNameGrpLayout = new QHBoxLayout(myNameGrp);
00197   myNameGrpLayout->setMargin(0);
00198   myNameGrpLayout->setSpacing(SPACING);
00199 
00200   myNameGrpLayout->addWidget( new QLabel(tr("FILTER_NAME"), myNameGrp) );
00201   myNameGrpLayout->addWidget( myName = new QLineEdit(myNameGrp) );
00202 
00203   // table
00204 
00205   myTable = new SMESHGUI_FilterTable( mySMESHGUI, aMainFrame, myTypes);
00206   myTable->SetEditable(myMode == EDIT);
00207   myTable->SetLibsEnabled(false);
00208 
00209   myListBox->setMinimumHeight((int)(myTable->sizeHint().height() * 0.5));
00210   //myListBox->setRowMode(QListWidget::FitToWidth); //VSR : TODO ???
00211   myListBox->setSelectionMode(QListWidget::SingleSelection);
00212 
00213   myOpenBtn->setAutoDefault(false);
00214   myAddBtn->setAutoDefault(false);
00215   myDeleteBtn->setAutoDefault(false);
00216 
00217   aMainLay->addWidget(aFileLab,    0, 0);
00218   aMainLay->addWidget(myFileName,  0, 1);
00219   aMainLay->addWidget(myOpenBtn,   0, 2);
00220   aMainLay->addWidget(aFiltersGrp, 1, 0, 1, 3);
00221   aMainLay->addWidget(myNameGrp,   2, 0, 1, 3);
00222   aMainLay->addWidget(myTable,     3, 0, 1, 3);
00223 
00224   // connect signals and slots
00225 
00226   connect(myFileName, SIGNAL(returnPressed()), this, SLOT(onReturnPressed()));
00227   connect(myOpenBtn, SIGNAL(clicked()), this, SLOT(onBrowse()));
00228 
00229   connect(myListBox, SIGNAL(itemSelectionChanged()),
00230           this, SLOT(onFilterChanged()));
00231 
00232   connect(myAddBtn, SIGNAL(clicked()), this, SLOT(onAddBtnPressed()));
00233   connect(myDeleteBtn, SIGNAL(clicked()), this, SLOT(onDeleteBtnPressed()));
00234 
00235   connect(myName, SIGNAL(textChanged(const QString&)),
00236            this, SLOT(onFilterNameChanged(const QString&)));
00237 
00238   connect(myTable, SIGNAL(EntityTypeChanged(const int)),
00239            this, SLOT(onEntityTypeChanged(const int)));
00240 
00241   connect(myTable, SIGNAL(NeedValidation()), this, SLOT(onNeedValidation()));
00242 
00243   return aMainFrame;
00244 }
00245 
00246 //=======================================================================
00247 // name    : SMESHGUI_FilterLibraryDlg::createButtonFrame
00248 // Purpose : Create frame containing buttons
00249 //=======================================================================
00250 QWidget* SMESHGUI_FilterLibraryDlg::createButtonFrame (QWidget* theParent)
00251 {
00252   QGroupBox* aGrp = new QGroupBox(theParent);
00253   QHBoxLayout* aLay = new QHBoxLayout(aGrp);
00254   aLay->setMargin(MARGIN);
00255   aLay->setSpacing(SPACING);
00256 
00257   myButtons[ BTN_OK    ] = new QPushButton(tr("SMESH_BUT_APPLY_AND_CLOSE"), aGrp);
00258   myButtons[ BTN_Apply ] = new QPushButton(tr("SMESH_BUT_APPLY"), aGrp);
00259 
00260   myButtons[ BTN_Close  ] = new QPushButton(tr("SMESH_BUT_CLOSE"),  aGrp);
00261   myButtons[ BTN_Help   ] = new QPushButton(tr("SMESH_BUT_HELP"),   aGrp);
00262 
00263   aLay->addWidget(myButtons[ BTN_OK     ]);
00264   aLay->addSpacing(10);
00265   aLay->addWidget(myButtons[ BTN_Apply  ]);
00266   aLay->addSpacing(10);
00267   aLay->addStretch();
00268   aLay->addWidget(myButtons[ BTN_Close  ]);
00269   aLay->addWidget(myButtons[ BTN_Help   ]);
00270 
00271   connect(myButtons[ BTN_OK     ], SIGNAL(clicked()), SLOT(onOk()));
00272   connect(myButtons[ BTN_Close  ], SIGNAL(clicked()), SLOT(onClose()));
00273   connect(myButtons[ BTN_Apply  ], SIGNAL(clicked()), SLOT(onApply()));
00274   connect(myButtons[ BTN_Help   ], SIGNAL(clicked()), SLOT(onHelp()));
00275 
00276   QMap<int, QPushButton*>::iterator anIter;
00277   for (anIter = myButtons.begin(); anIter != myButtons.end(); ++anIter)
00278     anIter.value()->setAutoDefault(false);
00279 
00280   updateMainButtons();
00281 
00282   return aGrp;
00283 }
00284 
00285 //=======================================================================
00286 // name    : SMESHGUI_FilterLibraryDlg::updateMainButtons
00287 // Purpose : Update visibility of main buttons (OK, Cancel, Close ...)
00288 //=======================================================================
00289 void SMESHGUI_FilterLibraryDlg::updateMainButtons()
00290 {
00291   myButtons[ BTN_Close  ]->show();
00292   if (myTypes.count() == 1) {
00293     myButtons[ BTN_Apply  ]->hide();
00294   } else {
00295     myButtons[ BTN_Apply  ]->show();
00296   }
00297 }
00298 
00299 //=======================================================================
00300 // name    : SMESHGUI_FilterLibraryDlg::~SMESHGUI_FilterLibraryDlg
00301 // Purpose : Destructor
00302 //=======================================================================
00303 SMESHGUI_FilterLibraryDlg::~SMESHGUI_FilterLibraryDlg()
00304 {
00305 }
00306 
00307 //=======================================================================
00308 // name    : SMESHGUI_FilterLibraryDlg::Init
00309 // Purpose : Init dialog fields, connect signals and slots, show dialog
00310 //=======================================================================
00311 void SMESHGUI_FilterLibraryDlg::Init (const int type, const int theMode)
00312 {
00313   QList<int> aTypes;
00314   aTypes.append(type);
00315   Init(aTypes, theMode);
00316 }
00317 
00318 //=======================================================================
00319 // name    : SMESHGUI_FilterLibraryDlg::Init
00320 // Purpose : Init dialog fields, connect signals and slots, show dialog
00321 //=======================================================================
00322 void SMESHGUI_FilterLibraryDlg::Init (const QList<int>& theTypes,
00323                                       const int theMode)
00324 {
00325   myMode = theMode;
00326   myTypes = theTypes;
00327   myTable->Init(theTypes);
00328   myCurrFilterName = "";
00329   myCurrFilter = -1;
00330   myListBox->blockSignals(true);
00331   myListBox->clear();
00332   myListBox->blockSignals(false);
00333   myName->clear();
00334   myTable->Clear();
00335 
00336   updateControlsVisibility();
00337   setEnabled(true);
00338 
00339   connect( mySMESHGUI, SIGNAL(SignalDeactivateActiveDialog()), SLOT(onDeactivate()));
00340   connect( mySMESHGUI, SIGNAL(SignalCloseAllDialogs()), SLOT(onClose()));
00341 
00342   if (myMode == ADD_TO)
00343   {
00344     setWindowTitle(tr("ADD_TO_TLT"));
00345     if (myFileName->text().isEmpty())
00346       myFileName->setText(getDefaultLibraryName());
00347     processNewLibrary();
00348   }
00349   else if (myMode == COPY_FROM)
00350   {
00351     setWindowTitle(tr("COPY_FROM_TLT"));
00352     if (myFileName->text().isEmpty())
00353       myFileName->setText(getDefaultLibraryName());
00354     processNewLibrary();
00355     if (myListBox->count() > 0)
00356       myListBox->setCurrentItem(0);
00357   }
00358   else
00359   {
00360     setWindowTitle(tr("EDIT_LIB_TLT"));
00361     if (myFileName->text().isEmpty())
00362       myFileName->setText(getDefaultLibraryName());
00363     processNewLibrary();
00364     if (myListBox->count() > 0)
00365       myListBox->setCurrentItem(0);
00366   }
00367 
00368   this->show();
00369 
00370   updateMainButtons();
00371   isPermissionValid(false);
00372 }
00373 
00374 //=======================================================================
00375 // name    : SMESHGUI_FilterLibraryDlg::updateControlsVisibility
00376 // Purpose : Update visibility of controls in accordance with myMode
00377 //=======================================================================
00378 void SMESHGUI_FilterLibraryDlg::updateControlsVisibility()
00379 {
00380   if (myMode == ADD_TO)
00381   {
00382     myNameGrp->show();
00383     myNameGrp->setEnabled(true);
00384     myAddBtn->hide();
00385     myDeleteBtn->hide();
00386     myTable->SetEditable(false);
00387   }
00388   else if (myMode == COPY_FROM)
00389   {
00390     myNameGrp->hide();
00391     myNameGrp->setEnabled(false);
00392     myAddBtn->hide();
00393     myDeleteBtn->hide();
00394     myTable->SetEditable(false);
00395   }
00396   else if (myMode == EDIT)
00397   {
00398     myNameGrp->show();
00399     myNameGrp->setEnabled(true);
00400     myAddBtn->show();
00401     myDeleteBtn->show();
00402     myTable->SetEditable(true);
00403   }
00404 
00405   qApp->processEvents();
00406   updateGeometry();
00407   adjustSize();
00408 }
00409 
00410 //=======================================================================
00411 // name    : SMESHGUI_FilterLibraryDlg::onApply
00412 // Purpose : SLOT called when "Apply" button pressed.
00413 //=======================================================================
00414 bool SMESHGUI_FilterLibraryDlg::onApply()
00415 {
00416   if (!isValid(true) || !isPermissionValid(false))
00417     return false;
00418 
00419   if (myLibrary->_is_nil()) {
00420     SUIT_MessageBox::information(SMESHGUI::desktop(), tr("SMESH_WRN_WARNING"),
00421                                  tr("LIBRARY_IS_NOT_LOADED"));
00422     return false;
00423   }
00424 
00425   if (myFileName->text() != myLibrary->GetFileName())
00426     myLibrary->SetFileName( myFileName->text().toLatin1().constData() );
00427 
00428   bool aResult = false;
00429 
00430   if (myMode == COPY_FROM || myListBox->count() == 0) {
00431     aResult = true;
00432   } else if (myMode == EDIT || myMode == ADD_TO) {
00433     SMESH::Filter_var aFilter = createFilter();
00434     if (!myListBox->selectedItems().empty() && 
00435         !myLibrary->Replace(myCurrFilterName.toLatin1().constData(),
00436                             myName->text().toLatin1().constData(),
00437                             aFilter.in())) {
00438       SUIT_MessageBox::information(SMESHGUI::desktop(), tr("SMESH_ERROR"),
00439                                    tr("ERROR_OF_EDITING"));
00440       aResult = false;
00441     }
00442     else
00443       aResult = true;
00444   }
00445 
00446   if (aResult && myMode != COPY_FROM)
00447     aResult = myLibrary->Save();
00448 
00449   if (aResult) {
00450     char* aFileName = myLibrary->GetFileName();
00451     getDefaultLibraryName() = QString(aFileName);
00452     delete aFileName;
00453   } else if (myMode != COPY_FROM) {
00454     SUIT_MessageBox::information(SMESHGUI::desktop(), tr("SMESH_ERROR"),
00455                                  tr("ERROR_OF_SAVING"));
00456   } else {
00457   }
00458 
00459   return aResult;
00460 }
00461 
00462 //=======================================================================
00463 // name    : SMESHGUI_FilterLibraryDlg::onOk
00464 // Purpose : SLOT called when "Ok" button pressed.
00465 //           Assign filters VTK viewer and close dialog
00466 //=======================================================================
00467 void SMESHGUI_FilterLibraryDlg::onOk()
00468 {
00469   if (onApply())
00470   {
00471     disconnect( mySMESHGUI, 0, this, 0);
00472     mySMESHGUI->ResetState();
00473     accept();
00474   }
00475 }
00476 
00477 //=======================================================================
00478 // name    : SMESHGUI_FilterLibraryDlg::onClose
00479 // Purpose : SLOT called when "Close" button pressed. Close dialog
00480 //=======================================================================
00481 void SMESHGUI_FilterLibraryDlg::onClose()
00482 {
00483   disconnect( mySMESHGUI, 0, this, 0);
00484   mySMESHGUI->ResetState();
00485   reject();
00486 }
00487 
00488 //=================================================================================
00489 // function : onHelp()
00490 // purpose  :
00491 //=================================================================================
00492 void SMESHGUI_FilterLibraryDlg::onHelp()
00493 {
00494   LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
00495   if (app) 
00496     app->onHelpContextModule(mySMESHGUI ? app->moduleName(mySMESHGUI->moduleName()) : QString(""), myHelpFileName);
00497   else {
00498     QString platform;
00499 #ifdef WIN32
00500     platform = "winapplication";
00501 #else
00502     platform = "application";
00503 #endif
00504     SUIT_MessageBox::warning(this, tr("WRN_WARNING"),
00505                              tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
00506                              arg(app->resourceMgr()->stringValue("ExternalBrowser", 
00507                                                                  platform)).
00508                              arg(myHelpFileName));
00509   }
00510 }
00511 
00512 //=======================================================================
00513 // name    : SMESHGUI_FilterLibraryDlg::onDeactivate
00514 // Purpose : SLOT called when dialog must be deativated
00515 //=======================================================================
00516 void SMESHGUI_FilterLibraryDlg::onDeactivate()
00517 {
00518   setEnabled(false);
00519 }
00520 
00521 //=======================================================================
00522 // name    : SMESHGUI_FilterLibraryDlg::enterEvent
00523 // Purpose : Event filter
00524 //=======================================================================
00525 void SMESHGUI_FilterLibraryDlg::enterEvent(QEvent*)
00526 {
00527   setEnabled(true);
00528 }
00529 
00530 //=================================================================================
00531 // function : closeEvent()
00532 // purpose  : Close dialog
00533 //=================================================================================
00534 void SMESHGUI_FilterLibraryDlg::closeEvent(QCloseEvent* e)
00535 {
00536   onClose();
00537 }
00538 
00539 //=======================================================================
00540 // name    : SMESHGUI_FilterLibraryDlg::getFileName
00541 // Purpose : Get file name
00542 //=======================================================================
00543 QString SMESHGUI_FilterLibraryDlg::getFileName() const
00544 {
00545   return myFileName != 0 ? myFileName->text() : QString("");
00546 }
00547 
00548 //================================================================
00549 // Function : setFileName
00550 // Purpose  : Set file name to line edit
00551 //================================================================
00552 void SMESHGUI_FilterLibraryDlg::setFileName(const QString& txt, const bool autoExt)
00553 {
00554   if (myFileName == 0)
00555     return;
00556   myFileName->setText(autoExt ? autoExtension(txt) : txt);
00557 }
00558 
00559 //================================================================
00560 // Function : autoExtension
00561 // Purpose  : Append extension to the file name
00562 //================================================================
00563 QString SMESHGUI_FilterLibraryDlg::autoExtension(const QString& theFileName) const
00564 {
00565   QString anExt = theFileName.section('.', -1);
00566   return anExt != "xml" && anExt != "XML" ? theFileName + ".xml" : theFileName;
00567 }
00568 
00569 //================================================================
00570 // Function : filterWildCards
00571 // Purpose  :
00572 //================================================================
00573 QStringList SMESHGUI_FilterLibraryDlg::filterWildCards(const QString& theFilter) const
00574 {
00575   QStringList res;
00576 
00577   int b = theFilter.lastIndexOf("(");
00578   int e = theFilter.lastIndexOf(")");
00579   if (b != -1 && e != -1)
00580   {
00581     QString content = theFilter.mid(b + 1, e - b - 1).trimmed();
00582     QStringList lst = content.split(" ", QString::SkipEmptyParts);
00583     for (QStringList::const_iterator it = lst.begin(); it != lst.end(); ++it)
00584       if ((*it).indexOf(".") != -1)
00585         res.append((*it).trimmed());
00586   }
00587   return res;
00588 }
00589 
00590 //=======================================================================
00591 // name    : SMESHGUI_FilterLibraryDlg::prepareFilters
00592 // Purpose : Prepare filters for dialog
00593 //=======================================================================
00594 QStringList SMESHGUI_FilterLibraryDlg::prepareFilters() const
00595 {
00596   static QStringList aList;
00597   if (aList.isEmpty())
00598   {
00599     aList.append(tr("XML_FILT"));
00600     //aList.append(tr("ALL_FILES_FILTER"));
00601   }
00602 
00603   return aList;
00604 }
00605 
00606 //================================================================
00607 // Function : onBrowse
00608 // Purpose  : SLOT. Display "Open file" dialog for chosing library name
00609 //================================================================
00610 void SMESHGUI_FilterLibraryDlg::onBrowse()
00611 {
00612   Dialog* aDlg = new Dialog(this, true);
00613   aDlg->setWindowTitle(tr("OPEN_LIBRARY"));
00614 
00615   //aDlg->setMode(myMode == COPY_FROM ? QFileDialogP::ExistingFile : QFileDialogP::AnyFile);
00616   aDlg->setFileMode(myMode == COPY_FROM ? QFileDialog::ExistingFile : QFileDialog::AnyFile);
00617   aDlg->setFilters(prepareFilters());
00618   aDlg->selectFile(getFileName());
00619 
00620   QPushButton* anOkBtn = (QPushButton*)aDlg->findChild<QPushButton*>("OK");
00621   if (anOkBtn != 0)
00622     anOkBtn->setText(tr("SMESH_BUT_OK"));
00623 
00624   if (aDlg->exec() != Accepted)
00625     return;
00626 
00627   QString fName = aDlg->selectedFile();
00628 
00629   if (fName.isEmpty())
00630     return;
00631 
00632   if (QFileInfo(fName).suffix().isEmpty())
00633     fName = autoExtension(fName);
00634 
00635   fName = QDir::convertSeparators(fName);
00636   QString prev = QDir::convertSeparators(getFileName());
00637 
00638   if (prev == fName)
00639     return;
00640 
00641   setFileName(fName);
00642 
00643   QListWidgetItem* item = myListBox->item( myListBox->count()-1 );
00644   QString aName = item ? item->text() : QString::null;
00645   processNewLibrary();
00646 
00647   if (myMode == ADD_TO)
00648   {
00649     myTable->Copy((SMESHGUI_FilterTable*)parentWidget());
00650     myCurrFilterName = "";
00651     myCurrFilter = -1;
00652     addFilterToLib(aName);
00653   }
00654 
00655   isPermissionValid(false);
00656 }
00657 
00658 //=======================================================================
00659 // name    : SMESHGUI_FilterLibraryDlg::processNewLibrary
00660 // Purpose : SLOT. Calleds when file name changed
00661 //=======================================================================
00662 void SMESHGUI_FilterLibraryDlg::processNewLibrary()
00663 {
00664   SMESH::FilterManager_var aFilterMgr = SMESH::GetFilterManager();
00665   if (aFilterMgr->_is_nil())
00666     return;
00667 
00668   myLibrary = aFilterMgr->LoadLibrary(autoExtension(getFileName()).toLatin1().constData());
00669   if (myLibrary->_is_nil()) {
00670     if (myMode == COPY_FROM) {
00671       SUIT_MessageBox::information(SMESHGUI::desktop(), tr("SMESH_ERROR"),
00672                                    tr("ERROR_LOAD"));
00673       return;
00674     } else {
00675       myLibrary = aFilterMgr->CreateLibrary();
00676       myLibrary->SetFileName(getFileName().toLatin1().constData());
00677     }
00678   }
00679 
00680   updateList();
00681 }
00682 
00683 //=======================================================================
00684 // name    : SMESHGUI_FilterLibraryDlg::updateList
00685 // Purpose : Fill list box with filter names
00686 //=======================================================================
00687 void SMESHGUI_FilterLibraryDlg::updateList()
00688 {
00689   QStringList aList;
00690   SMESH::string_array_var aNames = myLibrary->GetNames((SMESH::ElementType)myTable->GetType());
00691   for (int i = 0, n = aNames->length(); i < n; i++)
00692     aList.append(QString(aNames[ i ]));
00693   myListBox->blockSignals(true);
00694   myListBox->clear();
00695   myListBox->blockSignals(false);
00696   myListBox->addItems(aList);
00697   if (myListBox->count() == 0)
00698   {
00699     myTable->Clear(myTable->GetType());
00700     myName->clear();
00701     myName->setEnabled(false);
00702     myTable->SetEnabled(false);
00703   }
00704   else
00705   {
00706     myName->setEnabled(true);
00707     myTable->SetEnabled(true);
00708     if (myListBox->count())
00709     {
00710       myCurrFilterName = "";
00711       myListBox->setCurrentItem(0);
00712     }
00713   }
00714 }
00715 
00716 //=======================================================================
00717 // name    : SMESHGUI_FilterLibraryDlg::isNameValid
00718 // Purpose : Verify validity if entered data
00719 //=======================================================================
00720 bool SMESHGUI_FilterLibraryDlg::isNameValid(const bool theMess) const
00721 {
00722   // verify validity of filter name
00723   if (myName->isEnabled() && !myCurrFilterName.isEmpty()) {
00724     QString aCurrName = myName->text();
00725     if (aCurrName.isEmpty()) {
00726       if (theMess)
00727         SUIT_MessageBox::information(SMESHGUI::desktop(), tr("SMESH_INSUFFICIENT_DATA"),
00728                                      tr("EMPTY_FILTER_NAME"));
00729       return false;
00730     }
00731 
00732     SMESH::string_array_var aNames = myLibrary->GetAllNames();
00733     for (int f = 0, n = aNames->length(); f < n; f++) {
00734       if (aNames[ f ] == aCurrName && aNames[ f ] != myCurrFilterName) {
00735         if (theMess)
00736           SUIT_MessageBox::information(SMESHGUI::desktop(), tr("SMESH_INSUFFICIENT_DATA"),
00737                                        tr("ERROR_FILTER_NAME"));
00738         return false;
00739       }
00740     }
00741   }
00742 
00743   return true;
00744 }
00745 
00746 //=======================================================================
00747 // name    : SMESHGUI_FilterLibraryDlg::isPermissionValid
00748 // Purpose : Verify write permission on file
00749 //=======================================================================
00750 bool SMESHGUI_FilterLibraryDlg::isPermissionValid(const bool theIsExistingOnly)
00751 {
00752   if (myMode == COPY_FROM)
00753     return true;
00754 
00755   // Verify write permission
00756   bool isWritable = false;
00757 
00758   QString fName(myFileName->text());
00759   if (QFileInfo(fName).suffix().isEmpty())
00760     fName = autoExtension(fName);
00761 
00762   fName = QDir::convertSeparators(fName);
00763 
00764   if (QFileInfo(fName).exists()) {
00765     isWritable = QFileInfo(fName).isWritable();
00766   } else if (!theIsExistingOnly) {
00767     QFileInfo aDirInfo(QFileInfo(fName).absolutePath());
00768     isWritable = aDirInfo.isWritable();
00769     /*if (QDir(QFileInfo(fName).dirPath(true)).exists() ||
00770          QDir().mkdir(QFileInfo(fName).dirPath(true)))
00771     {
00772       QFile aFile(fName);
00773       if (aFile.open(IO_WriteOnly))
00774         isWritable = true;
00775       else
00776         aFile.close();
00777     }
00778     */
00779   } else {
00780     isWritable = true;
00781   }
00782 
00783   if (!isWritable) {
00784     SUIT_MessageBox::information(SMESHGUI::desktop(), tr("SMESH_WRN_WARNING"),
00785                                  tr("NO_PERMISSION"));
00786     return false;
00787   }
00788 
00789   return true;
00790 }
00791 
00792 //=======================================================================
00793 // name    : SMESHGUI_FilterLibraryDlg::isValid
00794 // Purpose : Verify validity if entered data
00795 //=======================================================================
00796 bool SMESHGUI_FilterLibraryDlg::isValid(const bool theMess) const
00797 {
00798   // verify validity of table
00799   if (!myTable->IsValid(theMess) || !isNameValid(theMess))
00800     return false;
00801   else
00802     return true;
00803 }
00804 
00805 //=======================================================================
00806 // name    : SMESHGUI_FilterLibraryDlg::onFilterChanged
00807 // Purpose : SLOT. Called when selected filter of library is changed
00808 //=======================================================================
00809 void SMESHGUI_FilterLibraryDlg::onFilterChanged()
00810 {
00811   QString theName = myListBox->currentItem() ? myListBox->currentItem()->text() : QString::null;
00812   if (myLibrary->_is_nil())
00813     return;
00814 
00815   // Save parameters of filter if it was changed
00816 
00817   if (!myCurrFilterName.isEmpty() && myTable->IsEditable())
00818   {
00819     if (!isValid(true))
00820     {
00821       myListBox->blockSignals(true);
00822       myListBox->setCurrentRow(myCurrFilter);
00823       myListBox->blockSignals(false);
00824       return;
00825     }
00826 
00827     SMESH::Filter_var aFilter = createFilter();
00828     myLibrary->Replace(myCurrFilterName.toLatin1().constData(), 
00829                        myName->text().toLatin1().constData(), 
00830                        aFilter);
00831   }
00832 
00833   // Fill table with filter parameters
00834 
00835   SMESH::Filter_var aFilter = myLibrary->Copy(theName.toLatin1().constData());
00836   myCurrFilterName = theName;
00837   myCurrFilter = myListBox->currentRow();
00838   myName->setText(theName);
00839 
00840 
00841   SMESH::Filter::Criteria_var aCriteria;
00842 
00843   myTable->Clear(myTable->GetType());
00844 
00845   if (CORBA::is_nil( aFilter ) || !aFilter->GetCriteria(aCriteria))
00846     return;
00847 
00848   for (int i = 0, n = aCriteria->length(); i < n; i++)
00849     myTable->AddCriterion(aCriteria[ i ], myTable->GetType());
00850 
00851   myTable->Update();
00852   updateControlsVisibility(); // IPAL19974
00853 }
00854 
00855 //=======================================================================
00856 // name    : SMESHGUI_FilterLibraryDlg::onReturnPressed
00857 // Purpose : SLOT. Called when enter button is pressed in library name field
00858 //           Reload library
00859 //=======================================================================
00860 void SMESHGUI_FilterLibraryDlg::onReturnPressed()
00861 {
00862   QListWidgetItem* item = myListBox->item( myListBox->count()-1 );
00863   QString aName = item ? item->text() : QString::null;
00864 
00865   processNewLibrary();
00866 
00867   if (myMode == ADD_TO)
00868   {
00869     myTable->Copy((SMESHGUI_FilterTable*)parentWidget());
00870     myCurrFilterName = "";
00871     myCurrFilter = -1;
00872     addFilterToLib(aName);
00873   }
00874 
00875   isPermissionValid(false);
00876 }
00877 
00878 //=======================================================================
00879 // name    : SMESHGUI_FilterLibraryDlg::enableMainButtons
00880 // Purpose : Update state of "OK", "Cancel" buttons
00881 //=======================================================================
00882 void SMESHGUI_FilterLibraryDlg::enableMainButtons()
00883 {
00884   /*bool isEnabled = isValid(false);
00885   if (myButtons.contains(BTN_OK))
00886     myButtons[ BTN_OK ]->setEnabled(isEnabled);
00887   else if (myButtons.contains(BTN_Apply))
00888     myButtons[ BTN_OK ]->setEnabled(isEnabled);
00889   if (myButtons.contains(BTN_Cancel))
00890     myButtons[ BTN_Cancel ]->setEnabled(isEnabled);
00891   else if (myButtons.contains(BTN_Close))
00892     myButtons[ BTN_Cancel ]->setEnabled(isEnabled);
00893     */
00894 }
00895 
00896 //=======================================================================
00897 // name    : SMESHGUI_FilterLibraryDlg::createFilter
00898 // Purpose : Cerate filter in accordance with library
00899 //=======================================================================
00900 SMESH::Filter_ptr SMESHGUI_FilterLibraryDlg::createFilter(const int theType)
00901 {
00902   int n = myTable->NumRows(theType);
00903 
00904   SMESH::Filter::Criteria_var aCriteria = new SMESH::Filter::Criteria;
00905   aCriteria->length(n);
00906 
00907   for (int i = 0; i < n; i++)
00908   {
00909     SMESH::Filter::Criterion aCriterion = SMESHGUI_FilterDlg::createCriterion();
00910     myTable->GetCriterion(i, aCriterion);
00911     aCriteria[ i ] = aCriterion;
00912   }
00913 
00914   SMESH::FilterManager_var aFilterMgr = SMESH::GetFilterManager();
00915   SMESH::Filter_var aFilter = aFilterMgr->CreateFilter();
00916   aFilter->SetCriteria(aCriteria.in());
00917 
00918   return aFilter._retn();
00919 }
00920 
00921 //=======================================================================
00922 // name    : SMESHGUI_FilterLibraryDlg::onAddBtnPressed
00923 // Purpose : SLOT. Called when "Add" button pressed
00924 //           Add new filter to the end of library
00925 //=======================================================================
00926 void SMESHGUI_FilterLibraryDlg::onAddBtnPressed()
00927 {
00928   // Save parameters of filter if it was changed
00929   if (!myCurrFilterName.isEmpty() && myTable->IsEditable())
00930   {
00931     if (!isValid(true))
00932       return;
00933 
00934     SMESH::Filter_var aFilter = createFilter();
00935     myLibrary->Replace(myCurrFilterName.toLatin1().constData(), 
00936                        myName->text().toLatin1().constData(), 
00937                        aFilter);
00938   }
00939   myTable->Clear(myTable->GetType());
00940 
00941   addFilterToLib(getDefaultFilterName());
00942 }
00943 
00944 //=======================================================================
00945 // name    : onAddBtnPressed()
00946 // Purpose : SLOT. Called when "Add" button pressed
00947 //           Add new filter to the end of library
00948 //=======================================================================
00949 void SMESHGUI_FilterLibraryDlg::addFilterToLib (const QString& theName)
00950 {
00951   if (myLibrary->_is_nil()) {
00952     SUIT_MessageBox::information(SMESHGUI::desktop(), tr("SMESH_WRN_WARNING"),
00953                                  tr("LIBRARY_IS_NOT_LOADED"));
00954     return;
00955   }
00956 
00957   // create filter
00958   SMESH::Filter_var aFilter = createFilter();
00959 
00960   // if name of filter already exist in the library assign default name for the filter
00961   QString aName(theName);
00962   SMESH::string_array_var aNames = myLibrary->GetAllNames();
00963   for (int i = 0, n = aNames->length(); i < n; i++)
00964     if (aName == QString(aNames[ i ]))
00965     {
00966       aName = getDefaultFilterName();
00967       break;
00968     }
00969 
00970   // add new filter in library
00971   bool aResult = !aFilter->GetPredicate()->_is_nil()
00972     ? myLibrary->Add(aName.toLatin1().constData(), aFilter)
00973     : myLibrary->AddEmpty(aName.toLatin1().constData(), (SMESH::ElementType)myTable->GetType());
00974 
00975   if (!aResult) {
00976     SUIT_MessageBox::information(SMESHGUI::desktop(), tr("SMESH_ERROR"),
00977                                  tr("ERROR_OF_ADDING"));
00978   }
00979 
00980   updateList();
00981   myCurrFilterName = "";
00982   myCurrFilter = -1;
00983   setSelected(aName);
00984 
00985   if (theName != aName)
00986     SUIT_MessageBox::information(SMESHGUI::desktop(), tr("SMESH_WARNING"),
00987                                  tr("ASSIGN_NEW_NAME").arg(theName).arg(aName));
00988 }
00989 
00990 //=======================================================================
00991 // name    : SMESHGUI_FilterLibraryDlg::getDefaultLibraryName
00992 // Purpose : Get default library name
00993 //=======================================================================
00994 QString& SMESHGUI_FilterLibraryDlg::getDefaultLibraryName() const
00995 {
00996   static QString aName;
00997   if (aName.isEmpty())
00998   {
00999     QString aHomeDir = QDir(QDir::home()).absolutePath();
01000     aName = aHomeDir + "/" + tr ("LIB_NAME");
01001   }
01002   return aName;
01003 }
01004 
01005 //=======================================================================
01006 // name    : SMESHGUI_FilterLibraryDlg::getDefaultFilterName
01007 // Purpose : Create default filter name
01008 //=======================================================================
01009 QString SMESHGUI_FilterLibraryDlg::getDefaultFilterName() const
01010 {
01011   QString aName;
01012 
01013   if (myTable->GetType() == SMESH::NODE)
01014     aName = tr("NODE");
01015   else if (myTable->GetType() == SMESH::EDGE)
01016     aName = tr("EDGE");
01017   else if (myTable->GetType() == SMESH::FACE)
01018     aName = tr("FACE");
01019   else if (myTable->GetType() == SMESH::VOLUME)
01020     aName = tr("VOLUME");
01021   else if (myTable->GetType() == SMESH::ALL)
01022     aName = tr("ELEMENT");
01023   else
01024     aName = tr("SELECTION");
01025 
01026   aName += tr("FILTER");
01027 
01028 
01029   QMap< QString, int > anAllNames;
01030   SMESH::string_array_var aNames = myLibrary->GetAllNames();
01031   for(int i = 0, n = aNames->length(); i < n; i++)
01032     anAllNames[ QString(aNames[ i ]) ] = -1;
01033 
01034   bool isNotValid = true;
01035   int k = 1;
01036   QString aNewName;
01037   while (isNotValid)
01038   {
01039     isNotValid = false;
01040     aNewName = aName + "_" + QString("%1").arg(k);
01041     if (anAllNames.contains(aNewName))
01042     {
01043       isNotValid = true;
01044       k++;
01045     }
01046   }
01047 
01048   return aNewName;
01049 }
01050 
01051 //=======================================================================
01052 // name    : SMESHGUI_FilterLibraryDlg::setSelected
01053 // Purpose : set selected item in list box containing filters
01054 //=======================================================================
01055 bool SMESHGUI_FilterLibraryDlg::setSelected(const QString& theName)
01056 {
01057   int anIndex = getIndex(theName);
01058   if (anIndex != -1)
01059   {
01060     myListBox->setCurrentRow(anIndex);
01061     myCurrFilterName = theName;
01062     myCurrFilter = anIndex;
01063   }
01064   return anIndex != -1;
01065 }
01066 
01067 //=======================================================================
01068 // name    : SMESHGUI_FilterLibraryDlg::getIndex
01069 // Purpose : Get index of the filter in list box
01070 //=======================================================================
01071 int SMESHGUI_FilterLibraryDlg::getIndex(const QString& theName) const
01072 {
01073   for (int i = 0, n = myListBox->count(); i < n; i++)
01074     if (myListBox->item(i)->text() == theName)
01075       return i;
01076   return -1;
01077 }
01078 
01079 //=======================================================================
01080 // name    : SMESHGUI_FilterLibraryDlg::onDeleteBtnPressed
01081 // Purpose : SLOT. Called when "Delete" button pressed
01082 //=======================================================================
01083 void SMESHGUI_FilterLibraryDlg::onDeleteBtnPressed()
01084 {
01085   if (myLibrary->_is_nil()) {
01086     SUIT_MessageBox::information(SMESHGUI::desktop(), tr("SMESH_WRN_WARNING"),
01087                                  tr("LIBRARY_IS_NOT_LOADED"));
01088     return;
01089   }
01090 
01091   int anIndex = getIndex(myCurrFilterName);
01092 
01093   if (anIndex == -1 || !myLibrary->Delete(myCurrFilterName.toLatin1().constData())) {
01094     SUIT_MessageBox::information(SMESHGUI::desktop(), tr("SMESH_ERROR"),
01095                                  tr("ERROR_OF_DELETING"));
01096   } else {
01097     myCurrFilterName = "";
01098     myCurrFilter = -1;
01099     delete myListBox->item(anIndex);
01100 
01101     if (anIndex >= 1)
01102       myListBox->item(anIndex - 1)->setSelected(true);
01103     else if (anIndex == 0 && myListBox->count() > 0)
01104       myListBox->item(0)->setSelected(true);
01105     else
01106       myTable->Clear();
01107   }
01108 
01109   myTable->SetEnabled(myListBox->count() > 0);
01110   if (myListBox->count() == 0) {
01111     myName->setText("");
01112     myName->setEnabled(false);
01113   }
01114 }
01115 
01116 //=======================================================================
01117 // name    : onFilterNameChanged()
01118 // Purpose : SLOT. Called when name of filter changed
01119 //           Change filter name in list box
01120 //=======================================================================
01121 void SMESHGUI_FilterLibraryDlg::onFilterNameChanged (const QString& theName)
01122 {
01123   int aCurrItem = myListBox->currentRow();
01124   if (aCurrItem == -1)
01125     return;
01126 
01127   myListBox->blockSignals(true);
01128   myListBox->item(aCurrItem)->setText(theName);
01129   myListBox->blockSignals(false);
01130 }
01131 
01132 //=======================================================================
01133 // name    : SMESHGUI_FilterLibraryDlg::SetTable
01134 // Purpose : Set table
01135 //=======================================================================
01136 void SMESHGUI_FilterLibraryDlg::SetTable(const SMESHGUI_FilterTable* theTable)
01137 {
01138   myTable->Copy(theTable);
01139   myName->setText(getDefaultFilterName());
01140   addFilterToLib(myName->text());
01141   myTable->Update();
01142 }
01143 
01144 //=======================================================================
01145 // name    : SMESHGUI_FilterLibraryDlg::GetTable
01146 // Purpose : Get table
01147 //=======================================================================
01148 const SMESHGUI_FilterTable* SMESHGUI_FilterLibraryDlg::GetTable() const
01149 {
01150   return myTable;
01151 }
01152 
01153 
01154 //=======================================================================
01155 // name    : SMESHGUI_FilterLibraryDlg::onEntityTypeChanged
01156 // Purpose : SLOT. Called when entiyt type changed
01157 //=======================================================================
01158 void SMESHGUI_FilterLibraryDlg::onEntityTypeChanged(const int theType)
01159 {
01160   if (myLibrary->_is_nil())
01161     return;
01162 
01163   myName->clear();
01164   myCurrFilterName = "";
01165   myCurrFilter = -1;
01166   updateList();
01167   if (myListBox->count())
01168     myListBox->setCurrentItem(0);
01169 }
01170 
01171 //=======================================================================
01172 // name    : SMESHGUI_FilterLibraryDlg::onNeedValidation
01173 // Purpose :
01174 //=======================================================================
01175 void SMESHGUI_FilterLibraryDlg::onNeedValidation()
01176 {
01177   if (!myCurrFilterName.isEmpty())
01178   {
01179     bool valid = isValid(true);
01180     myTable->SetValidity(valid);
01181 
01182     if (valid)
01183     {
01184       SMESH::Filter_var aFilter = createFilter(myTable->GetType());
01185       myLibrary->Replace(myCurrFilterName.toLatin1().constData(),
01186                          myName->text().toLatin1().constData(),
01187                          aFilter);
01188     }
01189   }
01190 }
01191 
01192 //=================================================================================
01193 // function : keyPressEvent()
01194 // purpose  :
01195 //=================================================================================
01196 void SMESHGUI_FilterLibraryDlg::keyPressEvent( QKeyEvent* e )
01197 {
01198   QDialog::keyPressEvent( e );
01199   if ( e->isAccepted() )
01200     return;
01201 
01202   if ( e->key() == Qt::Key_F1 ) {
01203     e->accept();
01204     onHelp();
01205   }
01206 }