Salome HOME
Prevent exception at shape selection if no mesh was pre-selected
[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.org
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 <qvalidator.h>
32
33 // validator for manual input of Ids
34
35 class SMESHGUI_IdValidator: public QValidator
36 {
37  public:
38
39   SMESHGUI_IdValidator(QWidget * parent, const char * name = 0, const int maxNbId = 0):
40     QValidator(parent,name), myMaxNbId(maxNbId) {}
41
42   State validate ( QString & text, int & pos) const
43   { 
44     text.replace( QRegExp(" *[^0-9]+ *"), " " ); 
45     if ( myMaxNbId && text.length() > myMaxNbId) { // truncate extra ids
46       int ind = 0, nbId = 0;
47       while ( ind < text.length() ) {
48         if ( text.at( ind ) != ' ' ) {
49           if ( ++nbId > myMaxNbId ) {
50             text.truncate( ind );
51             break;
52           }
53           ind = text.find( ' ', ind );
54           if ( ind < 0 ) break;
55         }
56         ind++;
57       }
58     }
59     if ( pos > text.length() )
60       pos = text.length();
61     return Acceptable;
62   }
63
64  private:
65   int myMaxNbId;
66 };
67
68 #endif