Salome HOME
f7ec15aa016e5335da0ee6349c390c48e931a09d
[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 // File   : SMESHGUI_IdValidator.h
22 // Author : Edward AGAPOV, Open CASCADE S.A.S.
23 //
24
25 #ifndef SMESHGUI_IDVALIDATOR_H
26 #define SMESHGUI_IDVALIDATOR_H
27
28 // SMESH includes
29 #include "SMESH_SMESHGUI.hxx"
30
31 // Qt includes
32 #include <QValidator>
33
34 // validator for manual input of Ids
35
36 class SMESHGUI_EXPORT SMESHGUI_IdValidator : public QValidator
37 {
38 public:
39   SMESHGUI_IdValidator( QWidget* parent, const int maxNbId = 0 ) :
40     QValidator( parent ), myMaxNbId( maxNbId ) {}
41
42   State validate( QString& input, int& pos ) const
43   { 
44     input.replace( QRegExp(" *[^0-9]+ *"), " " ); 
45     if ( myMaxNbId && input.length() > myMaxNbId ) {
46       // truncate extra ids
47       int ind = 0, nbId = 0;
48       while ( ind < input.length() ) {
49         if ( input.at( ind ) != ' ' ) {
50           if ( ++nbId > myMaxNbId ) {
51             input.truncate( ind );
52             break;
53           }
54           ind = input.indexOf( ' ', ind );
55           if ( ind < 0 ) break;
56         }
57         ind++;
58       }
59     }
60     if ( pos > input.length() )
61       pos = input.length();
62     return Acceptable;
63   }
64
65 private:
66   int myMaxNbId;
67 };
68
69 #endif // SMESHGUI_IDVALIDATOR_H