Back to index

salome-smesh  6.5.0
SMESHGUI_IdValidator.h
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_IdValidator.h
00025 // Author : Edward AGAPOV, Open CASCADE S.A.S.
00026 //
00027 #ifndef SMESHGUI_IDVALIDATOR_H
00028 #define SMESHGUI_IDVALIDATOR_H
00029 
00030 // SMESH includes
00031 #include "SMESH_SMESHGUI.hxx"
00032 
00033 // Qt includes
00034 #include <QValidator>
00035 
00036 // validator for manual input of Ids
00037 
00038 class SMESHGUI_EXPORT SMESHGUI_IdValidator : public QValidator
00039 {
00040 public:
00041   SMESHGUI_IdValidator( QWidget* parent, const int maxNbId = 0 ) :
00042     QValidator( parent ), myMaxNbId( maxNbId ) {}
00043 
00044   State validate( QString& input, int& pos ) const
00045   { 
00046     input.replace( QRegExp(" *[^0-9]+ *"), " " ); 
00047     if ( myMaxNbId && input.length() > myMaxNbId ) {
00048       // truncate extra ids
00049       int ind = 0, nbId = 0;
00050       while ( ind < input.length() ) {
00051         if ( input.at( ind ) != ' ' ) {
00052           if ( ++nbId > myMaxNbId ) {
00053             input.truncate( ind );
00054             break;
00055           }
00056           ind = input.indexOf( ' ', ind );
00057           if ( ind < 0 ) break;
00058         }
00059         ind++;
00060       }
00061     }
00062     if ( pos > input.length() )
00063       pos = input.length();
00064     return Acceptable;
00065   }
00066 
00067 private:
00068   int myMaxNbId;
00069 };
00070 
00071 #endif // SMESHGUI_IDVALIDATOR_H