Salome HOME
Join modifications from BR_Dev_For_4_0 tag V4_1_1.
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_IdValidator.h
1 //  SMESH SMESHGUI : GUI for SMESH component
2 //
3 //  Copyright (C) 2003  CEA
4 // 
5 //  This library is free software; you can redistribute it and/or 
6 //  modify it under the terms of the GNU Lesser General Public 
7 //  License as published by the Free Software Foundation; either 
8 //  version 2.1 of the License. 
9 // 
10 //  This library is distributed in the hope that it will be useful, 
11 //  but WITHOUT ANY WARRANTY; without even the implied warranty of 
12 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
13 //  Lesser General Public License for more details. 
14 // 
15 //  You should have received a copy of the GNU Lesser General Public 
16 //  License along with this library; if not, write to the Free Software 
17 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA 
18 // 
19 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 //
21 //
22 //
23 //  File   : SMESHGUI_IdValidator.h
24 //  Author : Edward AGAPOV
25 //  Module : SMESH
26 //  $Header: /dn05/salome/PAL/SMESH/SMESH_SRC/src/SMESHGUI/SMESHGUI_IdValidator.h
27
28 #ifndef SMESHGUI_IdValidator_HeaderFile
29 #define SMESHGUI_IdValidator_HeaderFile
30
31 #include "SMESH_SMESHGUI.hxx"
32
33 #include <qvalidator.h>
34
35 // validator for manual input of Ids
36
37 class SMESHGUI_EXPORT SMESHGUI_IdValidator: public QValidator
38 {
39  public:
40
41   SMESHGUI_IdValidator(QWidget * parent, const char * name = 0, const int maxNbId = 0):
42     QValidator(parent,name), myMaxNbId(maxNbId) {}
43
44   State validate ( QString & text, int & pos) const
45   { 
46     text.replace( QRegExp(" *[^0-9]+ *"), " " ); 
47     if ( myMaxNbId && text.length() > myMaxNbId) { // truncate extra ids
48       int ind = 0, nbId = 0;
49       while ( ind < text.length() ) {
50         if ( text.at( ind ) != ' ' ) {
51           if ( ++nbId > myMaxNbId ) {
52             text.truncate( ind );
53             break;
54           }
55           ind = text.find( ' ', ind );
56           if ( ind < 0 ) break;
57         }
58         ind++;
59       }
60     }
61     if ( pos > text.length() )
62       pos = text.length();
63     return Acceptable;
64   }
65
66  private:
67   int myMaxNbId;
68 };
69
70 #endif